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 --exclude-caches skips every folder marked with a CACHEDIR.TAG

Tar
~/tips/tip-tar-exclude-caches-tag

printf 'Signature: 8a477f597d28d172789f06886806bc55' > /mnt/user/appdata/app/cache/CACHEDIR.TAG

Leaves cache folders out of a tar backup by marking them with one small file.

Backups of an appdata folder tend to drag a lot of dead weight along: thumbnails, transcoding leftovers, metadata the app downloads again anyway. Listing all of those with –exclude gets old fast, and the list breaks the day an app renames a folder.

There is an older and simpler convention for this: a file called CACHEDIR.TAG inside a cache folder. The idea is that apps tag their own cache folders, and you can also mark any folder yourself:

printf 'Signature: 8a477f597d28d172789f06886806bc55' > /mnt/user/appdata/app/cache/CACHEDIR.TAG

GNU tar then skips the contents of every marked folder when you ask it to:

tar -czf app-backup.tar.gz --exclude-caches /mnt/user/appdata/app

With --exclude-caches the tag file itself is kept, so after a restore the empty cache folder is still marked. --exclude-caches-all leaves out the folder and the tag completely, and --exclude-caches-under drops everything below it but keeps the folder. I like the first one, because the mark survives the restore.

The caveat is that tar is strict about the tag. It has to be a regular file that begins with exactly those 43 characters, the word Signature, a colon, a space and that hex string. A typo does not cause an error: tar just backs up the folder as if the tag were not there. So after adding a tag, I would check the result with tar -tzf app-backup.tar.gz | grep cache once.

Further reading: tar(1) manual page, Cache Directory Tagging Specification

More Tar tips tar -xzf app-2.4.1.tar.gz -C /mnt/user/appdata/app --strip-components=1 All Tar tips (2) All topics

Link