reuse & rename core config

This commit is contained in:
Michael Jerger 2024-08-20 17:15:27 +02:00
parent 582138a645
commit 5d2ff87564
12 changed files with 182 additions and 171 deletions

View file

@ -1,7 +1,7 @@
#!/usr/bin/env bb
(require '[babashka.tasks :as tasks]
'[dda.backup.management :as mgm]
'[dda.backup.restic :as rc]
'[dda.backup.postgresql :as pg]
'[dda.backup.backup :as bak]
'[dda.backup.restore :as rs])
@ -28,8 +28,8 @@
(defn restic-repo-init!
[]
(spit "restic-pwd" "ThePassword")
(mgm/init! file-config)
(mgm/init! (merge db-config dry-run)))
(rc/init! file-config)
(rc/init! (merge db-config dry-run)))
(defn restic-backup!
[]

View file

@ -2,16 +2,16 @@
(:require
[orchestra.core :refer [defn-spec]]
[clojure.spec.alpha :as s]
[dda.backup.core.domain :as cd]))
[dda.backup.core.domain :as cd]
[dda.backup.restic.domain :as rd]))
(s/def ::files (s/+ string?))
(s/def ::config
(s/keys :req-un [::files
::cd/restic-repository ::cd/backup-path]
:opt-un [::cd/certificate-file ::cd/password-file ::cd/directory]))
(s/merge ::rd/config
(s/keys :req-un [::files])))
(defn-spec backup-files-command ::cd/commands
[config ::config]
(let [{:keys [files]} config]
[(cd/repo-command config (into ["backup"] files))]))
[(rd/repo-command config (into ["backup"] files))]))

View file

@ -1,11 +1,12 @@
(ns dda.backup.core
(:require [clojure.spec.alpha :as s]
))
(s/def ::dry-run boolean?)
(s/def ::debug boolean?)
(:require
[clojure.spec.alpha :as s]))
(def default {:dry-run false
:debug false
:days-to-keep 30
:months-to-keep 12})
:debug false})
(s/def ::execution
(s/keys :req-un []
:opt-un [::dry-run
::debug
::execution-directory]))

View file

@ -1,6 +1,5 @@
(ns dda.backup.core.domain
(:require
[orchestra.core :refer [defn-spec]]
[clojure.spec.alpha :as s]))
(s/def ::command (s/cat
@ -8,34 +7,10 @@
:params (s/* string?)))
(s/def ::commands (s/coll-of ::command))
(s/def ::certificate-file string?)
(s/def ::password-file string?)
(s/def ::restic-repository string?)
(s/def ::backup-path string?)
(s/def ::directory string?)
(s/def ::days-to-keep pos?)
(s/def ::months-to-keep pos?)
(s/def ::dry-run boolean?)
(s/def ::debug boolean?)
(s/def ::execution-directory string?)
(s/def ::config
(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 ::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))))
(s/def ::execution
(s/keys :req-un [::dry-run ::debug]
:opt-un [::execution-directory]))

View file

@ -1,33 +0,0 @@
(ns dda.backup.management.domain
(:require
[orchestra.core :refer [defn-spec]]
[clojure.spec.alpha :as s]
[dda.backup.core.domain :as cd]))
(s/def ::config
(s/keys :req-un [::cd/restic-repository ::cd/backup-path ::cd/days-to-keep ::cd/months-to-keep]
:opt-un [::cd/certificate-file ::cd/password-file]))
(defn-spec check-repo-command ::cd/commands
[config ::config]
[(cd/repo-command config ["check"])])
(defn-spec init-repo-command ::cd/commands
[config ::config]
[(cd/repo-command config ["init"])])
(defn-spec unlock-repo-command ::cd/commands
[config ::config]
[(cd/repo-command config ["--cleanup-cache" "unlock"])])
(defn-spec list-snapshot-command ::cd/commands
[config ::config]
[(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"])]))

View file

@ -1,21 +1,26 @@
(ns dda.backup.management
(ns dda.backup.restic
(:require
[orchestra.core :refer [defn-spec]]
[clojure.spec.alpha :as s]
[dda.backup.core.domain :as cd]
[dda.backup.management.domain :as domain]
[dda.backup.restic.domain :as domain]
[dda.backup.core :as core]
[dda.backup.infrastructure :as i]))
(def default
(merge core/default
{:days-to-keep 30
:months-to-keep 12}))
(s/def ::config
(s/keys :req-un [::cd/restic-repository
(s/keys :req-un [::cd/restic-repository
::cd/backup-path]
:opt-un [::cd/certificate-file
:opt-un [::cd/certificate-file
::cd/password-file
::cd/days-to-keep
::cd/months-to-keep
::cd/directory
::cd/dry-run
::cd/days-to-keep
::cd/months-to-keep
::cd/directory
::cd/dry-run
::cd/debug]))
(defn-spec initalized? boolean?
@ -24,8 +29,7 @@
(try
(i/execute! (domain/check-repo-command config-w-defaults) config-w-defaults)
true
(catch Exception e false)
)))
(catch Exception e false))))
(defn-spec init! nil?
[config ::config]
@ -41,4 +45,4 @@
(defn-spec forget! nil?
[config ::config]
(let [config-w-defaults (merge core/default config)]
(i/execute! (domain/forget-command config-w-defaults) config-w-defaults)))
(i/execute! (domain/forget-command config-w-defaults) config-w-defaults)))

View file

@ -0,0 +1,65 @@
(ns dda.backup.restic.domain
(:require
[orchestra.core :refer [defn-spec]]
[clojure.spec.alpha :as s]
[dda.backup.core.domain :as cd]))
(s/def ::certificate-file string?)
(s/def ::password-file string?)
(s/def ::restic-repository string?)
(s/def ::backup-path string?)
(s/def ::days-to-keep pos?)
(s/def ::months-to-keep pos?)
(s/def ::config
(s/keys :req-un [::restic-repository
::backup-path
::days-to-keep
::months-to-keep]
:opt-un [::certificate-file
::password-file
::cd/execution-directory]))
(defn-spec repo-command ::cd/command
[config ::config
command ::cd/command]
(let [{:keys [certificate-file password-file execution-directory restic-repository backup-path]} config]
(into
[]
(concat
(if (some? execution-directory)
[{:dir execution-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))))
(defn-spec check-repo-command ::cd/commands
[config ::config]
[(repo-command config ["check"])])
(defn-spec init-repo-command ::cd/commands
[config ::config]
[(repo-command config ["init"])])
(defn-spec unlock-repo-command ::cd/commands
[config ::config]
[(repo-command config ["--cleanup-cache" "unlock"])])
(defn-spec list-snapshot-command ::cd/commands
[config ::config]
[(repo-command config ["snapshots"])])
(defn-spec forget-command ::cd/commands
[config ::config]
(let [{:keys [days-to-keep months-to-keep]} config]
[(repo-command config ["forget" "--group-by" ""
"--keep-last" "1"
"--keep-daily" (str days-to-keep)
"--keep-monthly" (str months-to-keep) "--prune"])]))

View file

@ -2,18 +2,19 @@
(:require
[orchestra.core :refer [defn-spec]]
[clojure.spec.alpha :as s]
[dda.backup.core.domain :as cd]))
[dda.backup.core.domain :as cd]
[dda.backup.restic.domain :as rd]))
(s/def ::target-directory string?)
(s/def ::snapshot-id string?)
(s/def ::config
(s/keys :req-un [::target-directory ::snapshot-id
::cd/restic-repository ::cd/backup-path]
:opt-un [::cd/certificate-file ::cd/password-file ::cd/directory]))
(s/merge ::rd/config
(s/keys :req-un [::target-directory
::snapshot-id])))
(defn-spec restore-dir-command ::cd/commands
[config ::config]
(let [{:keys [target-directory snapshot-id]} config]
[["rm" "-rf" target-directory]
(cd/repo-command config ["restore" snapshot-id "--target" target-directory])]))
(rd/repo-command config ["restore" snapshot-id "--target" target-directory])]))

View file

@ -17,7 +17,7 @@
"file2"]]
(cut/backup-files-command {:restic-repository "repo"
:backup-path "dir-at-repo"
:directory "dir-to-backup"
:execution-directory "dir-to-backup"
:days-to-keep 39
:months-to-keep 3
:files ["file2" "file2"]}))))

View file

@ -2,11 +2,8 @@
(:require
[clojure.test :refer [deftest is are testing run-tests]]
[clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as st]
[dda.backup.core.domain :as cut]))
(st/instrument `cut/repo-command)
(deftest should-verify-command
(is (= {:app "restic", :params ["-r" "repo/dir" "-v" "init" "--cacert" "ca"]}
(s/conform ::cut/command ["restic" "-r" "repo/dir" "-v" "init" "--cacert" "ca"]))))
@ -16,42 +13,3 @@
{:app "restic", :params ["-r" "repo/dir" "-v" "init" "--cacert" "ca"]}]
(s/conform ::cut/commands [["ls"]
["restic" "-r" "repo/dir" "-v" "init" "--cacert" "ca"]]))))
(deftest should-calculate-repo-command
(is (= [{:dir "dir"}
"restic"
"-r"
"repo/dir"
"-v"
"--cacert"
"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"]
(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"]
(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"]
(cut/repo-command {:restic-repository "repo"
:backup-path "dir"
:days-to-keep 39
:months-to-keep 3}
["snapshots"]))))

View file

@ -1,32 +0,0 @@
(ns dda.backup.management.domain-test
(:require
[clojure.test :refer [deftest is are testing run-tests]]
[clojure.spec.test.alpha :as st]
[dda.backup.management.domain :as cut]))
(st/instrument `cut/init-repo-command)
(st/instrument `cut/unlock-repo-command)
(st/instrument `cut/forget-command)
(deftest should-calculate-init-repo-command
(is (= [["restic" "-r" "repo/dir" "-v" "init"]]
(cut/init-repo-command {:restic-repository "repo"
:backup-path "dir"
:days-to-keep 39
:months-to-keep 3}))))
(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"
:days-to-keep 39
:months-to-keep 3}))))
(deftest should-calculate-forget-command
(is (= [["restic" "-r" "repo/dir" "-v" "forget"
"--group-by" "" "--keep-last" "1"
"--keep-daily" "39" "--keep-monthly" "3" "--prune"]]
(cut/forget-command {:restic-repository "repo"
:backup-path "dir"
:days-to-keep 39
:months-to-keep 3}))))

View file

@ -0,0 +1,72 @@
(ns dda.backup.restic.domain-test
(:require
[clojure.test :refer [deftest is are testing run-tests]]
[clojure.spec.test.alpha :as st]
[dda.backup.restic.domain :as cut]))
(st/instrument `cut/repo-command)
(st/instrument `cut/init-repo-command)
(st/instrument `cut/unlock-repo-command)
(st/instrument `cut/forget-command)
(deftest should-calculate-repo-command
(is (= [{:dir "dir"}
"restic"
"-r"
"repo/dir"
"-v"
"--cacert"
"ca"
"backup"
"file1"
"file2"]
(cut/repo-command {:certificate-file "ca"
:execution-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"]
(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"]
(cut/repo-command {:restic-repository "repo"
:backup-path "dir"
:days-to-keep 39
:months-to-keep 3}
["snapshots"]))))
(deftest should-calculate-init-repo-command
(is (= [["restic" "-r" "repo/dir" "-v" "init"]]
(cut/init-repo-command {:restic-repository "repo"
:backup-path "dir"
:days-to-keep 39
:months-to-keep 3}))))
(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"
:days-to-keep 39
:months-to-keep 3}))))
(deftest should-calculate-forget-command
(is (= [["restic" "-r" "repo/dir" "-v" "forget"
"--group-by" "" "--keep-last" "1"
"--keep-daily" "39" "--keep-monthly" "3" "--prune"]]
(cut/forget-command {:restic-repository "repo"
:backup-path "dir"
:days-to-keep 39
:months-to-keep 3}))))