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

149 lines
5 KiB
Bash
Raw Normal View History

2023-03-10 14:01:32 +00:00
backup_pg_role_path='pg-role'
backup_pg_database_path='pg-database'
function init-command() {
2023-03-10 14:01:32 +00:00
restic -r ${RESTIC_REPOSITORY}/${backup_pg_role_path} -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() {
2023-03-10 14:01:32 +00:00
restic -r ${RESTIC_REPOSITORY}/${backup_pg_database_path} -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() {
2023-03-10 14:01:32 +00:00
restic -v -r ${RESTIC_REPOSITORY}/${backup_pg_role_path} unlock --cleanup-cache $@
}
function roles-forget-command() {
2023-03-10 14:01:32 +00:00
restic -v -r ${RESTIC_REPOSITORY}/${backup_pg_role_path} forget --group-by '' --keep-last 1 --keep-daily ${RESTIC_DAYS_TO_KEEP} --keep-monthly ${RESTIC_MONTHS_TO_KEEP} --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 | \
2023-03-10 14:01:32 +00:00
grep ${role_prefix} | restic -r ${RESTIC_REPOSITORY}/${backup_pg_role_path} 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 | \
2023-03-10 14:01:32 +00:00
grep ${role_prefix} | restic -r ${RESTIC_REPOSITORY}/${backup_pg_role_path} 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() {
2023-03-10 14:01:32 +00:00
restic -v -r ${RESTIC_REPOSITORY}/${backup_pg_database_path} unlock --cleanup-cache $@
}
function db-forget-command() {
2023-03-10 14:01:32 +00:00
restic -v -r ${RESTIC_REPOSITORY}/${backup_pg_database_path} forget --group-by '' --keep-last 1 --keep-daily ${RESTIC_DAYS_TO_KEEP} --keep-monthly ${RESTIC_MONTHS_TO_KEEP} --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 | \
2023-03-10 14:01:32 +00:00
restic -r ${RESTIC_REPOSITORY}/${backup_pg_database_path} 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 | \
2023-03-10 14:01:32 +00:00
restic -r ${RESTIC_REPOSITORY}/${backup_pg_database_path} 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() {
2023-03-10 14:19:09 +00:00
local snapshot_id="${1:-latest}"; shift
2021-11-12 08:54:17 +00:00
if [ -z ${CERTIFICATE_FILE} ];
then
roles-unlock-command
2023-03-10 14:19:09 +00:00
restic -r ${RESTIC_REPOSITORY}/${backup_pg_role_path} dump ${snapshot_id} 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}
2023-03-10 14:19:09 +00:00
restic -r ${RESTIC_REPOSITORY}/${backup_pg_role_path} dump ${snapshot_id} 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() {
2023-03-10 14:19:09 +00:00
local snapshot_id="${1:-latest}"; shift
2021-11-12 08:54:17 +00:00
if [ -z ${CERTIFICATE_FILE} ];
then
db-unlock-command
2023-03-10 14:19:09 +00:00
restic -r ${RESTIC_REPOSITORY}/${backup_pg_database_path} dump ${snapshot_id} 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}
2023-03-10 14:19:09 +00:00
restic -r ${RESTIC_REPOSITORY}/${backup_pg_database_path} dump ${snapshot_id} 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
2023-03-10 14:01:32 +00:00
}
function list-snapshot-roles() {
if [ -z ${CERTIFICATE_FILE} ];
then
restic -r ${RESTIC_REPOSITORY}/${backup_pg_role_path} snapshots
else
restic -r ${RESTIC_REPOSITORY}/${backup_pg_database_path} snapshots --cacert ${CERTIFICATE_FILE}
fi
}
function list-snapshot-db() {
if [ -z ${CERTIFICATE_FILE} ];
then
restic -r ${RESTIC_REPOSITORY}/${backup_pg_database_path} snapshots
else
restic -r ${RESTIC_REPOSITORY}/${backup_pg_database_path} snapshots --cacert ${CERTIFICATE_FILE}
fi
2020-12-31 14:05:56 +00:00
}