From c4868db7935a38b4db9cdf386b84f4055cec0e35 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch>
Date: Fri, 21 Jan 2022 15:48:55 +0100
Subject: [PATCH] couchdb: fixes with shellcheck

---
 plugins/localdump/couchdb.sh | 88 +++++++++++++++++-------------------
 1 file changed, 42 insertions(+), 46 deletions(-)

diff --git a/plugins/localdump/couchdb.sh b/plugins/localdump/couchdb.sh
index 126a8d1..fa35089 100755
--- a/plugins/localdump/couchdb.sh
+++ b/plugins/localdump/couchdb.sh
@@ -10,10 +10,11 @@
 #
 # 2017-03-24  ah,ds  v0.9  backup
 # 2017-03-27  .....  v1.0  restore
+# 2022-01-20         v1.1  fixes with shellcheck
 # ================================================================================
 
-if [ -z $BACKUP_TARGETDIR ]; then
-  echo ERROR: you cannot start `basename $0` directly
+if [ -z "$BACKUP_TARGETDIR" ]; then
+  echo ERROR: you cannot start $(basename $0) directly
   rc=$rc+1
   exit 1
 fi
@@ -41,10 +42,10 @@ function _couchapi(){
   # sParams="$sParams -u ${couchdbuser}:${couchdbpw}"
   sParams="$sParams -X ${method}"
   sParams="$sParams ${COUCHDB_URL}${apiurl}"
-  if [ ! -z $outfile ]; then
+  if [ ! -z "$outfile" ]; then
     sParams="$sParams -o ${outfile}"
   fi
-  curl $sParams 2>/dev/null
+  curl "$sParams" 2>/dev/null
 }
 
 function _couchGet(){
@@ -52,7 +53,7 @@ function _couchGet(){
 }
 
 function _getDblist(){
-   _couchGet _all_dbs | sed 's#\"#\n#g' | egrep -v "^(\[|\,|\])$"
+   _couchGet _all_dbs | sed 's#\"#\n#g' | grep -Ev "^(\[|\,|\])$"
 }
 
 
@@ -60,11 +61,10 @@ function _getDblist(){
 
 # get valid configured instances
 function getInstances(){
- for mycfg in `ls -1 ~/.iml_backup/couchdb/*.config`
+ for mycfg in $(ls -1 "~/.iml_backup/couchdb/*.config" )
  do
-   . $mycfg
-   if [ $? -eq 0 ]; then
-     echo `basename "${mycfg}" | cut -f 1 -d "."`
+   if . "$mycfg"; then
+     echo $(basename "${mycfg}" | cut -f 1 -d ".")
    fi
  done
 }
@@ -75,25 +75,24 @@ function getInstances(){
 # param  string  name of the instance to load
 function loadInstance(){
   COUCHDB_URL=
-  . ~/.iml_backup/couchdb/${1}.config
-  if [ $? -ne 0 ]; then
+  if ! . "~/.iml_backup/couchdb/${1}.config" ; then
     color error
     echo ERROR: invalid instance: $1 - the config file cannot be sourced
     color reset
     exit 1
   fi
-  if [ -z ${COUCHDB_URL} ]; then
+  if [ -z "${COUCHDB_URL}" ]; then
     color error
-    echo ERROR: invalid instance: $1 - the config file has no COUCHDB_URL
+    echo "ERROR: invalid instance: $1 - the config file has no COUCHDB_URL"
     color reset
     exit 1
   fi
 
   # parse ${COUCHDB_URL} ...
-  couchdbhost=`echo ${COUCHDB_URL} | cut -f 3 -d "/" | cut -f 2 -d "@" | cut -f 1 -d ":"`
-  couchdbport=`echo ${COUCHDB_URL} | cut -f 3 -d "/" | cut -f 2 -d "@" | cut -f 2 -d ":"`
-  couchdbuser=`echo ${COUCHDB_URL} | cut -f 3 -d "/" | cut -f 1 -d "@" | cut -f 1 -d ":"`
-  couchdbpw=`echo ${COUCHDB_URL} | cut -f 3 -d "/" | cut -f 1 -d "@" | cut -f 2 -d ":"`
+  couchdbhost=$(echo "${COUCHDB_URL}" | cut -f 3 -d "/" | cut -f 2 -d "@" | cut -f 1 -d ":")
+  couchdbport=$(echo "${COUCHDB_URL}" | cut -f 3 -d "/" | cut -f 2 -d "@" | cut -f 2 -d ":")
+  couchdbuser=$(echo "${COUCHDB_URL}" | cut -f 3 -d "/" | cut -f 1 -d "@" | cut -f 1 -d ":")
+  couchdbpw=$(  echo "${COUCHDB_URL}" | cut -f 3 -d "/" | cut -f 1 -d "@" | cut -f 2 -d ":")
 
 }
 
@@ -103,14 +102,12 @@ function loadInstance(){
 # backup with loop over instances
 function doBackup(){
   # for mycfg in `ls -1 ~/.iml_backup/couchdb/*.config`
-  for COUCHDB_INSTANCE in `getInstances`
+  for COUCHDB_INSTANCE in $(getInstances)
   do
-    loadInstance $COUCHDB_INSTANCE
+    loadInstance "$COUCHDB_INSTANCE"
 
-      echo --- instance: $COUCHDB_INSTANCE
-      curl --head -X GET $COUCHDB_URL 2>/dev/null | grep "^HTTP.*\ 200\ "
-
-      if [ $? -eq 0 ]; then
+      echo "--- instance: $COUCHDB_INSTANCE"
+      if curl --head -X GET "$COUCHDB_URL" 2>/dev/null | grep "^HTTP.*\ 200\ "; then
 
         _doBackupOfSingleInstance
 
@@ -119,12 +116,12 @@ function doBackup(){
         color error
         echo ERROR: couch DB instance is not available or canot be accessed with these credentials in config file
         # repeat curl to show the error message
-        curl -X GET $COUCHDB_URL
+        curl -X GET "$COUCHDB_URL"
         color reset
       fi
 
     echo
-    echo --- `date` done.
+    echo "--- $(date) done."
     echo
   done
 }
@@ -135,29 +132,29 @@ function doBackup(){
 function _doBackupOfSingleInstance(){
 
   create_targetdir
-  mkdir -p ${BACKUP_TARGETDIR}/${COUCHDB_INSTANCE} 2>/dev/null
+  mkdir -p "${BACKUP_TARGETDIR}/${COUCHDB_INSTANCE}" 2>/dev/null
 
   echo
   echo "    DUMP databases of instance ${COUCHDB_INSTANCE}"
   echo "    couchdbhost $couchdbhost on port $couchdbport with user $couchdbuser"
   echo
 
-  for dbname in `_getDblist`
+  for dbname in $(_getDblist)
   do
-    echo ----- `date` ${COUCHDB_INSTANCE} -- ${dbname}
-    OUTFILE=${BACKUP_TARGETDIR}/${COUCHDB_INSTANCE}/`get_outfile ${dbname}`.couchdbdump
-    python ${dirPythonPackages}/couchdb/tools/dump.py ${COUCHDB_URL}/${dbname} >${OUTFILE}
+    echo "----- $(date) ${COUCHDB_INSTANCE} -- ${dbname}"
+    OUTFILE=${BACKUP_TARGETDIR}/${COUCHDB_INSTANCE}/$(get_outfile "${dbname}").couchdbdump
+    python ${dirPythonPackages}/couchdb/tools/dump.py "${COUCHDB_URL}/${dbname}" >"${OUTFILE}"
 
     fetchrc
 
     # $myrc is last returncode - set in fetchrc
     if [ $myrc -eq 0 ]; then
       echo -n "gzip ... "
-      compress_file $OUTFILE
+      compress_file "$OUTFILE"
     else
       echo "ERROR occured - no gzip"
     fi
-    ls -l $OUTFILE*
+    ls -l "$OUTFILE"*
     echo
   done
 
@@ -173,10 +170,10 @@ function restoreByFile(){
   echo
   h2 "analyze dump $sMyfile"
 
-  COUCHDB_INSTANCE=`echo $sMyfile | sed "s#${BACKUP_TARGETDIR}##g" | sed "s#\./##g" | sed "s#^/##g" | cut -f 1 -d "/"`
+  COUCHDB_INSTANCE=$(echo "$sMyfile" | sed "s#${BACKUP_TARGETDIR}##g" | sed "s#\./##g" | sed "s#^/##g" | cut -f 1 -d "/")
   echo "detected COUCHDB_INSTANCE   : [${COUCHDB_INSTANCE}]"
-  if [ -z $sMyDb ]; then
-    sMyDb=`guessDB $sMyfile`
+  if [ -z "$sMyDb" ]; then
+    sMyDb=$(guessDB "$sMyfile")
     echo "detected db schema from file: [${sMyDb}]"
   else
     echo "db schema from param 2: [${sMyDb}]"
@@ -184,14 +181,13 @@ function restoreByFile(){
 
   echo
 
-  loadInstance $COUCHDB_INSTANCE
+  loadInstance "$COUCHDB_INSTANCE"
 
-  echo connect $couchdbhost on port $couchdbport with user $couchdbuser
-  curl --head -X GET $COUCHDB_URL 2>/dev/null | grep "^HTTP.*\ 200\ " >/dev/null
-  if [ $? -ne 0 ]; then
+  echo "connect $couchdbhost on port $couchdbport with user $couchdbuser"
+  if ! curl --head -X GET "$COUCHDB_URL" 2>/dev/null | grep "^HTTP.*\ 200\ " >/dev/null; then
     color error
     echo ERROR: couch DB instance is not available
-    curl -X GET $COUCHDB_URL
+    curl -X GET "$COUCHDB_URL"
     color reset
     exit 1
   fi
@@ -207,21 +203,21 @@ function restoreByFile(){
   #   echo DB exists ... need to drop it first
   # fi
 
-  h2 deleting database [$sMyDb] ...
+  h2 "deleting database [$sMyDb] ..."
   color cmd
-  _couchapi DELETE $sMyDb
+  _couchapi DELETE "$sMyDb"
   fetchrc
   color reset
 
-  h2 creating database [$sMyDb] ...
+  h2 "creating database [$sMyDb] ..."
   color cmd
-  _couchapi PUT $sMyDb
+  _couchapi PUT "$sMyDb"
   fetchrc
   color reset
 
   h2 import file ...
   color cmd
-  zcat ${sMyfile} | python ${dirPythonPackages}/couchdb/tools/load.py $COUCHDB_URL/$sMyDb
+  zcat "${sMyfile}" | python ${dirPythonPackages}/couchdb/tools/load.py "$COUCHDB_URL/$sMyDb"
   fetchrc
   color reset
   echo
@@ -276,6 +272,6 @@ else
 fi
 
 
-echo $0 $* [couchdb] final returncode rc=$rc
+echo "$0 $* [couchdb] final returncode rc=$rc"
 
 # --------------------------------------------------------------------------------
-- 
GitLab