finish refactoring functions

This commit is contained in:
bom 2020-12-31 15:05:56 +01:00
parent abef612581
commit 9c2164483b
6 changed files with 87 additions and 34 deletions

View file

@ -1,3 +0,0 @@
function init-file-repo() {
restic -r ${RESTIC_REPOSITORY}/files -v init
}

View file

@ -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
}

View file

@ -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}
}

View file

@ -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/

View file

@ -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
}

View file

@ -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"}]}