rsync and the Hard Link Limitation

I’ve been setting up a new backup regime on a hosted server that I’m helping to maintain.

One of the key issues right now is that it is there isn’t a solid provided backup regime, so I’ve spent the day playing around with different Linux backup solutions trying to get to the bottom of a simple, yet robust way of backing up.

I like rsync, but the problem is that you can’t use it’s hard linking feature over secure shell. As such, incremental backups are a pain.

I was about to give up on it, and work out a convoluted tar process when it occurred to me that the hard links might still be possible.

And to that end, I wrote this script:

<code>#!/bin/bash
SSHFS_MPOINT=/mnt/sshfs;

REMOTE_BACKUP_USER=root;
REMOTE_BACKUP_HOST=myserver.co.uk;
REMOTE_BACKUP_FOLDER=/home/livepath;
REMOTE_BACKUP_PORT=22;

LOCAL_BACKUP_FOLDER=/backups/myserver;

# End of options

# Create mount point directory
if [ -d $SSHFS_MPOINT ]; then
        #Do nowt
        :
else
        echo "Creating sshfs mount point:" $SSHFS_MPOINT;
        mkdir -p $SSHFS_MPOINT
fi

# Mount backup folder
sshfs -p $REMOTE_BACKUP_PORT $REMOTE_BACKUP_USER@$REMOTE_BACKUP_HOST:$REMOTE_BACKUP_FOLDER $SSHFS_MPOINT

# Now carry out the backup using hard links
rsync --link-dest=$LOCAL_BACKUP_FOLDER --compress --verbose --times --owner --group --ignore-times --links --perms --recursive --size-only --delete --force --numeric-ids --stats $SSHFS_MPOINT $LOCAL_BACKUP_FOLDER.`date +%F.%T` >> /var/log/rsync_backup.log

# Once done, unmount the remote machine
fusermount -u $SSHFS_MPOINT

exit 0;</code>

Essentially, I’m using FUSE and SSHFS to mount the file system to my local server. From here, I run the backup as though the rsync is carried out locally on the machine. Because the existing backup directory can be accessed locally, there isn’t a problem using the hard links feature.

Of course, the prerequisites are that you have FUSE and SSHFS installed on your system, and that there is an SSH key pair between your machine and the remote server to allow for seamless logon.

You also need at least one backup created in the $LOCAL_BACKUP_FOLDER directory before the script will work.

So far, this seems to be working nicely on a daily basis.