Skip to content
Snippets Groups Projects
Commit 44c4d6a5 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

Merge branch 'pgsql-snapshot-fallback' into 'master'

Pgsql env variables to configure snapshot

See merge request !152
parents fb38974b 6297eca8
No related branches found
No related tags found
1 merge request!152Pgsql env variables to configure snapshot
......@@ -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
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment