feat: add restore db roles

This commit is contained in:
Michael Jerger 2024-12-30 10:25:45 +01:00
parent e7617f6e37
commit f4f67b580a
4 changed files with 44 additions and 19 deletions

View file

@ -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!

View file

@ -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)))

View file

@ -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"}) []))))]]))

View file

@ -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"}))))