diff --git a/plugins/localdump/pgsql.sh b/plugins/localdump/pgsql.sh index 53fb8e2e644d5b68d5634ecc02d7fc5f7bddedb7..1db2be5f9cb792fee7093a1b1f86a523aadd06df 100755 --- a/plugins/localdump/pgsql.sh +++ b/plugins/localdump/pgsql.sh @@ -56,66 +56,65 @@ 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" - else - echo "INFO: create database failed for snapshots - dumping databases directly" - fi + 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;" - 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 ... " + 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 ... " - bUseSnapshot4DB=$bUseSnapshot + bUseSnapshot4DB=$bUseSnapshot - SNAPSHOTDB="${DATABASE}" - test $bUseSnapshot4DB -eq 1 && SNAPSHOTDB="${snapshotprefix}${DATABASE}" + SNAPSHOTDB="${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 - if [ $bUseSnapshot4DB -eq 1 ]; then - echo -n " snapshot ... " - su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" >/dev/null 2>&1 - if ! su ${PGUSER} -c "psql ${BACKUP_PARAMS} -c \"CREATE DATABASE ${SNAPSHOTDB} WITH TEMPLATE ${DATABASE};\"" >/dev/null 2>&1; then - echo -n " nope, using origin ... " - bUseSnapshot4DB=0 - SNAPSHOTDB="${DATABASE}" - fi + # drop snapshot db first - just in case + if [ $bUseSnapshot4DB -eq 1 ]; then + echo -n " snapshot ... " + su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" >/dev/null 2>&1 + if ! su ${PGUSER} -c "psql ${BACKUP_PARAMS} -c \"CREATE DATABASE ${SNAPSHOTDB} WITH TEMPLATE ${DATABASE};\"" >/dev/null; then + echo -n "Fallback using origin ... " + bUseSnapshot4DB=0 + SNAPSHOTDB="${DATABASE}" fi - if [ $myrc -eq 0 ]; then - - echo -n "backup ... " - if su ${PGUSER} -c "pg_dump ${BACKUP_PARAMS} -Fp ${SNAPSHOTDB} >$OUTFILE"; then - fetchrc >/dev/null - test $bUseSnapshot4DB -eq 1 && ( - echo -n "delete snapshot ... " - su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" - ) - - db._compressDumpfile "$OUTFILE" - else - fetchrc - test $bUseSnapshot4DB -eq 1 && su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" - fi + fi + if [ $myrc -eq 0 ]; then + + echo -n "backup ... " + if su ${PGUSER} -c "pg_dump ${BACKUP_PARAMS} -Fp ${SNAPSHOTDB} >$OUTFILE"; then + fetchrc >/dev/null + test $bUseSnapshot4DB -eq 1 && ( + echo -n "delete snapshot ... " + su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" + ) + + db._compressDumpfile "$OUTFILE" else - cecho error "ERROR!" + fetchrc + test $bUseSnapshot4DB -eq 1 && su ${PGUSER} -c "dropdb ${SNAPSHOTDB}" fi + else + cecho error "ERROR!" + fi - done - ) + done + cd - >/dev/null }