From 7dc92c6a2db3c97c1d0d3257197dd85ae70ddbcf Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Fri, 16 Aug 2024 16:29:19 +0200 Subject: [PATCH] add check & unlock --- src/dda/backup/backup.clj | 7 ++++ src/dda/backup/management.clj | 17 ++++++++- src/dda/backup/management/domain.clj | 43 ++++++++++++++-------- test/dda/backup/management/domain_test.clj | 24 ++++++------ 4 files changed, 64 insertions(+), 27 deletions(-) create mode 100644 src/dda/backup/backup.clj diff --git a/src/dda/backup/backup.clj b/src/dda/backup/backup.clj new file mode 100644 index 0000000..6c2eb7a --- /dev/null +++ b/src/dda/backup/backup.clj @@ -0,0 +1,7 @@ +(ns dda.backup.backup + (:require + [orchestra.core :refer [defn-spec]] + [clojure.spec.alpha :as s] + [dda.backup.core :as core] + [dda.backup.management.domain :as domain] + [dda.backup.infrastructure :as i])) \ No newline at end of file diff --git a/src/dda/backup/management.clj b/src/dda/backup/management.clj index 7cde3aa..3e7140d 100644 --- a/src/dda/backup/management.clj +++ b/src/dda/backup/management.clj @@ -10,7 +10,22 @@ (s/keys :req-un [::domain/restic-repository ::domain/backup-path] :opt-un [::domain/certificate-file ::core/debug ::core/dry-run])) +(defn-spec initalized? boolean? + [config ::config] + (let [config-w-defaults (merge core/default config)] + (try + (i/execute! (domain/check-repo-command config-w-defaults) config-w-defaults) + true + (catch Exception e false) + ))) + (defn-spec init! nil? [config ::config] (let [config-w-defaults (merge core/default config)] - (i/execute! (domain/init-repo-command config-w-defaults) config-w-defaults))) \ No newline at end of file + (when (not (initalized? config-w-defaults)) + (i/execute! (domain/init-repo-command config-w-defaults) config-w-defaults)))) + +(defn-spec unlock! nil? + [config ::config] + (let [config-w-defaults (merge core/default config)] + (i/execute! (domain/unlock-repo-command config-w-defaults) config-w-defaults))) diff --git a/src/dda/backup/management/domain.clj b/src/dda/backup/management/domain.clj index b5a4d6a..25d86b6 100644 --- a/src/dda/backup/management/domain.clj +++ b/src/dda/backup/management/domain.clj @@ -13,25 +13,38 @@ (s/keys :req-un [::restic-repository ::backup-path] :opt-un [::certificate-file ::password-file])) +(defn repo-command + [config + commands] + (let [{:keys [certificate-file password-file restic-repository backup-path]} config] + [(into + ["restic" "-r" (str restic-repository "/" backup-path) "-v"] + (concat + (cond + (some? certificate-file) + ["--cacert" certificate-file] + (some? password-file) + ["--password-file" password-file] + :else + []) + commands))])) + +(defn-spec check-repo-command ::core/commands + [config ::config] + (let [{:keys [certificate-file password-file restic-repository backup-path]} config] + (repo-command config ["check"]))) + (defn-spec init-repo-command ::core/commands [config ::config] (let [{:keys [certificate-file password-file restic-repository backup-path]} config] - (cond - (some? certificate-file) - [ ["restic" "-r" (str restic-repository "/" backup-path) "--cacert" certificate-file "-v" "init"]] - (some? password-file) - [["restic" "-r" (str restic-repository "/" backup-path) "--password-file" password-file "-v" "init"]] - :else - [ ["restic" "-r" (str restic-repository "/" backup-path) "-v" "init"]] - ))) + (repo-command config ["init"]))) + +(defn-spec unlock-repo-command ::core/commands + [config ::config] + (let [{:keys [certificate-file password-file restic-repository backup-path]} config] + (repo-command config ["--cleanup-cache" "unlock"]))) (defn-spec list-snapshot-command ::core/commands [config ::config] (let [{:keys [certificate-file password-file restic-repository backup-path]} config] - (cond - (some? certificate-file) - [["restic" "-r" (str restic-repository "/" backup-path) "--cacert" certificate-file "-v" "snapshots"]] - (some? password-file) - [["restic" "-r" (str restic-repository "/" backup-path) "--password-file" password-file "-v" "snapshots"]] - :else - [["restic" "-r" (str restic-repository "/" backup-path) "-v" "snapshots"]]))) + (repo-command config ["snapshots"]))) diff --git a/test/dda/backup/management/domain_test.clj b/test/dda/backup/management/domain_test.clj index 6d2c6eb..9e542c4 100644 --- a/test/dda/backup/management/domain_test.clj +++ b/test/dda/backup/management/domain_test.clj @@ -5,26 +5,28 @@ [dda.backup.management.domain :as cut])) (st/instrument `cut/init-repo-command) +(st/instrument `cut/unlock-repo-command) (st/instrument `cut/list-snapshot-command) -(deftest should-calculate-init-repo-command - (is (= [["restic" "-r" "repo/dir" "--cacert" "ca" "-v" "init"]] - (cut/init-repo-command {:certificate-file "ca" - :restic-repository "repo" - :backup-path "dir"}))) - (is (= [["restic" "-r" "repo/dir" "-v" "init"]] - (cut/init-repo-command {:restic-repository "repo" - :backup-path "dir"})))) - (deftest should-calculate-list-snapshot-command - (is (= [["restic" "-r" "repo/dir" "--cacert" "ca" "-v" "snapshots"]] + (is (= [["restic" "-r" "repo/dir" "-v" "--cacert" "ca" "snapshots"]] (cut/list-snapshot-command {:certificate-file "ca" :restic-repository "repo" :backup-path "dir"}))) - (is (= [["restic" "-r" "repo/dir" "--password-file" "password" "-v" "snapshots"]] + (is (= [["restic" "-r" "repo/dir" "-v" "--password-file" "password" "snapshots"]] (cut/list-snapshot-command {:password-file "password" :restic-repository "repo" :backup-path "dir"}))) (is (= [["restic" "-r" "repo/dir" "-v" "snapshots"]] (cut/list-snapshot-command {:restic-repository "repo" :backup-path "dir"})))) + +(deftest should-calculate-init-repo-command + (is (= [["restic" "-r" "repo/dir" "-v" "init"]] + (cut/init-repo-command {:restic-repository "repo" + :backup-path "dir"})))) + +(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"}))))