Simple mySQL backup script for cron – updated version

Valic —  November 24, 2010 — 3 Comments

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.

#!/bin/bash
#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
mv $DB_BACKUP/03 $DB_BACKUP/04
mv $DB_BACKUP/02 $DB_BACKUP/03
mv $DB_BACKUP/01 $DB_BACKUP/02
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

Enjoy

Valic

Posts Twitter Facebook

Editor in Chief at Debian-Tutorials, Linux enthusiast.

3 responses to Simple mySQL backup script for cron – updated version

  1. thanks mate really helped i appreciate it

  2. Thanks for information..

    Pooja

    MLM Developers India

Trackbacks and Pingbacks:

  1. Gliffy Opens Its API - Wordpress Video Tutorials - December 17, 2010

    [...] Simple mySQL backup script for cron – updated version | Linux … [...]

Leave a Reply