#!/bin/bash
# 
# Name: multimysqlbackup
# Summary: wrapper for automysqlbackup to backup multiple hosts
# Version: 0.2
# Author: Carsten Schoene <cs@linux-administrator.com>
# Last change: 26-06-2009
#
###############################################################################

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

# path to mysql backup script
AUTOMYSQLBACKUP="/usr/sbin/automysqlbackup"

# lock each job
LOCKJOB=1
###############################################################################
if [ -s "${CONFPATH}" ] ; then

	export MAILADDR

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

		# format: host;user;password;databases_separated_by_spaces;databases_to_exclude_separated_by_spaces;tables_to_exclude

		export DBHOST=`echo ${LINE} | awk -F\; '{print $1}'`
		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`

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

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

		elif [ "${LOCKJOB}" == "0" ] ; then
			${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

	echo "Config file not found: ${CONFPATH}"
	exit 1
fi

