Tag linux

Backup your Mac to remote server

15 November, 20:53, by Mark Zander Tags: , , , ,

There are quite a few examples out there in how to use rsync to duplicate how Timemachine does its backups.

This is the article which first inspired me. http://blog.interlinked.org/tutorials/rsync_time_machine.html

But I still had some problems with it. Firstly you either had to pull the backup from the Mac. My Mac is not on all the time. Least of all at night when backups are normally made.

The other option pushed the backup from the Mac but then you had to have SSH access to the Linux server that you were backing up to. Difficult to setup and not too secure because of the public/private key authentication scheme to avoid typing in your password.

So this is what I came up with.

 

Rsync Server on Linux:

The first thing you should setup is a Linux server that has plenty disk for the backups and has rsync installed. Your Mac(s) will be connecting to this server with rsync and transferring files to it.

I’m not going to do through the whole rsync installation. There are quite a few guides on the Internet for that. I will just describe my setup and leave you to fill in the rest.

I am using Debian 5.0 with rsync  version 3.0.3.

Here is what my /etc/rsyncd.conf

######
uid = root
gid = backup
use chroot = yes
max connections = 4
syslog facility = local5
pid file = /var/run/rsyncd.pid
[mac]
        path = /Public/Backup-Mac
        comment = Backup directory for Bunny's Mac
        read only = false
######

That requires a bit of explaining.

I am using ‘chroot’ which I am told is more secure. If you want to be even more secure I would suggest you change the ‘uid’ and ‘gid’ to something other than ‘root’.

You will notice that I am using rsync ‘modules’. The [mac] portion is sort of like a Samba mount for rsync and it defines where the Mac’s will be putting their files. Read up on rsync ‘modules’ as they really are powerful.

I have a 2 TB disk mounted on /media/Backup-Mac which is where rsync will put all the Mac’s backup files. Here is what the disk looks like.

######

drwxr-xr-x 9 root root    4096 2011-11-15 12:15 .
drwxr-xr-x 4 root root       0 2011-11-15 16:20 ..
drwxr-xr-x 3 root backup  4096 2011-10-12 14:48 backup-2011-11-12
drwxr-xr-x 3 root backup  4096 2011-10-13 09:03 backup-2011-11-13
drwxr-xr-x 3 root backup  4096 2011-10-14 09:37 backup-2011-11-14
drwxr-xr-x 3 root backup  4096 2011-11-15 10:43 backup-2011-11-15
lrwxrwxrwx 1 root root      17 2011-11-15 12:17 current -> backup-2011-11-15
drwx------ 2 root root   16384 2011-10-12 14:35 lost+found

######

That’s it for the Linux side.

Mac client side:

The real power of rsync is it will synchronize 2 directories very efficiently. It only transfers the changes and nothing else. The initial sync can take a long time but from there it is very fast in keeping things the same. Great for the WiFi link I use.

The other power of rsync is it’s ability to use hardlinks for incremental backups. That means that you can have what appears to be ‘snapshots’ of your files from a series of days but they take up only as much space as 1 complete backup plus the daily changes. See the article above for details on how rsync uses hardlinks.

The benefit of using hardlinks is when you take a look at one days backup you have the entire files structure just as it would have been on you Mac. This makes retrieving files as easy as navigating the directories.

Here is the script that I use on the Mac side:

######

#!/bin/sh

DATE=$(date "+%Y-%m-%d")
HOST="tecra"
MODULE="mac"
BWLIMIT="5000"

REMOTE_DIR="backup-$DATE"

LOCAL_DIR="/Users"

rsync -azP --bwlimit=$BWLIMIT --link-dest=/current  --exclude-from=/etc/rsync.exclude $LOCAL_DIR $HOST::$MODULE/$REMOTE_DIR

cd /var/tmp
mkdir current
cd current
rsync -rv --delete --include=current '--exclude=*' . $HOST::$MODULE
ln -s backup-$DATE ./current
rsync -azP current $HOST::$MODULE
cd /
rm -rf /var/tmp/current
#####

The meat of the script is the ‘rsync’ line. It is similar to most of the other examples on the Internet. I use ‘bwlimit’ to make sure I don’t swamp the WiFi link with the backup.

Most, if no all, people use SSH to create the ‘current’ link on the remove server. I figured out a way to do it just via rsync. The bit after the rsync command is where it gets done.