Bash backup scripts including mysql backup
I run a few database-driven sites and when I developed them I needed a script to back up the uploaded files and database. To make restoring the site easier, I basically just archived the whole site including the database. My back-up strategy was two part:
Part one:
A script on the web-server dumps the database into a text-file, and then combines that with a copy of the website contents and creates a compressed archive. I rename the last archive, if it exists, and delete any older ones. This means I only ever keep 2 back-ups on the live server. This script is controlled by the cron tab.
Part two:
A script on my machine at home, again controlled by the cron tab, executes. This connects to the remote server, and downloads the archive via scp, naming the archive using the current date.
It’s a fairly low-tech solution, but its simple and it works for me. The only tricky bit was setting up passwordless ssh login so the script in part 2 could access the live server. It was a tad involved but I used the instructions here. Also don’t forget to make the script exicutable!
Script one (Sits on the remote server)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #! /bin/bash # #Variables directory_to_backup="/dir/on/remote/server" backup_name="/path/and/name/of/archive1.tar" backup_old="/path/and/name/of/archive2.tar" db="/path/and/name/of/database/archive.tar" # If old backup exists remove it if [ -e $backup_name ] then if [ -e $backup_old ] then rm $backup_old fi mv $backup_name $backup_old fi # dump the DB mysqldump -u yourDbUsername -pyourDbPassword -h databaseHostName databaseName > $db # create archive if [ -e dbbackup.sql ] then tar -czvf $backup_name $directory_to_backup $db else tar -czvf $backup_name $directory_to_backup fi #remove DB backup rm $db |
Script two (sits on backup server)
1 2 3 4 | #! /bin/bash filename=`date +%y.%m.%d.tar` scp USERNAME@HOST.COM:/remote/file local/copy/$filename |
About this entry
You’re currently reading “Bash backup scripts including mysql backup,” an entry on random()
- Published:
- 12.04.07 / 2pm
- Category:
- Bash Linux & Apache
No comments
Jump to comment form | comments rss [?] | trackback uri [?]