Posted by
Mohammed in
Shell on
November 5, 2009 |
no responses
Here is a bash script to backup all mysql databases, including mysqlcheck.
The script has the following advantages:
* It will list all databases and will dump them separately
* all backups will be stored under /root/mysql_backups/
* backups will run on a daily rotational basis over 7days
* old backups will be removed only after completing new backup processes.
* you can enable or disable mysqlcheck
* separate log files for mysqlcheck and dump processes.
* option to enable or disable email alerts on success and failure
You can download the script from here.
#!/bin/bash
###########################################################
#
# A bash script to backup all mysql databases
#
# The script has the following advantages:
# * It will list all databases and will dump them separately
# * all backups will be stored under /root/mysql_backups/
# * backups will run on a daily rotational basis over 7days
# * old backups will be removed only after completing new backup processes.
# * you can enable or disable mysqlcheck
# * separate log files for mysqlcheck and dump processes.
# * option to enable or disable email alerts on success and failure
#
# Written By: Mohammed Abdurahiman <http://mohammedz.com>
# Date: Nov 5, 2009
#
###########################################################
### Variables ###
BKPROOT="/root/mysql_backups"
LOGPATH="$BKPROOT/logs"
LOGFILE="$LOGPATH/`basename $0`.log"
MYSQLCHECK_LOG="$LOGPATH/mysqlcheck.log"
DAY=`date +%a`
DATE=`date`
ALERTS_ON_SUCCESS="yes"
ALERTS_ON_FAILURE="yes"
MAILTO="root@mohammedz.com"
ENABLE_MYSQLCHECK="yes"
EMAIL_MYSQLCHECKLOGS="yes"
### Commands and Path to Binaries ###
MYSQL="/usr/bin/mysql"
DUMP="/usr/bin/mysqldump"
MAIL="/bin/mail"
if [[ ! -d $BKPROOT ]]; then
mkdir $BKPROOT
mkdir $LOGPATH
else
if [[ ! -d $LOGPATH ]]; then
mkdir $LOGPATH
fi
fi
function mail_failure() {
if [[ $ALERTS_ON_FAILURE == yes ]]; then
sed -n "/$DATE/,\$p" $LOGFILE |$MAIL -s"Error: MySQL Backup failed on $DATE" $MAILTO
fi
if [[ $EMAIL_MYSQLCHECKLOGS == yes ]]; then
cat $MYSQLCHECK_LOG |$MAIL -s"mysqlcheck on $DATE" $MAILTO
fi
}
function mail_success() {
if [[ $ALERTS_ON_SUCCESS == yes ]]; then
sed -n "/$DATE/,\$p" $LOGFILE |$MAIL -s"MySQL Backup completed successfully on $DATE" $MAILTO
fi
if [[ $EMAIL_MYSQLCHECKLOGS == yes ]]; then
cat $MYSQLCHECK_LOG |$MAIL -s"mysqlcheck on $DATE" $MAILTO
fi
}
echo "" > $MYSQLCHECK_LOG
echo "" >> $LOGFILE
echo "Starting backup service on `hostname` at $DATE" >> $LOGFILE
echo '################################################' >> $LOGFILE
DATABASES=(`mysql -uadmin -p'password' -e "show databases" |egrep -v "Database$"`)
if [[ -n $DATABASES ]]; then
if [[ -d $BKPROOT/$DAY ]]; then
# move last week's backup temporarily for deletion
tar -czf $BKPROOT/${DAY}.tgz $BKPROOT/$DAY &> /dev/null
rm -f $BKPROOT/$DAY/*.sql &> /dev/null
else
mkdir $BKPROOT/$DAY &> /dev/null
fi
for db in ${DATABASES[*]}; do
if [[ $ENABLE_MYSQLCHECK == yes ]]; then
mysqlcheck -ao -auto-repair -uadmin -p'password' $db >> $MYSQLCHECK_LOG 2>> $MYSQLCHECK_LOG
fi
#echo "Dumping database $db" >> $LOGFILE
$DUMP -uadmin -p'password' $db > $BKPROOT/$DAY/$db.sql
if [[ $? == 0 ]]; then
echo "$db: Dump completed" >> $LOGFILE
else
echo "$db: Dump failed" >> $LOGFILE
mail_failure
exit 1
fi
done
# remove temporary backup
rm -f $BKPROOT/${DAY}.tgz &> /dev/null
mail_success
else
echo "Error: Database list looks empty" >> $LOGFILE
echo "Please troubleshoot the issue" >> $LOGFILE
mail_failure
fi
Regards,
Mohammed
Leave a Reply