From d711d5484ee2816a2b6891f1fced26295f03dafb Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Sat, 28 Dec 2024 15:58:17 +0100 Subject: [PATCH] Fix repo check in combination with execution dir --- src/dda/backup/backup/domain.clj | 2 +- src/dda/backup/infrastructure.clj | 2 +- src/dda/backup/postgresql/domain.clj | 4 ++-- src/dda/backup/restic.clj | 1 - src/dda/backup/restic/domain.clj | 31 +++++++++++++++----------- src/dda/backup/restore/domain.clj | 4 ++-- test/dda/backup/restic/domain_test.clj | 30 ++++++++++++++++++++----- 7 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/dda/backup/backup/domain.clj b/src/dda/backup/backup/domain.clj index 00b678a..6002961 100644 --- a/src/dda/backup/backup/domain.clj +++ b/src/dda/backup/backup/domain.clj @@ -17,7 +17,7 @@ (defn-spec backup-files-command ::cd/commands [config ::backup-file-config] (let [{:keys [files]} config] - [(rd/repo-command config (into ["backup"] files))])) + [(rd/repo-command config (into ["backup"] files) false)])) (defn-spec backup-role-command ::cd/commands [config ::pd/pg-role-dump-config] diff --git a/src/dda/backup/infrastructure.clj b/src/dda/backup/infrastructure.clj index 15a3b45..675c6cb 100644 --- a/src/dda/backup/infrastructure.clj +++ b/src/dda/backup/infrastructure.clj @@ -28,4 +28,4 @@ (let [{:keys [dry-run debug]} config] (doseq [c commands] (when-not dry-run - (execute-single! c config))))) \ No newline at end of file + (apply t/shell c))))) \ No newline at end of file diff --git a/src/dda/backup/postgresql/domain.clj b/src/dda/backup/postgresql/domain.clj index df26fa2..fd2c55f 100644 --- a/src/dda/backup/postgresql/domain.clj +++ b/src/dda/backup/postgresql/domain.clj @@ -50,7 +50,7 @@ "--no-password"] command ["|" "grep" pg-role-prefix "|"] - (rd/repo-command config ["backup" "--stdin"]))))) + (rd/repo-command config ["backup" "--stdin"] false))))) (defn-spec pgdump-command ::cd/command [config ::pg-db-dump-config @@ -63,7 +63,7 @@ "--no-password"] command ["|"] - (rd/repo-command config ["backup" "--stdin"]))))) + (rd/repo-command config ["backup" "--stdin"] false))))) (defn-spec pgpass string? [config ::pg-config] diff --git a/src/dda/backup/restic.clj b/src/dda/backup/restic.clj index 3da74cb..f39b8ce 100644 --- a/src/dda/backup/restic.clj +++ b/src/dda/backup/restic.clj @@ -38,7 +38,6 @@ :default :error)))))) (defn-spec use-new-password? boolean? - "deprecated" [restic-config ::restic-config] (if (contains? restic-config :new-password-file) (= :initialized (check (merge restic-config {:password-file (:new-password-file restic-config)}))) diff --git a/src/dda/backup/restic/domain.clj b/src/dda/backup/restic/domain.clj index 10594e5..6a9882f 100644 --- a/src/dda/backup/restic/domain.clj +++ b/src/dda/backup/restic/domain.clj @@ -26,15 +26,20 @@ (defn-spec repo-command ::cd/command [config ::restic-config - command ::cd/command] + command ::cd/command + read-error-stream boolean?] (let [{:keys [certificate-file password-file execution-directory - restic-repository backup-path]} config] + restic-repository backup-path]} config + shell-option (if read-error-stream {:err :string} {})] (into [] (concat - (if (some? execution-directory) - [{:dir execution-directory}] - []) + (cond + (some? execution-directory) + [(merge shell-option {:dir execution-directory})] + read-error-stream + [shell-option] + :default []) ["restic" "-r" (str restic-repository "/" backup-path) "-v"] (cond (some? certificate-file) @@ -47,19 +52,19 @@ (defn-spec check-repo-command ::cd/commands [config ::restic-config] - [(repo-command config ["check"])]) + [(repo-command config ["check"] true)]) (defn-spec init-repo-command ::cd/commands [config ::restic-config] - [(repo-command config ["init"])]) + [(repo-command config ["init"] false)]) (defn-spec unlock-repo-command ::cd/commands [config ::restic-config] - [(repo-command config ["--cleanup-cache" "unlock"])]) + [(repo-command config ["--cleanup-cache" "unlock"] false)]) (defn-spec list-snapshot-command ::cd/commands [config ::restic-config] - [(repo-command config ["snapshots"])]) + [(repo-command config ["snapshots"] false)]) (defn-spec forget-command ::cd/commands [config ::restic-config] @@ -67,19 +72,19 @@ [(repo-command config ["forget" "--group-by" "''" "--keep-last" "1" "--keep-daily" (str days-to-keep) - "--keep-monthly" (str months-to-keep) "--prune"])])) + "--keep-monthly" (str months-to-keep) "--prune"] false)])) (defn-spec change-password-command ::cd/command [config ::restic-config] (if (contains? config :new-password-file) (let [{:keys [new-password-file]} config] - [(repo-command config ["--new-password-file" new-password-file - "key" "passwd"])]) + [(repo-command config ["--new-password-file" new-password-file + "key" "passwd"] false)]) (throw (Exception. "change-password: new password required")))) (defn-spec parse-check-error ::check-error [error string?] - (cond + (cond (clojure.string/includes? error "Fatal: unable to open config file") :not-initialized (clojure.string/includes? error "Fatal: wrong password or no key found") :wrong-password (clojure.string/includes? error "Resolving password failed") :no-password diff --git a/src/dda/backup/restore/domain.clj b/src/dda/backup/restore/domain.clj index aad25f4..50717d7 100644 --- a/src/dda/backup/restore/domain.clj +++ b/src/dda/backup/restore/domain.clj @@ -24,7 +24,7 @@ [config ::restore-file-config] (let [{:keys [restore-target-directory snapshot-id]} config] [["rm" "-rf" restore-target-directory] - (rd/repo-command config ["restore" snapshot-id "--target" restore-target-directory])])) + (rd/repo-command config ["restore" snapshot-id "--target" restore-target-directory] false)])) (defn-spec restore-db-command ::cd/commands [config ::restore-db-config] @@ -35,7 +35,7 @@ (into [] (concat - (rd/repo-command config ["dump" snapshot-id "stdin"]) + (rd/repo-command config ["dump" snapshot-id "stdin"] false) ["|"] (pd/psql-command config []))))]])) diff --git a/test/dda/backup/restic/domain_test.clj b/test/dda/backup/restic/domain_test.clj index 44453c6..e7d5297 100644 --- a/test/dda/backup/restic/domain_test.clj +++ b/test/dda/backup/restic/domain_test.clj @@ -10,7 +10,7 @@ (st/instrument `cut/forget-command) (deftest should-calculate-repo-command - (is (= [{:dir "dir"} + (is (= [{:dir "dir" :err :string} "restic" "-r" "repo/dir" @@ -26,27 +26,45 @@ :backup-path "dir" :days-to-keep 39 :months-to-keep 3} - ["backup" "file1" "file2"]))) - (is (= ["restic" "-r" "repo/dir" "-v" "--cacert" "ca" "snapshots"] + ["backup" "file1" "file2"] + true))) + (is (= [{:dir "dir"} + "restic" + "-r" + "repo/dir" + "-v" + "--cacert" + "ca" + "backup" + "file1" + "file2"] + (cut/repo-command {:certificate-file "ca" + :execution-directory "dir" + :restic-repository "repo" + :backup-path "dir" + :days-to-keep 39 + :months-to-keep 3} + ["backup" "file1" "file2"] false))) + (is (= [{:err :string} "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"]))) + ["snapshots"] true))) (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"]))) + ["snapshots"] false))) (is (= ["restic" "-r" "repo/dir" "-v" "snapshots"] (cut/repo-command {:restic-repository "repo" :backup-path "dir" :days-to-keep 39 :months-to-keep 3} - ["snapshots"])))) + ["snapshots"] false)))) (deftest should-calculate-init-repo-command (is (= [["restic" "-r" "repo/dir" "-v" "init"]]