diff --git a/infrastrucure/docker/image/resources/backup-file-functions.sh b/infrastrucure/docker/image/resources/backup-file-functions.sh new file mode 100644 index 0000000..cfeaee0 --- /dev/null +++ b/infrastrucure/docker/image/resources/backup-file-functions.sh @@ -0,0 +1,3 @@ +function init-file-repo() { + restic -r ${RESTIC_REPOSITORY}/files -v init +} diff --git a/infrastrucure/docker/image/resources/backup-pg-functions.sh b/infrastrucure/docker/image/resources/backup-pg-functions.sh index 780b38c..82d43b4 100644 --- a/infrastrucure/docker/image/resources/backup-pg-functions.sh +++ b/infrastrucure/docker/image/resources/backup-pg-functions.sh @@ -1,3 +1,27 @@ function init-role-repo() { restic -r ${RESTIC_REPOSITORY}/pg-role -v init } + +function init-database-repo() { + restic -r ${RESTIC_REPOSITORY}/pg-database -v init +} + +function create-pg-pass() { + local pg_host=${POSTGRES_HOST:-loclahost} + + echo "${pg_host}:${POSTGRES_DB}:${POSTGRES_USER}:${POSTGRES_PASSWORD}" > /root/.pgpass + echo "${POSTGRES_HOST}:template1:${POSTGRES_USER}:${POSTGRES_PASSWORD}" >> /root/.pgpass + chmod 0600 /root/.pgpass +} + +function backup-roles() { + local role_prefix="$1"; shift + + restic -v -r ${RESTIC_REPOSITORY}/pg-role unlock --cleanup-cache + + pg_dumpall -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U${POSTGRES_USER} --no-password --roles-only | + grep "${role_prefix}" | + restic -v -r ${RESTIC_REPOSITORY}/pg-role backup --stdin + + restic -v -r ${RESTIC_REPOSITORY}/pg-role forget --keep-last 1 --keep-within ${RESTIC_DAYS_TO_KEEP}d --prune +} \ No newline at end of file diff --git a/infrastrucure/docker/image/resources/backup.sh b/infrastrucure/docker/image/resources/backup.sh deleted file mode 100755 index 11030ae..0000000 --- a/infrastrucure/docker/image/resources/backup.sh +++ /dev/null @@ -1,50 +0,0 @@ -#!/bin/bash - -set -o pipefail - -function main() { - - #Start maintenance mode - /usr/local/bin/start-maintenance.sh - - file_env AWS_ACCESS_KEY_ID - file_env AWS_SECRET_ACCESS_KEY - - file_env POSTGRES_DB - file_env POSTGRES_PASSWORD - file_env POSTGRES_USER - - file_env RESTIC_PASSWORD - file_env RESTIC_DAYS_TO_KEEP 14 - - # backup roles - restic -v -r ${RESTIC_REPOSITORY}/pg-role unlock --cleanup-cache - - pg_dumpall -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U${POSTGRES_USER} --no-password --roles-only | - grep 'oc_' | - restic -v -r ${RESTIC_REPOSITORY}/pg-role backup --stdin - - restic -v -r ${RESTIC_REPOSITORY}/pg-role forget --keep-last 1 --keep-within ${RESTIC_DAYS_TO_KEEP}d --prune - - # backup database dump - restic -v -r ${RESTIC_REPOSITORY}/pg-database unlock --cleanup-cache - - pg_dump -d ${POSTGRES_DB} -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} \ - -U ${POSTGRES_USER} --no-password --serializable-deferrable | - restic -v -r ${RESTIC_REPOSITORY}/pg-database backup --stdin - - restic -v -r ${RESTIC_REPOSITORY}/pg-database forget --keep-last 1 --keep-within ${RESTIC_DAYS_TO_KEEP}d --prune - - # backup nextcloud filesystem - restic -v -r ${RESTIC_REPOSITORY}/files unlock --cleanup-cache - - cd /var/backups/ && restic -v -r ${RESTIC_REPOSITORY}/files backup . - - restic -v -r ${RESTIC_REPOSITORY}/files forget --keep-last 1 --keep-within ${RESTIC_DAYS_TO_KEEP}d --prune - - #End maintenance mode - /usr/local/bin/end-maintenance.sh -} - -source /usr/local/lib/functions.sh -main diff --git a/infrastrucure/docker/image/resources/entrypoint.sh b/infrastrucure/docker/image/resources/entrypoint.sh deleted file mode 100755 index 8bfa140..0000000 --- a/infrastrucure/docker/image/resources/entrypoint.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/bash - -function main() { - file_env POSTGRES_DB - file_env POSTGRES_PASSWORD - file_env POSTGRES_USER - - echo "${POSTGRES_HOST}:${POSTGRES_DB}:${POSTGRES_USER}:${POSTGRES_PASSWORD}" > /root/.pgpass - echo "${POSTGRES_HOST}:template1:${POSTGRES_USER}:${POSTGRES_PASSWORD}" >> /root/.pgpass - chmod 0600 /root/.pgpass - - # Idle process - while true; do - sleep 500000 - done -} - -source /usr/local/lib/functions.sh -main diff --git a/infrastrucure/docker/image/resources/init.sh b/infrastrucure/docker/image/resources/init.sh deleted file mode 100755 index e48a555..0000000 --- a/infrastrucure/docker/image/resources/init.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash - -function main() { - file_env AWS_ACCESS_KEY_ID - file_env AWS_SECRET_ACCESS_KEY - - file_env RESTIC_PASSWORD_FILE - - restic -r ${RESTIC_REPOSITORY}/pg-role -v init - restic -r ${RESTIC_REPOSITORY}/pg-database -v init - restic -r ${RESTIC_REPOSITORY}/files -v init -} - -source /usr/local/lib/functions.sh -main diff --git a/infrastrucure/docker/image/resources/install.sh b/infrastrucure/docker/image/resources/install.sh index 9f77101..a800284 100755 --- a/infrastrucure/docker/image/resources/install.sh +++ b/infrastrucure/docker/image/resources/install.sh @@ -12,3 +12,4 @@ update-ca-certificates install -m 0400 /tmp/functions.sh /usr/local/lib/ install -m 0400 /tmp/backup-pg-functions.sh /usr/local/lib/ +install -m 0400 /tmp/backup-file-functions.sh /usr/local/lib/ diff --git a/infrastrucure/docker/image/resources/restore.sh b/infrastrucure/docker/image/resources/restore.sh deleted file mode 100755 index 9eb6124..0000000 --- a/infrastrucure/docker/image/resources/restore.sh +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/bash - -set -Eeo pipefail - -function main() { - - #Start maintenance mode - #/usr/local/bin/start-maintenance.sh - - file_env AWS_ACCESS_KEY_ID - file_env AWS_SECRET_ACCESS_KEY - - file_env POSTGRES_DB - file_env POSTGRES_PASSWORD - file_env POSTGRES_USER - - file_env RESTIC_PASSWORD - - # create new db - psql -d template1 -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U ${POSTGRES_USER} \ - --no-password -c "DROP DATABASE \"${POSTGRES_DB}\";" - psql -d template1 -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U ${POSTGRES_USER} \ - --no-password -c "CREATE DATABASE \"${POSTGRES_DB}\";" - - # init restic - restic -v -r ${RESTIC_REPOSITORY}/pg-role snapshots - - # restore roles - restic -r ${RESTIC_REPOSITORY}/pg-role dump latest stdin | \ - psql -d template1 -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U ${POSTGRES_USER} \ - --no-password - - # restore db - restic -r ${RESTIC_REPOSITORY}/pg-database dump latest stdin | \ - psql -d ${POSTGRES_DB} -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U ${POSTGRES_USER} \ - --no-password - - # files - rm -rf /var/backups/* - restic -v -r $RESTIC_REPOSITORY/files restore latest --target /var/backups/ - - # adjust trusted domains - php /var/www/html/occ config:system:set trusted_domains 1 --value=cloud.test.meissa-gmbh.de - - #End maintenance mode - #/usr/local/bin/end-maintenance.sh -} - -source /usr/local/lib/functions.sh -main - diff --git a/infrastrucure/docker/test/serverspec.edn b/infrastrucure/docker/test/serverspec.edn index 5870ccf..841d2ed 100644 --- a/infrastrucure/docker/test/serverspec.edn +++ b/infrastrucure/docker/test/serverspec.edn @@ -2,4 +2,5 @@ {:name "postgresql-client-13"}] :file [{:path "/entrypoint.sh" :mod "700"} {:path "/usr/local/lib/functions.sh" :mod "400"} - {:path "/usr/local/lib/backup-pg-functions.sh" :mod "400"}]} + {:path "/usr/local/lib/backup-pg-functions.sh" :mod "400"} + {:path "/usr/local/lib/backup-file-functions.sh" :mod "400"}]}