Friday, February 19, 2010

Back me up, scotty!

Since a few months I'm running a VPS with a couple of hosted accounts on it. I just wanted to share my cheap, simple and robust backup solution.
The people of XLSHosting not only provide a wonderfull VPS service, they also provide a backup solution for your complete VPS. This is not what I will be discussing. I do however want to give them the credit for showing me the way to their backup script, normally intended for their own backup solution

Home-based backup

My goal was to backup everything on my VPS to my home. Diskspace is cheap, so I wanted to keep as much server history as possible on a USB harddisk. As it happens, I already am running a fileserver / printer server at home. It's an old EEE PC running on windows XP.

The backup configuration scenario is as follows:

  1. install rsync on your home windows machine with cwrsync
  2. make sure you generate an ssh keypair on your windows machine for the VPS server and install it.
  3. check that you can ssh from your VPS server to your home windows machine without having to provide a password. If you need to provide a password, you have made an error in installing the ssh keypair
  4. now upload your backup script. Of course you need to customize this.
  5. add an nightly entry in your crontab to dump your MySql databases to a file on the server
  6. add a second entry in your crontab (which executes later during the night then the DB dump) to execute the backup script
  7. check your email in the morning to see if all went well

My customisations

In the backup script below, there are 2 important customisations I made. The first one is that I have the result of the backup emailed to me including a summary of the data. The second one is that I choose to only copy files that don't exist yet on the target machine. This works with hardlinks and apparently it even works when the backup target is an NTFS partition, cool.

This is the output of the email I receive:

Fri Feb 19 03:01:01 CET 2010
dagelijkse backup voor dag 19 van maand 2, weekdag 5, naar daily/5
Directory voor deze backup: VPSbackup-daily/5/
Directory als basis voor incremental linken: ../../VPSbackup-daily/4/

Number of files: 7782
Number of files transferred: 3
Total file size: 66536313 bytes
Total transferred file size: 1178442 bytes
Literal data: 3345 bytes
Matched data: 1175097 bytes
File list size: 181673
File list generation time: 0.077 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 189594
Total bytes received: 6656

sent 189594 bytes received 6656 bytes 5528.17 bytes/sec
total size is 66536313 speedup is 339.04
Backup klaar.
Fri Feb 19 03:01:37 CET 2010


This is how my crontab looks:

01 1 * * * wget -O - -q -t 1 http://www.dothisforeachsite.be/cron.php
01 2 * * * /home/dothisforeachcustomeraccount/public_html/sites/default/backup.sh
01 3 * * * /root/xlsbackup 2>&1 | mail -s 'Backup report VPS server' youremail@yourdomain.com


This is how my backup scripts looks

#!/bin/bash
WEKEN=4
MAANDEN=12
H=VPSbackup
WEEKDAY=$(date +%u) # 1-7
MONTHDAY=$(date +%d | sed s/^0\\+// ) # 1-31
MONTH=$(date +%m | sed s/^0\\+//) # 01-12
WEEK=$(date +%U | sed s/^0\\+// ) # 00-53
if [ $WEEKDAY = 1 ]
then
PREVDAY=6
else
PREVDAY=$[WEEKDAY-1]
fi
MSG="daily backup for day $MONTHDAY of month $MONTH, weekday $WEEKDAY, to daily/$WEEKDAY"
DIR=$H-daily/$WEEKDAY/
PREVDIR=../../$H-daily/$PREVDAY/
if [ $WEEKDAY = 7 ]
then
N=$[WEEK%WEKEN]
MSG="weekly backup for week $WEEK to weekly/$N"
DIR=$H-weekly/$N/
fi
if [ $MONTHDAY = 1 ]
then
N=$[MONTH%MAANDEN]
MSG="monthly backup for month $MONTH to monthly/$N"
DIR=$H-monthly/$N/
fi
echo $(date)
echo $MSG
echo Directory for backup: $DIR
echo Directory as base for incremental linking: $PREVDIR
rsync --stats -e 'ssh -p 23576 -i /root/.ssh/id_rsa.backupscriptkey' --link-dest=$PREVDIR --delete -ax --exclude stats --exclude logs --exclude tmp --exclude imap /home yourusername@yourhomedomainwithdyndyns.dyndns.org:$DIR 2>&1
echo Backup ready.
echo $(date)


Now that also in Belgium the download limits on broadband connections are dissapearing, this is a very good remote backup solution that can be used by anyone that needs to backup a server.

No comments:

Post a Comment