diff --git a/plugins/localdump/pgsql.sh b/plugins/localdump/pgsql.sh index bbc9549f1dd0c422c253440053309325055cb918..5a896508dc34ba4d221020a984b3e0a7abfdd1ba 100755 --- a/plugins/localdump/pgsql.sh +++ b/plugins/localdump/pgsql.sh @@ -1,5 +1,7 @@ #!/bin/bash -# ================================================================================ # # LOCALDUMP :: POSTGRES +# ================================================================================ +# +# LOCALDUMP :: POSTGRES # create gzipped plain text backups from each scheme # # -------------------------------------------------------------------------------- @@ -8,12 +10,14 @@ # # 2016-11-10 ah,ds v1.0 # 2017-03-29 ..... v1.1 added restore +# 2022-01-20 v1.2 fixes with shellcheck # ================================================================================ - if [ -z $BACKUP_TARGETDIR ]; then - echo ERROR: you cannot start `basename $0` directly - exit 1 - fi +if [ -z "$BACKUP_TARGETDIR" ]; then + echo "ERROR: you cannot start $(basename $0) directly" + rc=$rc+1 + exit 1 +fi # -------------------------------------------------------------------------------- # CONFIG @@ -32,29 +36,29 @@ function doPgsqlBackup(){ create_targetdir # ----- change owner of directory because of su command - chown ${PGUSER}. ${BACKUP_TARGETDIR} + chown ${PGUSER}. "${BACKUP_TARGETDIR}" # ----- GO sSqlGetDblist="select datname from pg_database where not datistemplate and datallowconn order by datname;" - for DATABASE in `su ${PGUSER} -c "psql -At -c '$sSqlGetDblist' postgres" 2>/dev/null` + for DATABASE in $(su ${PGUSER} -c "psql -At -c '$sSqlGetDblist' postgres" 2>/dev/null) do echo "----- $DATABASE" echo -n "backup ... " - OUTFILE=${BACKUP_TARGETDIR}/`get_outfile ${DATABASE}`.sql + OUTFILE="${BACKUP_TARGETDIR}/$(get_outfile ${DATABASE}).sql" su ${PGUSER} -c "pg_dump -Fp ${DATABASE} >$OUTFILE" fetchrc if [ $myrc -eq 0 ]; then echo -n "compress ... " - compress_file $OUTFILE + compress_file "$OUTFILE" else color error echo "ERROR occured - no gzip" color reset # mv $OUTFILE $OUTFILE fi - ls -l $OUTFILE* + ls -l "$OUTFILE"* echo done } @@ -67,9 +71,9 @@ function restoreByFile(){ sMyfile=$1 sMyDb=$2 - if [ -z $sMyDb ]; then + if [ -z "$sMyDb" ]; then h2 "analyze dump $sMyfile" - sMyDb=`guessDB $sMyfile` + sMyDb="$(guessDB "$sMyfile" )" echo "detected db schema from file: [${sMyDb}]" else echo "db schema from param 2: [${sMyDb}]" @@ -77,22 +81,22 @@ function restoreByFile(){ echo - echo import to $sMyDb... + echo import to "$sMyDb"... - h2 deleting database [$sMyDb] ... + h2 "deleting database [$sMyDb] ..." color cmd su ${PGUSER} -c "dropdb ${sMyDb}" color reset - h2 ensure that database exists ... + h2 "ensure that database exists ..." color cmd su ${PGUSER} -c "psql -c \"CREATE DATABASE ${sMyDb};\"" fetchrc color reset - h2 import ... + h2 "import ..." ls -l "${sMyfile}" - echo import to database [${sMyDb}] + echo "import to database [${sMyDb}]" color cmd zcat "${sMyfile}" | su ${PGUSER} -c "psql -d ${sMyDb}" fetchrc @@ -123,6 +127,6 @@ function restoreByFile(){ fi - echo $0 $* [postgres] final returncode rc=$rc + echo "$0 $* [postgres] final returncode rc=$rc" # --------------------------------------------------------------------------------