Sub topic 9
Retention policies and reclaiming space
This chapter assumes you have a working restic or Borg repository with several snapshots in it. You should also be comfortable running the commands from the earlier chapters with the repository environment variables set (RESTIC_REPOSITORY and RESTIC_PASSWORD_FILE, or BORG_REPO and BORG_PASSCOMMAND). Borg examples assume Borg 1.2 or later.
A daily backup never stops growing. Without a plan, the repository fills the backup disk. A retention policy states which snapshots to keep. Reclaiming space is the separate step that frees the data only deleted snapshots used.
Two steps: remove the label, then free the data
Deduplicated repositories store file content as shared chunks. Many snapshots point at the same chunks. Deleting a snapshot therefore removes only its list of pointers. The chunks stay until no remaining snapshot refers to them, and a second operation must find and delete those orphaned chunks.
| Step | restic | Borg 1.2+ |
|---|---|---|
| Choose and delete snapshots by policy | `restic forget --keep-...` | `borg prune --keep-...` |
| Free unreferenced data | `restic prune` (or `forget --prune`) | `borg compact` |
| Check structure afterwards | `restic check` | `borg check` |
How keep rules work
Both tools use the same family of rules. You list the rules you want, and a snapshot survives if any rule selects it.
--keep-last Nkeeps the N most recent snapshots.--keep-daily Nkeeps the last snapshot of each of the N most recent days that have at least one snapshot.--keep-weekly Nand--keep-monthly Ndo the same per week and per month.--keep-yearly Ndoes the same per year.--keep-within DURATIONkeeps everything newer than the duration, such as14din Borg or14dand2y5min restic.
Two details matter in practice.
First, the rules count periods that contain snapshots, not calendar periods. If the server was off for a week, --keep-daily 7 reaches back further than seven calendar days. The backups you have are not thinned out just because a gap exists.
Second, the rules overlap. With --keep-daily 7 --keep-weekly 4 --keep-monthly 6, the newest snapshot of this week satisfies both the daily and the weekly rule. The policy keeps at most 17 snapshots, and usually fewer. Pick numbers by working backwards from how far back you need to restore. Daily for a week catches mistakes you notice quickly. Monthly for six months catches slow problems such as a corrupted file nobody opened.
Always preview first
A wrong retention rule destroys backups permanently. Both tools can show the result without changing anything.
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --dry-runborg prune --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --list --dry-runRestic prints a table of snapshots marked "keep" and "remove" with the reasons. Borg's --list prints each archive with a "Keeping" or "Pruning" label and the rule that applied. Read the output and check that the newest snapshot and a known-good older one are on the keep side before you run the real command.
Running retention with restic
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
restic checkforget deletes the snapshot records. --prune then runs the data cleanup. You can also run them separately, for example forget after every backup and prune once a week, because pruning is the slow and heavy part.
Prune works on pack files, the larger files restic stores chunks in. If a pack holds a mix of used and unused chunks, restic rewrites it with only the used ones. Several consequences follow:
- Prune takes an exclusive lock. Backups that start during a prune have to wait or fail.
- Prune reads and writes remote data. Over a slow link or with a cloud backend, it can take a long time and, with object storage, may incur request or transfer charges.
- Restic leaves packs with a small amount of waste alone. The
--max-unusedoption (default 5%) sets how much unused data it tolerates. Raising it makes prune faster and leaves more wasted space.
Grouping
By default, restic applies the policy separately to each group of snapshots that share the same host and the same set of paths. This keeps a policy from deleting one machine's backups because another machine made newer ones. It also means that if you change which paths you back up, the old snapshots form a separate group with its own count. You can change the grouping with --group-by, for example --group-by host, or restrict the command to one host with --host myserver.
Running retention with Borg
borg prune --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --list
borg compact
borg checkIn Borg 1.2 and later, prune only marks archives as deleted. The disk space returns when borg compact rewrites the repository segments. Older Borg 1.1 freed space inside prune itself, and borg compact does not exist there. Check borg --version if the command is missing. compact skips segments with little to reclaim, controlled by --threshold (default 10%).
Scope prune to one series of archives
Borg treats every archive in a repository as part of one series unless you restrict it. If several servers, or several jobs from one server, share a repository, an unrestricted borg prune applies the policy across all of them. A job that makes archives for /var/www can then push out the archives of /etc. Use the archive name prefix from the earlier naming scheme:
borg prune --glob-archives '{hostname}-*' --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --listHere --glob-archives (short form -a) matches only archives whose names begin with the host name. Borg 1.1 used --prefix for the same job. Giving each server its own repository avoids this problem entirely and also keeps the encryption keys separate.
Where to run retention
Retention deletes data, so who is allowed to run it is a security question. If the server being backed up can run forget --prune or borg prune, an attacker who takes over that server can also delete every backup. Two approaches reduce this risk:
- Use append-only access for the backup client: restic's
rest-server --append-onlyor Borg'sborg serve --append-onlyon the storage side. The client can add data but cannot permanently remove it. - Run retention from a separate, trusted machine that has the repository credentials, on its own schedule.
With append-only mode, deletions requested by a client are recorded rather than applied immediately, so you need that trusted machine to do the real cleanup. The trade-off is one more system to maintain.
Common errors
- Repository is locked. A backup, prune, or crashed earlier run holds a lock. Wait for the other process first. Only after confirming nothing is running, use
restic unlockorborg break-lock. - Disk still full after pruning with Borg. You forgot
borg compact, or the threshold left small segments alone. - Policy removed more than expected. Archives from different jobs shared a repository without a name filter, or restic's group changed after you altered the backup paths.
- Prune takes hours. The repository is large or remote. Run it less often, for example weekly, and keep
forgetfrequent. - Check fails after prune. Treat this as a real incident. Do not prune again until you understand it, and consider
restic check --read-dataorborg check --verify-dataonce you have time for the full read.
Putting it together
The script from the automation chapter already runs restic forget ... --prune after each backup. That works for a small repository. For a large one, consider this split, with backups daily and cleanup weekly:
#!/bin/bash
set -euo pipefail
export RESTIC_REPOSITORY=sftp:[email protected]:/srv/restic/myserver
export RESTIC_PASSWORD_FILE=/etc/restic/password
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
restic checkSave it as /usr/local/bin/restic-retention.sh, make it executable, and give it its own timer with OnCalendar=weekly. Running restic check straight after prune confirms the cleanup left a consistent repository.
Exercise
A Borg 1.2 repository holds archives named web1-<timestamp> and db1-<timestamp>, both created daily. You run borg prune --keep-daily 3 --list and then notice the disk usage did not change. Give two separate problems with this command and write the corrected commands for the web1 archives.
Show answer
First, the command has no name filter, so it applies the policy to both web1 and db1 archives as one series, which can delete archives of the wrong job. Second, prune in Borg 1.2 only marks archives as deleted, so space is not freed until borg compact runs. Corrected commands: borg prune --glob-archives 'web1-*' --keep-daily 3 --list --dry-run to preview, then borg prune --glob-archives 'web1-*' --keep-daily 3 --list followed by borg compact. Repeat with 'db1-*' for the other series.