From aaf1ffa8104867ab16fb63512e8a1266ea8468d5 Mon Sep 17 00:00:00 2001
From: root <root@sdvdbodsql01.slub-dresden.de>
Date: Tue, 9 Aug 2022 09:11:10 +0200
Subject: [PATCH] Implemented config loading logic. Generate config from
 template if missing. Minor fixes and changes

---
 config.json            | 11 +++++++++
 config_template.json   | 11 +++++++++
 sql-backup-sample.conf |  9 -------
 sql-backup.conf        |  9 -------
 sql-backup.py          | 52 ----------------------------------------
 sql-backup.sh          | 54 ++++++++++++++++++++++++++----------------
 6 files changed, 55 insertions(+), 91 deletions(-)
 delete mode 100644 sql-backup-sample.conf
 delete mode 100644 sql-backup.conf
 delete mode 100755 sql-backup.py

diff --git a/config.json b/config.json
index e69de29..825cf44 100644
--- a/config.json
+++ b/config.json
@@ -0,0 +1,11 @@
+[
+  {
+    "sqlHost": "localhost",
+    "sqlPort": "3306",
+    "sqlUser": "root",
+    "sqlPass": null,
+    "dbNames": "all",
+    "backupRoot": "/var/backups/sql1/",
+    "backupRetainDays": 30
+  }
+]
diff --git a/config_template.json b/config_template.json
index e69de29..348bf91 100644
--- a/config_template.json
+++ b/config_template.json
@@ -0,0 +1,11 @@
+[
+  {
+    "sqlHost": "localhost",
+    "sqlPort": "3306",
+    "sqlUser": "root",
+    "sqlPass": null,
+    "dbNames": "all",
+    "backupRoot": "/var/backups/sql/",
+    "backupRetainDays": 30
+  }
+]
diff --git a/sql-backup-sample.conf b/sql-backup-sample.conf
deleted file mode 100644
index 5cc4c12..0000000
--- a/sql-backup-sample.conf
+++ /dev/null
@@ -1,9 +0,0 @@
-{
-	"sqlHost": "localhost",
-	"sqlPort": "3306",
-	"sqlUser": "root",
-	"sqlPass": null,
-	"dbNames": "all",
-	"backupRoot": "/var/backups/sql/",
-	"backupRetainDays": 30
-}
diff --git a/sql-backup.conf b/sql-backup.conf
deleted file mode 100644
index edd8729..0000000
--- a/sql-backup.conf
+++ /dev/null
@@ -1,9 +0,0 @@
-{
-	"sqlHost": "localhost",
-	"sqlPort": "3306",
-	"sqlUser": "backup",
-	"sqlPass": "l)m0h4vtJ@D7*f5YOvLO5c=0g?4K3aEM",
-	"dbNames": "all",
-	"backupRoot": "/var/backups/sql/",
-	"backupRetainDays": 30
-}
diff --git a/sql-backup.py b/sql-backup.py
deleted file mode 100755
index 07de054..0000000
--- a/sql-backup.py
+++ /dev/null
@@ -1,52 +0,0 @@
-import os
-import json
-import subprocess
-import mysql.connector
-
-#########################
-###                   ###
-###     SETTINGS      ###
-###                   ###
-#########################
-
-try:
-	configFile = open('/opt/sql-backup/sql-backup.conf')
-	print('INFO: File exists, load it --> To be implemented')
-except:
-	print('WARN: No configuration file found. Attempting to backup all existing Databases as root.')
-	configFile = open('/opt/sql-backup/sql-backup-sample.conf')
-
-config = json.load(configFile)
-configFile.close()
-
-sqlHost = config['sqlHost']
-sqlPort = config['sqlPort']
-sqlUser = config['sqlUser']
-sqlPass = config['sqlPass']
-dbNames = config['dbNames']
-backupRoot = config['backupRoot']
-backupRetainDays = config['backupRetainDays']
-
-#########################
-###                   ###
-###  START OF SCRIPT  ###
-###                   ###
-#########################
-
-isDir = os.path.isdir(backupRoot)
-if isDir == False:
-	print('WARN: Backup root directory does not exist. Try to create it.')
-	try:
-		os.mkdir(backupRoot)
-	except:
-		print('ERROR: Could not create backup root directory. Stopping backup process')
-		quit()
-
-connection = mysql.connector.connect(host = sqlHost, port = sqlPort,
-	user = sqlUser, password = sqlPass)
-cursor = connection.cursor()
-cursor.execute('SHOW DATABASES;')
-dbNames = []
-for record in cursor.fetchall():
-	dbNames.append(record[0])
-print(dbNames)
diff --git a/sql-backup.sh b/sql-backup.sh
index 2250c84..6f06180 100755
--- a/sql-backup.sh
+++ b/sql-backup.sh
@@ -8,20 +8,36 @@ set -o nounset                              # Treat unset variables as an error
 #########################
 
 export PATH=/bin:/usr/bin:/usr/local/bin
+configFile=/opt/sql-backup/config.json
+configTemplate=/opt/sql-backup/config_template.json
 
-configFile=/etc/sql-backup/sql-backup.conf
-if test -f "configFile"; then
-  echo "INFO: Config file exists, load it --> To be implemented"
-else
-  echo "WARN: No configuration file found. Attempting to backup all existing Databases as root."
-  sqlHost="localhost"
-  sqlPort="3306"
-  sqlUser="root"
-  sqlPass=""
-  dbNames=($(mysql -u root -e "SHOW DATABASES;"))
-  backupRoot="/var/backups/sql/"
-  backupRetainDays=30
+if [[ ! -f $configFile ]]; then
+  echo 'WARN: No configuration file found. Generate file from template.'
+  if [[ ! -f $configTemplate ]]; then
+    echo 'ERROR: No configuration template found. Installation might be corrupted. Please run update.sh or' \
+         'reclone repository.'
+    exit 1
+  else
+    cp $configTemplate $configFile
+  fi
 fi
+
+echo 'INFO: Load config file.'
+sqlHost=$(cat $configFile | jq -r '.[0].sqlHost')
+sqlPort=$(cat $configFile | jq -r '.[0].sqlPort')
+sqlUser=$(cat $configFile | jq -r '.[0].sqlUser')
+sqlPass=$(cat $configFile | jq -r '.[0].sqlPass')
+dbNames=$(cat $configFile | jq -r '.[0].dbNames')
+if [ $dbNames == 'all' ]; then
+  if [ -z $sqlPass ]; then
+    dbNames=($(mysql -h$sqlHost -P$sqlPort -u$sqlUser -e "SHOW DATABASES;" -s --skip-column-names))
+  else
+    dbNames=($(mysql -h$sqlHost -P$sqlPort -u$sqlUser -p$sqlPass -e "SHOW DATABASES;" -s --skip-column-names))
+  fi
+fi
+backupRoot=$(cat $configFile | jq -r '.[0].backupRoot')
+backupRetainDays=$(cat $configFile | jq -r '.[0].backupRetainDays')
+
 today=$(date +"%y%m%d")
 
 #########################
@@ -44,22 +60,18 @@ else
   }
 fi
 
-
 for dbName in "${dbNames[@]}";
 do
 
-  if [ $dbName == "Database" ] || \
-     [ $dbName == "information_schema" ] || \
-     [ $dbName == "mysql" ] || \
-     [ $dbName == "performance_schema" ] || \
-     [ $dbName == "phpmyadmin" ];
-  then
+  if [ $dbName == 'information_schema' ] || \
+     [ $dbName == 'performance_schema' ] || \
+     [ $dbName == 'mysql' ] || \
+     [ $dbName == 'phpmyadmin' ]; then
     continue
   fi
 
   backupPath=$backupRoot$dbName
   mkdir -p ${backupPath}
-  cd $backupPath
 
   if [ -z $sqlPass ];
   then
@@ -81,7 +93,7 @@ do
     delDate=$(date +"%y%m%d" --date="${backupRetainDays} days ago")
     delFile=${dbName}-${delDate}.sql
 
-    for f in *; do
+    for f in $backupPath/*; do
       if [[ "$f" < "$delFile" ]]; then
         echo "delete $f"
         rm -f $f
-- 
GitLab