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]
: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)))
(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]
: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"])))

View file

@ -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"}))))