Rsync incremental snapshots

I have been using rsync for backup and other things for a long time. It has a link-dest option that allows doing incremental snapshots similar to Time Machine on Macs.

With this method, each backup is a separate folder with a full snapshot of the source folder. However, only the changed files from the last backup are transferred and take up additional disk space. This is done using hard links, allowing any backup folder to be deleted without affecting the others.

Here is a simple bash script based on scripts that I use:

DATE=`date +%Y%m%d-%H%M%S`
rsync -e ssh --rsync-path='sudo rsync' -aPvx --delete \
--link-dest='../_latest' --modify-window=10 $1 $2/tmp-$DATE \
&& mv $2/tmp-$DATE $2/$DATE \
&& ln -nfs $2/$DATE $2/_latest

It takes two arguments, a source and destination, which are much the same as the rsync arguments except that the destination will have each backup in a folder with a date stamp name. _latest will symlink to the latest. It’s set up for the source to be remote and to run via sudo on the remote so it will have root privileges. If you don’t want that, you can remove the -e ssh --rsync-path='sudo rsync' part.

Running would look like:

myscript backups/important-files

rsnapshot does something like this on a cron-like schedule, removing snapshots after a certain number. It is more of a computer wide setup though, with a configuration file and daemon.