#!/bin/bash
# 
# Name: multimysqlbackup
# Summary: wrapper for automysqlbackup to backup multiple hosts
# Version: 0.1
# Author: Carsten Schoene <cs@linux-administrator.com>
# Last change: 23-05-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

	# set new IFS
	IFS="\t\n"

	for LINE in `cat ${CONFPATH} | grep -v ^#` ; do

		# format: host;user;password;databases_separated_by_spaces	

		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}'`

		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

