In this example, each database will be stored separately and like earlier example we will save all databases and we will have the last 4 copies.

#Simple mySQL backup script for cron – updated version

# Modify the following to suit your environment
export DB_BACKUP=”/home/user/mysql_backup”
export DB_USER=”root”
export DB_PASSWD=”***********”
export DATE=”`date +”%d%b”`”
export MYSQL=”/usr/bin/mysql”
export MYSQLDUMP=”/usr/bin/mysqldump”

# Backup part
echo “mySQL_backup”
echo “———————-”
echo “* Rotating backups…”
rm -rf $DB_BACKUP/04
mkdir $DB_BACKUP/01

cd $DB_BACKUP/ && cd $DB_BACKUP/01
$MYSQL -u $DB_USER –password=$DB_PASSWD -Bse ‘show databases’ |while read m; \
do $MYSQLDUMP -u $DB_USER –password=$DB_PASSWD `echo $m` > `echo $m`.sql;done
bzip2 *sql

echo “* Creating new backup…”
echo “Backup done! `date`” > /tmp/my_report.log

# You can set the script to send you mail when backup it’s finished.
mail -s “MySql Backup report” [email protected] < /tmp/my_report.log
echo "----------------------"
echo "Done"
exit 0


Page 1 of 11