refactor for repo-command

This commit is contained in:
Michael Jerger 2024-08-20 08:17:51 +02:00
parent 1a49fe3c10
commit 582138a645
7 changed files with 61 additions and 51 deletions

View file

@ -2,38 +2,47 @@
(require '[babashka.tasks :as tasks] (require '[babashka.tasks :as tasks]
'[dda.backup.management :as mgm] '[dda.backup.management :as mgm]
'[dda.backup.postgresql :as ps] '[dda.backup.postgresql :as pg]
'[dda.backup.backup :as bak] '[dda.backup.backup :as bak]
'[dda.backup.restore :as rs]) '[dda.backup.restore :as rs])
(def restic-repo {:password-file "restic-pwd" (def restic-repo {:password-file "restic-pwd"
:restic-repository "restic-repo"}) :restic-repository "restic-repo"})
(def file-config (merge restic-repo {:backup-path "files"
:files ["test-backup"]
:target-directory "test-restore"}))
(def db-config (merge restic-repo {:backup-path "db"
:pg-db "mydb"
:pg-user "user"
:pg-password "password"}))
(def dry-run {:dry-run true :debug true})
(defn prepare! (defn prepare!
[] []
(ps/create-pg-pass! (merge restic-repo {:backup-path "db" (pg/create-pg-pass! db-config))
:pg-db "mydb"
:pg-user "user"
:pg-password "password"})))
(defn restic-repo-init! (defn restic-repo-init!
[] []
(spit "restic-pwd" "ThePassword") (spit "restic-pwd" "ThePassword")
(mgm/init! (merge restic-repo {:backup-path "files"})) (mgm/init! file-config)
(mgm/init! (merge restic-repo {:backup-path "db" :dry-run true :debug true}))) (mgm/init! (merge db-config dry-run)))
(defn restic-backup! (defn restic-backup!
[] []
(tasks/shell "mkdir" "-p" "test-backup") (tasks/shell "mkdir" "-p" "test-backup")
(spit "test-backup/file" "I was here") (spit "test-backup/file" "I was here")
(bak/backup! (merge restic-repo {:backup-path "files" (bak/backup! file-config))
:files ["test-backup"]})))
(defn restic-restore! (defn restic-restore!
[] []
(tasks/shell "mkdir" "-p" "test-restore") (tasks/shell "mkdir" "-p" "test-restore")
(rs/restore! (merge restic-repo {:backup-path "files" (rs/restore! file-config)
:target-directory "test-restore"}))) (pg/drop-create-db! (merge db-config dry-run))
)
(prepare!) (prepare!)
(restic-repo-init!) (restic-repo-init!)

View file

@ -14,4 +14,4 @@
(defn-spec backup-files-command ::cd/commands (defn-spec backup-files-command ::cd/commands
[config ::config] [config ::config]
(let [{:keys [files]} config] (let [{:keys [files]} config]
(cd/repo-command config (into ["backup"] files)))) [(cd/repo-command config (into ["backup"] files))]))

View file

@ -20,22 +20,22 @@
(s/keys :req-un [::restic-repository ::backup-path ::days-to-keep ::months-to-keep] (s/keys :req-un [::restic-repository ::backup-path ::days-to-keep ::months-to-keep]
:opt-un [::certificate-file ::password-file ::directory])) :opt-un [::certificate-file ::password-file ::directory]))
(defn-spec repo-command ::commands (defn-spec repo-command ::command
[config ::config [config ::config
command ::command] command ::command]
(let [{:keys [certificate-file password-file directory restic-repository backup-path]} config] (let [{:keys [certificate-file password-file directory restic-repository backup-path]} config]
[(into (into
[] []
(concat (concat
(if (some? directory) (if (some? directory)
[{:dir directory}] [{:dir directory}]
[]) [])
["restic" "-r" (str restic-repository "/" backup-path) "-v"] ["restic" "-r" (str restic-repository "/" backup-path) "-v"]
(cond (cond
(some? certificate-file) (some? certificate-file)
["--cacert" certificate-file] ["--cacert" certificate-file]
(some? password-file) (some? password-file)
["--password-file" password-file] ["--password-file" password-file]
:else :else
[]) [])
command))])) command))))

View file

@ -10,24 +10,24 @@
(defn-spec check-repo-command ::cd/commands (defn-spec check-repo-command ::cd/commands
[config ::config] [config ::config]
(cd/repo-command config ["check"])) [(cd/repo-command config ["check"])])
(defn-spec init-repo-command ::cd/commands (defn-spec init-repo-command ::cd/commands
[config ::config] [config ::config]
(cd/repo-command config ["init"])) [(cd/repo-command config ["init"])])
(defn-spec unlock-repo-command ::cd/commands (defn-spec unlock-repo-command ::cd/commands
[config ::config] [config ::config]
(cd/repo-command config ["--cleanup-cache" "unlock"])) [(cd/repo-command config ["--cleanup-cache" "unlock"])])
(defn-spec list-snapshot-command ::cd/commands (defn-spec list-snapshot-command ::cd/commands
[config ::config] [config ::config]
(cd/repo-command config ["snapshots"])) [(cd/repo-command config ["snapshots"])])
(defn-spec forget-command ::cd/commands (defn-spec forget-command ::cd/commands
[config ::config] [config ::config]
(let [{:keys [days-to-keep months-to-keep]} config] (let [{:keys [days-to-keep months-to-keep]} config]
(cd/repo-command config ["forget" "--group-by" "" [(cd/repo-command config ["forget" "--group-by" ""
"--keep-last" "1" "--keep-last" "1"
"--keep-daily" (str days-to-keep) "--keep-daily" (str days-to-keep)
"--keep-monthly" (str months-to-keep) "--prune"]))) "--keep-monthly" (str months-to-keep) "--prune"])]))

View file

@ -28,6 +28,7 @@
::cd/dry-run ::cd/dry-run
::cd/debug])) ::cd/debug]))
(defn-spec create-pg-pass! nil? (defn-spec create-pg-pass! nil?
[config ::config] [config ::config]
(let [config-w-defaults (merge default config)] (let [config-w-defaults (merge default config)]
@ -37,7 +38,7 @@
(defn-spec drop-create-db! nil? (defn-spec drop-create-db! nil?
[config ::config] [config ::config]
(let [config-w-defaults (merge default config)] (let [config-w-defaults (merge default config)]
((i/execute! (domain/db-drop-create-command config-w-defaults))))) (i/execute! (domain/db-drop-create-command config-w-defaults) config-w-defaults)))
;; function backup-roles() { ;; function backup-roles() {
;; local role_prefix="$1"; shift ;; local role_prefix="$1"; shift

View file

@ -15,5 +15,5 @@
(defn-spec restore-dir-command ::cd/commands (defn-spec restore-dir-command ::cd/commands
[config ::config] [config ::config]
(let [{:keys [target-directory snapshot-id]} config] (let [{:keys [target-directory snapshot-id]} config]
(into [["rm" "-rf" target-directory]] [["rm" "-rf" target-directory]
(cd/repo-command config ["restore" snapshot-id "--target" target-directory])))) (cd/repo-command config ["restore" snapshot-id "--target" target-directory])]))

View file

@ -18,7 +18,7 @@
["restic" "-r" "repo/dir" "-v" "init" "--cacert" "ca"]])))) ["restic" "-r" "repo/dir" "-v" "init" "--cacert" "ca"]]))))
(deftest should-calculate-repo-command (deftest should-calculate-repo-command
(is (= [[{:dir "dir"} (is (= [{:dir "dir"}
"restic" "restic"
"-r" "-r"
"repo/dir" "repo/dir"
@ -27,29 +27,29 @@
"ca" "ca"
"backup" "backup"
"file1" "file1"
"file2"]] "file2"]
(cut/repo-command {:certificate-file "ca" (cut/repo-command {:certificate-file "ca"
:directory "dir" :directory "dir"
:restic-repository "repo" :restic-repository "repo"
:backup-path "dir" :backup-path "dir"
:days-to-keep 39 :days-to-keep 39
:months-to-keep 3} :months-to-keep 3}
["backup" "file1" "file2"]))) ["backup" "file1" "file2"])))
(is (= [["restic" "-r" "repo/dir" "-v" "--cacert" "ca" "snapshots"]] (is (= ["restic" "-r" "repo/dir" "-v" "--cacert" "ca" "snapshots"]
(cut/repo-command {:certificate-file "ca" (cut/repo-command {:certificate-file "ca"
:restic-repository "repo" :restic-repository "repo"
:backup-path "dir" :backup-path "dir"
:days-to-keep 39 :days-to-keep 39
:months-to-keep 3} :months-to-keep 3}
["snapshots"]))) ["snapshots"])))
(is (= [["restic" "-r" "repo/dir" "-v" "--password-file" "password" "snapshots"]] (is (= ["restic" "-r" "repo/dir" "-v" "--password-file" "password" "snapshots"]
(cut/repo-command {:password-file "password" (cut/repo-command {:password-file "password"
:restic-repository "repo" :restic-repository "repo"
:backup-path "dir" :backup-path "dir"
:days-to-keep 39 :days-to-keep 39
:months-to-keep 3} :months-to-keep 3}
["snapshots"]))) ["snapshots"])))
(is (= [["restic" "-r" "repo/dir" "-v" "snapshots"]] (is (= ["restic" "-r" "repo/dir" "-v" "snapshots"]
(cut/repo-command {:restic-repository "repo" (cut/repo-command {:restic-repository "repo"
:backup-path "dir" :backup-path "dir"
:days-to-keep 39 :days-to-keep 39