
- Authors

- Name
- Nadim Tuhin
- @nadimtuhin
On this page
My backup was supposed to leave the database out. It packed the whole thing, and I only noticed when I looked inside.
The script made a tar archive of an application folder before each release, skipping the local database directory and temporary state:
tar -czf backup.tar.gz --exclude='db/' app/
Then I checked what was inside:
tar -tf backup.tar.gz | grep '^app/db'
# app/db/
# app/db/data.sqlite
The trap was one character: the trailing slash in --exclude='db/'.
Why the slash breaks it
When tar walks the tree, each path it looks at arrives without a trailing slash. So when it visits app/db, it compares app/db to the pattern db/. The pattern wants a slash at the end, and the match fails.
Since app/db didn't match, tar goes into the directory. Next it checks app/db/data.sqlite. That path has characters after the slash, so it doesn't match db/ either. Net result: tar excludes nothing and bundles the whole database.
It worked on my Mac
This is GNU tar behavior (I checked 1.35). macOS bsdtar 3.5.3 does match db/, which is why the script looked fine on my Mac. You can see it in one test on GNU tar:
mkdir -p test/db && touch test/db/data.sqlite test/app.js
# Fails to exclude:
tar -cf - --exclude='db/' test | tar -tf -
# test/db/
# test/db/data.sqlite
# Correctly excludes:
tar -cf - --exclude='db' test | tar -tf -
# test/
# test/app.js
Drop the trailing slash and tar matches the directory name directly. It skips test/db straight away and never looks at its children. If you want to drop the contents but keep the empty directory, use db/* instead.
Takeaway
Never put a trailing slash on a directory in a tar exclude pattern. And test the backup on the machine that actually runs it.
Related posts
- TIL: piping into tail hides a failed command
A failed git push piped into tail still exited 0, and the script moved on. set -o pipefail makes the pipeline report the failure.
- My Hermes backup said DONE, but a tar exclude dropped every database
My agent backup script printed DONE and uploaded 346 MB. The first restore showed the databases were never in the archive. One tar exclude did it.
- TIL: 'No module named' after an update meant a stale process
My agent said httpx was missing. It was installed. The running process had started before the update and was still on the old environment.