This will show you how to do incremental backups using rsync. This was done on Ubuntu 11.10 Oneiric Server Edition. Each time a backup is run the oldest backup is removed and all the unchanged files from the newest backup will be hard linked into the next backup. Hard links allow the data of a file to exists in multiple locations on the file system. A hard link is only an additional name for data that already exists on a partition, the operating system makes no distinction between the original file and the hard link created from the file. If the original file is deleted as long as there is at least one hard link the data will remain on the partition.
The script below will create 7 backups, each time a backup is run the oldest backup will be deleted.
rm -rf /backups/backup.7
mv /backups/backup.6 /backups/backup.7
mv /backups/backup.5 /backups/backup.6
mv /backups/backup.4 /backups/backup.5
mv /backups/backup.3 /backups/backup.4
mv /backups/backup.2 /backups/backup.3
mv /backups/backup.1 /backups/backup.2
mv /backups/backup.0 /backups/backup.1
rsync --archive --progress --delete --super --hard-links --acls --xattrs --link-dest=/backups/backup.1 /source /backups/backup.0
The first line will delete the oldest backup. The next 7 lines will increment each backup number so that “backup.6″ will become “backup.7″. Options “–archive –hard-links –acls –xattrs” preserve file permissions, modification times, file links and some other things. If you don’t want to backup file permissions or don’t use acls you can remove some of these settings. Options “–progress –super” shows a progress bar when coping files and tells rsync to run as root which is needed to preserve file owners. Option “–delete” deletes extraneous files from the backup, by default if a file is deleted from the source location rsync will keep the file in the backup including this option will remove any files from the backup that no longer exists on the source location. This option will not delete any files from previous backups.
Option “–link-dest=/backups/backup.1″ is the folder that rsync will use for hard links. Any files found in this folder that have not changed will be hard linked into the next backup then any changed or new files will be copied. Option “/source” is the directory that rsync will backup. Option “/backups/backup.0″ is the directory to place the newest backup any changes to this location will need to be reflected in the previous 8 lines.
To run the backup every day create a file in “/etc/cron.daily”.
administrator@ubuntu:~$ sudo nano /etc/cron.daily/rsyncbackup
After adding the lines above to the file and saving give the file executable permissions.
administrator@ubuntu:~$ sudo chmod +x /etc/cron.daily/rsyncbackup