#!/bin/bash
# 
# Name: multimysqlbackup
# Summary: wrapper for automysqlbackup to backup multiple hosts
# Version: 0.3
# Author: Carsten Schoene <cs@linux-administrator.com>
# Last change: 23-08-2021
#
###############################################################################

# path to host,db definitions
CONFPATH="/etc/multimysqlbackup.conf"

if [ $# -gt 0 ]; then
	# include custom config
	if [ -f "$1" ] ; then
		CONFPATH="$1"
	fi
fi

# path to mysql backup script
AUTOMYSQLBACKUP="$(which automysqlbackup 2>/dev/null)"

# lock each job
LOCKJOB=1

# enable plesk detection
AUTOPLESKBACKUP=1

# disable output on skipped configs
SILENTERROR=1
###############################################################################
if [ -s "${CONFPATH}" ] ; then

	export MAILADDR

	for LINE in `cat ${CONFPATH} | grep -v ^# | grep -v ^$ | sed -e s@" "@"%20%"@g` ; do

		#format: host[:port];user;password;databases_separated_by_spaces;exclude_databases;exclude_tables;compression;quiet_rotation{no|yes}

		export DBHOST=`echo ${LINE} | awk -F\; '{print $1}' | awk -F: '{print $1}'`
		export DBPORT=`echo ${LINE} | awk -F\; '{print $1}' | awk -F: '{print $2}'`
		if [ -z "${DBPORT}" ] ; then
			DBPORT="3306"
		fi 
		export USERNAME=`echo ${LINE} | awk -F\; '{print $2}'`
		export PASSWORD=`echo ${LINE} | awk -F\; '{print $3}'`
		export DBNAMES=`echo ${LINE} | awk -F\; '{print $4}'|sed -e s@"%20%"@" "@g`
		export DBEXCLUDE=`echo ${LINE} | awk -F\; '{print $5}'|sed -e s@"%20%"@" "@g`
		export TABLEEXCLUDE=`echo ${LINE} | awk -F\; '{print $6}'|sed -e s@"%20%"@" "@g`
		export COMP=`echo ${LINE} | awk -F\; '{print $7}'|sed -e s@"%20%"@" "@g`
		export QUIETROTA=`echo ${LINE} | awk -F\; '{print $8}'|sed -e s@"%20%"@" "@g`

		if [ ! -f /var/lock/${DBHOST}.lock ] && [ "${LOCKJOB}" == "1" ] ; then

			touch /var/lock/${DBHOST}.lock
			sh -c "${AUTOMYSQLBACKUP}"
			rm -f /var/lock/${DBHOST}.lock

		elif [ "${LOCKJOB}" == "0" ] ; then
			sh -c "${AUTOMYSQLBACKUP}"
		else
			echo "LOCKJOB is enabled and /var/lock/${DBHOST}.lock file  was found, please check! Script skips backup for host: ${DBHOST}"
			exit 2
		fi
	done
else
	if [ "${SILENTERROR}" == "0" ] ; then
		echo "Config file not found: ${CONFPATH} (skipping)"
	fi
fi

if [ "${AUTOPLESKBACKUP}" == "1" ] ; then
	# plesk autodetection
	if [ -s /usr/local/psa/version ] && [ -s /etc/psa/.psa.shadow ] ; then

		export DBHOST="localhost"
		export DBPORT="3306"
		export USERNAME="admin"
		export PASSWORD=`cat /etc/psa/.psa.shadow`
		export DBNAMES="all"
		export DBEXCLUDE=""
		export TABLEEXCLUDE=""
		export COMP="bzip2"
		export QUIETROTA="no"
		if [ ! -f /var/lock/${DBHOST}.lock ] && [ "${LOCKJOB}" == "1" ] ; then

			touch /var/lock/${DBHOST}.lock
			sh -c "${AUTOMYSQLBACKUP}"
			rm -f /var/lock/${DBHOST}.lock

		elif [ "${LOCKJOB}" == "0" ] ; then
			sh -c "${AUTOMYSQLBACKUP}"
		else
			echo "LOCKJOB is enabled and /var/lock/${DBHOST}.lock file  was found, please check! Script skips backup for host: ${DBHOST}"
			exit 3
		fi
	else
		if [ "${SILENTERROR}" == "0" ] ; then
			echo "Plesk configuration not found: /usr/local/psa/version & /etc/psa/.psa.shadow (skipping)"
		fi
	fi
fi
