diff --git a/infrastructure/backup/test/resources/test.bb b/infrastructure/backup/test/resources/test.bb index eb37fa7..9228be7 100755 --- a/infrastructure/backup/test/resources/test.bb +++ b/infrastructure/backup/test/resources/test.bb @@ -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!) diff --git a/src/dda/backup/backup.clj b/src/dda/backup/backup.clj index 4b50176..89d177d 100644 --- a/src/dda/backup/backup.clj +++ b/src/dda/backup/backup.clj @@ -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) )) \ No newline at end of file diff --git a/src/dda/backup/backup/domain.clj b/src/dda/backup/backup/domain.clj index 33d0111..89d0186 100644 --- a/src/dda/backup/backup/domain.clj +++ b/src/dda/backup/backup/domain.clj @@ -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))])) diff --git a/src/dda/backup/infrastructure.clj b/src/dda/backup/infrastructure.clj index f8602e0..65991db 100644 --- a/src/dda/backup/infrastructure.clj +++ b/src/dda/backup/infrastructure.clj @@ -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 diff --git a/src/dda/backup/postgresql.clj b/src/dda/backup/postgresql.clj index 76d7414..6413a20 100644 --- a/src/dda/backup/postgresql.clj +++ b/src/dda/backup/postgresql.clj @@ -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))) diff --git a/src/dda/backup/postgresql/domain.clj b/src/dda/backup/postgresql/domain.clj index 5333019..f873605 100644 --- a/src/dda/backup/postgresql/domain.clj +++ b/src/dda/backup/postgresql/domain.clj @@ -11,23 +11,21 @@ (s/def ::pg-password string?) (s/def ::config - (s/keys :req-un [::pg-host - ::pg-port - ::pg-db - ::pg-password - ::pg-user - ::cd/restic-repository - ::cd/backup-path] - :opt-un [::cd/certificate-file - ::cd/password-file])) + (s/keys :req-un [::pg-host + ::pg-port + ::pg-db + ::pg-password + ::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 "\\\";\"")])])) diff --git a/src/dda/backup/restic.clj b/src/dda/backup/restic.clj index 1d26353..5be66a4 100644 --- a/src/dda/backup/restic.clj +++ b/src/dda/backup/restic.clj @@ -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))) \ No newline at end of file diff --git a/src/dda/backup/restic/domain.clj b/src/dda/backup/restic/domain.clj index 62c891f..d06ae11 100644 --- a/src/dda/backup/restic/domain.clj +++ b/src/dda/backup/restic/domain.clj @@ -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" diff --git a/src/dda/backup/restore.clj b/src/dda/backup/restore.clj index c191ace..7951ad0 100644 --- a/src/dda/backup/restore.clj +++ b/src/dda/backup/restore.clj @@ -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))) \ No newline at end of file diff --git a/src/dda/backup/restore/domain.clj b/src/dda/backup/restore/domain.clj index 25cec86..20f930e 100644 --- a/src/dda/backup/restore/domain.clj +++ b/src/dda/backup/restore/domain.clj @@ -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])]))