Compare commits
3 commits
46eb2ada33
...
1a49fe3c10
Author | SHA1 | Date | |
---|---|---|---|
1a49fe3c10 | |||
4df437f707 | |||
26822dd8fd |
9 changed files with 152 additions and 31 deletions
|
@ -1,4 +1,4 @@
|
||||||
FROM ubuntu:jammy
|
FROM ubuntu:24.04
|
||||||
|
|
||||||
# install it
|
# install it
|
||||||
ADD resources /tmp/
|
ADD resources /tmp/
|
||||||
|
|
|
@ -15,7 +15,7 @@ function babashka_install() {
|
||||||
function main() {
|
function main() {
|
||||||
{
|
{
|
||||||
upgradeSystem
|
upgradeSystem
|
||||||
apt-get install -qqy ca-certificates curl gnupg postgresql-client-14 restic
|
apt-get install -qqy ca-certificates curl gnupg postgresql-client-16 restic
|
||||||
curl -Ss --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/postgresql-common_pgdg_archive_keyring.gpg
|
curl -Ss --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/postgresql-common_pgdg_archive_keyring.gpg
|
||||||
sh -c 'echo "deb [signed-by=/etc/apt/trusted.gpg.d/postgresql-common_pgdg_archive_keyring.gpg] https://apt.postgresql.org/pub/repos/apt jammy-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
|
sh -c 'echo "deb [signed-by=/etc/apt/trusted.gpg.d/postgresql-common_pgdg_archive_keyring.gpg] https://apt.postgresql.org/pub/repos/apt jammy-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
|
||||||
upgradeSystem
|
upgradeSystem
|
||||||
|
|
|
@ -2,30 +2,40 @@
|
||||||
|
|
||||||
(require '[babashka.tasks :as tasks]
|
(require '[babashka.tasks :as tasks]
|
||||||
'[dda.backup.management :as mgm]
|
'[dda.backup.management :as mgm]
|
||||||
|
'[dda.backup.postgresql :as ps]
|
||||||
'[dda.backup.backup :as bak]
|
'[dda.backup.backup :as bak]
|
||||||
'[dda.backup.restore :as rs])
|
'[dda.backup.restore :as rs])
|
||||||
|
|
||||||
|
(def restic-repo {:password-file "restic-pwd"
|
||||||
|
:restic-repository "restic-repo"})
|
||||||
|
|
||||||
|
(defn prepare!
|
||||||
|
[]
|
||||||
|
(ps/create-pg-pass! (merge restic-repo {:backup-path "db"
|
||||||
|
:pg-db "mydb"
|
||||||
|
:pg-user "user"
|
||||||
|
:pg-password "password"})))
|
||||||
|
|
||||||
(defn restic-repo-init!
|
(defn restic-repo-init!
|
||||||
[]
|
[]
|
||||||
(spit "restic-pwd" "ThePassword")
|
(spit "restic-pwd" "ThePassword")
|
||||||
(mgm/init! {:password-file "restic-pwd"
|
(mgm/init! (merge restic-repo {:backup-path "files"}))
|
||||||
:restic-repository "restic-repo"}))
|
(mgm/init! (merge restic-repo {:backup-path "db" :dry-run true :debug true})))
|
||||||
|
|
||||||
(defn restic-backup!
|
(defn restic-backup!
|
||||||
[]
|
[]
|
||||||
(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! {:password-file "restic-pwd"
|
(bak/backup! (merge restic-repo {:backup-path "files"
|
||||||
:restic-repository "restic-repo"
|
:files ["test-backup"]})))
|
||||||
:files ["test-backup"]}))
|
|
||||||
|
|
||||||
(defn restic-restore!
|
(defn restic-restore!
|
||||||
[]
|
[]
|
||||||
(tasks/shell "mkdir" "-p" "test-restore")
|
(tasks/shell "mkdir" "-p" "test-restore")
|
||||||
(rs/restore! {:password-file "restic-pwd"
|
(rs/restore! (merge restic-repo {:backup-path "files"
|
||||||
:restic-repository "restic-repo"
|
:target-directory "test-restore"})))
|
||||||
:target-directory "test-restore"}))
|
|
||||||
|
|
||||||
|
(prepare!)
|
||||||
(restic-repo-init!)
|
(restic-repo-init!)
|
||||||
(restic-backup!)
|
(restic-backup!)
|
||||||
(restic-restore!)
|
(restic-restore!)
|
||||||
|
|
|
@ -9,8 +9,16 @@
|
||||||
[dda.backup.infrastructure :as i]))
|
[dda.backup.infrastructure :as i]))
|
||||||
|
|
||||||
(s/def ::config
|
(s/def ::config
|
||||||
(s/keys :req-un [::domain/files ::cd/restic-repository ::cd/backup-path]
|
(s/keys :req-un [::domain/files
|
||||||
:opt-un [::cd/certificate-file ::cd/directory ::cd/debug ::cd/dry-run]))
|
::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]))
|
||||||
|
|
||||||
(defn-spec backup! nil?
|
(defn-spec backup! nil?
|
||||||
[config ::config]
|
[config ::config]
|
||||||
|
|
|
@ -8,9 +8,15 @@
|
||||||
[dda.backup.infrastructure :as i]))
|
[dda.backup.infrastructure :as i]))
|
||||||
|
|
||||||
(s/def ::config
|
(s/def ::config
|
||||||
(s/keys :req-un [::cd/restic-repository ::cd/backup-path]
|
(s/keys :req-un [::cd/restic-repository
|
||||||
:opt-un [::cd/certificate-file ::cd/directory ::cd/debug
|
::cd/backup-path]
|
||||||
::cd/dry-run ::cd/days-to-keep ::cd/months-to-keep]))
|
: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 initalized? boolean?
|
(defn-spec initalized? boolean?
|
||||||
[config ::config]
|
[config ::config]
|
||||||
|
|
|
@ -1,26 +1,43 @@
|
||||||
(ns dda.backup.postgres
|
(ns dda.backup.postgresql
|
||||||
(: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.core.domain :as cd]
|
||||||
[dda.backup.management.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]))
|
||||||
|
|
||||||
;; function drop-create-db() {
|
(def default
|
||||||
;; psql -d template1 -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U ${POSTGRES_USER} \
|
(merge core/default
|
||||||
;; --no-password -c "DROP DATABASE \"${POSTGRES_DB}\";"
|
{:pg-host "localhost"
|
||||||
;; psql -d template1 -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U ${POSTGRES_USER} \
|
:pg_port 5432}))
|
||||||
;; --no-password -c "CREATE DATABASE \"${POSTGRES_DB}\";"
|
|
||||||
;; }
|
|
||||||
|
|
||||||
;; function create-pg-pass() {
|
(s/def ::config
|
||||||
;; local pg_host=${POSTGRES_HOST:-localhost}
|
(s/keys :req-un [::domain/pg-db
|
||||||
|
::domain/pg-user
|
||||||
|
::domain/pg-password
|
||||||
|
::cd/restic-repository
|
||||||
|
::cd/backup-path]
|
||||||
|
: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]))
|
||||||
|
|
||||||
;; echo "${pg_host}:${POSTGRES_DB}:${POSTGRES_USER}:${POSTGRES_PASSWORD}" > /root/.pgpass
|
(defn-spec create-pg-pass! nil?
|
||||||
;; echo "${POSTGRES_HOST}:template1:${POSTGRES_USER}:${POSTGRES_PASSWORD}" >> /root/.pgpass
|
[config ::config]
|
||||||
;; chmod 0600 /root/.pgpass
|
(let [config-w-defaults (merge default config)]
|
||||||
;; }
|
(spit "/root/.pgpass" (domain/pgpass config-w-defaults))))
|
||||||
|
|
||||||
|
|
||||||
|
(defn-spec drop-create-db! nil?
|
||||||
|
[config ::config]
|
||||||
|
(let [config-w-defaults (merge default config)]
|
||||||
|
((i/execute! (domain/db-drop-create-command config-w-defaults)))))
|
||||||
|
|
||||||
;; function backup-roles() {
|
;; function backup-roles() {
|
||||||
;; local role_prefix="$1"; shift
|
;; local role_prefix="$1"; shift
|
||||||
|
|
41
src/dda/backup/postgresql/domain.clj
Normal file
41
src/dda/backup/postgresql/domain.clj
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
(ns dda.backup.postgresql.domain
|
||||||
|
(:require
|
||||||
|
[orchestra.core :refer [defn-spec]]
|
||||||
|
[clojure.spec.alpha :as s]
|
||||||
|
[dda.backup.core.domain :as cd]))
|
||||||
|
|
||||||
|
(s/def ::pg-host string?)
|
||||||
|
(s/def ::pg-port pos-int?)
|
||||||
|
(s/def ::pg-db string?)
|
||||||
|
(s/def ::pg-user string?)
|
||||||
|
(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]))
|
||||||
|
|
||||||
|
(defn-spec psql-command ::cd/command
|
||||||
|
[config ::config
|
||||||
|
command string?]
|
||||||
|
(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]))
|
||||||
|
|
||||||
|
|
||||||
|
(defn-spec pgpass string?
|
||||||
|
[config ::config]
|
||||||
|
(let [{:keys [pg-host pg-db pg-user pg-password]} config]
|
||||||
|
(str pg-host ":" pg-db ":" pg-user ":" pg-password)))
|
||||||
|
|
||||||
|
(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 "\\\";\""))]))
|
|
@ -12,8 +12,16 @@
|
||||||
|
|
||||||
(s/def ::config
|
(s/def ::config
|
||||||
(s/keys :req-un [::domain/target-directory
|
(s/keys :req-un [::domain/target-directory
|
||||||
::cd/restic-repository ::cd/backup-path]
|
::cd/restic-repository
|
||||||
:opt-un [::domain/snapshot-id ::cd/certificate-file ::cd/directory ::cd/debug ::cd/dry-run]))
|
::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]))
|
||||||
|
|
||||||
(defn-spec restore! nil?
|
(defn-spec restore! nil?
|
||||||
[config ::config]
|
[config ::config]
|
||||||
|
|
31
test/dda/backup/postgresql/domain_test.clj
Normal file
31
test/dda/backup/postgresql/domain_test.clj
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
(ns dda.backup.postgresql.domain-test
|
||||||
|
(:require
|
||||||
|
[clojure.test :refer [deftest is are testing run-tests]]
|
||||||
|
[clojure.spec.test.alpha :as st]
|
||||||
|
[dda.backup.postgresql.domain :as cut]))
|
||||||
|
|
||||||
|
(st/instrument `cut/pgpass)
|
||||||
|
(st/instrument `cut/db-drop-create-command)
|
||||||
|
|
||||||
|
(deftest should-calculate-pgpass
|
||||||
|
(is (= "localhost:mydb:user:password"
|
||||||
|
(cut/pgpass {:restic-repository "repo"
|
||||||
|
:backup-path "dir-at-repo"
|
||||||
|
:pg-host "localhost"
|
||||||
|
:pg-port 5432
|
||||||
|
:pg-db "mydb"
|
||||||
|
:pg-user "user"
|
||||||
|
:pg-password "password"}))))
|
||||||
|
|
||||||
|
(deftest should-calculate-db-drop-create-command
|
||||||
|
(is (= [["psql" "-d" "template1" "-h" "localhost" "-p" "5432" "-U" "user"
|
||||||
|
"--no-password" "-c" "\"DROP DATABASE \\\"mydb\\\";\""]
|
||||||
|
["psql" "-d" "template1" "-h" "localhost" "-p" "5432" "-U" "user"
|
||||||
|
"--no-password" "-c" "\"CREATE DATABASE \\\"mydb\\\";\""]]
|
||||||
|
(cut/db-drop-create-command {:restic-repository "repo"
|
||||||
|
:backup-path "dir-at-repo"
|
||||||
|
:pg-host "localhost"
|
||||||
|
:pg-port 5432
|
||||||
|
:pg-db "mydb"
|
||||||
|
:pg-user "user"
|
||||||
|
:pg-password "password"}))))
|
Loading…
Reference in a new issue