PDA

View Full Version : [How To] Automated bash cron backup.


Club3G
01-13-2006, 02:44 PM
Originally a question, figured it out, thought I'd share.

To automatically make a backup of your database using cron:


#! /bin/bash
# Automated database datestamp backup

mysqldump --opt -Q -u dbusername -pPassword dbname > /path/to/backups/`date --iso-8601`.sql

Open it up, replace:


dbusername
Password
dbname


With the proper information. Note that there is no space between -pPassword, it's intentional.

Then replace "/path/to/backups/" with the actual path that you want to put them in. Make sure the directory exists.

Put the backup.sh file in the appropriate cron folder. I'm running mine weekly, so mine is in /etc/cron.weekly/. CHMOD it +x (chmod +x backup.sh)

Output of it is a database file, named (the date).sql.

Putting it in cron.weekly will run it every Sunday night at Midnight, and give you a file that looks like this:


[cron.weekly]# sh backup.sh
[cron.weekly]# ls /home/backups/
2006-01-13.sql
[cron.weekly]#

Club3G
01-15-2006, 05:33 AM
Just want to throw this one and only bump.

If you're a geek such as myself and have FTP access out from work (and a decent connection) you can setup a cron job there to download 'em automatically as well.

Peace of mind. :)