On a Mac, you can use Time Machine to maintain backups of your hard drive, with versions, so that you can jump back to any point in time that Time Machine has made a backup. But what if you wanted to do that only for a few folders, and with more control over the whole process? This post describes how to achieve this using rsync and the snapshot feature of RockStor.
Versions of backup folders using rsync
This post was inspired by Time Machine for every Unix out there, which uses the –link-dest option of rsync to maintain links to older versions of files. With RockStor, we can achieve the same thing with snapshots, which are an efficient way of capturing the state of a folder at a specific point in time.
A better way. rsync + Snapshots = Versioned Backup
Here’s how I take backups of a folder on my Mac, to a shared folder on RockStor, and maintain versions of this folder. (See the earlier blog post to make a RockStor Share accessible as a shared folder on your Mac).
Every time I want to make a backup, I do the following
- Copy files using rsync
Using rsync, I copy the Mac folder to the shared folder. The first time, this operation could take a long time since it copies the entire folder contents over. But the subsequent ones will be faster since only the changes from the previous time will be copied.
rsync -avz <source_folder> <shared_folder>
- Take a snapshot of the share
A snapshot is a point in time representation of the contents of the Share. RockStor allows users to take snapshots of shares using either the web-ui or the command line interface (CLI). Since I like to automate my backup process, I use the CLI over ssh to issue a snapshot command for that share. (In my script I generate a snapshot name that contains the current date and time, so that I can identify the snapshot folders easily). The command to create a snapshot with the name ‘mysnapshot’ for the share ‘myshare’ is as follows.
ssh firstname.lastname@example.org "shares share myshare snapshot add -v mysnapshot"
The snapshot command is issued over ssh. The CLI works as a series of subsystems, and you navigate to the subsystem you desire and issue the appropriate command. Here’s what the different components of the command mean
- shares – this takes you to the ‘shares’ subsystem
- share myshare – takes you to the subsystem for operations on the share ‘myshare’
- snapshot add -v mysnapshot is the command to add a snapshot with name ‘mysnapshot’ for the share. The -v flag makes the snapshot visible as a hidden folder in the Samba export.
This way I have a list of snapshots, at multiple points in time, so I can go back and retrieve files from an older snapshot, if I accidentally lose anything in my current folder on the Mac.
(The full script is at https://gist.github.com/sujeetsr/8606389 )
Retrieving files from snapshots
Let see an example of retrieving a file from a snapshot, when it has been deleted from the share.
In my folder share2, I have a few image files, which you can see in the screenshot below. At this point, I have not created any snapshots.
I now take a snapshot of the share, and the snapshot appears as a hidden folder in the share. The snapshot contains all the files that were in the share at the time the snapshot was taken, as seen below.
Now I delete the file called ‘ocean_bean_1.jpg’, and as you can see from the screenshot below, it is not present in the share itself, but is still present in the snapshot. So If I need to retrieve it at any later point in time, I can always get it from that snapshot folder.
Snapshots thus are an easy and efficient way to maintain versions of folders, and enable you to retrieve files, or versions of files that were present at some point in the past.