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

on snapshot mode it can fallback for single database to normal pg_dump of origin

parent d22505cd
No related branches found
No related tags found
1 merge request!149on snapshot mode it can fallback for single database to normal pg_dump of origin
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
# 2023-09-20 v1.4 FIX could not change directory to "/root": Permission denied # 2023-09-20 v1.4 FIX could not change directory to "/root": Permission denied
# 2024-10-02 ah v1.5 rename backup and restore function # 2024-10-02 ah v1.5 rename backup and restore function
# 2024-12-13 ah v1.6 backup uses a snapshot db # 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
# ================================================================================ # ================================================================================
if [ -z "$BACKUP_TARGETDIR" ]; then if [ -z "$BACKUP_TARGETDIR" ]; then
...@@ -43,21 +44,10 @@ function pgsql.backup(){ ...@@ -43,21 +44,10 @@ function pgsql.backup(){
local OUTFILE local OUTFILE
local snapshotprefix="snapshot_" local snapshotprefix="snapshot_"
local bUseSnapshot local bUseSnapshot
local bUseSnapshot4DB
typeset -i bUseSnapshot=0 typeset -i bUseSnapshot=0
# 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
create_targetdir create_targetdir
# ----- change owner of directory because of su command # ----- change owner of directory because of su command
...@@ -69,31 +59,48 @@ function pgsql.backup(){ ...@@ -69,31 +59,48 @@ function pgsql.backup(){
( (
cd /tmp 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"
else
echo "INFO: create database failed for snapshots - dumping databases directly"
fi
sSqlGetDblist="select datname from pg_database where not datistemplate and datallowconn order by datname;" sSqlGetDblist="select datname from pg_database where not datistemplate and datallowconn order by datname;"
for DATABASE in $( su ${PGUSER} -c "psql ${BACKUP_PARAMS} -At -c '$sSqlGetDblist' postgres" 2>/dev/null | grep -v "^$snapshotprefix" ) for DATABASE in $( su ${PGUSER} -c "psql ${BACKUP_PARAMS} -At -c '$sSqlGetDblist' postgres" 2>/dev/null | grep -v "^$snapshotprefix" )
do do
echo -n "__DB__${SERVICENAME} backup $DATABASE ... " echo -n "__DB__${SERVICENAME} backup $DATABASE ... "
bUseSnapshot4DB=$bUseSnapshot
SNAPSHOTDB="${DATABASE}" SNAPSHOTDB="${DATABASE}"
test $bUseSnapshot -eq 1 && SNAPSHOTDB="${snapshotprefix}${DATABASE}" test $bUseSnapshot4DB -eq 1 && SNAPSHOTDB="${snapshotprefix}${DATABASE}"
OUTFILE="${BACKUP_TARGETDIR}/$(get_outfile ${DATABASE}).sql" OUTFILE="${BACKUP_TARGETDIR}/$(get_outfile ${DATABASE}).sql"
myrc=0 myrc=0
# drop snapshot db first - just in case # drop snapshot db first - just in case
test $bUseSnapshot -eq 1 && ( if [ $bUseSnapshot4DB -eq 1 ]; then
echo -n " snapshot ... " echo -n " snapshot ... "
su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" >/dev/null 2>&1 su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" >/dev/null 2>&1
su ${PGUSER} -c "psql ${BACKUP_PARAMS} -c \"CREATE DATABASE ${SNAPSHOTDB} WITH TEMPLATE ${DATABASE};\"" >/dev/null if ! su ${PGUSER} -c "psql ${BACKUP_PARAMS} -c \"CREATE DATABASE ${SNAPSHOTDB} WITH TEMPLATE ${DATABASE};\"" >/dev/null 2>&1; then
fetchrc >/dev/null echo -n " nope, using origin ... "
) bUseSnapshot4DB=0
SNAPSHOTDB="${DATABASE}"
fi
fi
if [ $myrc -eq 0 ]; then if [ $myrc -eq 0 ]; then
echo -n "backup ... " echo -n "backup ... "
if su ${PGUSER} -c "pg_dump ${BACKUP_PARAMS} -Fp ${SNAPSHOTDB} >$OUTFILE"; then if su ${PGUSER} -c "pg_dump ${BACKUP_PARAMS} -Fp ${SNAPSHOTDB} >$OUTFILE"; then
fetchrc >/dev/null fetchrc >/dev/null
test $bUseSnapshot -eq 1 && ( test $bUseSnapshot4DB -eq 1 && (
echo -n "delete snapshot ... " echo -n "delete snapshot ... "
su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" su ${PGUSER} -c "dropdb ${SNAPSHOTDB}"
) )
...@@ -101,7 +108,7 @@ function pgsql.backup(){ ...@@ -101,7 +108,7 @@ function pgsql.backup(){
db._compressDumpfile "$OUTFILE" db._compressDumpfile "$OUTFILE"
else else
fetchrc fetchrc
test $bUseSnapshot -eq 1 && su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" test $bUseSnapshot4DB -eq 1 && su ${PGUSER} -c "dropdb ${SNAPSHOTDB}"
fi fi
else else
cecho error "ERROR!" cecho error "ERROR!"
...@@ -172,21 +179,6 @@ else ...@@ -172,21 +179,6 @@ else
fi fi
# case $1 in
# backup)
# test $SERVICEFOUND -eq 1 && pgsql.backup
# ;;
# restore)
# shift 1
# test $SERVICEFOUND -eq 1 && pgsql.restore $*
# ;;
# *)
# echo ERROR: wrong syntax:
# echo $0 $*
# exit 1
# esac
echo "__DB__$SERVICENAME INFO: $0 $* [$SERVICENAME] final returncode rc=$rc" echo "__DB__$SERVICENAME INFO: $0 $* [$SERVICENAME] final returncode rc=$rc"
# -------------------------------------------------------------------------------- # --------------------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment