Hardlink with rsync

Sometimes you want to create a quick snapshot of a few files / folders. rsync is a great tool for this. And sometimes you don’t want to copy the files because that takes up space and you might be copying them to the same filesystem. Hardlinking is quite a nice option in this case (if the filesystem supports it)

I needed to create a quick snapshot of a few virtual tapes. I use virtual tapes which I copy over to physical tapes. Because this is a long proces I create a snapshot of them before I start transferring them to a physical tape.

So how to have rsync hardlink them instead of copy? Quite easy…. use the –link-dest option of the rsync command:

–link-dest=DIR hardlink to files in DIR when unchanged

The trick is convincing rsync it already copied the file / folder.

I have the following folder tree:

/
|-- VirtualTape
|-- slots
|-- slot1
|-- slot2
....
|-- slot19
|-- snap

The snap folder will contain my snapshot of some of the slots of the VirtualTape folder. To link the contents of slot19 for example I go to the VirtualTape folder and execute the following command

rsync -rv –link-dest=../slots slots/slot19 snap

What this does is recursively sync slot19 towards the folder snap. With the –link-dest I point rsync towards the original path. It’s relative to the destination folder. rsync will detect the files have not changed (of course not, it’s the same source) and happily creates links in the snap folder.
I end up with the following folder tree:

/
|-- VirtualTape
   |-- slots
      |-- slot1
      |-- slot2
       ....
      |-- slot19
         | virtualtape-19 contents
   |-- snap
      |-- slot19
        | virtualtape-19 contents hardlinked

As the contents are hardlinked they do not take up extra space. (They will when the files in the virtal tape slot get replaced). And the rsync action is very fast.

One thing to remember: If the files which you link to are being modified instead of being replaced, you link to modified files instead of the one you want to snapshot