Sub topic 7
Deciding what to include and exclude
This chapter assumes you can already run a first backup with restic or Borg, as in the previous chapters. It covers how to decide what goes into the repository, how to write exclusion rules in each tool, and how to check that the rules do what you meant.
Why selection matters
Every file you back up costs storage, network time, and backup time. Every file you leave out is a file you cannot restore. Selection errors are quiet. A backup that skips /var/www still reports success.
A useful rule is to back up what you cannot recreate, and skip what the system rebuilds on its own. A server has four kinds of data:
- Configuration you wrote or edited, such as
/etc. - Data that users or applications produced, such as
/home,/var/www, and uploads. - State that is unsafe to copy while it is changing, such as live database files.
- Data that can be regenerated, such as caches, build output, package downloads, and temporary files.
The first two belong in the backup. The third needs special handling, covered in the database chapter. The fourth is the main candidate for exclusion.
Start with an explicit include list
Both tools back up only the paths you give them. The examples so far used /etc /home /var/www. This is an allow-list. A new directory such as /srv/uploads stays out of the backup until you add it.
The alternative is to back up / and exclude what you do not want. That protects you from forgetting a directory, but you must exclude pseudo-filesystems yourself.
| Approach | Strength | Risk |
|---|---|---|
| Include list (`/etc /home /var/www`) | Small, predictable, easy to review | New data directories are silently missed |
| Whole root (`/`) with excludes | Misses nothing by default | Must exclude `/proc`, `/sys`, `/dev`, `/run`, `/tmp`, and mounted network storage |
For most servers, use the include list and review it whenever you deploy a new service. If you back up /, add --one-file-system (-x in restic). It stops the tool from crossing into other mounted filesystems, which keeps out /proc, /sys, and NFS mounts. It also keeps out any separate partition you do want, such as a dedicated /home. In that case, list that mount point as an additional path.
Typical candidates
| Path | Usually | Reason |
|---|---|---|
| `/etc` | Include | Hand-written configuration, small |
| `/home` | Include, minus caches | User data |
| `/var/www`, `/srv` | Include | Site content and uploads |
| `/root` | Include | Scripts, SSH keys, shell history |
| `/var/lib/mysql`, `/var/lib/postgresql` | Exclude, dump instead | Live files are inconsistent when copied |
| `/var/lib/docker` | Exclude, back up volumes and compose files | Large, rebuildable images and layers |
| `/var/cache`, `/var/tmp`, `/tmp` | Exclude | Disposable |
| `/var/log` | Your call | Needed for audits, but rotates quickly |
| `/proc`, `/sys`, `/dev`, `/run` | Exclude | Not real files |
The package list is not a file on disk, but you need it to rebuild. Store it as a file inside a backed-up directory. On Debian or Ubuntu, apt-mark showmanual > /etc/manual-packages.txt does this. After a restore, it tells you what to reinstall.
The decision for a single path follows a short sequence:
Exclusions in restic
restic takes --exclude PATTERN on the command line and --exclude-file FILE for a list of patterns. The file has one pattern per line. Blank lines and lines starting with # are ignored.
# /etc/restic/excludes.txt
/home/*/.cache
/home/*/Downloads
/var/www/*/node_modules
/var/www/*/storage/logs
*.tmp
*.swpHow patterns match:
- A pattern starting with
/is anchored to the full path. - A pattern without a leading
/can match at any depth, so*.tmpskips every file ending in.tmp. *matches within one path component and does not cross/. Use**to cross directories, as in/var/www/**/cache.- Matching is case-sensitive.
--iexcludeand--iexclude-fileignore case.
restic has three more exclusion switches:
--exclude-cachesskips any directory that contains a validCACHEDIR.TAGfile. Many tools write one, including pip and Cargo.--exclude-if-present NAMEskips a directory that contains a file calledNAME. Create.nobackupin a directory to drop it from the backup without editing any central list.--exclude-larger-than 1Gskips individual files above a size. Use it as a safety net, not a policy, because it silently drops large files you may need.
The call matches the script from the automation chapter:
restic backup \
--exclude-file=/etc/restic/excludes.txt \
--exclude-caches \
--exclude-if-present .nobackup \
/etc /home /var/wwwExclusions in Borg
Borg takes --exclude (-e) and --exclude-from FILE. By default patterns use fnmatch style. Prefix a pattern with sh: for shell-style globbing, where * stays within one directory and **/ crosses directories.
borg create --stats --compression zstd \
--exclude 'sh:/home/*/.cache' \
--exclude 'sh:/home/*/Downloads' \
--exclude 'sh:/var/www/*/node_modules' \
--exclude-caches \
--exclude-if-present .nobackup \
'::{hostname}-{now:%Y-%m-%dT%H:%M:%S}' /etc /home /var/wwwBorg also offers --keep-exclude-tags. With it, a directory skipped because of .nobackup or CACHEDIR.TAG still has the tag file stored. A restore then recreates the marker, so the directory stays excluded if you back it up again.
For longer rule sets, use a patterns file with --patterns-from. Each line starts with a command: R names a root to back up, - excludes, + includes, and P sets the default pattern style. Borg uses the first matching rule, so put exceptions before the broader exclusion.
# /etc/borg/patterns.txt
P sh
R /etc
R /home
R /var/www
+ /home/*/.cache/important-app
- /home/*/.cache
- /home/*/Downloads
- /var/www/*/node_modulesRun it with borg create --stats '::{hostname}-{now:%Y-%m-%dT%H:%M:%S}' --patterns-from /etc/borg/patterns.txt. You give no path arguments because the R lines supply the roots.
The same ordering rule helps in restic. A ! prefix in an exclude file re-includes paths that a broader earlier pattern excluded. Patterns later in the file override earlier ones, so place ! lines after the pattern they relax. Check the result with a dry run before relying on it.
Verify the rules with a dry run
Never trust an exclusion list you have not tested. A pattern that is too broad drops data. A pattern that is too narrow wastes space. Both succeed without an error.
For restic, a dry run with -vv prints each file it would process. Pipe the output through grep for things that should be absent:
restic backup --dry-run -vv \
--exclude-file=/etc/restic/excludes.txt --exclude-caches \
/etc /home /var/www | grep -E '/(\.cache|node_modules)/' \
|| echo "no excluded paths appeared"For Borg, --dry-run --list prints a status letter per path. The letter A marks a file that would be added, M one modified, and x one excluded. Add --filter=x to see only the excluded paths:
borg create --dry-run --list --filter=x \
--exclude 'sh:/home/*/.cache' \
'::test' /etc /home /var/wwwAfter a real backup, compare sizes. In restic, restic stats latest shows the raw size of the snapshot. In Borg, the --stats output shows the original size of the archive. If it jumped by gigabytes since yesterday, a new directory entered the backup.
Common errors
- Anchoring mistakes.
cachematches every file or directory calledcachein the tree, including a source directory you wanted./var/cachematches only that path. - Trailing slash. Pattern behavior around directory slashes differs between tools and versions. Write patterns without a trailing slash and test them.
- Quoting. Unquoted globs in a shell command are expanded by the shell before the tool sees them. Always put the pattern in single quotes.
- Relative paths. If you pass
.or a relative path, patterns anchored with/will not match. Use absolute paths in scheduled jobs. - Excluded root-owned secrets. Do not exclude
/etc/ssl/privateor/root/.sshto save space. The repository is encrypted, and losing those keys makes recovery much harder.
Exercises
Exercise
Your server stores site files in /var/www, and each site has a node_modules directory that npm can rebuild. Write a restic exclude rule that skips them at any site, and say how you would confirm the rule works without creating a snapshot.
Show answer
Add /var/www/*/node_modules to the exclude file (or **/node_modules to match at any depth). Then run restic backup --dry-run -vv --exclude-file=/etc/restic/excludes.txt /var/www and search the output for node_modules with grep. No matches means the rule is working. The dry run writes nothing to the repository.
Exercise
A Borg patterns file contains - /home/*/.cache followed by + /home/*/.cache/important-app. The important-app directory is missing from the archive. What is wrong, and how do you fix it?
Show answer
Borg uses the first matching rule, so the exclusion on the first line matches before the include is reached. Move the + line above the - line. Then verify with borg create --dry-run --list --filter=x that the directory is no longer listed as excluded.