add check & unlock

This commit is contained in:
Michael Jerger 2024-08-16 16:29:19 +02:00
parent 36a6d7991b
commit 7dc92c6a2d
4 changed files with 64 additions and 27 deletions

View file

@ -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]))

View file

@ -10,7 +10,22 @@
(s/keys :req-un [::domain/restic-repository ::domain/backup-path] (s/keys :req-un [::domain/restic-repository ::domain/backup-path]
:opt-un [::domain/certificate-file ::core/debug ::core/dry-run])) :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? (defn-spec init! nil?
[config ::config] [config ::config]
(let [config-w-defaults (merge core/default config)] (let [config-w-defaults (merge core/default config)]
(i/execute! (domain/init-repo-command config-w-defaults) config-w-defaults))) (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)))

View file

@ -13,25 +13,38 @@
(s/keys :req-un [::restic-repository ::backup-path] (s/keys :req-un [::restic-repository ::backup-path]
:opt-un [::certificate-file ::password-file])) :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 (defn-spec init-repo-command ::core/commands
[config ::config] [config ::config]
(let [{:keys [certificate-file password-file restic-repository backup-path]} config] (let [{:keys [certificate-file password-file restic-repository backup-path]} config]
(cond (repo-command config ["init"])))
(some? certificate-file)
[ ["restic" "-r" (str restic-repository "/" backup-path) "--cacert" certificate-file "-v" "init"]] (defn-spec unlock-repo-command ::core/commands
(some? password-file) [config ::config]
[["restic" "-r" (str restic-repository "/" backup-path) "--password-file" password-file "-v" "init"]] (let [{:keys [certificate-file password-file restic-repository backup-path]} config]
:else (repo-command config ["--cleanup-cache" "unlock"])))
[ ["restic" "-r" (str restic-repository "/" backup-path) "-v" "init"]]
)))
(defn-spec list-snapshot-command ::core/commands (defn-spec list-snapshot-command ::core/commands
[config ::config] [config ::config]
(let [{:keys [certificate-file password-file restic-repository backup-path]} config] (let [{:keys [certificate-file password-file restic-repository backup-path]} config]
(cond (repo-command config ["snapshots"])))
(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"]])))

View file

@ -5,26 +5,28 @@
[dda.backup.management.domain :as cut])) [dda.backup.management.domain :as cut]))
(st/instrument `cut/init-repo-command) (st/instrument `cut/init-repo-command)
(st/instrument `cut/unlock-repo-command)
(st/instrument `cut/list-snapshot-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 (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" (cut/list-snapshot-command {:certificate-file "ca"
:restic-repository "repo" :restic-repository "repo"
:backup-path "dir"}))) :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" (cut/list-snapshot-command {:password-file "password"
:restic-repository "repo" :restic-repository "repo"
:backup-path "dir"}))) :backup-path "dir"})))
(is (= [["restic" "-r" "repo/dir" "-v" "snapshots"]] (is (= [["restic" "-r" "repo/dir" "-v" "snapshots"]]
(cut/list-snapshot-command {:restic-repository "repo" (cut/list-snapshot-command {:restic-repository "repo"
:backup-path "dir"})))) :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"}))))