2023-03-10 14:01:32 +00:00
|
|
|
backup_pg_role_path='pg-role'
|
|
|
|
backup_pg_database_path='pg-database'
|
|
|
|
|
2021-11-26 11:19:11 +00:00
|
|
|
function init-command() {
|
2023-03-10 14:01:32 +00:00
|
|
|
restic -r ${RESTIC_REPOSITORY}/${backup_pg_role_path} -v init $@
|
2021-11-26 11:19:11 +00:00
|
|
|
}
|
2021-11-12 08:54:17 +00:00
|
|
|
|
2021-11-26 11:19:11 +00:00
|
|
|
function init-role-repo() {
|
2021-11-12 08:54:17 +00:00
|
|
|
|
|
|
|
if [ -z ${CERTIFICATE_FILE} ];
|
|
|
|
then
|
2021-11-26 11:19:11 +00:00
|
|
|
init-command
|
2021-11-12 08:54:17 +00:00
|
|
|
else
|
2021-11-26 11:19:11 +00:00
|
|
|
init-command --cacert ${CERTIFICATE_FILE}
|
2021-11-12 08:54:17 +00:00
|
|
|
fi
|
|
|
|
|
2020-12-31 14:05:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-26 11:19:11 +00:00
|
|
|
function init-database-command() {
|
2023-03-10 14:01:32 +00:00
|
|
|
restic -r ${RESTIC_REPOSITORY}/${backup_pg_database_path} -v init $@
|
2021-11-26 11:19:11 +00:00
|
|
|
}
|
|
|
|
|
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
|
2021-11-26 11:19:11 +00:00
|
|
|
init-database-command
|
2021-11-12 08:54:17 +00:00
|
|
|
else
|
2021-11-26 11:19:11 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-26 11:19:11 +00:00
|
|
|
function roles-unlock-command() {
|
2023-03-10 14:01:32 +00:00
|
|
|
restic -v -r ${RESTIC_REPOSITORY}/${backup_pg_role_path} unlock --cleanup-cache $@
|
2021-11-26 11:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 $@
|
2021-11-26 11:19:11 +00:00
|
|
|
}
|
|
|
|
|
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
|
2021-11-26 11:19:11 +00:00
|
|
|
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
|
2021-11-26 11:19:11 +00:00
|
|
|
roles-forget-command
|
2021-11-12 08:54:17 +00:00
|
|
|
else
|
2021-11-26 11:19:11 +00:00
|
|
|
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}
|
2021-11-26 11:19:11 +00:00
|
|
|
roles-forget-command --cacert ${CERTIFICATE_FILE}
|
2021-11-12 08:54:17 +00:00
|
|
|
fi
|
2020-12-31 14:05:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-26 11:19:11 +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 $@
|
2021-11-26 11:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 $@
|
2021-11-26 11:19:11 +00:00
|
|
|
}
|
|
|
|
|
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
|
2021-11-26 11:19:11 +00:00
|
|
|
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
|
2021-11-26 11:19:11 +00:00
|
|
|
db-forget-command
|
2021-11-12 08:54:17 +00:00
|
|
|
else
|
2021-11-26 11:19:11 +00:00
|
|
|
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}
|
2021-11-26 11:19:11 +00:00
|
|
|
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
|
2021-11-26 11:19:11 +00:00
|
|
|
roles-unlock-command
|
2023-03-10 14:19:09 +00:00
|
|
|
restic -r ${RESTIC_REPOSITORY}/${backup_pg_role_path} dump ${snapshot_id} stdin | \
|
2021-11-26 11:19:11 +00:00
|
|
|
psql -d template1 -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U ${POSTGRES_USER} \
|
|
|
|
--no-password
|
2021-11-12 08:54:17 +00:00
|
|
|
else
|
2021-11-26 11:19:11 +00:00
|
|
|
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} | \
|
2021-11-26 11:19:11 +00:00
|
|
|
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
|
2021-11-26 11:19:11 +00:00
|
|
|
db-unlock-command
|
2023-03-10 14:19:09 +00:00
|
|
|
restic -r ${RESTIC_REPOSITORY}/${backup_pg_database_path} dump ${snapshot_id} stdin | \
|
2021-11-26 11:19:11 +00:00
|
|
|
psql -d ${POSTGRES_DB} -h ${POSTGRES_SERVICE} -p ${POSTGRES_PORT} -U ${POSTGRES_USER} \
|
|
|
|
--no-password
|
2021-11-12 08:54:17 +00:00
|
|
|
else
|
2021-11-26 11:19:11 +00:00
|
|
|
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} | \
|
2021-11-26 11:19:11 +00:00
|
|
|
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
|
|
|
}
|