From c0542b359809faec330650a34c80dde8cba34806 Mon Sep 17 00:00:00 2001
From: Timm Fitschen <timm.fitschen@ds.mpg.de>
Date: Mon, 19 Nov 2018 12:54:32 +0100
Subject: [PATCH] FIX backup script

---
 backup.sh                     | 13 +++++++------
 patches/utils/patch_header.sh | 32 ++++++++++++++++----------------
 2 files changed, 23 insertions(+), 22 deletions(-)

diff --git a/backup.sh b/backup.sh
index 05a7518..eb59ce7 100755
--- a/backup.sh
+++ b/backup.sh
@@ -25,8 +25,9 @@
 #dump a database with all procedures, permissions, structure and data
 
 BACKUPDIR=./backup #The directory which the dump is to be stored to. Do not change it here. Use the --backupdir=./my/dir/ option!
-PRINT_HELP="--backupdir=BACKUPDIR\n\tThe directory which the dump is to be stored to. (Defaults to ./backup)"
+PRINT_HELP="--backupdir=BACKUPDIR\n\tThe directory which the dump is to be stored to. (Defaults to ./backup)\n"
 
+. .config
 # load useful stuff - scans the parameters and so on...
 . patches/utils/patch_header.sh
 
@@ -36,12 +37,12 @@ PRINT_HELP="--backupdir=BACKUPDIR\n\tThe directory which the dump is to be store
 
 
 function backup {
-	# parameters: login-path, database, outfile
+	# parameters: connection, database, outfile
 	if [ -e "$3" ]; then
 		failure "dumpfile already exists."
 	fi
-	echo -n "Dumping database $3 to $4 ... "
-	$CMD_MYSQL_DUMP --opt --default-character-set=utf8 --routines --login-path=$1 $2 > ${3}
+    echo "Dumping database $2 to $3 ... "
+	$CMD_MYSQL_DUMP $1 --opt --default-character-set=utf8 --routines $2 > ${3}
 	success
 }
 
@@ -56,7 +57,7 @@ function backup {
 
 
 DATE=$(date -u --rfc-3339=ns | sed 's/ /T/g')
-BACKUPFILE=${BACKUPDIR}/${DATABASE}.${DATE}.dump.sql
-backup $LOGIN_PATH $DATABASE $BACKUPFILE
+BACKUPFILE=${BACKUPDIR}/${DATABASE_NAME}.${DATE}.dump.sql
+backup $MYSQL_CONNECTION $DATABASE_NAME $BACKUPFILE
 
 
diff --git a/patches/utils/patch_header.sh b/patches/utils/patch_header.sh
index 707387d..b51e971 100644
--- a/patches/utils/patch_header.sh
+++ b/patches/utils/patch_header.sh
@@ -28,13 +28,13 @@ CMD_OPTIONS="options:\n\n-h, --help\n\tShow brief help.\n-l LOGIN_PATH, --login-
 set -e
 
 function _print_help() {
-  printf $USAGE
-  printf $CMD_OPTIONS
+  printf "$USAGE"
+  printf "$CMD_OPTIONS"
   if [ -n "$PRINT_HELP" ]; then
-    printf $PRINT_HELP
+    printf -- "$PRINT_HELP"
   fi
   if [ -n "$1" ]; then
-    printf $1
+    printf "$1"
   fi
 }
 while test $# -gt 0; do
@@ -60,7 +60,7 @@ while test $# -gt 0; do
  -u)
   shift
   if test $# -gt 0; then
-   DBUSER=$1
+   DATABASE_USER=$1
   else
    echo "no database specified"
    exit 1
@@ -68,7 +68,7 @@ while test $# -gt 0; do
   shift
   ;;
  --user*)
-  DBUSER=`echo $1 | sed -e 's/^[^=]*=//g'`
+  DATABASE_USER=`echo $1 | sed -e 's/^[^=]*=//g'`
   shift
   ;;
  -l)
@@ -88,7 +88,7 @@ while test $# -gt 0; do
  -d)
   shift
   if test $# -gt 0; then
-   DATABASE=$1
+   DATABASE_NAME=$1
   else
    echo "no database specified"
    exit 1
@@ -96,7 +96,7 @@ while test $# -gt 0; do
   shift
   ;;
  --database*)
-  DATABASE=`echo $1 | sed -e 's/^[^=]*=//g'`
+  DATABASE_NAME=`echo $1 | sed -e 's/^[^=]*=//g'`
   shift
   ;;
  --patch*)
@@ -113,9 +113,9 @@ while test $# -gt 0; do
  esac
 done
 
-if [ -z "$DATABASE" ]
+if [ -z "$DATABASE_NAME" ]
 then
- print_help "Please specify the database."
+ _print_help "Please specify the database."
  exit 1
 fi
 
@@ -124,7 +124,7 @@ then
   MYSQL_CONNECTION="--login-path=$LOGIN_PATH"
 else
   MYSQL_CONNECTION=""
-  if [ "$DBUSER" ]
+  if [ "$DATABASE_USER" ]
   then
     MYSQL_CONNECTION="--user=$DBUSER"
   fi
@@ -135,7 +135,7 @@ else
 fi
 
 if [ -n "$PATCH" ]; then
-    echo -ne "applying patch $PATCH to $DATABASE ... "
+    echo -ne "applying patch $PATCH to $DATABASE_NAME ... "
 fi
 
 function success {
@@ -155,7 +155,7 @@ function uptodate {
 # @param $1: db version string, e.g. v2.0.0
 # @return: 0 on success, 1 on failure
 function check_version {
-    local version=$($CMD_MYSQL $MYSQL_CONNECTION -D $DATABASE -B -e "Select CaosDBVersion();")
+    local version=$($CMD_MYSQL $MYSQL_CONNECTION -D $DATABASE_NAME -B -e "Select CaosDBVersion();")
 	if [[ "$(echo $version | sed 's/^CaosDBVersion()\s//')" = "$1" ]]; then
         return 0
 	fi
@@ -168,13 +168,13 @@ function update_version {
 }
 
 function dump_table {
-    $CMD_MYSQL_DUMP $MYSQL_CONNECTION $DATABASE $1 > ${DATABASE}.${1}.${OLD_VERSION}.dump.sql
+    $CMD_MYSQL_DUMP $MYSQL_CONNECTION $DATABASE_NAME $1 > ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql
 }
 
 
 function mysql_execute {
 	set +e
-	$CMD_MYSQL $MYSQL_CONNECTION -D $DATABASE -e "$1"
+	$CMD_MYSQL $MYSQL_CONNECTION -D $DATABASE_NAME -e "$1"
 	ret=${PIPESTATUS[0]}
 	if [ "$ret" -ne 0 ]; then
 		failure "MYSQL ERROR"
@@ -183,5 +183,5 @@ function mysql_execute {
 }
 
 function redo_table {
-	$CMD_MYSQL $MYSQL_CONNECTION -D $DATABASE < ${DATABASE}.${1}.${OLD_VERSION}.dump.sql
+	$CMD_MYSQL $MYSQL_CONNECTION -D $DATABASE_NAME < ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql
 }
-- 
GitLab