feat: add config load feature

This commit is contained in:
Michael Jerger 2025-01-13 12:49:19 +01:00
parent a04dc613ae
commit 09b005ecd8
17 changed files with 139 additions and 185 deletions

View file

@ -9,9 +9,9 @@
;; ---------------------------------------------------------
:deps
{;; Application
org.clojure/clojure {:mvn/version "1.11.4"}
org.clojure/spec.alpha {:mvn/version "0.5.238"}
orchestra/orchestra {:mvn/version "2021.01.01-1"}
aero/aero {:mvn/version "1.1.6"}
cheshire/cheshire {:mvn/version "5.13.0"}
com.widdindustries/cljc.java-time {:mvn/version "0.1.21"}}
;; ---------------------------------------------------------

View file

@ -7,5 +7,4 @@ ADD local/ /usr/local/lib/dda-backup
RUN init-bb.bb
#ADD resources2 /tmp/
#RUN install -m 0700 -o root -g root /tmp/test.bb /usr/local/bin/
#RUN install -m 0700 -o root -g root /tmp/check.bb /usr/local/bin/
#RUN test.bb
#RUN FILE_PASSWORD_FILE=/tmp/file_password test.bb

View file

@ -1,34 +0,0 @@
#!/usr/bin/env bb
(require '[dda.backup.cred-rot :as cr])
(def restic-repo {:password-file "/restic-pwd"
:restic-repository "/restic-repo"
:debug true})
(def file-config (merge restic-repo {:backup-path "files"
:files ["/test-backup"]
:restore-target-directory "/test-restore"}))
(def cred-config (merge file-config
{:restic-repository "/restic-repo/files"
:new-password-config {:new-password-file "/new-pw"
:valid-from "2024-12-17 00:00:00"}}))
(def dry-run {:dry-run true :debug true})
(defn prepare!
[]
(spit "/restic-pwd" "ThePassword")
(spit "/new-pw" "newPassword"))
(defn change-password!
[]
(println "change-password!")
(cr/change-password! cred-config))
(prepare!)
(change-password!)

View file

@ -1,23 +0,0 @@
#!/usr/bin/env bb
(require '[dda.backup.restic :as rc])
(def restic-repo {:password-file "/restic-pwd"
:restic-repository "/restic-repo"
:debug true})
(def file-config (merge restic-repo {:backup-path "files"
:files ["/test-backup"]
:restore-target-directory "/test-restore"}))
(def cred-config (merge file-config {:new-password-file "new-pw"}))
(def dry-run {:dry-run true :debug true})
(defn restic-repo-check
[]
(println "restic-repo-check")
(println (rc/check file-config)))
(restic-repo-check)

View file

@ -0,0 +1,24 @@
{:restic-repo {:password-file #env-or-file "FILE_PASSWORD_FILE"
:restic-repository "/restic-repo"
:debug true}
:file-config #merge [#ref [:restic-repo]
{:new-password-file "/tmp/file_new_password"
:backup-path "files"
:execution-directory "/var/backups/"
:files ["test-backup"]
:restore-target-directory "test-restore"}]
:file-config-with-new #merge [#ref [:file-config]
{:password-file "/tmp/file_new_password"}]
:db-config #merge [#ref [:restic-repo]
{:new-password-file "/tmp/file_new_password"
:backup-path "db"
:pg-db "mydb"
:pg-user "user"
:pg-password "password"}]
:db-roles-config #merge [#ref [:restic-repo]
{:new-password-file "/tmp/file_new_password"
:backup-path "db-roles"
:pg-db "mydb"
:pg-user "user"
:pg-password "password"}]
:dry-run {:dry-run true :debug true}}

View file

@ -0,0 +1 @@
new

View file

@ -0,0 +1 @@
old

View file

@ -1,58 +1,65 @@
#!/usr/bin/env bb
(require '[babashka.tasks :as tasks]
'[dda.backup.config :as cfg]
'[dda.backup.restic :as rc]
'[dda.backup.postgresql :as pg]
'[dda.backup.backup :as bak]
'[dda.backup.restore :as rs])
(def restic-repo {:password-file "/restic-pwd"
:new-password-file "/new-restic-pwd"
:restic-repository "/restic-repo"
:debug true})
(def file-config (merge restic-repo {:backup-path "files"
:execution-directory "/var/backups/"
:files ["test-backup"]
:restore-target-directory "test-restore"}))
(def dry-run {:dry-run true :debug true})
(def config (cfg/read-config "/tmp/config.edn"))
(defn prepare!
[]
(spit "/tmp/file_password" "file-password")
(spit "/restic-pwd" "oldPassword")
(spit "/new-restic-pwd" "newPassword")
(println config)
(tasks/shell "mkdir" "-p" "/var/backups/test-backup")
(spit "/var/backups/test-backup/file" "I was here")
(tasks/shell "mkdir" "-p" "/var/backups/test-restore"))
(tasks/shell "mkdir" "-p" "/var/backups/test-restore")
(pg/create-pg-pass! (:db-config config)))
(defn restic-repo-init!
[]
(println "restic-repo-init!")
(rc/init! file-config))
(println "\nrestic-repo-init!")
(rc/init! (:file-config config))
(rc/init! (merge (:db-config config) (:dry-run config)))
(rc/init! (merge (:db-roles-config config) (:dry-run config))))
(defn restic-backup!
[]
(println "restic-backup!")
(bak/backup-file! file-config))
(println "\nrestic-backup!")
(bak/backup-file! (:file-config config))
(bak/backup-db-roles! (merge (:db-roles-config config) (:dry-run config)))
(bak/backup-db! (merge (:db-config config) (:dry-run config))))
(defn list-snapshots!
[]
(println "list-snapshots!")
(rc/list-snapshots! file-config))
(println "\nlist-snapshots!")
(rc/list-snapshots! (:file-config config))
(rc/list-snapshots! (merge (:db-roles-config config) (:dry-run config)))
(rc/list-snapshots! (merge (:db-config config) (:dry-run config))))
(defn restic-restore!
[]
(println "restic-restore!")
(rs/restore-file! file-config))
(println "\nrestic-restore!")
(rs/restore-file! (:file-config config))
(pg/drop-create-db! (merge (:db-config config) (:dry-run config)))
(rs/restore-db-roles! (merge (:db-roles-config config) (:dry-run config)))
(rs/restore-db! (merge (:db-config config) (:dry-run config))))
(defn change-password!
[]
(println "change-password!")
(rc/change-password! file-config))
(println "\nchange-password!")
(rc/change-password! (:file-config config)))
(defn restic-backup-with-new!
[]
(println "\nrestic-backup with new!")
(bak/backup-file! (:file-config-with-new config)))
(defn list-snapshots-with-new!
[]
(println "\nlist-snapshots with new!")
(rc/list-snapshots! (:file-config-with-new config)))
(prepare!)
@ -61,3 +68,8 @@
(list-snapshots!)
(restic-restore!)
(change-password!)
(restic-backup!)
(list-snapshots!)
(restic-restore!)
(restic-backup-with-new!)
(list-snapshots-with-new!)

View file

@ -1,6 +1,5 @@
FROM dda-backup:latest
# install it
#ADD local/ /usr/local/lib/dda-backup
ADD resources /tmp/
RUN ENV_PASSWORD=env-password FILE_PASSWORD_FILE=/tmp/file_password /tmp/test.bb
RUN install -m 0700 -o root -g root /tmp/test.bb /usr/local/bin/
RUN FILE_PASSWORD_FILE=/tmp/file_password test.bb

View file

@ -1,3 +0,0 @@
{:deps {org.clojure/spec.alpha {:mvn/version "0.4.233"}
orchestra/orchestra {:mvn/version "2021.01.01-1"}
org.domaindrivenarchitecture/dda-backup {:local/root "/usr/local/lib/dda-backup"}}}

View file

@ -0,0 +1,24 @@
{:restic-repo {:password-file #env-or-file "FILE_PASSWORD_FILE"
:restic-repository "/restic-repo"
:debug true}
:file-config #merge [#ref [:restic-repo]
{:new-password-file "/tmp/file_new_password"
:backup-path "files"
:execution-directory "/var/backups/"
:files ["test-backup"]
:restore-target-directory "test-restore"}]
:file-config-with-new #merge [#ref [:file-config]
{:password-file "/tmp/file_new_password"}]
:db-config #merge [#ref [:restic-repo]
{:new-password-file "/tmp/file_new_password"
:backup-path "db"
:pg-db "mydb"
:pg-user "user"
:pg-password "password"}]
:db-roles-config #merge [#ref [:restic-repo]
{:new-password-file "/tmp/file_new_password"
:backup-path "db-roles"
:pg-db "mydb"
:pg-user "user"
:pg-password "password"}]
:dry-run {:dry-run true :debug true}}

View file

@ -0,0 +1 @@
new

View file

@ -0,0 +1 @@
old

View file

@ -1,29 +0,0 @@
#!/usr/bin/env bb
(require '[babashka.tasks :as tasks])
(defn curl-and-check!
[filename artifact-url sha256-url]
(let [filepath (str "/tmp/" filename)]
(tasks/shell "curl" "-SsLo" filepath artifact-url)
(tasks/shell "curl" "-SsLo" "/tmp/checksum" sha256-url)
(tasks/shell "bash" "-c" (str "echo \" " filepath "\"|tee -a /tmp/checksum"))
;(tasks/shell "sha256sum" "-c" "--status" "/tmp/checksum")
))
(defn tar-install!
[filename binname]
(let [filepath (str "/tmp/" filename)]
(tasks/shell "tar" "-C" "/tmp" "-xzf" filepath)
(tasks/shell "install" "-m" "0700" "-o" "root" "-g" "root" (str "/tmp/" binname) "/usr/local/bin/")))
(defn install!
[filename]
(tasks/shell "install" "-m" "0700" "-o" "root" "-g" "root" (str "/tmp/" filename) "/usr/local/bin/"))
(curl-and-check!
"provs-syspec.jar"
"https://repo.prod.meissa.de/attachments/0a1da41e-aa5b-4a3e-a3b1-215cf2d5b021"
"https://repo.prod.meissa.de/attachments/f227cf65-cb0f-46a7-a6cd-28f46917412a")
(install! "provs-syspec.jar")
(tasks/shell "java" "-jar" "/usr/local/bin/provs-syspec.jar" "local" "-c" "/tmp/spec.yml" )

View file

@ -1,7 +0,0 @@
package:
- name: "restic"
command:
- command: "bb -h"
- command: "/tmp/test.bb"

View file

@ -1,102 +1,68 @@
#!/usr/bin/env bb
(require '[babashka.tasks :as tasks]
'[dda.backup.core :as bc]
'[dda.backup.config :as cfg]
'[dda.backup.restic :as rc]
'[dda.backup.postgresql :as pg]
'[dda.backup.backup :as bak]
'[dda.backup.restore :as rs])
(def restic-repo {:password-file "/restic-pwd"
:new-password-file "/new-restic-pwd"
:restic-repository "/restic-repo"
:debug true})
(def file-config (merge restic-repo {:backup-path "files"
:execution-directory "/var/backups/"
:files ["test-backup"]
:restore-target-directory "test-restore"}))
(def file-config-with-new (merge (dissoc file-config :new-password-file)
{:password-file "/new-restic-pwd"}))
(def db-config (merge restic-repo {:backup-path "db"
:pg-db "mydb"
: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})
(def config (cfg/read-config "/tmp/config.edn"))
(defn prepare!
[]
(spit "/tmp/file_password" "file-password")
(spit "/restic-pwd" "oldPassword")
(spit "/new-restic-pwd" "newPassword")
(println config)
(tasks/shell "mkdir" "-p" "/var/backups/test-backup")
(spit "/var/backups/test-backup/file" "I was here")
(tasks/shell "mkdir" "-p" "/var/backups/test-restore")
(pg/create-pg-pass! db-config))
(defn check-env-or-file
[]
(println "\ncheck-env-or-file")
(println (bc/env-or-file "FILE_PASSWORD"))
(println (bc/env-or-file "ENV_PASSWORD")))
(pg/create-pg-pass! (:db-config config)))
(defn restic-repo-init!
[]
(println "\nrestic-repo-init!")
(rc/init! file-config)
(rc/init! (merge db-config dry-run))
(rc/init! (merge db-roles-config dry-run)))
(rc/init! (:file-config config))
(rc/init! (merge (:db-config config) (:dry-run config)))
(rc/init! (merge (:db-roles-config config) (:dry-run config))))
(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)))
(bak/backup-file! (:file-config config))
(bak/backup-db-roles! (merge (:db-roles-config config) (:dry-run config)))
(bak/backup-db! (merge (:db-config config) (:dry-run config))))
(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)))
(rc/list-snapshots! (:file-config config))
(rc/list-snapshots! (merge (:db-roles-config config) (:dry-run config)))
(rc/list-snapshots! (merge (:db-config config) (:dry-run config))))
(defn restic-restore!
[]
(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)))
(rs/restore-file! (:file-config config))
(pg/drop-create-db! (merge (:db-config config) (:dry-run config)))
(rs/restore-db-roles! (merge (:db-roles-config config) (:dry-run config)))
(rs/restore-db! (merge (:db-config config) (:dry-run config))))
(defn change-password!
[]
(println "\nchange-password!")
(rc/change-password! file-config))
(rc/change-password! (:file-config config)))
(defn restic-backup-with-new!
[]
(println "\nrestic-backup with new!")
(bak/backup-file! file-config-with-new))
(bak/backup-file! (:file-config-with-new config)))
(defn list-snapshots-with-new!
[]
(println "\nlist-snapshots with new!")
(rc/list-snapshots! file-config-with-new))
(rc/list-snapshots! (:file-config-with-new config)))
(prepare!)
(check-env-or-file)
(restic-repo-init!)
(restic-backup!)
(list-snapshots!)
@ -106,4 +72,4 @@
(list-snapshots!)
(restic-restore!)
(restic-backup-with-new!)
(list-snapshots-with-new!)
(list-snapshots-with-new!)

22
src/dda/backup/config.clj Normal file
View file

@ -0,0 +1,22 @@
(ns dda.backup.config
"{:test huhu :long-name #env LOGNAME :gopass-pw #gopass [sopra/test.de] :gopass-field-url #gopass [sopra/test.de url]}"
(:require [aero.core :as aero]
[dda.backup.core :as bc]
[dda.backup.infrastructure :as i]))
(defmethod aero/reader 'env-or-file
[{:keys [profile] :as opts} tag value]
(bc/env-or-file value))
(defmethod aero/reader 'gopass
[{:keys [profile] :as opts} tag value]
(i/execute-out! (into ["gopass" "show" "-y" "-o"] value) {}))
(defn read-config
[file]
(try
(aero/read-config file)
(catch Exception e
(do (println (str "Warn: " e))
{}))
))