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") (tasks/shell "mkdir" "-p" "test-backup")
(spit "test-backup/file" "I was here") (spit "test-backup/file" "I was here")
(bak/backup! file-config)) (bak/backup-file! file-config))
(defn restic-restore! (defn restic-restore!
[] []
(tasks/shell "mkdir" "-p" "test-restore") (tasks/shell "mkdir" "-p" "test-restore")
(rs/restore! file-config) (rs/restore-file! file-config)
(pg/drop-create-db! (merge db-config dry-run)) (pg/drop-create-db! (merge db-config dry-run)))
)
(prepare!) (prepare!)
(restic-repo-init!) (restic-repo-init!)

View file

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

View file

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

View file

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

View file

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

View file

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

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

View file

@ -2,31 +2,22 @@
(:require (:require
[orchestra.core :refer [defn-spec]] [orchestra.core :refer [defn-spec]]
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
[dda.backup.core.domain :as cd]
[dda.backup.restore.domain :as domain] [dda.backup.restore.domain :as domain]
[dda.backup.core :as core] [dda.backup.restic :as restic]
[dda.backup.management :as mgm]
[dda.backup.infrastructure :as i])) [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/def ::restore-file-config
(s/keys :req-un [::domain/target-directory (s/merge ::restic/restic-config
::cd/restic-repository (s/keys :req-un [::domain/target-directory]
::cd/backup-path] :opt-un [::domain/snapshot-id])))
: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]))
(defn-spec restore! nil? (defn-spec restore-file! nil?
[config ::config] [config ::restore-file-config]
(let [config-w-defaults (merge default config)] (let [config-w-defaults (merge default config)]
(mgm/unlock! config-w-defaults) (restic/unlock! config-w-defaults)
(i/execute! (i/execute!
(domain/restore-dir-command config-w-defaults) (domain/restore-dir-command config-w-defaults)
config-w-defaults))) config-w-defaults)))

View file

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