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]
'[dda.backup.management :as mgm]
'[dda.backup.postgresql :as ps]
'[dda.backup.postgresql :as pg]
'[dda.backup.backup :as bak]
'[dda.backup.restore :as rs])
(def restic-repo {:password-file "restic-pwd"
: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!
[]
(ps/create-pg-pass! (merge restic-repo {:backup-path "db"
:pg-db "mydb"
:pg-user "user"
:pg-password "password"})))
(pg/create-pg-pass! db-config))
(defn restic-repo-init!
[]
(spit "restic-pwd" "ThePassword")
(mgm/init! (merge restic-repo {:backup-path "files"}))
(mgm/init! (merge restic-repo {:backup-path "db" :dry-run true :debug true})))
(mgm/init! file-config)
(mgm/init! (merge db-config dry-run)))
(defn restic-backup!
[]
(tasks/shell "mkdir" "-p" "test-backup")
(spit "test-backup/file" "I was here")
(bak/backup! (merge restic-repo {:backup-path "files"
:files ["test-backup"]})))
(bak/backup! file-config))
(defn restic-restore!
[]
(tasks/shell "mkdir" "-p" "test-restore")
(rs/restore! (merge restic-repo {:backup-path "files"
:target-directory "test-restore"})))
(rs/restore! file-config)
(pg/drop-create-db! (merge db-config dry-run))
)
(prepare!)
(restic-repo-init!)

View file

@ -14,4 +14,4 @@
(defn-spec backup-files-command ::cd/commands
[config ::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]
:opt-un [::certificate-file ::password-file ::directory]))
(defn-spec repo-command ::commands
(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))]))
(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))))

View file

@ -10,24 +10,24 @@
(defn-spec check-repo-command ::cd/commands
[config ::config]
(cd/repo-command config ["check"]))
[(cd/repo-command config ["check"])])
(defn-spec init-repo-command ::cd/commands
[config ::config]
(cd/repo-command config ["init"]))
[(cd/repo-command config ["init"])])
(defn-spec unlock-repo-command ::cd/commands
[config ::config]
(cd/repo-command config ["--cleanup-cache" "unlock"]))
[(cd/repo-command config ["--cleanup-cache" "unlock"])])
(defn-spec list-snapshot-command ::cd/commands
[config ::config]
(cd/repo-command config ["snapshots"]))
[(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"])))
[(cd/repo-command config ["forget" "--group-by" ""
"--keep-last" "1"
"--keep-daily" (str days-to-keep)
"--keep-monthly" (str months-to-keep) "--prune"])]))

View file

@ -28,6 +28,7 @@
::cd/dry-run
::cd/debug]))
(defn-spec create-pg-pass! nil?
[config ::config]
(let [config-w-defaults (merge default config)]
@ -37,7 +38,7 @@
(defn-spec drop-create-db! nil?
[config ::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() {
;; local role_prefix="$1"; shift

View file

@ -15,5 +15,5 @@
(defn-spec restore-dir-command ::cd/commands
[config ::config]
(let [{:keys [target-directory snapshot-id]} config]
(into [["rm" "-rf" target-directory]]
(cd/repo-command config ["restore" snapshot-id "--target" target-directory]))))
[["rm" "-rf" 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"]]))))
(deftest should-calculate-repo-command
(is (= [[{:dir "dir"}
(is (= [{:dir "dir"}
"restic"
"-r"
"repo/dir"
@ -27,29 +27,29 @@
"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"]]
"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"]]
(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"]]
(is (= ["restic" "-r" "repo/dir" "-v" "snapshots"]
(cut/repo-command {:restic-repository "repo"
:backup-path "dir"
:days-to-keep 39