From 9c2164483bbe3fb47d84063c55475cc8bb88bdb8 Mon Sep 17 00:00:00 2001 From: bom Date: Thu, 31 Dec 2020 15:05:56 +0100 Subject: [PATCH] finish refactoring functions --- .../image/resources/backup-file-functions.sh | 3 - .../image/resources/backup-pg-functions.sh | 27 --------- .../docker/image/resources/file-functions.sh | 23 +++++++ .../docker/image/resources/install.sh | 4 +- .../docker/image/resources/pg-functions.sh | 60 +++++++++++++++++++ infrastrucure/docker/test/serverspec.edn | 4 +- 6 files changed, 87 insertions(+), 34 deletions(-) delete mode 100644 infrastrucure/docker/image/resources/backup-file-functions.sh delete mode 100644 infrastrucure/docker/image/resources/backup-pg-functions.sh create mode 100644 infrastrucure/docker/image/resources/file-functions.sh create mode 100644 infrastrucure/docker/image/resources/pg-functions.sh diff --git a/infrastrucure/docker/image/resources/backup-file-functions.sh b/infrastrucure/docker/image/resources/backup-file-functions.sh deleted file mode 100644 index cfeaee0..0000000 --- a/infrastrucure/docker/image/resources/backup-file-functions.sh +++ /dev/null @@ -1,3 +0,0 @@ -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 deleted file mode 100644 index 82d43b4..0000000 --- a/infrastrucure/docker/image/resources/backup-pg-functions.sh +++ /dev/null @@ -1,27 +0,0 @@ -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/file-functions.sh b/infrastrucure/docker/image/resources/file-functions.sh new file mode 100644 index 0000000..defbdcf --- /dev/null +++ b/infrastrucure/docker/image/resources/file-functions.sh @@ -0,0 +1,23 @@ +function init-file-repo() { + restic -r ${RESTIC_REPOSITORY}/files -v init +} + + +function backup-directory() { + local directory="$1"; shift + + restic -v -r ${RESTIC_REPOSITORY}/files unlock --cleanup-cache + + cd ${directory} && 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 +} + +function restore-directory() { + local directory="$1"; shift + + restic -v -r ${RESTIC_REPOSITORY}/files unlock --cleanup-cache + + rm -rf ${directory}* + restic -v -r $RESTIC_REPOSITORY/files restore latest --target ${directory} +} \ No newline at end of file diff --git a/infrastrucure/docker/image/resources/install.sh b/infrastrucure/docker/image/resources/install.sh index a800284..c27c1c5 100755 --- a/infrastrucure/docker/image/resources/install.sh +++ b/infrastrucure/docker/image/resources/install.sh @@ -11,5 +11,5 @@ apt-get -qqy install wget postgresql-client-13 restic > /dev/null; 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/ +install -m 0400 /tmp/pg-functions.sh /usr/local/lib/ +install -m 0400 /tmp/file-functions.sh /usr/local/lib/ diff --git a/infrastrucure/docker/image/resources/pg-functions.sh b/infrastrucure/docker/image/resources/pg-functions.sh new file mode 100644 index 0000000..aa22a8d --- /dev/null +++ b/infrastrucure/docker/image/resources/pg-functions.sh @@ -0,0 +1,60 @@ +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 drop-create-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}\";" +} + +function create-pg-pass() { + local pg_host=${POSTGRES_HOST:-localhost} + + 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 +} + +function backup-db-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 +} + +function restore-roles() { + restic -v -r ${RESTIC_REPOSITORY}/pg-role unlock --cleanup-cache + + restic -r ${RESTIC_REPOSITORY}/pg-role dump latest stdin | \ + psql -d template1 -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U ${POSTGRES_USER} \ + --no-password +} + +function restore-db() { + restic -v -r ${RESTIC_REPOSITORY}/pg-database unlock --cleanup-cache + + restic -r ${RESTIC_REPOSITORY}/pg-database dump latest stdin | \ + psql -d ${POSTGRES_DB} -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U ${POSTGRES_USER} \ + --no-password +} \ No newline at end of file diff --git a/infrastrucure/docker/test/serverspec.edn b/infrastrucure/docker/test/serverspec.edn index 841d2ed..6e0121e 100644 --- a/infrastrucure/docker/test/serverspec.edn +++ b/infrastrucure/docker/test/serverspec.edn @@ -2,5 +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-file-functions.sh" :mod "400"}]} + {:path "/usr/local/lib/pg-functions.sh" :mod "400"} + {:path "/usr/local/lib/file-functions.sh" :mod "400"}]}