Choose the right tool
Rsync is an ultimate tool for backup purposes. It offers transmitting data
remotely and securely over SSH. Also, it offers
--link-dest option which
guarantees files are not duplicated in a filesystem thanks to hard links.
By the way, the same way it does proprietary Apple Time Machine.
Start writing a script
Sure, we will use many more other useful options, not just
--link-dest. Here’s a simplified version of my
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
-atypical option for backup, archive mode (recursive copy, copy symlinks as symlinks, preserve ownerships). Preserving ownerships works only if rsync is run on our backup machine with root rights – see
-vbe verbose, but not too much
--deletedelete extraneous files from destination directories
--delete-excludedalso delete excluded files from destination directories
--compress-level=9highest compression level (CPU is fast, network ain’t)
--numeric-idsdon’t map uid/gid to users/groups
--rsync-path=<some-path>to preserve ownerships we need to run rsync as root on our backup machine.
--exclude-from=<some-path>exclude some directories and files from backup
--link-dest=<some-path>magic. Hardlink unchanged files to files in <some-path> directory
/home/cinandirectory to backup
-e sshspecify the remote shell to use
<path>directory where will be backup saved
mv ... & rm ... && lnmark the newest backup as complete. Delete the old backup link directory (don’t worry, you won’t lose your data, it’s just a symlink) and symlink the newest backup to current-backup directory (useful for future backups which will use this directory in
On the bottom of this article I’ll show complete script.
Keep an eye on slashes! There’s a huge difference between /home/cinan and /home/cinan/ (source directory). Without the final slash, rsync will copy the directory in its entirety. With the trailing slash, it will copy the contents of the directory but won’t recreate the directory.
Little bit of security
The sync-user usershould be able to run
sudo rsync without asking a password. Simply
add this line to
Before running the script, sacrifice more of your time for sake of security. The
--rsync-path option can be quite dangerous. On server-side setup rrsync first.
Basically it allow sync-user to run rsync in defined something-like-chroot
environment. Read more about rrsync here.
Now you can run the script. First time it can take longer time but future backups are incremental, so rsync will transmit only changed files.
Impact of hard links
Every time the backup process create a new directory called backup-$date.
Thanks to that it’s really easy to get files from 22nd Oct 2013 or 29 Nov 2013.
Also, it is space efficient solution because of hardlinks. If file
exists in backup from 1st Jan 2013 and also from 10th Feb 2013, data of the file
is saved on HDD only once. However, it doesn’t mean if you delete a file from January
backup directory then the same file will be deleted in February backup directory –
the February backup file preserves.
Look at directory sizes. First backup is the biggest, other directory sizes are just diffs.
More complete script
It isn’t very robust, but it works and I’m happy with it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
I want my data encrypted
Check out 2/2 part.