Sub topic 10
Checking that the repository is healthy
A backup repository can become damaged without anyone noticing. A disk develops bad sectors, a transfer is cut off, or a pruning job is interrupted. You usually find out when you try to restore. This chapter covers the integrity checks that restic and Borg provide, what each level proves, and how to schedule them.
This chapter assumes you have already initialized a repository and created at least one backup. It also assumes the environment variables from earlier chapters (RESTIC_REPOSITORY, RESTIC_PASSWORD_FILE, BORG_REPO, BORG_PASSCOMMAND) are set.
What an integrity check can and cannot prove
An integrity check answers one question: is the data in the repository internally consistent? It can detect missing files, truncated files, corrupted data and broken references between snapshots and the data they point to.
It cannot tell you that:
- you backed up the right paths,
- the database dump inside the snapshot is usable,
- you still have the passphrase,
- the restore procedure works on a fresh machine.
Those questions are answered by restore tests and the recovery drill in a later chapter. Treat check as necessary but not sufficient.
Three levels of checking
Both tools offer a cheap structural check and an expensive full-data check. The cost difference is large, so most setups run the cheap one often and the expensive one rarely.
| Level | restic | Borg | What it reads |
|---|---|---|---|
| Structure | `restic check` | `borg check` | Indexes, snapshot and archive metadata |
| Partial data | `restic check --read-data-subset=...` | `borg check --verify-data --last N` | A slice of the stored data |
| Full data | `restic check --read-data` | `borg check --verify-data` | All stored data, hashed and verified |
Checking a restic repository
The basic check verifies the repository structure. It confirms that the index is consistent, that every snapshot's trees can be loaded, and that the pack files the index expects exist. It does not read the contents of every pack file.
restic checkOn success, restic prints no errors were found and exits with status 0. Any problem produces a non-zero exit status, which is what lets a script or systemd unit notice a failure.
To verify the data itself, add --read-data. restic then downloads and hashes every pack file. On a remote repository, that means transferring the entire repository over the network.
restic check --read-dataFor large repositories, check a portion each run. The --read-data-subset option accepts a fraction, a percentage or a size:
restic check --read-data-subset=1/10
restic check --read-data-subset=5%
restic check --read-data-subset=2GWith the fraction form n/t, restic divides the pack files into t groups and reads group n. If you run 1/10 on the first week, 2/10 on the second, and so on up to 10/10, you cover the whole repository in ten runs. A percentage or a size selects a random sample, so repeated runs may cover some data more than once.
restic repair options
If check reports errors, do not start repairing immediately. First make sure the problem is in the repository and not in your network or a stale cache. Rerun the check once. Then copy the repository if you can, because repair commands change it.
restic repair index
restic repair snapshotsrepair index rebuilds the index from the pack files that exist. repair snapshots rewrites snapshots that reference missing data so they no longer point at it. The affected files are lost from those snapshots, but the remaining files stay restorable. Neither command can recreate data that is gone from the repository. After a repair, run restic check again.
Checking a Borg repository
borg check has two parts. The repository part checks the low-level segment files. The archives part checks that each archive's metadata is consistent and that the chunks it references exist.
borg checkYou can run each part alone, which is useful when you want to split work across schedules:
borg check --repository-only
borg check --archives-onlyLike restic's default check, this does not decrypt and verify every data chunk. For that, use --verify-data. It reads and authenticates all stored data, so it is slow and, on a remote repository, transfers a lot of data.
borg check --verify-dataTo limit the cost, restrict the archives examined:
borg check --archives-only --last 3
borg check --archives-only --first 1
borg check --archives-only --match-archives 'myserver-*'The --match-archives option exists in Borg 1.2 and later. Older 1.x versions use --prefix or -a. Check borg check --help on your installed version.
Borg exit codes distinguish severity: 0 means success, 1 means warnings, and 2 means errors. A script that treats only 2 as failure will miss warnings, so decide deliberately which codes should alert you.
Borg repair
borg check --repair can fix some damage, but it is destructive by design. It removes references to missing chunks and can drop damaged archive data. Borg's own documentation warns to back up the repository first and to treat repair as a last resort. Never put --repair in an automated job.
# Run by hand only, after copying the repository:
borg check --repairChoosing a schedule
A common pattern balances cost against how quickly you want to notice damage:
| Frequency | Check | Purpose |
|---|---|---|
| After each backup | None, or rely on the backup tool's exit code | Backup completed without error |
| Weekly | Structure check | Catch missing or inconsistent metadata |
| Monthly | Rotating data subset | Catch silent corruption across the whole repository over time |
| Quarterly | Restore test | Prove that recovery works |
Remote storage changes the math. Reading all data from a cloud object store may cost egress fees and hours of transfer. For SFTP or SSH targets you can sometimes run the check on the storage host itself, close to the disks, if the tool is installed there. Borg's repository-only check is a natural fit for this.
Handling locks and failed runs
Both tools take locks while they operate. If a previous job was killed, a stale lock can make the next check fail with a message about a locked repository.
Before removing a lock, confirm that no other backup, prune or check is running against that repository from any host. Removing a lock under a live process risks damage.
restic list locks
restic unlockFor Borg, the equivalent is borg break-lock, with the same warning. Do not add either command unconditionally to your scripts, because that defeats the lock's purpose.
A scheduled check script
This script runs a structure check and one slice of a rotating data check. It chooses the slice from the week of the year, so a full cycle completes every four weeks. set -euo pipefail makes the script, and therefore the systemd unit, exit non-zero when a check fails.
#!/bin/bash
set -euo pipefail
export RESTIC_REPOSITORY=sftp:[email protected]:/srv/restic/myserver
export RESTIC_PASSWORD_FILE=/etc/restic/password
slice=$(( (10#$(date +%V) % 4) + 1 ))
restic check
restic check --read-data-subset="${slice}/4"Save it as /usr/local/bin/restic-check.sh, make it executable, and run it from a second systemd service and timer, built the same way as the backup timer from the scheduling chapter but with a weekly OnCalendar=weekly. Keep it separate from the backup job. A long data check should not delay tonight's backup, and a failing check should be reported on its own.
The 10# prefix forces base 10, because date +%V returns values such as 08 that bash would otherwise treat as invalid octal.
Common mistakes
- Treating a successful backup as proof of health. A backup writes new data. It does not re-read old data, so silent corruption in older snapshots goes unseen.
- Running only the structure check and assuming the data is verified. The structure check can pass while pack contents are damaged.
- Ignoring the exit status. If a script prints the result but never fails, no alert fires.
- Running
--repairautomatically. Repair changes the repository and can discard data. - Checking only from the machine that wrote the data. A local cache can hide problems. For restic, a check against the repository without a stale cache gives a more honest answer, and
restic cache --cleanupremoves old cache directories.
Exercise
Your restic repository holds 2 TB on a remote host that bills for outbound transfer. You want every pack file verified within a quarter without reading everything at once. Which command would you schedule, and how often?
Show answer
Run restic check --read-data-subset=n/13 weekly, where n is the week number within the quarter (1 through 13), so each run reads one thirteenth of the pack files and 13 runs cover everything. Also run a plain restic check weekly for the structure. A simpler rotation such as n/4 monthly also works, but each run then transfers a quarter of the repository.
Exercise
A Borg check exits with status 1. Should your alerting treat this as a failure, and what should you do next?
Show answer
Yes, treat it as something to investigate. Status 1 means warnings, not clean success. Read the output to see what was reported, rerun the check to rule out a transient problem, and run borg check --verify-data if the warning relates to data. Do not run --repair until you have copied the repository.
Exercise
Why can a repository pass restic check and still fail to restore a file?
Show answer
The default check verifies structure and consistency, not the contents of every pack file. A damaged pack can pass until you run restic check --read-data or a subset of it. Separately, the repository may be intact but lack the file because it was never included in the backup, which only a restore test reveals.