From 582138a645670c4e6414b6159ade3de173f9d41e Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Tue, 20 Aug 2024 08:17:51 +0200 Subject: [PATCH] refactor for repo-command --- infrastructure/backup/test/resources/test.bb | 31 ++++++++++++------- src/dda/backup/backup/domain.clj | 2 +- src/dda/backup/core/domain.clj | 32 ++++++++++---------- src/dda/backup/management/domain.clj | 16 +++++----- src/dda/backup/postgresql.clj | 3 +- src/dda/backup/restore/domain.clj | 4 +-- test/dda/backup/core/domain_test.clj | 24 +++++++-------- 7 files changed, 61 insertions(+), 51 deletions(-) diff --git a/infrastructure/backup/test/resources/test.bb b/infrastructure/backup/test/resources/test.bb index 214e077..1ec4c85 100755 --- a/infrastructure/backup/test/resources/test.bb +++ b/infrastructure/backup/test/resources/test.bb @@ -2,38 +2,47 @@ (require '[babashka.tasks :as tasks] '[dda.backup.management :as mgm] - '[dda.backup.postgresql :as ps] + '[dda.backup.postgresql :as pg] '[dda.backup.backup :as bak] '[dda.backup.restore :as rs]) (def restic-repo {:password-file "restic-pwd" :restic-repository "restic-repo"}) +(def file-config (merge restic-repo {:backup-path "files" + :files ["test-backup"] + :target-directory "test-restore"})) + + +(def db-config (merge restic-repo {:backup-path "db" + :pg-db "mydb" + :pg-user "user" + :pg-password "password"})) + +(def dry-run {:dry-run true :debug true}) + (defn prepare! [] - (ps/create-pg-pass! (merge restic-repo {:backup-path "db" - :pg-db "mydb" - :pg-user "user" - :pg-password "password"}))) + (pg/create-pg-pass! db-config)) (defn restic-repo-init! [] (spit "restic-pwd" "ThePassword") - (mgm/init! (merge restic-repo {:backup-path "files"})) - (mgm/init! (merge restic-repo {:backup-path "db" :dry-run true :debug true}))) + (mgm/init! file-config) + (mgm/init! (merge db-config dry-run))) (defn restic-backup! [] (tasks/shell "mkdir" "-p" "test-backup") (spit "test-backup/file" "I was here") - (bak/backup! (merge restic-repo {:backup-path "files" - :files ["test-backup"]}))) + (bak/backup! file-config)) (defn restic-restore! [] (tasks/shell "mkdir" "-p" "test-restore") - (rs/restore! (merge restic-repo {:backup-path "files" - :target-directory "test-restore"}))) + (rs/restore! file-config) + (pg/drop-create-db! (merge db-config dry-run)) + ) (prepare!) (restic-repo-init!) diff --git a/src/dda/backup/backup/domain.clj b/src/dda/backup/backup/domain.clj index 53712b0..f5a2b70 100644 --- a/src/dda/backup/backup/domain.clj +++ b/src/dda/backup/backup/domain.clj @@ -14,4 +14,4 @@ (defn-spec backup-files-command ::cd/commands [config ::config] (let [{:keys [files]} config] - (cd/repo-command config (into ["backup"] files)))) + [(cd/repo-command config (into ["backup"] files))])) diff --git a/src/dda/backup/core/domain.clj b/src/dda/backup/core/domain.clj index e841aba..e508244 100644 --- a/src/dda/backup/core/domain.clj +++ b/src/dda/backup/core/domain.clj @@ -20,22 +20,22 @@ (s/keys :req-un [::restic-repository ::backup-path ::days-to-keep ::months-to-keep] :opt-un [::certificate-file ::password-file ::directory])) -(defn-spec repo-command ::commands +(defn-spec repo-command ::command [config ::config command ::command] (let [{:keys [certificate-file password-file directory restic-repository backup-path]} config] - [(into - [] - (concat - (if (some? directory) - [{:dir directory}] - []) - ["restic" "-r" (str restic-repository "/" backup-path) "-v"] - (cond - (some? certificate-file) - ["--cacert" certificate-file] - (some? password-file) - ["--password-file" password-file] - :else - []) - command))])) + (into + [] + (concat + (if (some? directory) + [{:dir directory}] + []) + ["restic" "-r" (str restic-repository "/" backup-path) "-v"] + (cond + (some? certificate-file) + ["--cacert" certificate-file] + (some? password-file) + ["--password-file" password-file] + :else + []) + command)))) diff --git a/src/dda/backup/management/domain.clj b/src/dda/backup/management/domain.clj index 0520628..a44d2ac 100644 --- a/src/dda/backup/management/domain.clj +++ b/src/dda/backup/management/domain.clj @@ -10,24 +10,24 @@ (defn-spec check-repo-command ::cd/commands [config ::config] - (cd/repo-command config ["check"])) + [(cd/repo-command config ["check"])]) (defn-spec init-repo-command ::cd/commands [config ::config] - (cd/repo-command config ["init"])) + [(cd/repo-command config ["init"])]) (defn-spec unlock-repo-command ::cd/commands [config ::config] - (cd/repo-command config ["--cleanup-cache" "unlock"])) + [(cd/repo-command config ["--cleanup-cache" "unlock"])]) (defn-spec list-snapshot-command ::cd/commands [config ::config] - (cd/repo-command config ["snapshots"])) + [(cd/repo-command config ["snapshots"])]) (defn-spec forget-command ::cd/commands [config ::config] (let [{:keys [days-to-keep months-to-keep]} config] - (cd/repo-command config ["forget" "--group-by" "" - "--keep-last" "1" - "--keep-daily" (str days-to-keep) - "--keep-monthly" (str months-to-keep) "--prune"]))) + [(cd/repo-command config ["forget" "--group-by" "" + "--keep-last" "1" + "--keep-daily" (str days-to-keep) + "--keep-monthly" (str months-to-keep) "--prune"])])) diff --git a/src/dda/backup/postgresql.clj b/src/dda/backup/postgresql.clj index f28f2a1..76d7414 100644 --- a/src/dda/backup/postgresql.clj +++ b/src/dda/backup/postgresql.clj @@ -28,6 +28,7 @@ ::cd/dry-run ::cd/debug])) + (defn-spec create-pg-pass! nil? [config ::config] (let [config-w-defaults (merge default config)] @@ -37,7 +38,7 @@ (defn-spec drop-create-db! nil? [config ::config] (let [config-w-defaults (merge default config)] - ((i/execute! (domain/db-drop-create-command config-w-defaults))))) + (i/execute! (domain/db-drop-create-command config-w-defaults) config-w-defaults))) ;; function backup-roles() { ;; local role_prefix="$1"; shift diff --git a/src/dda/backup/restore/domain.clj b/src/dda/backup/restore/domain.clj index 3d7c392..3be2087 100644 --- a/src/dda/backup/restore/domain.clj +++ b/src/dda/backup/restore/domain.clj @@ -15,5 +15,5 @@ (defn-spec restore-dir-command ::cd/commands [config ::config] (let [{:keys [target-directory snapshot-id]} config] - (into [["rm" "-rf" target-directory]] - (cd/repo-command config ["restore" snapshot-id "--target" target-directory])))) + [["rm" "-rf" target-directory] + (cd/repo-command config ["restore" snapshot-id "--target" target-directory])])) diff --git a/test/dda/backup/core/domain_test.clj b/test/dda/backup/core/domain_test.clj index 84ae9c3..1c0493f 100644 --- a/test/dda/backup/core/domain_test.clj +++ b/test/dda/backup/core/domain_test.clj @@ -18,7 +18,7 @@ ["restic" "-r" "repo/dir" "-v" "init" "--cacert" "ca"]])))) (deftest should-calculate-repo-command - (is (= [[{:dir "dir"} + (is (= [{:dir "dir"} "restic" "-r" "repo/dir" @@ -27,29 +27,29 @@ "ca" "backup" "file1" - "file2"]] - (cut/repo-command {:certificate-file "ca" - :directory "dir" - :restic-repository "repo" - :backup-path "dir" - :days-to-keep 39 - :months-to-keep 3} - ["backup" "file1" "file2"]))) - (is (= [["restic" "-r" "repo/dir" "-v" "--cacert" "ca" "snapshots"]] + "file2"] + (cut/repo-command {:certificate-file "ca" + :directory "dir" + :restic-repository "repo" + :backup-path "dir" + :days-to-keep 39 + :months-to-keep 3} + ["backup" "file1" "file2"]))) + (is (= ["restic" "-r" "repo/dir" "-v" "--cacert" "ca" "snapshots"] (cut/repo-command {:certificate-file "ca" :restic-repository "repo" :backup-path "dir" :days-to-keep 39 :months-to-keep 3} ["snapshots"]))) - (is (= [["restic" "-r" "repo/dir" "-v" "--password-file" "password" "snapshots"]] + (is (= ["restic" "-r" "repo/dir" "-v" "--password-file" "password" "snapshots"] (cut/repo-command {:password-file "password" :restic-repository "repo" :backup-path "dir" :days-to-keep 39 :months-to-keep 3} ["snapshots"]))) - (is (= [["restic" "-r" "repo/dir" "-v" "snapshots"]] + (is (= ["restic" "-r" "repo/dir" "-v" "snapshots"] (cut/repo-command {:restic-repository "repo" :backup-path "dir" :days-to-keep 39