From a941665f061b9aaed00173352cb55cfc71b271fe Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Fri, 16 Aug 2024 19:13:11 +0200 Subject: [PATCH] refactoring: mv params to map --- src/dda/backup/backup.clj | 7 +++---- src/dda/backup/backup/domain.clj | 12 +++++++++--- src/dda/backup/management/domain.clj | 15 ++++++++++----- src/dda/backup/restore.clj | 24 ++++++++++++++++++++++++ src/dda/backup/restore/domain.clj | 19 +++++++++++++++++++ test/dda/backup/backup/domain_test.clj | 4 ++-- test/dda/backup/restore/domain_test.clj | 24 ++++++++++++++++++++++++ 7 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 src/dda/backup/restore.clj create mode 100644 src/dda/backup/restore/domain.clj create mode 100644 test/dda/backup/restore/domain_test.clj diff --git a/src/dda/backup/backup.clj b/src/dda/backup/backup.clj index 14c2f2d..98ca16c 100644 --- a/src/dda/backup/backup.clj +++ b/src/dda/backup/backup.clj @@ -9,16 +9,15 @@ [dda.backup.infrastructure :as i])) (s/def ::config - (s/keys :req-un [::cd/restic-repository ::cd/backup-path] + (s/keys :req-un [::domain/files ::cd/restic-repository ::cd/backup-path] :opt-un [::cd/certificate-file ::cd/directory ::cd/debug ::cd/dry-run])) (defn-spec backup! nil? - [config ::config - files (s/* string?)] + [config ::config] (let [config-w-defaults (merge core/default config)] (mgm/unlock! config-w-defaults) (i/execute! - (domain/backup-files-command config-w-defaults files) + (domain/backup-files-command config-w-defaults) config-w-defaults) (mgm/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 16e30dd..53712b0 100644 --- a/src/dda/backup/backup/domain.clj +++ b/src/dda/backup/backup/domain.clj @@ -4,8 +4,14 @@ [clojure.spec.alpha :as s] [dda.backup.core.domain :as cd])) +(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])) + (defn-spec backup-files-command ::cd/commands - [config ::cd/config - files (s/* string?)] - (let [{:keys [certificate-file password-file restic-repository backup-path]} config] + [config ::config] + (let [{:keys [files]} config] (cd/repo-command config (into ["backup"] files)))) diff --git a/src/dda/backup/management/domain.clj b/src/dda/backup/management/domain.clj index cd95f44..0520628 100644 --- a/src/dda/backup/management/domain.clj +++ b/src/dda/backup/management/domain.clj @@ -1,26 +1,31 @@ (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 ::cd/config] + [config ::config] (cd/repo-command config ["check"])) (defn-spec init-repo-command ::cd/commands - [config ::cd/config] + [config ::config] (cd/repo-command config ["init"])) (defn-spec unlock-repo-command ::cd/commands - [config ::cd/config] + [config ::config] (cd/repo-command config ["--cleanup-cache" "unlock"])) (defn-spec list-snapshot-command ::cd/commands - [config ::cd/config] + [config ::config] (cd/repo-command config ["snapshots"])) (defn-spec forget-command ::cd/commands - [config ::cd/config] + [config ::config] (let [{:keys [days-to-keep months-to-keep]} config] (cd/repo-command config ["forget" "--group-by" "" "--keep-last" "1" diff --git a/src/dda/backup/restore.clj b/src/dda/backup/restore.clj new file mode 100644 index 0000000..aecfb34 --- /dev/null +++ b/src/dda/backup/restore.clj @@ -0,0 +1,24 @@ +(ns dda.backup.restore + (: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.infrastructure :as i])) + +(def default (merge core/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/directory ::cd/debug ::cd/dry-run])) + +(defn-spec restore! nil? + [config ::config] + (let [config-w-defaults (merge default config)] + (mgm/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 new file mode 100644 index 0000000..3d7c392 --- /dev/null +++ b/src/dda/backup/restore/domain.clj @@ -0,0 +1,19 @@ +(ns dda.backup.restore.domain + (:require + [orchestra.core :refer [defn-spec]] + [clojure.spec.alpha :as s] + [dda.backup.core.domain :as cd])) + +(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])) + +(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])))) diff --git a/test/dda/backup/backup/domain_test.clj b/test/dda/backup/backup/domain_test.clj index 17496d7..4444819 100644 --- a/test/dda/backup/backup/domain_test.clj +++ b/test/dda/backup/backup/domain_test.clj @@ -19,5 +19,5 @@ :backup-path "dir-at-repo" :directory "dir-to-backup" :days-to-keep 39 - :months-to-keep 3} - ["file2" "file2"])))) + :months-to-keep 3 + :files ["file2" "file2"]})))) diff --git a/test/dda/backup/restore/domain_test.clj b/test/dda/backup/restore/domain_test.clj new file mode 100644 index 0000000..ec761ca --- /dev/null +++ b/test/dda/backup/restore/domain_test.clj @@ -0,0 +1,24 @@ +(ns dda.backup.restore.domain-test + (:require + [clojure.test :refer [deftest is are testing run-tests]] + [clojure.spec.test.alpha :as st] + [dda.backup.restore.domain :as cut])) + +(st/instrument `cut/restore-dir-command) + +(deftest should-calculate-restore-dir + (is (= [["rm" "-rf" "dir-to-backup"] + ["restic" + "-r" + "repo/dir-at-repo" + "-v" + "restore" + "latest" + "--target" + "dir-to-backup"]] + (cut/restore-dir-command {:restic-repository "repo" + :backup-path "dir-at-repo" + :target-directory "dir-to-backup" + :days-to-keep 39 + :months-to-keep 3 + :snapshot-id "latest"}))))