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

pgsql: fixes with shellcheck

parent acbce830
Branches
No related tags found
1 merge request!26Eliminate scheduler
#!/bin/bash #!/bin/bash
# ================================================================================ # # LOCALDUMP :: POSTGRES # ================================================================================
#
# LOCALDUMP :: POSTGRES
# create gzipped plain text backups from each scheme # create gzipped plain text backups from each scheme
# #
# -------------------------------------------------------------------------------- # --------------------------------------------------------------------------------
...@@ -8,12 +10,14 @@ ...@@ -8,12 +10,14 @@
# #
# 2016-11-10 ah,ds v1.0 # 2016-11-10 ah,ds v1.0
# 2017-03-29 ..... v1.1 added restore # 2017-03-29 ..... v1.1 added restore
# 2022-01-20 v1.2 fixes with shellcheck
# ================================================================================ # ================================================================================
if [ -z $BACKUP_TARGETDIR ]; then if [ -z "$BACKUP_TARGETDIR" ]; then
echo ERROR: you cannot start `basename $0` directly echo "ERROR: you cannot start $(basename $0) directly"
exit 1 rc=$rc+1
fi exit 1
fi
# -------------------------------------------------------------------------------- # --------------------------------------------------------------------------------
# CONFIG # CONFIG
...@@ -32,29 +36,29 @@ function doPgsqlBackup(){ ...@@ -32,29 +36,29 @@ function doPgsqlBackup(){
create_targetdir create_targetdir
# ----- change owner of directory because of su command # ----- change owner of directory because of su command
chown ${PGUSER}. ${BACKUP_TARGETDIR} chown ${PGUSER}. "${BACKUP_TARGETDIR}"
# ----- GO # ----- GO
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 -At -c '$sSqlGetDblist' postgres" 2>/dev/null` for DATABASE in $(su ${PGUSER} -c "psql -At -c '$sSqlGetDblist' postgres" 2>/dev/null)
do do
echo "----- $DATABASE" echo "----- $DATABASE"
echo -n "backup ... " 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" su ${PGUSER} -c "pg_dump -Fp ${DATABASE} >$OUTFILE"
fetchrc fetchrc
if [ $myrc -eq 0 ]; then if [ $myrc -eq 0 ]; then
echo -n "compress ... " echo -n "compress ... "
compress_file $OUTFILE compress_file "$OUTFILE"
else else
color error color error
echo "ERROR occured - no gzip" echo "ERROR occured - no gzip"
color reset color reset
# mv $OUTFILE $OUTFILE # mv $OUTFILE $OUTFILE
fi fi
ls -l $OUTFILE* ls -l "$OUTFILE"*
echo echo
done done
} }
...@@ -67,9 +71,9 @@ function restoreByFile(){ ...@@ -67,9 +71,9 @@ function restoreByFile(){
sMyfile=$1 sMyfile=$1
sMyDb=$2 sMyDb=$2
if [ -z $sMyDb ]; then if [ -z "$sMyDb" ]; then
h2 "analyze dump $sMyfile" h2 "analyze dump $sMyfile"
sMyDb=`guessDB $sMyfile` sMyDb="$(guessDB "$sMyfile" )"
echo "detected db schema from file: [${sMyDb}]" echo "detected db schema from file: [${sMyDb}]"
else else
echo "db schema from param 2: [${sMyDb}]" echo "db schema from param 2: [${sMyDb}]"
...@@ -77,22 +81,22 @@ function restoreByFile(){ ...@@ -77,22 +81,22 @@ function restoreByFile(){
echo echo
echo import to $sMyDb... echo import to "$sMyDb"...
h2 deleting database [$sMyDb] ... h2 "deleting database [$sMyDb] ..."
color cmd color cmd
su ${PGUSER} -c "dropdb ${sMyDb}" su ${PGUSER} -c "dropdb ${sMyDb}"
color reset color reset
h2 ensure that database exists ... h2 "ensure that database exists ..."
color cmd color cmd
su ${PGUSER} -c "psql -c \"CREATE DATABASE ${sMyDb};\"" su ${PGUSER} -c "psql -c \"CREATE DATABASE ${sMyDb};\""
fetchrc fetchrc
color reset color reset
h2 import ... h2 "import ..."
ls -l "${sMyfile}" ls -l "${sMyfile}"
echo import to database [${sMyDb}] echo "import to database [${sMyDb}]"
color cmd color cmd
zcat "${sMyfile}" | su ${PGUSER} -c "psql -d ${sMyDb}" zcat "${sMyfile}" | su ${PGUSER} -c "psql -d ${sMyDb}"
fetchrc fetchrc
...@@ -123,6 +127,6 @@ function restoreByFile(){ ...@@ -123,6 +127,6 @@ function restoreByFile(){
fi fi
echo $0 $* [postgres] final returncode rc=$rc echo "$0 $* [postgres] final returncode rc=$rc"
# -------------------------------------------------------------------------------- # --------------------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment