Site Backups

backups

If you have ever experienced the horror of losing files or a database, then you’ll understand the importance of implementing and TESTING your backups and site recovery capabilities.

I’ve developed some scripts that run on my local Debian server, which backs up my Hostgator sites and associated databases on a daily as well as a weekly basis.

Note: Unless you have a pre-existing arrangement in place, do not assume that your web host provider has adequate backup/recovery protection for your web sites. You’ve been warned.

Crontab Configuration

Scheduled backups are performed with the use of a crontab and some shell scripts. You can create your crontab with:

dev$ crontab -e

30 1 * * 6 sh /home/username/weekly.sh > /dev/null 2>&1
30 0 * * * sh /home/username/daily.sh > /dev/null 2>&1

daily.sh

Daily backups use names such as client1_Monday.sql.gz and client1_Monday.tgz. This weeks backup will overwrite last week’s backup and is then download to the offsite backup server. Don’t forget to ‘chmod +x daily.sh’ which contains:

# client1
ssh www.client1.com ‘tar czf client1_`date +%A`.tgz public_html’
ssh www.client1.com ‘mysqldump –opt –user=client1_username –password=xxxxxx client1_dbname | gzip -v9 – > client1_`date +%A`.sql.gz’
scp www.client1.com:client1_`date +%A`.tgz /var/backups/client1
scp www.client1.com:client1_`date +%A`.sql.gz /var/backups/client1

weekly.sh

These backups use names, such as client1_2013_09_29.sql.gz and client1_2013_09_29.tgz. They are downloaded from the client server and then deleted from the production server. The weekly.sh file contains:

# client1
ssh www.client1.com ‘tar czf client1_`date +%Y_%m_%d`.tgz public_html’
ssh www.client1.com ‘mysqldump –opt –user=client1_username –password= xxxxxx client1_dbname | gzip -v9 – > client1_`date +%Y_%m_%d`.sql.gz’
scp www.client1.com:client1_`date +%Y_%m_%d`.tgz /var/backups/client1
scp www.client1.com:client1_`date +%Y_%m_%d`.sql.gz /var/backups/client1
ssh www.client1.com ‘rm client1_`/bin/date +%Y_%m_%d`.tgz’
ssh www.client1.com ‘rm client1_`/bin/date +%Y_%m_%d`.sql.gz’

Feel free to implement more functionality, such as email notifications, purging, testings, etc.

Note: Please be cognizant of the bandwidth you use. You may end up with a ‘Bandwidth exceeded’ message if you download the dailies to your offsite backup server.

Comments are closed.