From 5d2ff8756486dd538ac5bc9f62e4aa4f0599d76a Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Tue, 20 Aug 2024 17:15:27 +0200 Subject: [PATCH] reuse & rename core config --- infrastructure/backup/test/resources/test.bb | 6 +- src/dda/backup/backup/domain.clj | 10 +-- src/dda/backup/core.clj | 17 ++--- src/dda/backup/core/domain.clj | 37 ++-------- src/dda/backup/management/domain.clj | 33 --------- src/dda/backup/{management.clj => restic.clj} | 26 ++++--- src/dda/backup/restic/domain.clj | 65 +++++++++++++++++ src/dda/backup/restore/domain.clj | 11 +-- test/dda/backup/backup/domain_test.clj | 2 +- test/dda/backup/core/domain_test.clj | 42 ----------- test/dda/backup/management/domain_test.clj | 32 --------- test/dda/backup/restic/domain_test.clj | 72 +++++++++++++++++++ 12 files changed, 182 insertions(+), 171 deletions(-) delete mode 100644 src/dda/backup/management/domain.clj rename src/dda/backup/{management.clj => restic.clj} (71%) create mode 100644 src/dda/backup/restic/domain.clj delete mode 100644 test/dda/backup/management/domain_test.clj create mode 100644 test/dda/backup/restic/domain_test.clj diff --git a/infrastructure/backup/test/resources/test.bb b/infrastructure/backup/test/resources/test.bb index 1ec4c85..eb37fa7 100755 --- a/infrastructure/backup/test/resources/test.bb +++ b/infrastructure/backup/test/resources/test.bb @@ -1,7 +1,7 @@ #!/usr/bin/env bb (require '[babashka.tasks :as tasks] - '[dda.backup.management :as mgm] + '[dda.backup.restic :as rc] '[dda.backup.postgresql :as pg] '[dda.backup.backup :as bak] '[dda.backup.restore :as rs]) @@ -28,8 +28,8 @@ (defn restic-repo-init! [] (spit "restic-pwd" "ThePassword") - (mgm/init! file-config) - (mgm/init! (merge db-config dry-run))) + (rc/init! file-config) + (rc/init! (merge db-config dry-run))) (defn restic-backup! [] diff --git a/src/dda/backup/backup/domain.clj b/src/dda/backup/backup/domain.clj index f5a2b70..33d0111 100644 --- a/src/dda/backup/backup/domain.clj +++ b/src/dda/backup/backup/domain.clj @@ -2,16 +2,16 @@ (:require [orchestra.core :refer [defn-spec]] [clojure.spec.alpha :as s] - [dda.backup.core.domain :as cd])) + [dda.backup.core.domain :as cd] + [dda.backup.restic.domain :as rd])) (s/def ::files (s/+ string?)) (s/def ::config - (s/keys :req-un [::files - ::cd/restic-repository ::cd/backup-path] - :opt-un [::cd/certificate-file ::cd/password-file ::cd/directory])) + (s/merge ::rd/config + (s/keys :req-un [::files]))) (defn-spec backup-files-command ::cd/commands [config ::config] (let [{:keys [files]} config] - [(cd/repo-command config (into ["backup"] files))])) + [(rd/repo-command config (into ["backup"] files))])) diff --git a/src/dda/backup/core.clj b/src/dda/backup/core.clj index 0329127..2d11cbf 100644 --- a/src/dda/backup/core.clj +++ b/src/dda/backup/core.clj @@ -1,11 +1,12 @@ (ns dda.backup.core - (:require [clojure.spec.alpha :as s] - )) - -(s/def ::dry-run boolean?) -(s/def ::debug boolean?) + (:require + [clojure.spec.alpha :as s])) (def default {:dry-run false - :debug false - :days-to-keep 30 - :months-to-keep 12}) \ No newline at end of file + :debug false}) + +(s/def ::execution + (s/keys :req-un [] + :opt-un [::dry-run + ::debug + ::execution-directory])) diff --git a/src/dda/backup/core/domain.clj b/src/dda/backup/core/domain.clj index e508244..c137266 100644 --- a/src/dda/backup/core/domain.clj +++ b/src/dda/backup/core/domain.clj @@ -1,6 +1,5 @@ (ns dda.backup.core.domain (:require - [orchestra.core :refer [defn-spec]] [clojure.spec.alpha :as s])) (s/def ::command (s/cat @@ -8,34 +7,10 @@ :params (s/* string?))) (s/def ::commands (s/coll-of ::command)) -(s/def ::certificate-file string?) -(s/def ::password-file string?) -(s/def ::restic-repository string?) -(s/def ::backup-path string?) -(s/def ::directory string?) -(s/def ::days-to-keep pos?) -(s/def ::months-to-keep pos?) +(s/def ::dry-run boolean?) +(s/def ::debug boolean?) +(s/def ::execution-directory string?) -(s/def ::config - (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 ::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)))) +(s/def ::execution + (s/keys :req-un [::dry-run ::debug] + :opt-un [::execution-directory])) diff --git a/src/dda/backup/management/domain.clj b/src/dda/backup/management/domain.clj deleted file mode 100644 index a44d2ac..0000000 --- a/src/dda/backup/management/domain.clj +++ /dev/null @@ -1,33 +0,0 @@ -(ns dda.backup.management.domain - (:require - [orchestra.core :refer [defn-spec]] - [clojure.spec.alpha :as s] - [dda.backup.core.domain :as cd])) - -(s/def ::config - (s/keys :req-un [::cd/restic-repository ::cd/backup-path ::cd/days-to-keep ::cd/months-to-keep] - :opt-un [::cd/certificate-file ::cd/password-file])) - -(defn-spec check-repo-command ::cd/commands - [config ::config] - [(cd/repo-command config ["check"])]) - -(defn-spec init-repo-command ::cd/commands - [config ::config] - [(cd/repo-command config ["init"])]) - -(defn-spec unlock-repo-command ::cd/commands - [config ::config] - [(cd/repo-command config ["--cleanup-cache" "unlock"])]) - -(defn-spec list-snapshot-command ::cd/commands - [config ::config] - [(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"])])) diff --git a/src/dda/backup/management.clj b/src/dda/backup/restic.clj similarity index 71% rename from src/dda/backup/management.clj rename to src/dda/backup/restic.clj index 564b1ba..1d26353 100644 --- a/src/dda/backup/management.clj +++ b/src/dda/backup/restic.clj @@ -1,21 +1,26 @@ -(ns dda.backup.management +(ns dda.backup.restic (:require [orchestra.core :refer [defn-spec]] [clojure.spec.alpha :as s] [dda.backup.core.domain :as cd] - [dda.backup.management.domain :as domain] + [dda.backup.restic.domain :as domain] [dda.backup.core :as core] [dda.backup.infrastructure :as i])) +(def default + (merge core/default + {:days-to-keep 30 + :months-to-keep 12})) + (s/def ::config - (s/keys :req-un [::cd/restic-repository + (s/keys :req-un [::cd/restic-repository ::cd/backup-path] - :opt-un [::cd/certificate-file + :opt-un [::cd/certificate-file ::cd/password-file - ::cd/days-to-keep - ::cd/months-to-keep - ::cd/directory - ::cd/dry-run + ::cd/days-to-keep + ::cd/months-to-keep + ::cd/directory + ::cd/dry-run ::cd/debug])) (defn-spec initalized? boolean? @@ -24,8 +29,7 @@ (try (i/execute! (domain/check-repo-command config-w-defaults) config-w-defaults) true - (catch Exception e false) - ))) + (catch Exception e false)))) (defn-spec init! nil? [config ::config] @@ -41,4 +45,4 @@ (defn-spec forget! nil? [config ::config] (let [config-w-defaults (merge core/default config)] - (i/execute! (domain/forget-command config-w-defaults) config-w-defaults))) + (i/execute! (domain/forget-command config-w-defaults) config-w-defaults))) \ No newline at end of file diff --git a/src/dda/backup/restic/domain.clj b/src/dda/backup/restic/domain.clj new file mode 100644 index 0000000..62c891f --- /dev/null +++ b/src/dda/backup/restic/domain.clj @@ -0,0 +1,65 @@ +(ns dda.backup.restic.domain + (:require + [orchestra.core :refer [defn-spec]] + [clojure.spec.alpha :as s] + [dda.backup.core.domain :as cd])) + +(s/def ::certificate-file string?) +(s/def ::password-file string?) +(s/def ::restic-repository string?) +(s/def ::backup-path string?) +(s/def ::days-to-keep pos?) +(s/def ::months-to-keep pos?) + +(s/def ::config + (s/keys :req-un [::restic-repository + ::backup-path + ::days-to-keep + ::months-to-keep] + :opt-un [::certificate-file + ::password-file + ::cd/execution-directory])) + +(defn-spec repo-command ::cd/command + [config ::config + command ::cd/command] + (let [{:keys [certificate-file password-file execution-directory restic-repository backup-path]} config] + (into + [] + (concat + (if (some? execution-directory) + [{:dir execution-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)))) + +(defn-spec check-repo-command ::cd/commands + [config ::config] + [(repo-command config ["check"])]) + +(defn-spec init-repo-command ::cd/commands + [config ::config] + [(repo-command config ["init"])]) + +(defn-spec unlock-repo-command ::cd/commands + [config ::config] + [(repo-command config ["--cleanup-cache" "unlock"])]) + +(defn-spec list-snapshot-command ::cd/commands + [config ::config] + [(repo-command config ["snapshots"])]) + +(defn-spec forget-command ::cd/commands + [config ::config] + (let [{:keys [days-to-keep months-to-keep]} config] + [(repo-command config ["forget" "--group-by" "" + "--keep-last" "1" + "--keep-daily" (str days-to-keep) + "--keep-monthly" (str months-to-keep) "--prune"])])) \ No newline at end of file diff --git a/src/dda/backup/restore/domain.clj b/src/dda/backup/restore/domain.clj index 3be2087..25cec86 100644 --- a/src/dda/backup/restore/domain.clj +++ b/src/dda/backup/restore/domain.clj @@ -2,18 +2,19 @@ (:require [orchestra.core :refer [defn-spec]] [clojure.spec.alpha :as s] - [dda.backup.core.domain :as cd])) + [dda.backup.core.domain :as cd] + [dda.backup.restic.domain :as rd])) (s/def ::target-directory string?) (s/def ::snapshot-id string?) (s/def ::config - (s/keys :req-un [::target-directory ::snapshot-id - ::cd/restic-repository ::cd/backup-path] - :opt-un [::cd/certificate-file ::cd/password-file ::cd/directory])) + (s/merge ::rd/config + (s/keys :req-un [::target-directory + ::snapshot-id]))) (defn-spec restore-dir-command ::cd/commands [config ::config] (let [{:keys [target-directory snapshot-id]} config] [["rm" "-rf" target-directory] - (cd/repo-command config ["restore" snapshot-id "--target" target-directory])])) + (rd/repo-command config ["restore" snapshot-id "--target" target-directory])])) diff --git a/test/dda/backup/backup/domain_test.clj b/test/dda/backup/backup/domain_test.clj index 4444819..199ae72 100644 --- a/test/dda/backup/backup/domain_test.clj +++ b/test/dda/backup/backup/domain_test.clj @@ -17,7 +17,7 @@ "file2"]] (cut/backup-files-command {:restic-repository "repo" :backup-path "dir-at-repo" - :directory "dir-to-backup" + :execution-directory "dir-to-backup" :days-to-keep 39 :months-to-keep 3 :files ["file2" "file2"]})))) diff --git a/test/dda/backup/core/domain_test.clj b/test/dda/backup/core/domain_test.clj index 1c0493f..7f7c2c3 100644 --- a/test/dda/backup/core/domain_test.clj +++ b/test/dda/backup/core/domain_test.clj @@ -2,11 +2,8 @@ (:require [clojure.test :refer [deftest is are testing run-tests]] [clojure.spec.alpha :as s] - [clojure.spec.test.alpha :as st] [dda.backup.core.domain :as cut])) -(st/instrument `cut/repo-command) - (deftest should-verify-command (is (= {:app "restic", :params ["-r" "repo/dir" "-v" "init" "--cacert" "ca"]} (s/conform ::cut/command ["restic" "-r" "repo/dir" "-v" "init" "--cacert" "ca"])))) @@ -16,42 +13,3 @@ {:app "restic", :params ["-r" "repo/dir" "-v" "init" "--cacert" "ca"]}] (s/conform ::cut/commands [["ls"] ["restic" "-r" "repo/dir" "-v" "init" "--cacert" "ca"]])))) - -(deftest should-calculate-repo-command - (is (= [{:dir "dir"} - "restic" - "-r" - "repo/dir" - "-v" - "--cacert" - "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"] - (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"] - (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"] - (cut/repo-command {:restic-repository "repo" - :backup-path "dir" - :days-to-keep 39 - :months-to-keep 3} - ["snapshots"])))) \ No newline at end of file diff --git a/test/dda/backup/management/domain_test.clj b/test/dda/backup/management/domain_test.clj deleted file mode 100644 index f732e28..0000000 --- a/test/dda/backup/management/domain_test.clj +++ /dev/null @@ -1,32 +0,0 @@ -(ns dda.backup.management.domain-test - (:require - [clojure.test :refer [deftest is are testing run-tests]] - [clojure.spec.test.alpha :as st] - [dda.backup.management.domain :as cut])) - -(st/instrument `cut/init-repo-command) -(st/instrument `cut/unlock-repo-command) -(st/instrument `cut/forget-command) - -(deftest should-calculate-init-repo-command - (is (= [["restic" "-r" "repo/dir" "-v" "init"]] - (cut/init-repo-command {:restic-repository "repo" - :backup-path "dir" - :days-to-keep 39 - :months-to-keep 3})))) - -(deftest should-calculate-unlock-repo-command - (is (= [["restic" "-r" "repo/dir" "-v" "--cleanup-cache" "unlock"]] - (cut/unlock-repo-command {:restic-repository "repo" - :backup-path "dir" - :days-to-keep 39 - :months-to-keep 3})))) - -(deftest should-calculate-forget-command - (is (= [["restic" "-r" "repo/dir" "-v" "forget" - "--group-by" "" "--keep-last" "1" - "--keep-daily" "39" "--keep-monthly" "3" "--prune"]] - (cut/forget-command {:restic-repository "repo" - :backup-path "dir" - :days-to-keep 39 - :months-to-keep 3})))) diff --git a/test/dda/backup/restic/domain_test.clj b/test/dda/backup/restic/domain_test.clj new file mode 100644 index 0000000..d4e7a4f --- /dev/null +++ b/test/dda/backup/restic/domain_test.clj @@ -0,0 +1,72 @@ +(ns dda.backup.restic.domain-test + (:require + [clojure.test :refer [deftest is are testing run-tests]] + [clojure.spec.test.alpha :as st] + [dda.backup.restic.domain :as cut])) + +(st/instrument `cut/repo-command) +(st/instrument `cut/init-repo-command) +(st/instrument `cut/unlock-repo-command) +(st/instrument `cut/forget-command) + +(deftest should-calculate-repo-command + (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"]))) + (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"] + (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"] + (cut/repo-command {:restic-repository "repo" + :backup-path "dir" + :days-to-keep 39 + :months-to-keep 3} + ["snapshots"])))) + +(deftest should-calculate-init-repo-command + (is (= [["restic" "-r" "repo/dir" "-v" "init"]] + (cut/init-repo-command {:restic-repository "repo" + :backup-path "dir" + :days-to-keep 39 + :months-to-keep 3})))) + +(deftest should-calculate-unlock-repo-command + (is (= [["restic" "-r" "repo/dir" "-v" "--cleanup-cache" "unlock"]] + (cut/unlock-repo-command {:restic-repository "repo" + :backup-path "dir" + :days-to-keep 39 + :months-to-keep 3})))) + +(deftest should-calculate-forget-command + (is (= [["restic" "-r" "repo/dir" "-v" "forget" + "--group-by" "" "--keep-last" "1" + "--keep-daily" "39" "--keep-monthly" "3" "--prune"]] + (cut/forget-command {:restic-repository "repo" + :backup-path "dir" + :days-to-keep 39 + :months-to-keep 3})))) \ No newline at end of file