refactor & add env-or-file

This commit is contained in:
Michael Jerger 2024-08-21 16:55:59 +02:00
parent 8f69d2c1d6
commit 9d45731246
6 changed files with 32 additions and 17 deletions

View file

@ -4,4 +4,4 @@ FROM c4k-forgejo-backup:latest
RUN apt update && apt install -qqy openjdk-17-jre-headless
ADD local/ /usr/local/lib/dda-backup
ADD resources /tmp/
RUN /tmp/test.bb
RUN ENV_PASSWORD=env-password /tmp/test.bb

View file

@ -1,6 +1,7 @@
#!/usr/bin/env bb
(require '[babashka.tasks :as tasks]
'[dda.backup.core :as bc]
'[dda.backup.restic :as rc]
'[dda.backup.postgresql :as pg]
'[dda.backup.backup :as bak]
@ -11,7 +12,7 @@
(def file-config (merge restic-repo {:backup-path "files"
:files ["test-backup"]
:target-directory "test-restore"}))
:restore-target-directory "test-restore"}))
(def db-config (merge restic-repo {:backup-path "db"
@ -23,24 +24,27 @@
(defn prepare!
[]
(spit "file_password" "file-password")
(println (bc/env-or-file "file_password"))
(println (bc/env-or-file "env_password"))
(spit "restic-pwd" "ThePassword")
(tasks/shell "mkdir" "-p" "test-backup")
(spit "test-backup/file" "I was here")
(tasks/shell "mkdir" "-p" "test-restore")
(pg/create-pg-pass! db-config))
(defn restic-repo-init!
[]
(spit "restic-pwd" "ThePassword")
(rc/init! file-config)
(rc/init! (merge db-config dry-run)))
(defn restic-backup!
[]
(tasks/shell "mkdir" "-p" "test-backup")
(spit "test-backup/file" "I was here")
(bak/backup-file! file-config)
(bak/backup-db! (merge db-config dry-run)))
(defn restic-restore!
[]
(tasks/shell "mkdir" "-p" "test-restore")
(rs/restore-file! file-config)
(pg/drop-create-db! (merge db-config dry-run))
(rs/restore-db! (merge db-config dry-run)))

View file

@ -1,6 +1,8 @@
(ns dda.backup.core
(:require
[clojure.spec.alpha :as s]))
[orchestra.core :refer [defn-spec]]
[clojure.spec.alpha :as s]
[clojure.string :as st]))
(def default {:dry-run false
:debug false})
@ -10,3 +12,12 @@
:opt-un [::dry-run
::debug
::execution-directory]))
(defn-spec env-or-file string?
[name string?]
(let [name-upper (st/upper-case name)
name-lower (st/lower-case name)
from-env (System/getenv name-upper)]
(if (some? from-env)
from-env
(slurp name-lower))))

View file

@ -13,7 +13,7 @@
(s/def ::restore-file-config
(s/merge ::restic/restic-config
(s/keys :req-un [::domain/target-directory]
(s/keys :req-un [::domain/restore-target-directory]
:opt-un [::domain/snapshot-id])))
(s/def ::restore-db-config

View file

@ -7,12 +7,12 @@
[dda.backup.restic.domain :as rd]
[dda.backup.postgresql.domain :as pd]))
(s/def ::target-directory string?)
(s/def ::restore-target-directory string?)
(s/def ::snapshot-id string?)
(s/def ::restore-file-config
(s/merge ::rd/restic-config
(s/keys :req-un [::target-directory
(s/keys :req-un [::restore-target-directory
::snapshot-id])))
(s/def ::restore-db-config
@ -22,9 +22,9 @@
(defn-spec restore-dir-command ::cd/commands
[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])]))
(let [{:keys [restore-target-directory snapshot-id]} config]
[["rm" "-rf" restore-target-directory]
(rd/repo-command config ["restore" snapshot-id "--target" restore-target-directory])]))
(defn-spec restore-db-command ::cd/commands
[config ::restore-db-config]

View file

@ -19,7 +19,7 @@
"dir-to-backup"]]
(cut/restore-dir-command {:restic-repository "repo"
:backup-path "dir-at-repo"
:target-directory "dir-to-backup"
:restore-target-directory "dir-to-backup"
:days-to-keep 39
:months-to-keep 3
:snapshot-id "latest"}))))