#!/bin/sh
##############################################
# File: dbbackup.sh
# Description: A shell script to backup Multiple MySQL database
# Features: The script can create a MySQL dump, compress it
# then both store a copy in the server and send a
# copy to the defined email address
# Supported OS: FreeBSD and RedHat, should work with other distributions
#
# Version: 1.0
# License: GPL
#
#How to install:
#
# 1. Upload dbbackup.sh to your account. i.e. in your home directory
# 2. Set the permission for the script to 755
#
#Cron:
#
# Example: This will cause the cron to run every day at 2AM. Be sure
# to change "/path/to" to the location where you placed dbbackup.sh.
#
# 0 2 * * * sh /path/to/dbbackup.sh
#
#
# BY KIONIC INC (www.kionic.com)
# Questions, comments, suggestions, or improvements please send to
# stephen@kionic.com
#############################################
####DEFINE THESE#############################
#Enter the cpanel username here
USER="control panel username";
#Enter the cpanel password here
PASSWORD="control panel password";
#Enter the database names here
DATABASES="dbname1 dbname2";
#Zipping options. 1 means you will get mail with a bz2 copy else you will get a gz copy bz2 is more powerful.
BZIP="0";
#Enter the full path to where you would like to store the backup here
#i.e. make a folder in your home directory, example: dbbackup
#then the path is /home/username/dbbackup
PATH="/path/to";
#Enter the email address you would like to receive the backup
EMAIL="youremail@here.com";
####END CHANGE HERE##############################
#The main function which will create the required backup and send the mail
backup ()
{
DATE=`/bin/date "+%d-%m-%Y"`;
DUMP="$DATABASE-$DATE";
EXT1=".sql";
EXT2=".tar";
EXT3=".gz";
EXT4=".bz2";
#Locate the mysqldump and mail commands
if [ -f /etc/redhat-release ]
then
MAIL="/bin/mail";
else
MAIL="/usr/bin/mail";
fi
if [ -e "/usr/bin/mysql" ]; then
MYSQLDUMP="/usr/bin/mysqldump";
else
MYSQLDUMP="/usr/local/bin/mysqldump";
fi
#Create the MySQL dump
echo "$PATH/$DUMP$EXT1";
$MYSQLDUMP --add-drop-table --user $USER --password=$PASSWORD $DATABASE >> "$PATH/$DUMP$EXT1";
#Compress the dump
cd $PATH;
/bin/tar -cf $DATABASE-$DATE$EXT2 $DATABASE-$DATE$EXT1;
echo $BZIP;
#Zip and Send mail - as an attachment
if [ $BZIP -eq "0" ]
then
/bin/gzip $DATABASE-$DATE$EXT2;
/usr/bin/uuencode $DUMP$EXT2$EXT3 $DUMP$EXT2$EXT3 |$MAIL -s "Mysql Backup For $DATABASE" $EMAIL ;
elif [ $BZIP -eq "1" ]
then
/usr/bin/bzip2 $DATABASE-$DATE$EXT2;
/usr/bin/uuencode $DUMP$EXT2$EXT4 $DUMP$EXT2$EXT4 |$MAIL -s "Mysql Backup For $DATABASE" $EMAIL ;
fi
#Remove the mysql dump (but still keep the compressed copy)
/bin/rm -f $DUMP$EXT1;
#Task complete
cd;
}
# Function which backs up database one by one.
for DATABASE in $DATABASES
do
echo $DATABASE;
backup;
done
exit;
|