From 46f9f8dd4f8638f1b92f2b38fb09fcdaed9ba7f8 Mon Sep 17 00:00:00 2001
From: Patrick Ditzel <patrick@localhost.localdomain>
Date: Sun, 17 Dec 2017 21:08:34 +0000
Subject: [PATCH] Make some changes in the mainscript und the postgresmodule. Added the fist experiments for mariadb.

---
 config/mariadb.cfg     |    8 ---
 dbb-modules/postgresql |   21 ++++++----
 dbb                    |   22 +++++-----
 dbb-modules/mariadb    |   41 ++++++++++++++++----
 4 files changed, 58 insertions(+), 34 deletions(-)

diff --git a/config/mariadb.cfg b/config/mariadb.cfg
index 8e55cd9..9ba75d3 100644
--- a/config/mariadb.cfg
+++ b/config/mariadb.cfg
@@ -1,11 +1,5 @@
 ################################################################################
-# ToDo
 
-MYSQL_BACKUP_LOCAL=FALSE
+MARIADB_BACKUP_REMOTE=YES
 
 ################################################################################
-# ToDo
-
-MYSQL_BACKUP_REMOTE=FALSE
-
-################################################################################
\ No newline at end of file
diff --git a/dbb b/dbb
index e8fc54e..0c24fe2 100755
--- a/dbb
+++ b/dbb
@@ -29,17 +29,17 @@
 	# Check if the configuration exists and is not empty
 	if [ -r $BACKUPCFG ] && [ -s $BACKUPCFG ]; then
 		# If true then read it
-		. $BACKUPCFG
+		source $BACKUPCFG
 		if [ -d $BACKUPCFG.d ]; then
-			if [ ! "$(ls -A $BACKUPCFG.d)" ]; then
+			if [ ! "$(find $BACKUPCFG.d/*.cfg | cut -d "." -f1)" ]; then
 				echo "Configurationdirectory for modules exist but it is empty" | /usr/bin/logger -s -i -t databasebackup
 			else
 				echo "Read module configuration" | /usr/bin/logger -s -i -t databasebackup
-				for MODULECFG in $(ls $BACKUPCFG.d); do . $BACKUPCFG.d/$MODULECFG; done
+				for MODULECFG in $(find $BACKUPCFG.d/*.cfg | cut -d "/" -f4 | cut -d "." -f1); do source $BACKUPCFG.d/$MODULECFG.cfg; done
 			fi
 		else
 			echo "Configurationdirectory does not exists, can't load any configurationfile" | /usr/bin/logger -s -i -t databasebackup
-		fi	
+		fi
 	else
 		# If not throw an errormessage
 		echo "The configfile does not exists or is empty" | /usr/bin/logger -s -i -t databasebackup
@@ -48,15 +48,15 @@
 	fi
 	if [ "$ENABLE_DEBUG" = "TRUE" ]; then
 		# If debugoutput is enabled show the configurationfile without comments
-		echo "####dd############################################################################" | /usr/bin/logger -s -i -t databasebackup
+		echo "##################################################################################" | /usr/bin/logger -s -i -t databasebackup
 		while read -r configline; do
 			echo "$configline" | grep -v '^$' | grep -v '^#' | /usr/bin/logger -s -i -t databasebackup
 		done <<< "$(cat $BACKUPCFG)"
-		for MODCFGLINE in $(ls $BACKUPCFG.d); do
+		for MODCFGLINE in $(find $BACKUPCFG.d/*.cfg | cut -d "." -f1); do
 			echo "Debuginfo for $MODCFGLINE" | /usr/bin/logger -s -i -t databasebackup
 			while read -r modconfigline; do
 				echo "$modconfigline" | grep -v '^$' | grep -v '^#' | /usr/bin/logger -s -i -t databasebackup
-			done <<< $(cat $BACKUPCFG.d/$MODCFGLINE)
+			done <<< "$(cat $BACKUPCFG.d/$MODCFGLINE.cfg)"
 		done
 		echo "################################################################################" | /usr/bin/logger -s -i -t databasebackup
 	fi
@@ -70,7 +70,7 @@
 		# If not create it
 		mkdir -p "$TMP_DIR"
 	fi
-	
+
 }
 
 function set_logger {
@@ -99,8 +99,8 @@
 		if [ ! "$(ls -A $BACKUPCFG.d)" ] && [ ! "$(ls -A $INSTALLATION_PATH_PREFIX/lib/dbb-modules)" ]; then
 			debug "Configurationdirectory or directory for modules exist but it is empty"
 		else
-			for MODULE in $(ls $BACKUPCFG.d | cut -d "." -f1); do 
-				. $INSTALLATION_PATH_PREFIX/lib/dbb-modules/$MODULE
+			for MODULE in $(find $BACKUPCFG.d/*.cfg | cut -d "/" -f4 | cut -d "." -f1); do
+				source $INSTALLATION_PATH_PREFIX/lib/dbb-modules/$MODULE
 				debug "Load module: $INSTALLATION_PATH_PREFIX/lib/dbb-modules/$MODULE"
 				# Check the dependenncies defined in the modulefunction -> should be defined in configfiles
 				check-$MODULE-deps
@@ -234,7 +234,7 @@
 		debug "Load dbb-modules"
 		load_dbbmodules
 		# Run modul-main-functions
-		for MODULEMAIN in $(ls $BACKUPCFG.d | cut -d "." -f1); do 
+		for MODULEMAIN in $(find $BACKUPCFG.d/*.cfg | cut -d "/" -f4 | cut -d "." -f1); do
 			debug "run $MODULEMAIN-main - function of module $MODULEMAIN"
 			$MODULEMAIN-main
 		done
diff --git a/dbb-modules/mariadb b/dbb-modules/mariadb
index f45817c..e47e7c7 100644
--- a/dbb-modules/mariadb
+++ b/dbb-modules/mariadb
@@ -3,17 +3,42 @@
 # MariaBD/MySQL remote
 #
 # The Information what MySQL/MariaDB - databases should be backuped are defined in ~/.my.cnf
+#
+# note:for MARIADB in $(ls .my.cnf-* | cut -d "-" -f2); do mysqldump --defaults-file="~/.my.cnf-$MARIADB" --skip-dump-date $MARIADB > $TMP_DIR/$(cat ~/.my.cnf-$DATABASE | grep host | cut -d "=" -f2); done
+# note: for each MySQL- or MariaDB-Database create a seperate configfile .my.cnf-dbname ind the ~ of the backupuser:
+# note: replase the suffixe -dbname with the name of the database. The seperating "-" can also be a ".".
+#	[mysqldump]
+# host=HOSTNAME
+# user=DBUSERNAME
+# password=PASSWORD
+# database=DATABASENAME
 
-function run_mysql_backups {
-	debug "Dump remote database $MYSQLDB from $MYSQLDBHOST"
-	/usr/bin/mysqldump --skip-dump-date -h "$MYSQLDBHOST" -u "$MYSQLDBUSER" -p"$MYSQLPASSWD" "$MYSQLDB" > "$TMP_DIR"/"$MYSQLDBHOST"_"$MYSQLDB".mysql
-	debug "Diff MySQLDump $MYSQLDB"
-	diff "$TMP_DIR"/"$MYSQLDBHOST"_"$MYSQLDB".mysql "$BACKUP_DIR"/"$MYSQLDBHOST"_"$MYSQLDB".mysql > /dev/null 2>&1
+function check-mariadb-deps {
+	if [ ! -e /usr/bin/mysqldump ]; then
+		debug "It seems that you dont have mysqldump installed. You may have problems to backup remote databases"
+	fi
+# TODO: Check for ~/.my.cnf-*
+}
+
+function mariadb_remote {
+	MARIADBHOSTNAME=$(cat ~/.my.cnf-$DATABASE | grep host | cut -d "=" -f2)
+	for MARIADB in $(ls .my.cnf-* | cut -d "-" -f2)
+		do
+		/usr/bin/mysqldump --defaults-file="~/.my.cnf-$MARIADB" --skip-dump-date $MARIADB > $TMP_DIR/$MARIADBHOSTNAME_$MARIADB.mysql
+		debug "Diff MySQLDump $MYSQLDB"
+		diff "$TMP_DIR"/"$MARIADBHOSTNAME"_"$MARIADB".mysql "$BACKUP_DIR"/"$MARIADBHOSTNAME"_"$MARIADB".mysql > /dev/null 2>&1
 	if [ $? -ne 0 ]; then
 		debug "Differences found -> moving to BACKUP_DIR"
-	        mv "$TMP_DIR"/"$MYSQLDBHOST"_"$MYSQLDB".mysql "$BACKUP_DIR"/"$MYSQLDBHOST"_"$MYSQLDB".mysql
+	        mv "$TMP_DIR"/"$MARIADBHOSTNAME"_"$MARIADB".mysql "$BACKUP_DIR"/"$MARIADBHOSTNAME"_"$MARIADB".mysql
 	else
-	        debug "No differences found an Database $MYSQLDB"
+	        debug "No differences found in Database $MARIADB"
 	fi
 	echo ""
-}
\ No newline at end of file
+	done
+}
+
+function mariadb-main {
+	if [ "$MARIADB_BACKUP_REMOTE" = "TRUE" ]; then
+		mariadb_remote
+	fi
+}
diff --git a/dbb-modules/postgresql b/dbb-modules/postgresql
index 5202d87..f50c10a 100644
--- a/dbb-modules/postgresql
+++ b/dbb-modules/postgresql
@@ -3,12 +3,18 @@
 # dependencies
 
 function check-postgresql-deps {
-	if [ ! -e /usr/bin/psql ]; then
-		debug "It seems that you dont have psql installed. You may have problems to backup remote databases" 
+	if [ ! -e /usr/bin/pg_dump ]; then
+		debug "It seems that you dont have psql installed. You may have problems to backup remote databases"
 	fi
 	numberRegex='^[0-9]+$'
 	if ! [[ $(id -u postgres 2>&1) =~ $numberRegex ]] ; then
-		debug "It seems that you dont have the user postgres on your computer. You may have problems to backup local postgres databases" 
+		debug "It seems that you dont have the user postgres on your computer. You may have problems to backup local postgres databases"
+	fi
+	if [ -r ~/.pgpass ]; then
+		debug "function check-postgresql-deps: ~/.pgpass exists, doing reomte backup."
+	else
+		# If the ~/.pgpass-file is missig, deactivate remote backup of postgresql.
+		POSTGRES_BACKUP_REMOTE=FLASE
 	fi
 }
 
@@ -19,7 +25,7 @@
 	debug "Dump all from Postgres local"
 	# Dump all databases in one file as user postgres (Debian)
 	sudo -H -i -u postgres pg_dumpall > "$TMP_DIR"/local_dumpall.pgsql
-	debug "Diff alldumps from Postgres local" 
+	debug "Diff alldumps from Postgres local"
 	# Check if there are differences between the actual dump and the last dump
 	backup_file_handler "$TMP_DIR $BACKUP_DIR" local_dumpall.pgsql
 }
@@ -57,7 +63,7 @@
 		echo "" | $LOGGER
 		echo "The backupuser does not the permission to act as user postgres" | $LOGGER
 		echo "Please add the following line to your /etc/sudoers:" | $LOGGER
-		echo "backupuser ALL=(postgres) NOPASSWD: /usr/bin/psql,/usr/bin/pg_dump" | $LOGGER
+		echo "backupuser ALL=(postgres) NOPASSWD:/usr/bin/psql,/usr/bin/pg_dump,/usr/bin/pg_dumpall" | $LOGGER
 		echo "" | $LOGGER
 	fi
 }
@@ -108,11 +114,11 @@
 		# If yes the check for the ~/.pgpass-file. Here are the remotedatabases specified
 		if [ -r ~/.pgpass ]; then
 			# parallelize the following
-			while read -r LINE; do 
+			while read -r LINE; do
 				# For each entry do the backup
 				debug "run dump with params $LINE"
 				# CAUTION: No doublequotes in the following line. The var $LINE has to be splittet!
-                                dump_remote_pgdb $LINE
+	        dump_remote_pgdb $LINE
 			# To get the params for the function the .pgpass-file is striped from the comments,
 			# the ":" are replaces against whitespaces and only the first four coloums are used
 			# so we give "host port database user" to the function
@@ -136,4 +142,3 @@
 		run_remote_pg_backups
 	fi
 }
-

--
Gitblit v1.10.0