The shape of the backup
A backup strategy has three parts: a way to capture data, a record of what was captured, and a way to restore the captured data in the right order. A full backup captures everything and starts a chain. Each incremental backup captures the changes since the previous backup in the chain, and the chain is restored in order, starting from the full archive. The record is what lets the next run know what changed.
In GNU tar, the record is a snapshot file that you name with --listed-incremental. The first run with a new snapshot file is a full backup, because nothing is recorded yet. Later runs that reuse the file write only what changed and update the file as they go.
Step one: a full backup
Start with a directory of notes. Create two files, then run tar with a new snapshot file. The archive will contain the directory and both files, and the snapshot file will record their state.
mkdir -p notes
echo first > notes/a.txt
echo second > notes/b.txt
tar --listed-incremental=snap.snar -cf full.tar notesThe archive full.tar is a complete copy of the tree. The file snap.snar is plain text that tar wrote. Do not edit it by hand, and do not delete it, because deleting it makes the next run a full backup again.
Step two: an incremental backup
Change one file and add another. Reuse the same snapshot file. Tar compares the current state with the recorded state and includes only the entries that differ. The unchanged file is not written to the new archive.
sleep 1
echo updated >> notes/a.txt
echo third > notes/c.txt
tar --listed-incremental=snap.snar -cf incr.tar notesThe sleep 1 is a small guard. Snapshot comparisons use file times, and a change made within the same instant as the previous backup can be missed. A pause of a second makes the example reliable. Listing incr.tar should show a.txt and c.txt, and it should not show b.txt.
tar -tf incr.tarReusing one snapshot file for every run makes a chain that is only as good as its order. If you lose the full archive, the incremental archives cannot be restored correctly. Keep each archive and its snapshot together, and store copies off the machine.
Step three: restore the chain in order
Restore by extracting the full archive first and then the incremental archive into the same target directory. The incremental archive then overlays the changes. Extract into a new directory so that the original tree stays untouched, which is what makes the test meaningful.
mkdir -p restore
tar -xf full.tar -C restore
tar -xf incr.tar -C restore
diff -r notes restore/notes && echo restoredThe final comparison is the proof. If the restored tree matches the current notes exactly, the chain is complete. If it does not, the difference shows which step is wrong. A backup that has not been restored and compared is not yet a backup.
Design choices that matter
The approach above is simple, and it has limits. A deleted file is recorded as deleted in the snapshot, but the plain extraction shown here does not remove files from the restore target, so a restore can keep a file that was deleted after the full backup. For a full restore of a changed tree, extract into an empty directory, as shown. Keep the snapshot file alongside the archives, because it is the only record of which files each run covered.
A second choice is the naming of archives. Put the date or a sequence number in each name, so the chain is visible. The tool does not need the names, but a person restoring a machine at night does.
Automating the chain
A backup you run by hand is a backup you eventually forget. A small script can run the same steps on a schedule and keep the snapshot file in one known place. The script should refuse to make an incremental when the full archive is missing, because an incremental without its base cannot be restored. It should also restore into a scratch directory and compare every time it runs, so a broken chain shows up on the day it breaks, not on the day you need it.
Choose a clear rule for when to start a new full backup, such as once a week or after a large change. Each new full archive gets a new snapshot file, so the old chain stays intact until you retire it. Keep at least one complete chain on a different disk or machine, because a backup on the same disk protects against mistakes but not against the failure of that disk.
Hands-on: build, change, and restore
Make the full and incremental archives from the steps above, then restore them into an empty directory and compare the result. Use the commands in order, and do not skip the comparison, because it is the part that proves the backup works.
tar --listed-incremental=snap.snar -cf full.tar notes
sleep 1
echo more >> notes/b.txt
tar --listed-incremental=snap.snar -cf incr.tar notes
mkdir -p check
tar -xf full.tar -C check
tar -xf incr.tar -C check
diff -r notes check/notes && echo verifiedIf the final line prints verified, the two archives together reproduce the notes tree. You have built the core of an incremental backup from the same tool you use to unpack a tarball.