diff --git a/infrastructure/backup/test/Dockerfile b/infrastructure/backup/test/Dockerfile index bab92b0..b6d31fd 100644 --- a/infrastructure/backup/test/Dockerfile +++ b/infrastructure/backup/test/Dockerfile @@ -4,4 +4,4 @@ FROM c4k-forgejo-backup:latest RUN apt update && apt install -qqy openjdk-17-jre-headless ADD local/ /usr/local/lib/dda-backup ADD resources /tmp/ -RUN /tmp/test.bb +RUN ENV_PASSWORD=env-password /tmp/test.bb diff --git a/infrastructure/backup/test/resources/test.bb b/infrastructure/backup/test/resources/test.bb index cba7666..b7f4754 100755 --- a/infrastructure/backup/test/resources/test.bb +++ b/infrastructure/backup/test/resources/test.bb @@ -1,6 +1,7 @@ #!/usr/bin/env bb (require '[babashka.tasks :as tasks] + '[dda.backup.core :as bc] '[dda.backup.restic :as rc] '[dda.backup.postgresql :as pg] '[dda.backup.backup :as bak] @@ -11,7 +12,7 @@ (def file-config (merge restic-repo {:backup-path "files" :files ["test-backup"] - :target-directory "test-restore"})) + :restore-target-directory "test-restore"})) (def db-config (merge restic-repo {:backup-path "db" @@ -23,24 +24,27 @@ (defn prepare! [] + (spit "file_password" "file-password") + (println (bc/env-or-file "file_password")) + (println (bc/env-or-file "env_password")) + (spit "restic-pwd" "ThePassword") + (tasks/shell "mkdir" "-p" "test-backup") + (spit "test-backup/file" "I was here") + (tasks/shell "mkdir" "-p" "test-restore") (pg/create-pg-pass! db-config)) (defn restic-repo-init! - [] - (spit "restic-pwd" "ThePassword") + [] (rc/init! file-config) (rc/init! (merge db-config dry-run))) (defn restic-backup! - [] - (tasks/shell "mkdir" "-p" "test-backup") - (spit "test-backup/file" "I was here") + [] (bak/backup-file! file-config) (bak/backup-db! (merge db-config dry-run))) (defn restic-restore! - [] - (tasks/shell "mkdir" "-p" "test-restore") + [] (rs/restore-file! file-config) (pg/drop-create-db! (merge db-config dry-run)) (rs/restore-db! (merge db-config dry-run))) diff --git a/src/dda/backup/core.clj b/src/dda/backup/core.clj index 2d11cbf..9eb552a 100644 --- a/src/dda/backup/core.clj +++ b/src/dda/backup/core.clj @@ -1,6 +1,8 @@ (ns dda.backup.core (:require - [clojure.spec.alpha :as s])) + [orchestra.core :refer [defn-spec]] + [clojure.spec.alpha :as s] + [clojure.string :as st])) (def default {:dry-run false :debug false}) @@ -10,3 +12,12 @@ :opt-un [::dry-run ::debug ::execution-directory])) + +(defn-spec env-or-file string? + [name string?] + (let [name-upper (st/upper-case name) + name-lower (st/lower-case name) + from-env (System/getenv name-upper)] + (if (some? from-env) + from-env + (slurp name-lower)))) diff --git a/src/dda/backup/restore.clj b/src/dda/backup/restore.clj index 648ed07..63054e3 100644 --- a/src/dda/backup/restore.clj +++ b/src/dda/backup/restore.clj @@ -13,7 +13,7 @@ (s/def ::restore-file-config (s/merge ::restic/restic-config - (s/keys :req-un [::domain/target-directory] + (s/keys :req-un [::domain/restore-target-directory] :opt-un [::domain/snapshot-id]))) (s/def ::restore-db-config diff --git a/src/dda/backup/restore/domain.clj b/src/dda/backup/restore/domain.clj index b8dd0b4..aad25f4 100644 --- a/src/dda/backup/restore/domain.clj +++ b/src/dda/backup/restore/domain.clj @@ -7,12 +7,12 @@ [dda.backup.restic.domain :as rd] [dda.backup.postgresql.domain :as pd])) -(s/def ::target-directory string?) +(s/def ::restore-target-directory string?) (s/def ::snapshot-id string?) (s/def ::restore-file-config (s/merge ::rd/restic-config - (s/keys :req-un [::target-directory + (s/keys :req-un [::restore-target-directory ::snapshot-id]))) (s/def ::restore-db-config @@ -22,9 +22,9 @@ (defn-spec restore-dir-command ::cd/commands [config ::restore-file-config] - (let [{:keys [target-directory snapshot-id]} config] - [["rm" "-rf" target-directory] - (rd/repo-command config ["restore" snapshot-id "--target" target-directory])])) + (let [{:keys [restore-target-directory snapshot-id]} config] + [["rm" "-rf" restore-target-directory] + (rd/repo-command config ["restore" snapshot-id "--target" restore-target-directory])])) (defn-spec restore-db-command ::cd/commands [config ::restore-db-config] diff --git a/test/dda/backup/restore/domain_test.clj b/test/dda/backup/restore/domain_test.clj index b1c7214..5813783 100644 --- a/test/dda/backup/restore/domain_test.clj +++ b/test/dda/backup/restore/domain_test.clj @@ -19,7 +19,7 @@ "dir-to-backup"]] (cut/restore-dir-command {:restic-repository "repo" :backup-path "dir-at-repo" - :target-directory "dir-to-backup" + :restore-target-directory "dir-to-backup" :days-to-keep 39 :months-to-keep 3 :snapshot-id "latest"}))))