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

pgsql handle env variables to disable snapshots or set times for disconnect

parent 174b597c
No related branches found
No related tags found
1 merge request!152Pgsql env variables to configure snapshot
......@@ -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,8 +71,13 @@ function pgsql.backup(){
# ----- GO
# prevent could not change directory to "/root": Permission denied
cd /tmp
cd /tmp || exit
# Enable snapshots?
if [ "$LD_PGSQL_SNAPSHOTS" = "0" ]; then
echo "INFO: Snapshots disabled by LD_PGSQL_SNAPSHOTS=0"
bUseSnapshot=0
else
# Detect a readonly or writeable postgres host.
TESTDB="imlbackup_createtest"
su ${PGUSER} -c "dropdb ${TESTDB}" >/dev/null 2>&1
......@@ -73,12 +89,15 @@ function pgsql.backup(){
else
echo "INFO: create database failed for snapshots - dumping databases directly"
fi
fi
# disconnect after 21:00 and before 05:00
# enable disconnect?
if [ $bUseSnapshot -eq 1 ]; then
if [ $iHour -ge 21 ] || [ $iHour -lt 5 ]; then
echo "INFO: clients will be disconnected for snapshot database creation"
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