Backing up MySQL databases easily with cron
26 07 2006CODE:
#! /bin/bash
# Ameir Abdeldayem
# http://www.ameir.net
# You are free to modify and distribute this code,
# so long as you keep my name and URL in it.
# your MySQL server's name
SERVER=abubakr
# your MySQL server's location (IP address is best)
HOST=localhost
# MySQL username
USER=root
# MySQL password
PASS=
# List all of the MySQL databases that you want to backup in here,
# each seperated by a space
DBS="ameir"
# set to 'y' if you want to backup all your databases. this will override
# the database selection above.
DUMPALL=n
# MySQL dump options
OPTIONS=" --quick --add-drop-table --add-locks --extended-insert --lock-tables"
# directory to backup to
BACKDIR=~/backups
# date format that is appended to filename
DATE=`date +'%m-%d-%Y'`
# set to 'y' if you'd like to be emailed the backup (requires mutt)
MAIL=y
# email addresses to send backups to, separated by a space
EMAILS="email@gmail.com email@inbox.com email@walla.com email@goowy.com"
SUBJECT="MySQL backup on $SERVER ($DATE)"
#--------------------------------------------------------------
# check of the backup directory exists
# if not, create it
if [ -e $BACKDIR ]
then
echo Backups directory already exists
else
mkdir $BACKDIR
fi
if [ $DUMPALL = "y" ]
then
echo Dumping all your databases...
mysqldump -h $HOST --user=$USER --password=$PASS $OPTIONS --all-databases > \
$BACKDIR/$SERVER-mysqlbackup-ALL-$DATE.sql
gzip -f -9 $BACKDIR/$SERVER-mysqlbackup-ALL-$DATE.sql
else
echo Backing up MySQL databases...
for database in $DBS
do
mysqldump -h $HOST --user=$USER --password=$PASS $database > \
$BACKDIR/$SERVER-mysqlbackup-$database-$DATE.sql
gzip -f -9 $BACKDIR/$SERVER-mysqlbackup-$database-$DATE.sql
done
fi
# if you have the mail program 'mutt' installed on
# your server, this script will have mutt attach the backup
# and send it to the email addresses in $EMAILS
BODY="Your backup is ready! Find more useful scripts and info at http://www.ameir.net"
if [ $MAIL = "y" ]
then
echo "$BODY" | mutt -s "$SUBJECT" \
-a $BACKDIR/*$DATE.sql.gz $EMAILS
echo "Your backup has been emailed to you!"
fi
echo Your backup is complete!
# Ameir Abdeldayem
# http://www.ameir.net
# You are free to modify and distribute this code,
# so long as you keep my name and URL in it.
# your MySQL server's name
SERVER=abubakr
# your MySQL server's location (IP address is best)
HOST=localhost
# MySQL username
USER=root
# MySQL password
PASS=
# List all of the MySQL databases that you want to backup in here,
# each seperated by a space
DBS="ameir"
# set to 'y' if you want to backup all your databases. this will override
# the database selection above.
DUMPALL=n
# MySQL dump options
OPTIONS=" --quick --add-drop-table --add-locks --extended-insert --lock-tables"
# directory to backup to
BACKDIR=~/backups
# date format that is appended to filename
DATE=`date +'%m-%d-%Y'`
# set to 'y' if you'd like to be emailed the backup (requires mutt)
MAIL=y
# email addresses to send backups to, separated by a space
EMAILS="email@gmail.com email@inbox.com email@walla.com email@goowy.com"
SUBJECT="MySQL backup on $SERVER ($DATE)"
#--------------------------------------------------------------
# check of the backup directory exists
# if not, create it
if [ -e $BACKDIR ]
then
echo Backups directory already exists
else
mkdir $BACKDIR
fi
if [ $DUMPALL = "y" ]
then
echo Dumping all your databases...
mysqldump -h $HOST --user=$USER --password=$PASS $OPTIONS --all-databases > \
$BACKDIR/$SERVER-mysqlbackup-ALL-$DATE.sql
gzip -f -9 $BACKDIR/$SERVER-mysqlbackup-ALL-$DATE.sql
else
echo Backing up MySQL databases...
for database in $DBS
do
mysqldump -h $HOST --user=$USER --password=$PASS $database > \
$BACKDIR/$SERVER-mysqlbackup-$database-$DATE.sql
gzip -f -9 $BACKDIR/$SERVER-mysqlbackup-$database-$DATE.sql
done
fi
# if you have the mail program 'mutt' installed on
# your server, this script will have mutt attach the backup
# and send it to the email addresses in $EMAILS
BODY="Your backup is ready! Find more useful scripts and info at http://www.ameir.net"
if [ $MAIL = "y" ]
then
echo "$BODY" | mutt -s "$SUBJECT" \
-a $BACKDIR/*$DATE.sql.gz $EMAILS
echo "Your backup has been emailed to you!"
fi
echo Your backup is complete!
Categories : Linux Luvin'
Trackbacks : No Trackbacks »

Trackbacks
No Trackbacks