From dedc6dc085ccabf597d2f2bf5236ae25ebda96b1 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch>
Date: Thu, 20 Jan 2022 15:22:58 +0100
Subject: [PATCH] mysql fixes with shellcheck

---
 plugins/localdump/mysql.sh | 37 +++++++++++++++++++------------------
 1 file changed, 19 insertions(+), 18 deletions(-)

diff --git a/plugins/localdump/mysql.sh b/plugins/localdump/mysql.sh
index 87153e9..7215c7f 100755
--- a/plugins/localdump/mysql.sh
+++ b/plugins/localdump/mysql.sh
@@ -10,10 +10,11 @@
 #
 # 2016-11-10  ah,ds  v0.8  needs to be testet
 # 2017-03-28  .....  v1.0  added 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
@@ -33,33 +34,33 @@ function doMysqlBackup(){
 
   create_targetdir
 
-  for DATABASE_DIR in `find $SOURCE_DIR/* -type d -prune`;
+  for DATABASE_DIR in $(find $SOURCE_DIR/* -type d -prune);
   do
     # DATABASE=`basename $DATABASE_DIR`
-    DATABASE=`basename $DATABASE_DIR | sed "s#\@002d#-#g" `
-    TABLECOUNT=`find $DATABASE_DIR/. -type f -name *frm | wc -l`
-    echo --- database $DATABASE - $TABLECOUNT tables
+    DATABASE=$(basename "$DATABASE_DIR" | sed "s#\@002d#-#g" )
+    TABLECOUNT=$(find "$DATABASE_DIR"/. -type f -name "*frm" | wc -l)
+    echo "--- database $DATABASE - $TABLECOUNT tables"
     if [ $TABLECOUNT -gt 0 ]; then
       echo -n "backup ... "
-      OUTFILE=${BACKUP_TARGETDIR}/`get_outfile ${DATABASE}`.sql
+      OUTFILE="${BACKUP_TARGETDIR}/$(get_outfile ${DATABASE}).sql"
       # task 1251 - "--master-data=2" was removed
       $MYSQLDUMP --opt \
                --default-character-set=utf8 \
                --flush-logs \
                --single-transaction \
                --no-autocommit \
-               --result-file=$OUTFILE \
-               $DATABASE
+               --result-file="$OUTFILE" \
+               "$DATABASE"
       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"*
     fi
     echo
   done
@@ -73,9 +74,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}]"
@@ -83,7 +84,7 @@ function restoreByFile(){
 
   echo
 
-  echo import to $sMyDb...
+  echo import to "$sMyDb"...
 
   h2 ensure that database exists ...
   color cmd
@@ -91,10 +92,10 @@ function restoreByFile(){
   color reset
 
   h2 import ...
-  ls -l $sMyfile
-  echo import to database [${sMyDb}]
+  ls -l "$sMyfile"
+  echo "import to database [${sMyDb}]"
   color cmd
-  zcat $sMyfile | mysql "${sMyDb}"
+  zcat "$sMyfile" | mysql "${sMyDb}"
   fetchrc
   color reset
 
@@ -130,6 +131,6 @@ else
   fi
 fi
 
-echo $0 $* [mysql] final returncode rc=$rc
+echo "$0 $* [mysql] final returncode rc=$rc"
 
 # --------------------------------------------------------------------------------
-- 
GitLab