#!/bin/bash
 
#####
# Set these values!
####
 
 
# space seperated list of domain names (will be used as part of the output path)
domains=( naturesgifts.co.nz )
#list of corresponding DB names
sqldbs=( wealthhe_ngdb )
 
#list of IDs and passwords
usernames=( wealthhe_ngu )
passwords=( "x?Jf5dJKD;jW" )
 
#Directory to save generated sql files (domain name is appended)
opath=$HOME/backups
 
# your mysql host
mysqlhost=localhost
 
 
 
#####
# End of config values
#####
 
 
#date to append
suffix=$(date +%Y-%m-%d)
 
 
#run on each domain
for (( i = 0 ; i < ${#domains[@]} ; i++ ))
do
	#set current output path
	cpath=$opath${domains[$i]}
 
	#check if we need to make path
	if [ -d $cpath ]
	then
		# direcotry exists, we're good to continue
		filler="just some action to prevent syntax error"
	else
		#we need to make the directory
		echo Creating $cpath
		mkdir -p $cpath
	fi
 
	#now do the backup
	SQLFILE=${cpath}/${sqldbs[$i]}_$suffix.sql.gz
 
	mysqldump --opt --lock-tables=false  -c -h $mysqlhost --user ${usernames[$i]} --password=${passwords[$i]} ${sqldbs[$i]} 2>error | gzip > $SQLFILE
 
	if [ -s error ]
	then	   
		printf "WARNING: An error occured while attempting to backup %s  \n\tError:\n\t" ${sqldbs[$i]} 
		cat error
		rm -f er
	else
		printf "%s was backed up successfully to %s\n\n" ${sqldbs[$i]} $SQLFILE
	fi
done