Skip to content
Snippets Groups Projects

support backup of single database or given databases

Merged Hahn Axel (hahn) requested to merge backup-single-db into master
2 files
+ 40
12
Compare changes
  • Side-by-side
  • Inline

Files

+ 16
3
@@ -18,6 +18,7 @@
@@ -18,6 +18,7 @@
# 2024-12-16 ah v1.7 on snapshot mode it can fallback for single database to normal pg_dump of origin
# 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
# 2024-12-17 ah v1.8 handle env variables to disable snapshots or set times for disconnect
# 2025-02-03 ah v1.9 pg_dump in temp directory; dump file will be moved to backup dir
# 2025-02-03 ah v1.9 pg_dump in temp directory; dump file will be moved to backup dir
 
# 2025-05-01 ah v1.10 support backup of single database or given databases
# ================================================================================
# ================================================================================
if [ -z "$BACKUP_TARGETDIR" ]; then
if [ -z "$BACKUP_TARGETDIR" ]; then
@@ -38,6 +39,12 @@ fi
@@ -38,6 +39,12 @@ fi
# FUNCTION
# FUNCTION
# --------------------------------------------------------------------------------
# --------------------------------------------------------------------------------
 
# get a list of existing databases
 
function pgsql.list(){
 
sSqlGetDblist="select datname from pg_database where not datistemplate and datallowconn order by datname;"
 
su ${PGUSER} -c "psql ${BACKUP_PARAMS} -At -c '$sSqlGetDblist' postgres" 2>/dev/null | grep -v "^$snapshotprefix"
 
}
 
# backup all databases
# backup all databases
function pgsql.backup(){
function pgsql.backup(){
@@ -77,7 +84,7 @@ function pgsql.backup(){
@@ -77,7 +84,7 @@ function pgsql.backup(){
bUseSnapshot=0
bUseSnapshot=0
else
else
# Detect a readonly or writeable postgres host.
# Detect a readonly or writeable postgres host.
TESTDB="imlbackup_createtest"
TESTDB="imlbackup_createtest_${snapshotprefix}"
su ${PGUSER} -c "dropdb ${TESTDB}" >/dev/null 2>&1
su ${PGUSER} -c "dropdb ${TESTDB}" >/dev/null 2>&1
if su ${PGUSER} -c "psql ${BACKUP_PARAMS} -c \"CREATE DATABASE ${TESTDB};\"" >/dev/null 2>&1
if su ${PGUSER} -c "psql ${BACKUP_PARAMS} -c \"CREATE DATABASE ${TESTDB};\"" >/dev/null 2>&1
then
then
@@ -99,8 +106,14 @@ function pgsql.backup(){
@@ -99,8 +106,14 @@ function pgsql.backup(){
fi
fi
fi
fi
sSqlGetDblist="select datname from pg_database where not datistemplate and datallowconn order by datname;"
sDBList="$*"
for DATABASE in $( su ${PGUSER} -c "psql ${BACKUP_PARAMS} -At -c '$sSqlGetDblist' postgres" 2>/dev/null | grep -v "^$snapshotprefix" )
if [ -z "$sDBList" ]; then
 
sDBList="$( pgsql.list )"
 
echo "INFO: found databases:"
 
sed "s#^# - #g" <<< "$sDBList"
 
fi
 
 
for DATABASE in $sDBList
do
do
echo -n "__DB__${SERVICENAME} backup $DATABASE ... "
echo -n "__DB__${SERVICENAME} backup $DATABASE ... "
Loading