finished refactoring

This commit is contained in:
Michael Jerger 2024-08-20 18:06:24 +02:00
parent 5d2ff87564
commit f2c382190d
10 changed files with 72 additions and 104 deletions

View file

@ -35,14 +35,13 @@
[]
(tasks/shell "mkdir" "-p" "test-backup")
(spit "test-backup/file" "I was here")
(bak/backup! file-config))
(bak/backup-file! file-config))
(defn restic-restore!
[]
(tasks/shell "mkdir" "-p" "test-restore")
(rs/restore! file-config)
(pg/drop-create-db! (merge db-config dry-run))
)
(rs/restore-file! file-config)
(pg/drop-create-db! (merge db-config dry-run)))
(prepare!)
(restic-repo-init!)

View file

@ -2,30 +2,21 @@
(:require
[orchestra.core :refer [defn-spec]]
[clojure.spec.alpha :as s]
[dda.backup.core.domain :as cd]
[dda.backup.backup.domain :as domain]
[dda.backup.core :as core]
[dda.backup.management :as mgm]
[dda.backup.restic :as restic]
[dda.backup.infrastructure :as i]))
(s/def ::config
(s/keys :req-un [::domain/files
::cd/restic-repository
::cd/backup-path]
:opt-un [::cd/certificate-file
::cd/password-file
::cd/days-to-keep
::cd/months-to-keep
::cd/directory
::cd/dry-run
::cd/debug]))
(s/def ::backup-file-config
(s/merge ::restic/restic-config
(s/keys :req-un [::domain/files]
:opt-un [])))
(defn-spec backup! nil?
[config ::config]
(let [config-w-defaults (merge core/default config)]
(mgm/unlock! config-w-defaults)
(defn-spec backup-file! nil?
[config ::backup-file-config]
(let [config-w-defaults (merge restic/default config)]
(restic/unlock! config-w-defaults)
(i/execute!
(domain/backup-files-command config-w-defaults)
config-w-defaults)
(mgm/forget! config-w-defaults)
(restic/forget! config-w-defaults)
))

View file

@ -7,11 +7,11 @@
(s/def ::files (s/+ string?))
(s/def ::config
(s/def ::backup-file-config
(s/merge ::rd/config
(s/keys :req-un [::files])))
(defn-spec backup-files-command ::cd/commands
[config ::config]
[config ::backup-file-config]
(let [{:keys [files]} config]
[(rd/repo-command config (into ["backup"] files))]))

View file

@ -5,7 +5,7 @@
(defn-spec execute! nil?
[commands ::core/command
config ::core/config]
config ::core/execution]
(let [{:keys [dry-run debug]} config]
(doseq [c commands]
(when debug

View file

@ -2,7 +2,6 @@
(:require
[orchestra.core :refer [defn-spec]]
[clojure.spec.alpha :as s]
[dda.backup.core.domain :as cd]
[dda.backup.postgresql.domain :as domain]
[dda.backup.core :as core]
[dda.backup.infrastructure :as i]))
@ -12,31 +11,23 @@
{:pg-host "localhost"
:pg_port 5432}))
(s/def ::config
(s/def ::psql-config
(s/merge ::core/execution
(s/keys :req-un [::domain/pg-db
::domain/pg-user
::domain/pg-password
::cd/restic-repository
::cd/backup-path]
::domain/pg-password]
:opt-un [::domain/pg-host
::domain/pg-port
::cd/certificate-file
::cd/password-file
::cd/days-to-keep
::cd/months-to-keep
::cd/directory
::cd/dry-run
::cd/debug]))
::domain/pg-port])))
(defn-spec create-pg-pass! nil?
[config ::config]
[config ::psql-config]
(let [config-w-defaults (merge default config)]
(spit "/root/.pgpass" (domain/pgpass config-w-defaults))))
(defn-spec drop-create-db! nil?
[config ::config]
[config ::psql-config]
(let [config-w-defaults (merge default config)]
(i/execute! (domain/db-drop-create-command config-w-defaults) config-w-defaults)))

View file

@ -15,19 +15,17 @@
::pg-port
::pg-db
::pg-password
::pg-user
::cd/restic-repository
::cd/backup-path]
:opt-un [::cd/certificate-file
::cd/password-file]))
::pg-user]
:opt-un []))
(defn-spec psql-command ::cd/command
[config ::config
command string?]
command ::cd/command]
(let [{:keys [pg-host pg-port pg-user]} config]
["psql" "-d" "template1" "-h" pg-host "-p" (str pg-port) "-U" pg-user
"--no-password" "-c" command]))
(into
["psql" "-d" "template1" "-h" pg-host "-p" (str pg-port) "-U" pg-user
"--no-password" "-c"]
command)))
(defn-spec pgpass string?
[config ::config]
@ -37,5 +35,5 @@
(defn-spec db-drop-create-command ::cd/commands
[config ::config]
(let [{:keys [pg-db]} config]
[(psql-command config (str "\"DROP DATABASE \\\"" pg-db "\\\";\""))
(psql-command config (str "\"CREATE DATABASE \\\"" pg-db "\\\";\""))]))
[(psql-command config [(str "\"DROP DATABASE \\\"" pg-db "\\\";\"")])
(psql-command config [(str "\"CREATE DATABASE \\\"" pg-db "\\\";\"")])]))

View file

@ -2,7 +2,6 @@
(:require
[orchestra.core :refer [defn-spec]]
[clojure.spec.alpha :as s]
[dda.backup.core.domain :as cd]
[dda.backup.restic.domain :as domain]
[dda.backup.core :as core]
[dda.backup.infrastructure :as i]))
@ -12,37 +11,35 @@
{:days-to-keep 30
:months-to-keep 12}))
(s/def ::config
(s/keys :req-un [::cd/restic-repository
::cd/backup-path]
:opt-un [::cd/certificate-file
::cd/password-file
::cd/days-to-keep
::cd/months-to-keep
::cd/directory
::cd/dry-run
::cd/debug]))
(s/def ::restic-config
(s/merge ::core/execution
(s/keys :req-un [::domain/restic-repository
::domain/backup-path]
:opt-un [::domain/certificate-file
::domain/password-file
::domain/days-to-keep
::domain/months-to-keep])))
(defn-spec initalized? boolean?
[config ::config]
(let [config-w-defaults (merge core/default config)]
[restic-config ::restic-config]
(let [config-w-defaults (merge core/default restic-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)]
[restic-config ::restic-config]
(let [config-w-defaults (merge core/default restic-config)]
(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)]
[restic-config ::restic-config]
(let [config-w-defaults (merge core/default restic-config)]
(i/execute! (domain/unlock-repo-command config-w-defaults) config-w-defaults)))
(defn-spec forget! nil?
[config ::config]
(let [config-w-defaults (merge core/default config)]
[restic-config ::restic-config]
(let [config-w-defaults (merge core/default restic-config)]
(i/execute! (domain/forget-command config-w-defaults) config-w-defaults)))

View file

@ -11,7 +11,7 @@
(s/def ::days-to-keep pos?)
(s/def ::months-to-keep pos?)
(s/def ::config
(s/def ::restic-config
(s/keys :req-un [::restic-repository
::backup-path
::days-to-keep
@ -21,9 +21,10 @@
::cd/execution-directory]))
(defn-spec repo-command ::cd/command
[config ::config
[config ::restic-config
command ::cd/command]
(let [{:keys [certificate-file password-file execution-directory restic-repository backup-path]} config]
(let [{:keys [certificate-file password-file execution-directory
restic-repository backup-path]} config]
(into
[]
(concat
@ -41,23 +42,23 @@
command))))
(defn-spec check-repo-command ::cd/commands
[config ::config]
[config ::restic-config]
[(repo-command config ["check"])])
(defn-spec init-repo-command ::cd/commands
[config ::config]
[config ::restic-config]
[(repo-command config ["init"])])
(defn-spec unlock-repo-command ::cd/commands
[config ::config]
[config ::restic-config]
[(repo-command config ["--cleanup-cache" "unlock"])])
(defn-spec list-snapshot-command ::cd/commands
[config ::config]
[config ::restic-config]
[(repo-command config ["snapshots"])])
(defn-spec forget-command ::cd/commands
[config ::config]
[config ::restic-config]
(let [{:keys [days-to-keep months-to-keep]} config]
[(repo-command config ["forget" "--group-by" ""
"--keep-last" "1"

View file

@ -2,31 +2,22 @@
(:require
[orchestra.core :refer [defn-spec]]
[clojure.spec.alpha :as s]
[dda.backup.core.domain :as cd]
[dda.backup.restore.domain :as domain]
[dda.backup.core :as core]
[dda.backup.management :as mgm]
[dda.backup.restic :as restic]
[dda.backup.infrastructure :as i]))
(def default (merge core/default {:snapshot-id "latest"}))
(def default (merge restic/default
{:snapshot-id "latest"}))
(s/def ::config
(s/keys :req-un [::domain/target-directory
::cd/restic-repository
::cd/backup-path]
:opt-un [::domain/snapshot-id
::cd/certificate-file
::cd/password-file
::cd/days-to-keep
::cd/months-to-keep
::cd/directory
::cd/dry-run
::cd/debug]))
(s/def ::restore-file-config
(s/merge ::restic/restic-config
(s/keys :req-un [::domain/target-directory]
:opt-un [::domain/snapshot-id])))
(defn-spec restore! nil?
[config ::config]
(defn-spec restore-file! nil?
[config ::restore-file-config]
(let [config-w-defaults (merge default config)]
(mgm/unlock! config-w-defaults)
(restic/unlock! config-w-defaults)
(i/execute!
(domain/restore-dir-command config-w-defaults)
config-w-defaults)))

View file

@ -8,13 +8,13 @@
(s/def ::target-directory string?)
(s/def ::snapshot-id string?)
(s/def ::config
(s/def ::restore-file-config
(s/merge ::rd/config
(s/keys :req-un [::target-directory
::snapshot-id])))
(defn-spec restore-dir-command ::cd/commands
[config ::config]
[config ::restore-file-config]
(let [{:keys [target-directory snapshot-id]} config]
[["rm" "-rf" target-directory]
(rd/repo-command config ["restore" snapshot-id "--target" target-directory])]))