Garry Conn

How to Automatically Backup Your Web Server

Project Description: The Automatic Website Backup Project aims to help you make automatic daily backups of your website and rsync the data to a remote server.

Instructions:

cd into your website parent directory and mkdir /backups.

cd /var/www/yourname.com/
mkdir backups/

cd into the /backups directory and create a file named backup.sh

cd backups/
nano backup.sh

If you do not need to backup a MySQL database then paste this code:

#!/bin/sh

THESITE="yoursite.com"
THEDATE=`date +%m%d%Y%H%M`

tar czf /var/www/$THESITE/backups/sitebackup_${THESITE}_${THEDATE}.tar -C / var/www/$THESITE/public_html
gzip /var/www/$THESITE/backups/sitebackup_${THESITE}_${THEDATE}.tar

find /var/www/$THESITE/backups/site* -mtime +5 -exec rm {} \;

If you do need to backup a MySQL database then paste this code:

#!/bin/sh

THESITE="yoursite.com"
THEDB="your_database_name"
THEDBUSER="your_database_user"
THEDBPW="your_database_password"
THEDATE=`date +%m%d%Y%H%M`

mysqldump -u $THEDBUSER -p${THEDBPW} $THEDB | gzip > /var/www/$THESITE/backups/dbbackup_${THEDB}_${THEDATE}.bak.gz

tar czf /var/www/$THESITE/backups/sitebackup_${THESITE}_${THEDATE}.tar -C / var/www/$THESITE/public_html
gzip /var/www/$THESITE/backups/sitebackup_${THESITE}_${THEDATE}.tar

find /var/www/$THESITE/backups/site* -mtime +5 -exec rm {} \;
find /var/www/$THESITE/backups/db* -mtime +5 -exec rm {} \;

Change yoursite.com to the actual name of your site. If you are using the MySQL version change your_database_name to your actual database name, your_database_user to your actual database username and your_database_password to your actual database user password, and then save the file.

Make backup.sh executable otherwise it will not run.

chmod +x backup.sh

Run the script once manually to verify it is working properly. Note: Depending on the size of your site it can take a few moments to process. Just be patient.

/var/www/yoursite.com/backups/backup.sh

Verify the script worked by looking inside the /backup directory for the .tar.gz backup copy of your website. You can easily ls or even better du -sh * which will kill two birds with one stone by verifying the backup is there and the size.

du -sh *
4.0K	backup.sh
172K	dbbackup_your_database_name_2907121223.bak.gz
180M	sitebackup_yoursite.com_2907121223.tar.gz

On the local server create a cron job to run the script daily. The first minute of the first hour each day should be fine.

crontab -e

Then add this line and change yourname.com to the actual name of your site

 1       1       *       *       *      /var/www/yoursite.com/backups/backup.sh

On the remote server create a cron job to rsync the data daily. The 11th minute of the first hour each day should be fine. This allows the local server 10 minutes to complete the task of making the daily backup.

10      1       *       *       *      rsync -e ssh -a --delete username@yoursite.com:/var/www/yoursite.com/backups/ /backup01/yoursite.com/backups/

Make sure that rsync has been installed and is running on both servers.

apt-get install rsync
/etc/init.d/rsync start

Verify rsync is working properly by manaully running this command from the remote server

rsync -e ssh -a --delete username@yoursite.com:/var/www/yoursite.com/backups/ /backup01/yoursite.com/backups/

Check to see if the backup is in the /backup01/yoursite.com/backups/ directory. You can use the du -sh * command to verify the copy is there and the size is correct.

du -sh *
180M	yoursitename.com
cd yoursitename.com
du -sh *
180M	backups
cd backups
du -sh *
4.0K	backup.sh
176K	dbbackup_your_database_name_2907121223.bak.gz
180M	sitebackup_yoursitename.com_2907121223.tar.gz

If you should have any comments, questions or suggestions please post them using the comment form below. Your comments will be made public so that other people can read them.