diff --git a/infrastructure/backup/image/resources/restore.sh b/infrastructure/backup/image/resources/restore.sh index 12895f3..8fdf395 100755 --- a/infrastructure/backup/image/resources/restore.sh +++ b/infrastructure/backup/image/resources/restore.sh @@ -3,6 +3,8 @@ set -Eexo pipefail function main() { + local db_snapshot_id="${1:-latest}" + local file_snapshot_id="${2:-latest}" file_env AWS_ACCESS_KEY_ID file_env AWS_SECRET_ACCESS_KEY @@ -12,7 +14,7 @@ function main() { file_env POSTGRES_USER # Restore latest snapshot into /var/backups/restore - restore-directory '/var/backups/restore' + restore-directory '/var/backups/restore' ${file_snapshot_id} rm -rf /var/backups/gitea/* rm -rf /var/backups/git/repositories/* @@ -27,11 +29,11 @@ function main() { # Restore db drop-create-db - restore-db + restore-db ${db_snapshot_id} } source /usr/local/lib/functions.sh source /usr/local/lib/pg-functions.sh source /usr/local/lib/file-functions.sh -main +main "$@" diff --git a/infrastructure/backup/test/Dockerfile b/infrastructure/backup/test/Dockerfile new file mode 100644 index 0000000..c67e37c --- /dev/null +++ b/infrastructure/backup/test/Dockerfile @@ -0,0 +1,7 @@ +FROM c4k-forgejo-backup:latest + +# install it +RUN apt update && apt install -qqy openjdk-17-jre-headless +ADD resources /tmp/ +RUN rm -rf /root/.m2 +RUN /tmp/install-test.bb diff --git a/infrastructure/backup/test/resources/bb.edn b/infrastructure/backup/test/resources/bb.edn new file mode 100644 index 0000000..acb67e5 --- /dev/null +++ b/infrastructure/backup/test/resources/bb.edn @@ -0,0 +1,4 @@ +{:deps {org.clojure/spec.alpha {:mvn/version "0.4.233"} + orchestra/orchestra {:mvn/version "2021.01.01-1"} + org.domaindrivenarchitecture/dda-backup {:mvn/version "0.1.1-SNAPSHOT"}}} + diff --git a/infrastructure/backup/test/resources/install-test.bb b/infrastructure/backup/test/resources/install-test.bb new file mode 100755 index 0000000..357e4d6 --- /dev/null +++ b/infrastructure/backup/test/resources/install-test.bb @@ -0,0 +1,32 @@ +#!/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/")) + +(tasks/shell "bb" "/tmp/test.bb") +(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 "apt" "update") +(tasks/shell "apt" "install" "-qqy" "openjdk-17-jre-headless") +(tasks/shell "java" "-jar" "/usr/local/bin/provs-syspec.jar" "local" "-c" "/tmp/spec.yml" ) diff --git a/infrastructure/backup/test/resources/spec.yml b/infrastructure/backup/test/resources/spec.yml new file mode 100644 index 0000000..19d0edf --- /dev/null +++ b/infrastructure/backup/test/resources/spec.yml @@ -0,0 +1,7 @@ +package: + - name: "restic" + +command: + - command: "bb -h" + - command: "/tmp/test.bb" + \ No newline at end of file diff --git a/infrastructure/backup/test/resources/test.bb b/infrastructure/backup/test/resources/test.bb new file mode 100755 index 0000000..a48931b --- /dev/null +++ b/infrastructure/backup/test/resources/test.bb @@ -0,0 +1,26 @@ +#!/usr/bin/env bb + +(require '[babashka.tasks :as tasks] + '[dda.backup.management :as mgm]) + +(defn restic-repo-init! + [] + (spit "restic-pwd" "ThePassword") + (mgm/init! {:password-file "restic-pwd" + :restic-repository "restic-repo"})) + +(defn restic-backup! + [] + (tasks/shell "mkdir" "-p" "test-backup") + (spit "test-backup/file" "I was here") + (tasks/shell "restic" "backup" "--password-file" "restic-pwd" "--repo" "restic-repo" "test-backup")) + +(defn restic-restore! + [] + (tasks/shell "mkdir" "-p" "test-restore") + (tasks/shell "restic" "restore" "--password-file" "restic-pwd" "--repo" "restic-repo" "--target" "test-restore" "latest") + ) + +(restic-repo-init!) +(restic-backup!) +(restic-restore!)