brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: tar -g keeps a snapshot file so the next backup only holds what changed

Backup
~/tips/tip-tar-listed-incremental-snapshot

tar -czf /backup/docs-0.tar.gz -g /backup/docs.snar /home/user/docs

Makes tar backups that only contain the files that changed since the previous run.

A full tar backup every night is simple, and it also copies the same unchanged files every single night. GNU tar can do incremental backups by itself, with no extra tool, as long as you give it a place to remember what it saw last time.

That place is a snapshot file, passed with -g (long form --listed-incremental):

tar -czf /backup/docs-0.tar.gz -g /backup/docs.snar /home/user/docs

The first time, docs.snar does not exist yet, so tar creates it and puts everything in the archive. That is the level 0, the full backup. Run the same line again later with a new archive name, say docs-1.tar.gz, and tar compares against the snapshot, only adds what changed and updates the snapshot file for the next run.

To restore, extract the archives in the order you made them. The man page says tar does not actually read the snapshot when extracting, it only needs the option, so /dev/null is the usual stand-in:

tar -xzf /backup/docs-0.tar.gz -g /dev/null -C /restore
tar -xzf /backup/docs-1.tar.gz -g /dev/null -C /restore

The caveat is the snapshot file. Because tar rewrites it on every run, each new archive only holds the changes since the run before, and you need the whole chain to restore. Lose the .snar and the next run is a full backup again. If you would rather have each incremental build on the same full backup, keep a copy of the level 0 snapshot and hand tar a fresh copy of it every time, which is what the man page means by using the snapshot of level N-1.

Further reading: tar(1) manual page

More Backup tips restic snapshots --group-by host,paths mysqldump --routines --events mydb > mydb.sql rsync -a --files-from=list.txt /home/me/ backup:/backup/ restic find "*.txt" restic stats All Backup tips (45) All topics

Link