Use rsync For Snapshot Mac Backups
posted by davzie in Scripting, Work Life

Can you imagine how you’d feel if your hard-drive died, right this second? I don’t think I need to stress the importance of a routine backup, if you’re still reading this I guess you understand the need to have a backup in-place.
I came across a problem where I wanted to backup all of my files on a daily basis but I wanted deleted items to remain on my backup device. I quickly realised this posed a problem whereby those deleted files would eventually build up to a huge, massive amount of disk-space. Here was my solution.
This backup script works in the following way:
- The daily backup will run each night, transferring any new or changed files to my backup device but leaving deleted items on the backup device. The folder it transfers to is ‘current’.
- Every week the daily backup will run again but straight afterwards will be copied to a folder called snapshot.0. The weekly backup will then run, removing any files from ‘current’ that aren’t on the source drive.
The Code:
daily_backup.sh
#/bin/sh rsync -a --progress /Volumes/source/ /Volumes/dest/current/
weekly_backup.sh
#/bin/sh rsync -a --progress /Volumes/source/ /Volumes/dest/current/ rm -rf /Volumes/dest/snapshot.3 mv /Volumes/dest/snapshot.2 /Volumes/dest/snapshot.3 mv /Volumes/dest/snapshot.1 /Volumes/dest/snapshot.2 mv /Volumes/dest/snapshot.0 /Volumes/dest/snapshot.1 cd /Volumes/dest/current find . -print | cpio -dumpvl /Volumes/dest/snapshot.0 rsync -a --delete --progress /Volumes/source/ /Volumes/dest/current/
Prerequisites
- Make sure /Volumes/source has the following folders
- current
- shapshot.0
- snapshot.1
- snapshot.2
- snapshot.3
- If you encounter problems, run the script as a super-user ‘sudo daily_backup.sh’
Explanation
This process repeats for four weeks until I have snapshot.0, snapshot.1, snapshot.2, snapshot.3. It copies the files to each snapshot folder by using ‘cpio’. The magic of this little tool is that should you have three folders of snapshots that only have a few files changed between them and each snapshot folder is around 2G in size then the total size of all three of those snapshot folders will be 2G + the size of the changed / new files.
cpio – magic
The ‘cpio’ process creates ‘hard-links’ to files on the drive. Meaning if the files haven’t changed between snapshots, then there’s no need to duplicate that space since they are the same file. Of course if a file is changed rsync is built to first unlink these files before making any updates thus giving you a months worth of deleted items for very little file space being sacrificed.
If you get stuck, leave a comment, I’ll do my best to get back to you.
Doesn’t Time Machine manage the files in backups anyway? you can set it to Back up files now, then delete the file you dont want, and it will remain on the backups.
credit where it’s due: http://www.mikerubel.org/computers/rsync_snapshots/
most articles on rsync snapshots cite this source.