From b2ce0c4246d3288c18f8fb3ef3abeb517bd0a336 Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch> Date: Tue, 17 Dec 2024 10:00:53 +0100 Subject: [PATCH 1/2] pgsql handle env variables to disable snapshots or set times for disconnect --- plugins/localdump/pgsql.sh | 53 ++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/plugins/localdump/pgsql.sh b/plugins/localdump/pgsql.sh index 86ecf86..b75658e 100755 --- a/plugins/localdump/pgsql.sh +++ b/plugins/localdump/pgsql.sh @@ -16,6 +16,7 @@ # 2024-10-02 ah v1.5 rename backup and restore function # 2024-12-13 ah v1.6 backup uses a snapshot db # 2024-12-16 ah v1.7 on snapshot mode it can fallback for single database to normal pg_dump of origin +# 2024-12-17 ah v1.8 handle env variables to disable snapshots or set times for disconnect # ================================================================================ if [ -z "$BACKUP_TARGETDIR" ]; then @@ -46,11 +47,21 @@ function pgsql.backup(){ local bUseSnapshot local bUseSnapshot4DB local bDisconnect - local iHour; + local mytime typeset -i bUseSnapshot=0 typeset -i bDisconnect=0 - typeset -i iHour; iHour=$( date +%H) + + # handle config from jobs/env + LD_PGSQL_SNAPSHOTS="${LD_PGSQL_SNAPSHOTS:-1}" + LD_PGSQL_DISCONNECT_BEFORE="${LD_PGSQL_DISCONNECT_BEFORE:-00:00}" + LD_PGSQL_DISCONNECT_AFTER="${LD_PGSQL_DISCONNECT_AFTER:-24:00}" + + echo "${LD_PGSQL_DISCONNECT_BEFORE}_${LD_PGSQL_DISCONNECT_AFTER}" \ + | grep -q "[0-2][0-9]:[0-5][0-9]_[0-2][0-9]:[0-5][0-9]" \ + || echo "ERROR: LD_PGSQL_DISCONNECT_BEFORE and LD_PGSQL_DISCONNECT_AFTER must be in the format HH:MM" + + mytime="$( date +%H:%M)" create_targetdir @@ -60,25 +71,33 @@ function pgsql.backup(){ # ----- GO # prevent could not change directory to "/root": Permission denied - cd /tmp - - # Detect a readonly or writeable postgres host. - TESTDB="imlbackup_createtest" - su ${PGUSER} -c "dropdb ${TESTDB}" >/dev/null 2>&1 - if su ${PGUSER} -c "psql ${BACKUP_PARAMS} -c \"CREATE DATABASE ${TESTDB};\"" >/dev/null 2>&1 - then - bUseSnapshot=1 - su ${PGUSER} -c "dropdb ${TESTDB}" - echo "INFO: enabling snapshots" + cd /tmp || exit + + # Enable snapshots? + if [ "$LD_PGSQL_SNAPSHOTS" = "0" ]; then + echo "INFO: Snapshots disabled by LD_PGSQL_SNAPSHOTS=0" + bUseSnapshot=0 else - echo "INFO: create database failed for snapshots - dumping databases directly" + # Detect a readonly or writeable postgres host. + TESTDB="imlbackup_createtest" + su ${PGUSER} -c "dropdb ${TESTDB}" >/dev/null 2>&1 + if su ${PGUSER} -c "psql ${BACKUP_PARAMS} -c \"CREATE DATABASE ${TESTDB};\"" >/dev/null 2>&1 + then + bUseSnapshot=1 + su ${PGUSER} -c "dropdb ${TESTDB}" + echo "INFO: enabling snapshots" + else + echo "INFO: create database failed for snapshots - dumping databases directly" + fi fi - # disconnect after 21:00 and before 05:00 - if [ $bUseSnapshot -eq 1 ]; then - if [ $iHour -ge 21 ] || [ $iHour -lt 5 ]; then - echo "INFO: clients will be disconnected for snapshot database creation" + # enable disconnect? + if [ $bUseSnapshot -eq 1 ]; then + if [ "$mytime" \< "$LD_PGSQL_DISCONNECT_BEFORE" ] || [ "$mytime" \> "$LD_PGSQL_DISCONNECT_AFTER" ]; then + echo "INFO: clients will be disconnected for snapshot database creation." bDisconnect=1 + else + echo "INFO: clients will NOT be disconnected for snapshot database creation - which may fail then." fi fi -- GitLab From 6297eca82c7f073996468699ddde3f338def1dee Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch> Date: Tue, 17 Dec 2024 10:01:49 +0100 Subject: [PATCH 2/2] update docs --- docs/30_Configuration/60_File_env.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/30_Configuration/60_File_env.md b/docs/30_Configuration/60_File_env.md index 55f7dbf..2fceb92 100644 --- a/docs/30_Configuration/60_File_env.md +++ b/docs/30_Configuration/60_File_env.md @@ -60,6 +60,19 @@ RESTIC_PARAMS=--no-scan You can override used parameters during backup. The parameters for mysqldump are in env_defaults. If you want to change them then put ``LD_MYSQL_DUMP_PARAMS=...`` into env file. +### Localdump :: Pgsql + +Postgres backup supports backups with a snapshot database using `CREATE DATABASE snapshot_<SCHEMA> WITH TEMPLATE <SCHEMA>`. +The backup script detects if a server is writable or readonly (slave). On a writable system the snapshot creation is activated. + +The creation of a snapshot database fails if a user is connected on the origin database. It is posible to disconnect all users but shouldn't be triggered during production times. You can configure a night time when to allow the disconnect (default: no disconnect). + +You can put these variables into env: + +* LD_PGSQL_SNAPSHOTS=0 - disable snapshot on a writable database server +* LD_PGSQL_DISCONNECT_BEFORE - time in the morning until to allow disconnect; eg. "03:00" +* LD_PGSQL_DISCONNECT_AFTER - time in the night when to allow disconnect; eg "22:00" + ### Transfer :: Restic There are some environment variables for tuning -- GitLab