Sub topic 3
How repositories, snapshots and deduplication work
This chapter explains what restic and Borg write to disk when you run a backup. It assumes you can use a Linux shell and SSH, and that you know what a hash is. Knowing the storage model helps you predict repository size, understand why pruning is a separate step, and see why losing a key is fatal.
The repository
A repository is a directory, or an object-storage prefix, that holds all backup data in a format only the backup tool can read. It is not a copy of your file tree. Everything in it is encrypted, so a repository on an untrusted server reveals little beyond approximate sizes.
Both tools use the same ideas:
- Files are split into chunks.
- Each chunk is identified by a hash and stored once.
- A snapshot (restic) or archive (Borg) is a small piece of metadata that lists which chunks rebuild which files.
The names differ. In this chapter, "snapshot" means restic's snapshot or Borg's archive unless the difference matters.
Content-defined chunking
Splitting files into fixed-size blocks would break deduplication. Inserting one byte near the start of a file shifts every later block, so every block hash changes.
Both tools use content-defined chunking instead. A rolling hash slides over the data, and a chunk boundary is declared wherever the hash matches a pattern. Boundaries therefore depend on the content, not on the offset. An insertion changes only the chunk or two around it, and the later chunks keep their hashes.
| Property | restic | Borg 1.x |
|---|---|---|
| Chunker | Rabin fingerprint | Buzhash |
| Typical chunk size | About 1 MiB average, 512 KiB to 8 MiB | About 2 MiB average, 512 KiB to 8 MiB |
| Chunk identity | SHA-256 of the plaintext chunk | Keyed hash (HMAC-SHA256, or BLAKE2b with `repokey-blake2`) |
| Chunker secret | Random polynomial stored in the repository config | Seed stored in the key |
The per-repository secret means an attacker cannot easily fingerprint your chunk boundaries. It also means two different repositories chunk the same data differently, so you cannot merge them or deduplicate across them.
What is on disk
restic groups encrypted chunks (called blobs) into pack files. It also writes tree blobs, which are directory listings, and index files that map blob hashes to packs.
config repository ID, version, chunker polynomial
keys/ encrypted master-key files, one per passphrase
data/xx/ pack files with data and tree blobs
index/ blob hash -> pack file, offset, length
snapshots/ one small file per snapshot
locks/ lock files for concurrent accessBorg 1.x appends encrypted objects to large segment files and keeps a repository index. It stores each archive's metadata as chunks in the same repository.
config repository settings; with repokey, also the key
data/0/ segment files holding chunks (up to about 500 MiB each)
index.N chunk ID -> segment and offset
hints.N bookkeeping, such as segment usage
integrity.N checksums of the index filesBorg also keeps a chunks cache on the client, under ~/.cache/borg. It lets the client skip unchanged files without asking the server. If you delete the cache, backups still work but the next run is slow while the cache rebuilds.
Snapshots are cheap full backups
Every snapshot behaves like a full backup. You restore from it without replaying a chain of increments. Deduplication makes this affordable, because a snapshot adds only the chunks that are new to the repository.
The tools also skip unchanged files. If a file's path, size and modification time match the previous run, the tool reuses its chunk list without rereading the file. This is why the second backup of a large tree is fast. It also means a file whose contents change without changing its mtime is missed.
You can watch deduplication happen with restic. This example uses a local repository and a random 50 MB file:
export RESTIC_REPOSITORY=/tmp/demo-repo
export RESTIC_PASSWORD=demo-only-password
mkdir -p /tmp/demo-data
head -c 50M /dev/urandom > /tmp/demo-data/big.bin
restic init
restic backup /tmp/demo-data
echo "small change" >> /tmp/demo-data/big.bin
restic backup /tmp/demo-data
restic snapshots
restic stats --mode restore-size
restic stats --mode raw-datarestore-size is about 100 MB, the sum of what two restores would write. raw-data is only slightly above 50 MB, because the second snapshot reused almost every chunk. The appended bytes changed only the final chunk.
Borg shows the same thing in its per-archive statistics:
export BORG_REPO=/tmp/demo-borg
export BORG_PASSPHRASE=demo-only-passphrase
borg init --encryption=repokey-blake2
borg create --stats ::first /tmp/demo-data
echo "another change" >> /tmp/demo-data/big.bin
borg create --stats ::second /tmp/demo-data
borg infoIn the second run's output, "Original size" is about 50 MB but "Deduplicated size" is only a few MB. borg info reports the total for the whole repository.
Encryption and compression
Both tools encrypt on the client before any data leaves the machine.
- restic uses AES-256 in counter mode with Poly1305-AES for authentication. Your passphrase unlocks a key file in
keys/through the scrypt key-derivation function. That key file holds a random master key, and the master key is what encrypts the data. - Borg uses AES-256-CTR with HMAC-SHA256, or BLAKE2b under
repokey-blake2. Withrepokeythe encrypted key lives inside the repository'sconfig. Withkeyfileit lives on the client under~/.config/borg/keys.
Compression happens before encryption, because encrypted data does not compress. restic compresses in repository format version 2 (restic 0.14 and later) and applies it automatically to new repositories. In Borg you choose it per backup, as in --compression zstd.
A passphrase is not the encryption key. It only unlocks the key. This is why restic can have several passphrases for one repository, and why changing a passphrase does not re-encrypt your data.
Deleting is two steps
Because many snapshots share chunks, deleting a snapshot cannot simply delete its chunks. Other snapshots may still reference them. Both tools therefore separate the work:
- Remove the snapshot metadata. In restic this is
forget. In Borg it isdeleteorprune. - Find chunks that no remaining snapshot references and free their space. In restic this is
prune. In Borg 1.2 and later it iscompact.
Until the second step runs, the repository does not shrink. Retention is covered in a later chapter. For now, remember that forget alone reclaims no space.
Failure modes
- Shared chunks. One damaged chunk can corrupt every snapshot that references it. Deduplication saves space but concentrates risk, so you need integrity checks and a second copy of the repository.
- Concurrent access. restic lets several hosts back up to one repository at once, using shared locks. A prune takes an exclusive lock. Borg 1.x takes an exclusive lock for most operations, so share a repository only if clients run one at a time.
- Stale locks. A killed backup can leave a lock behind. restic has
restic unlock, and Borg hasborg break-lock. Use them only when you are sure no other process is running. - Cache loss. For Borg, a lost or mismatched client cache triggers a slow resync. It does not lose data.
- Version differences. Borg 2.x changes commands (
rcreatereplacesinit) and the repository format, so Borg 1.x repositories are not directly compatible. The examples in this tutorial use Borg 1.x syntax.
Exercise
You back up a 40 GB directory to a restic repository. The next day, 200 MB of files change and you back up again. Roughly how much new data does the second snapshot add to the repository, and why can you still restore the whole 40 GB from it without the first snapshot?
Show answer
It adds roughly 200 MB, less after compression. Only the chunks that are new to the repository are written. The second snapshot's metadata lists the hash of every chunk it needs, both the reused ones and the new ones, so it is a complete description of the tree. The reused chunks stay in the repository as long as at least one snapshot references them.
Exercise
You run restic forget --keep-daily 7 and the repository size on disk does not change. Why, and what finishes the job?
Show answer
forget only removes snapshot metadata. The chunks those snapshots referenced are still stored, even if nothing references them anymore. Running restic prune, or adding --prune to forget, finds unreferenced data and frees the space. In Borg the equivalent second step is borg compact.