How does snapshots with hard-links work?

Created by Germar
Keywords:
hard-link snapshot smart remove delete inode filesystem

From my Answer in https://answers.launchpad.net/backintime/+question/123486:

If you create a new file on a Linux filesystem (e.g. ext3) the data will have a unique number that is called inode. The path of the file is a link to this inode (there is a database which stores which file point to which inode). Also every inode has a counter for how many links point to this inode.
After you created a new file the counter is 1. Now you make a new hardlink. The filesystem now just has to store the new path pointing to the existing inode into the database and increase the counter of our inode by 1.
If you remove a file than only the link from the path to that inode is removed and the counter is decreased by 1. If you have removed all links to that inode so the counter is zero the filesystem knows that it can override that block next time you save a new file.

First time you create a new backup with BIT all files will have an inode counter = 1

snapshot0:
path inode counter
fileA 1 1
fileB 2 1
fileC 3 1

Lets say you now change fileB, delete fileC and have a new fileD. BIT first makes hardlinks of all files. rsync than delete all hardlinks of files that has changed and copy the new files.

snapshot0:
path inode counter
fileA 1 2
fileB 2 1
fileC 3 1

snapshot1:
path inode counter
fileA 1 2
fileB 4 1
fileD 5 1

Now change fileB again and make a new snapshot

snapshot0:
path inode counter
fileA 1 3
fileB 2 1
fileC 3 1

snapshot1:
path inode counter
fileA 1 3
fileB 4 1
fileD 5 2

snapshot2:
path inode counter
fileA 1 3
fileB 6 1
fileD 5 2

Finally smart-remove is going to remove snapshot0. All that is done by smart-remove is to 'rm -rf' (force delete everything) the whole directory of snapshot0

snapshot0: (path no longer exist)
path inode counter
        1 2
        2 0
        3 0

snapshot1:
path inode counter
fileA 1 2
fileB 4 1
fileD 5 2

snapshot2:
path inode counter
fileA 1 2
fileB 6 1
fileD 5 2

fileA is still untouched, fileB is still available in two different versions and fileC is gone for good. The blocks on your hdd that stored the data for inode 2 and 3 can now get overriden.

I hope this will shed a light on the 'magic' behind BIT. If it's even more confusing don't hesitate to ask ;)

Regards,
Germar