dda-backup/infrastructure/docker/image/resources/pg-functions.sh

125 lines
4 KiB
Bash
Raw Normal View History

function init-command() {
restic -r ${RESTIC_REPOSITORY}/pg-role -v init $@
}
2021-11-12 08:54:17 +00:00
function init-role-repo() {
2021-11-12 08:54:17 +00:00
if [ -z ${CERTIFICATE_FILE} ];
then
init-command
2021-11-12 08:54:17 +00:00
else
init-command --cacert ${CERTIFICATE_FILE}
2021-11-12 08:54:17 +00:00
fi
2020-12-31 14:05:56 +00:00
}
function init-database-command() {
restic -r ${RESTIC_REPOSITORY}/pg-database -v init $@
}
2020-12-31 14:05:56 +00:00
function init-database-repo() {
2021-11-12 08:54:17 +00:00
if [ -z ${CERTIFICATE_FILE} ];
then
init-database-command
2021-11-12 08:54:17 +00:00
else
init-database-command --cacert ${CERTIFICATE_FILE}
2021-11-12 08:54:17 +00:00
fi
2020-12-31 14:05:56 +00:00
}
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 roles-unlock-command() {
restic -v -r ${RESTIC_REPOSITORY}/pg-role unlock --cleanup-cache $@
}
function roles-forget-command() {
restic -v -r ${RESTIC_REPOSITORY}/pg-role forget --keep-last 1 --keep-within ${RESTIC_DAYS_TO_KEEP}d --prune $@
}
2020-12-31 14:05:56 +00:00
function backup-roles() {
local role_prefix="$1"; shift
2021-11-12 08:54:17 +00:00
if [ -z ${CERTIFICATE_FILE} ];
then
roles-unlock-command
pg_dumpall -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U${POSTGRES_USER} --no-password --roles-only | \
grep ${role_prefix} | restic -r ${RESTIC_REPOSITORY}/pg-role backup --stdin
roles-forget-command
2021-11-12 08:54:17 +00:00
else
roles-unlock-command --cacert ${CERTIFICATE_FILE}
pg_dumpall -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U${POSTGRES_USER} --no-password --roles-only | \
grep ${role_prefix} | restic -r ${RESTIC_REPOSITORY}/pg-role backup --stdin --cacert ${CERTIFICATE_FILE}
roles-forget-command --cacert ${CERTIFICATE_FILE}
2021-11-12 08:54:17 +00:00
fi
2020-12-31 14:05:56 +00:00
}
function db-unlock-command() {
restic -v -r ${RESTIC_REPOSITORY}/pg-database unlock --cleanup-cache $@
}
function db-forget-command() {
restic -v -r ${RESTIC_REPOSITORY}/pg-database forget --keep-last 1 --keep-within ${RESTIC_DAYS_TO_KEEP}d --prune $@
}
2020-12-31 14:05:56 +00:00
function backup-db-dump() {
2021-11-12 08:54:17 +00:00
if [ -z ${CERTIFICATE_FILE} ];
then
db-unlock-command
pg_dump -d ${POSTGRES_DB} -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} \
-U ${POSTGRES_USER} --no-password --serializable-deferrable | \
restic -r ${RESTIC_REPOSITORY}/pg-database backup --stdin
db-forget-command
2021-11-12 08:54:17 +00:00
else
db-unlock-command --cacert ${CERTIFICATE_FILE}
pg_dump -d ${POSTGRES_DB} -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} \
-U ${POSTGRES_USER} --no-password --serializable-deferrable | \
restic -r ${RESTIC_REPOSITORY}/pg-database backup --stdin --cacert ${CERTIFICATE_FILE}
db-forget-command --cacert ${CERTIFICATE_FILE}
2021-11-12 08:54:17 +00:00
fi
2020-12-31 14:05:56 +00:00
}
function restore-roles() {
2021-11-12 08:54:17 +00:00
if [ -z ${CERTIFICATE_FILE} ];
then
roles-unlock-command
restic -r ${RESTIC_REPOSITORY}/pg-role dump latest stdin | \
psql -d template1 -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U ${POSTGRES_USER} \
--no-password
2021-11-12 08:54:17 +00:00
else
roles-unlock-command --cacert ${CERTIFICATE_FILE}
2021-11-26 11:35:48 +00:00
restic -r ${RESTIC_REPOSITORY}/pg-role dump latest stdin --cacert ${CERTIFICATE_FILE} | \
psql -d template1 -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U ${POSTGRES_USER} \
2021-11-26 11:35:48 +00:00
--no-password
2021-11-12 08:54:17 +00:00
fi
2020-12-31 14:05:56 +00:00
}
function restore-db() {
2021-11-12 08:54:17 +00:00
if [ -z ${CERTIFICATE_FILE} ];
then
db-unlock-command
restic -r ${RESTIC_REPOSITORY}/pg_database dump latest stdin | \
psql -d ${POSTGRES_DB} -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U ${POSTGRES_USER} \
--no-password
2021-11-12 08:54:17 +00:00
else
db-unlock-command --cacert ${CERTIFICATE_FILE}
2021-11-26 11:35:48 +00:00
restic -r ${RESTIC_REPOSITORY}/pg_database dump latest stdin --cacert ${CERTIFICATE_FILE} | \
psql -d ${POSTGRES_DB} -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U ${POSTGRES_USER} \
2021-11-26 11:35:48 +00:00
--no-password
2021-11-12 08:54:17 +00:00
fi
2020-12-31 14:05:56 +00:00
}