diff --git a/plugins/localdump/pgsql.sh b/plugins/localdump/pgsql.sh index 0fb225d3c8c77388bc59ed6d63abeec649c709b6..76f0839008e855a10a3af7a2783f854737416d44 100755 --- a/plugins/localdump/pgsql.sh +++ b/plugins/localdump/pgsql.sh @@ -42,6 +42,21 @@ function pgsql.backup(){ local SNAPSHOTDB local OUTFILE local snapshotprefix="snapshot_" + local bUseSnapshot + + 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 @@ -52,38 +67,47 @@ function pgsql.backup(){ # prevent could not change directory to "/root": Permission denied ( - cd /tmp - - 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" ) - do - echo -n "__DB__${SERVICENAME} backup $DATABASE ... " - SNAPSHOTDB="${snapshotprefix}${DATABASE}" - OUTFILE="${BACKUP_TARGETDIR}/$(get_outfile ${DATABASE}).sql" - - echo -n " snapshot ... " - # drop snapshot db first - just in case - su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" >/dev/null 2>&1 - su ${PGUSER} -c "psql ${BACKUP_PARAMS} -c \"CREATE DATABASE ${SNAPSHOTDB} WITH TEMPLATE ${DATABASE};\"" >/dev/null - fetchrc >/dev/null - if [ $myrc -eq 0 ]; then - - echo -n "backup ... " - if su ${PGUSER} -c "pg_dump ${BACKUP_PARAMS} -Fp ${SNAPSHOTDB} >$OUTFILE"; then - fetchrc >/dev/null - echo -n "delete snapshot ... " - su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" + cd /tmp + + 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" ) + do + echo -n "__DB__${SERVICENAME} backup $DATABASE ... " - db._compressDumpfile "$OUTFILE" + SNAPSHOTDB="${DATABASE}" + test $bUseSnapshot -eq 1 && SNAPSHOTDB="${snapshotprefix}${DATABASE}" + + OUTFILE="${BACKUP_TARGETDIR}/$(get_outfile ${DATABASE}).sql" + + myrc=0 + + # drop snapshot db first - just in case + test $bUseSnapshot -eq 1 && ( + echo -n " snapshot ... " + su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" >/dev/null 2>&1 + su ${PGUSER} -c "psql ${BACKUP_PARAMS} -c \"CREATE DATABASE ${SNAPSHOTDB} WITH TEMPLATE ${DATABASE};\"" >/dev/null + fetchrc >/dev/null + ) + if [ $myrc -eq 0 ]; then + + echo -n "backup ... " + if su ${PGUSER} -c "pg_dump ${BACKUP_PARAMS} -Fp ${SNAPSHOTDB} >$OUTFILE"; then + fetchrc >/dev/null + test $bUseSnapshot -eq 1 && ( + echo -n "delete snapshot ... " + su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" + ) + + db._compressDumpfile "$OUTFILE" + else + fetchrc + test $bUseSnapshot -eq 1 && su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" + fi else - fetchrc - su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" + cecho error "ERROR!" fi - else - cecho error "ERROR!" - fi - done + done ) }