Instructions from this site for my own Knowledge Base: You always wanted to BACKUP your most important database somewhere in your Linux system, as well as send the dump by email, so that you can recover the entire content if the system crashes. You can use these 2 scripts. First Step: -Install the mutt client that will transfer emails on the command-line : “apt-get install mutt” or “yum install mutt” -Create the backup directory : “mkdir /home/backups” Second Step: - Copy these 2 scripts on your root directory or your user directory : #!/bin/sh # Script name : auto_mysql_dump.sh # Backup the dbname database dir=`date +%Y-%m-%d` dbname=`mybase` if [ -d /home/backups ]; then mkdir /home/backups/$dir mysqldump -B —user=user_of_my_base —password=pwd_of_my_base —host=host_of_my_base $dbname > /home/backups/$dir/$dbname.sql if [ $?=0 ]; then #Bzip2 the dump.sql bzip2 -z9v /home/backups/$dir/$dbname.sql #Remove the dump.sql from disk rm -f /home/backups/$dir/$dbname.sql fi fi # End of script auto_mysql_dump.sh #!/bin/sh # Script Name : auto_mail_dump.sh # Sends an email with the dump realized before dir=`date +%Y-%m-%d` dbname=`mybase` mutt -s “Today backup” -a /home/backups/$dir/$dbname.sql.bz2 user@tosend.com < /dev/null # End of script auto_mail_dump.sh -Don’t forget to change the access to make them executable: “chmod 700 auto_mysql_dump.sh” “chmod 700 auto_mail_dump.sh” Third step: -Edit the CronTab to schedule the execution of the two scripts. “crontab -e” (you will use the vi editor) We consider that the 2 scripts are in the /root directory -I want the dump to be executed at 8.30 everyday -I want the mail to be sent at 9.00 everyday Thus I add these 2 rows after the existing lines : Hit the “i” to insert new characters… 30 8 * * * /root/auto_mysql_dump.sh > /dev/null 00 9 * * * /root/auto_mail_dump.sh > /dev/null Save the crontab by hitting : “Esc” + “:wq” (means Write and Quit) What you should do now : Once you’ve written the scripts, test-them ! Enjoy the automatic backup from now on :-)
7/26/2011 ~ 2 min read