UPDATE: I have been informed that there is a more efficient way of doing this. Instead of using cp to create copies with links, there is the –link-dest parameter in rsync that can be used.
So instead of this:
You can do this:
I have not had a chance to modify the examples and scripts below, and have not tested this myself yet, but it appears correct.
Using rsync in combination with hard links, its very simple and easy to setup incremental backups in Linux (and UNIX). You end up with a full snapshot of each increment, accessible as if they were the original copy that was backed up.
Here are the two links that helped me:
The first link shows how to setup a seperate backup server that connects to your other machines and backs up files from those, but I wrote a backup script that copies to a local drive. The following my first attempt at a script to backup home directories. I wrote a better script with less hard coded values that will be shown later, but this one is a better example of how it works:
Note that “cp -al” copies the files using hard links. Hard links are different from soft links in that the file won’t get deleted until all hard links to it are gone and each hard link to the same file acts as if it is the file (whereas soft links are pointers to the real file). So each hard linked copy of my home directory is a complete snapshot of it, but no space is wasted in duplicate files. I have a cronjob that runs as root for this script that runs every morning, so I end up with a complete snapshot for each day of the last 7 days. Restoring from backup is as easy as copying the files back to their original location, and any of the increments can be copied as is.
“home-backup.excludes” is a list of directories that are excluded from the backup (see rsync man page for format of this file). This is a example of some excludes (“someuser” being a user’s home directory):
Also note that only root should have any permissions to the /backup directory, so that in the case that a user account gets compromised, the attacker can’t delete the backups for that user (permissions of files remain identical). If an attacker gains root access, then all bets are off, that’s why a seperate backup server is a good idea.
I have made a more modular version of the script. It could use more improvements, but this is the current version of it: