Hassing.org

Remote Backup Solution

This is a tutorial on setting up a basic remote webdav backup system on a Linux server. The operating system used in this tutorial is Ubuntu Intrepid Ibex on a slicehost server. It should work without problems on any Linux server. If you know how to install packages on your system you should be fine. The webdav server is BingoDisk by Joyent. But again, it should work with any webdav server.

Preparing webdav

First you need to setup your target. Start by installing davfs2 and creating a mount folder:

sudo apt-get install davfs2
sudo mkdir /backups

Now add the webdav mount point by adding the following line to /etc/fstab:

https://webdav.server.com/path/ /backups davfs user,noauto,rw 0 0

Add this line to /etc/davfs2/secrets. You need to have your webdav credentials there if you want the script running automatically.

https://webdav.server.com/path/ webdav_username webdav_password

To mount the webdav server you need to make mount.davfs SUID and add your user to the davfs2 group.

sudo chmod u+s /sbin/mount.davfs
sudo adduser user_name davfs2

Now you should be able to mount and unmount /backups.

mount /backups
umount /backups

The umount command should say something like “waiting while mount.davfs (pid x) synchronizes the cache”. Now you have your target system ready for use.

Creating the backup script

The basic steps for the backup script are:

A simple version that backups your websites could look like this:

#!/bin/bash
LOCAL=/local_backups
TARGET=/backups
BACKUP=`date "+%Y-%m-%d"`
tar cf $LOCAL/www_$BACKUP.tar -C /var/www/ site1 site2 site3
gzip $LOCAL/www_$BACKUP.tar
mount $TARGET
mv $LOCAL/www_$BACKUP.tar.gz $TARGET/
# feed output to /dev/null to prevent cronjob email about cache sync.
umount $TARGET > /dev/null

This will pack /var/www/site1, /var/www/site2, and /var/www/site3 into a tar file, gzip it, and move it to the webdav server. Alternatively you could use rsync or a similar tool to handle the backup. I suggest using AutoMySQLBackup if you need to backup a mysql server. It is fairly easy to incorporate it into the above script.

When you have a script ready that fits you then save it as ~/bin/backup.sh and make it executable.

chmod u+x ~/bin/backup.sh

and finally …

Automagic backups

Now all we need is to setup a cronjob that runs the script. Use crontab to add the cronjob.

crontab -e

If this is your first time setting up cronjobs before then it works by listing one job per line. In this format:

minute hour day_of_month month day_of_week command

A few examples:

1 0 * * * /path/to/script.sh # every day, 1 minute past midnight
0 */2 * * * /path/to/script.sh # every 2 hours on the hour
0 15 * * 1 /path/to/script.sh # every monday at 3pm

Once you’ve added your line just save the file and exit the editor. That all there is to it. Now you have remote backup of your system and you should be able to sleep a bit better at night. If you have any thoughts or questions feel free to write a comment or email me.