From f4f67b580a7e6f72abab13dd277268fccfed5df0 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Mon, 30 Dec 2024 10:25:45 +0100 Subject: [PATCH] feat: add restore db roles --- infrastructure/backup/test/resources/test.bb | 11 +++++++++- src/dda/backup/restore.clj | 22 +++++--------------- src/dda/backup/restore/domain.clj | 14 ++++++++++++- test/dda/backup/restore/domain_test.clj | 16 ++++++++++++++ 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/infrastructure/backup/test/resources/test.bb b/infrastructure/backup/test/resources/test.bb index ac9d6f2..44f3a18 100755 --- a/infrastructure/backup/test/resources/test.bb +++ b/infrastructure/backup/test/resources/test.bb @@ -22,6 +22,11 @@ :pg-user "user" :pg-password "password"})) +(def db-roles-config (merge restic-repo {:backup-path "db-roles" + :pg-db "mydb" + :pg-user "user" + :pg-password "password"})) + (def dry-run {:dry-run true :debug true}) (defn prepare! @@ -46,18 +51,21 @@ [] (println "\nrestic-repo-init!") (rc/init! file-config) - (rc/init! (merge db-config dry-run))) + (rc/init! (merge db-config dry-run)) + (rc/init! (merge db-roles-config dry-run))) (defn restic-backup! [] (println "\nrestic-backup!") (bak/backup-file! file-config) + (bak/backup-db-roles! (merge db-roles-config dry-run)) (bak/backup-db! (merge db-config dry-run))) (defn list-snapshots! [] (println "\nlist-snapshots!") (rc/list-snapshots! file-config) + (rc/list-snapshots! (merge db-roles-config dry-run)) (rc/list-snapshots! (merge db-config dry-run))) @@ -66,6 +74,7 @@ (println "\nrestic-restore!") (rs/restore-file! file-config) (pg/drop-create-db! (merge db-config dry-run)) + (rs/restore-db-roles! (merge db-roles-config dry-run)) (rs/restore-db! (merge db-config dry-run))) (defn change-password! diff --git a/src/dda/backup/restore.clj b/src/dda/backup/restore.clj index 57a5988..e4f47a4 100644 --- a/src/dda/backup/restore.clj +++ b/src/dda/backup/restore.clj @@ -41,20 +41,8 @@ (restic/unlock! config-2-use) (i/execute! (domain/restore-db-command config-2-use) config-2-use))) - -;; function restore-roles() { -;; local snapshot_id="${1:-latest}"; shift - -;; if [ -z ${CERTIFICATE_FILE} ]; -;; then -;; roles-unlock-command -;; restic -r ${RESTIC_REPOSITORY}/${backup_pg_role_path} dump ${snapshot_id} stdin | \ -;; psql -d template1 -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U ${POSTGRES_USER} \ -;; --no-password -;; else -;; roles-unlock-command --cacert ${CERTIFICATE_FILE} -;; restic -r ${RESTIC_REPOSITORY}/${backup_pg_role_path} dump ${snapshot_id} stdin --cacert ${CERTIFICATE_FILE} | \ -;; psql -d template1 -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U ${POSTGRES_USER} \ -;; --no-password -;; fi -;; } +(defn-spec restore-db-roles! nil? + [config ::restore-db-config] + (let [config-2-use (config-w-defaults config)] + (restic/unlock! config-2-use) + (i/execute! (domain/restore-db-roles-command config-2-use) config-2-use))) diff --git a/src/dda/backup/restore/domain.clj b/src/dda/backup/restore/domain.clj index 50717d7..1c42047 100644 --- a/src/dda/backup/restore/domain.clj +++ b/src/dda/backup/restore/domain.clj @@ -38,4 +38,16 @@ (rd/repo-command config ["dump" snapshot-id "stdin"] false) ["|"] (pd/psql-command config []))))]])) - + +(defn-spec restore-db-roles-command ::cd/commands + [config ::restore-db-config] + (let [{:keys [snapshot-id]} config] + [["bash" "-c" + (st/join + " " + (into + [] + (concat + (rd/repo-command config ["dump" snapshot-id "stdin"] false) + ["|"] + (pd/psql-command (merge config {:pg-db "template1"}) []))))]])) diff --git a/test/dda/backup/restore/domain_test.clj b/test/dda/backup/restore/domain_test.clj index 5813783..40547f9 100644 --- a/test/dda/backup/restore/domain_test.clj +++ b/test/dda/backup/restore/domain_test.clj @@ -39,3 +39,19 @@ :days-to-keep 39 :months-to-keep 3 :snapshot-id "latest"})))) + +(deftest should-calculate-restore-db-roles + (is (= [["bash" + "-c" + (str "restic -r repo/dir-at-repo -v dump latest stdin | " + "psql -d template1 -h localhost -p 5432 -U user --no-password")]] + (cut/restore-db-roles-command {:restic-repository "repo" + :backup-path "dir-at-repo" + :pg-host "localhost" + :pg-port 5432 + :pg-db "mydb" + :pg-user "user" + :pg-password "password" + :days-to-keep 39 + :months-to-keep 3 + :snapshot-id "latest"}))))