Skip to content
Go back

Your Borg Target: ZFS + Restricted Keys

By KingPin 15 min read
Your Borg Target: ZFS + Restricted Keys
Contents

Your Off-Site Backup Works Fine Until You Need 400 GB Back

You already have an off-site Borg repo. It is encrypted, it is deduplicated, and borgmatic pushes to it every night. Then a disk dies on a Tuesday, and you discover that restoring from off-site means pulling the whole thing through your home download link, if the provider is up at all.

The fix is a second Borg target on a box you own, on the same LAN as your clients. This post builds one on ZFS with three cheap layers: restricted borg serve keys, append-only mode, and ZFS snapshots under each repo. A compromised client can then write new backups but cannot take the old ones down with it. The setup comes from a converted 4-bay NAS (Debian 13, ZFS 2.3, Borg 1.4) that backs up four machines, which I will call web-1, pi-1, desk-1 and lab-1.

If you need the basics first, the borgmatic post covers installing and scheduling, and the 3-2-1-1-0 post covers why you want more than one copy. This one covers the server side nobody writes about.

Why a Second Target When You Already Have One

Three reasons, in order of how often they bite you.

  1. Restore speed. A LAN restore runs at disk and network speed. An off-site restore runs at whatever your ISP gives you, and you will do it while annoyed.
  2. Provider outages and surprises. Accounts get suspended, billing cards expire, and providers have bad days. One repo at one provider is a single copy that happens to be far away.
  3. The rule of copies. Two Borg repos in two places, one of them off-site, is the cheapest way to get from “a backup” to 3-2-1.

The local copy is the one you restore from. The off-site copy is the one that survives the fire.

The Layout: One User, One Dataset Per Host

The server needs one system user and one ZFS dataset per client. Borg compresses on the client before it encrypts, so the data arrives incompressible and ZFS compression gains little. The live box has it off. Leaving the default lz4 on costs almost nothing either. A 1 MiB recordsize suits Borg’s large segment files.

setup-borg-target.sh
# One system user. A minimal shell reduces attack surface (Borg docs advice).
sudo useradd --system --home-dir /var/lib/borg \
--create-home --shell /bin/sh borg
# Parent dataset: children inherit these properties.
sudo zfs create -o compression=off -o recordsize=1M tank/borg
# One child per client, owned by the borg user.
for host in web-1 pi-1 desk-1 lab-1; do
sudo zfs create -o quota=100G tank/borg/$host
sudo chown borg:borg /tank/borg/$host
done
zfs list -r -o name,used,quota,compression,recordsize tank/borg

Per-host datasets buy you three things:

One honest to-do: the quota= option above is my recommendation. The live box has no quotas yet, so right now one misbehaving client could eat the pool. Add them.

Borg also has its own limit. borg serve --storage-quota 100G caps a repo from the server side, and it applies to newly initialized repos too. The ZFS quota is the layer the client cannot negotiate with, so I prefer it.

authorized_keys: The Key Only Runs borg serve

Each client gets its own SSH key, and each key is pinned to one command and one directory. This is the live file on the box, keys elided:

/var/lib/borg/.ssh/authorized_keys
command="borg serve --restrict-to-path /tank/borg/lab-1 --append-only",restrict ssh-ed25519 AAAA... lab-1
command="borg serve --restrict-to-path /tank/borg/web-1",restrict ssh-ed25519 AAAA... web-1
command="borg serve --restrict-to-path /tank/borg/pi-1",restrict ssh-ed25519 AAAA... pi-1
command="borg serve --restrict-to-path /tank/borg/desk-1",restrict ssh-ed25519 AAAA... desk-1

Each part earns its place:

There are two flavors of path restriction in Borg 1.4. --restrict-to-path allows a directory and its subdirectories, so one key can reach several repos under /tank/borg/web-1. --restrict-to-repository allows exactly one repo path, no subdirectories. If each client has a single repo, prefer --restrict-to-repository /tank/borg/web-1/main. It is the tighter fence. The live box uses the path form because a host directory may hold more than one repo later.

Add keepalives so a dropped connection does not leave a stale lock. The Borg docs recommend this on both ends:

client ~/.ssh/config, then server sshd_config
# client
Host nas
ServerAliveInterval 10
ServerAliveCountMax 30
# server
ClientAliveInterval 10
ClientAliveCountMax 30

Append-Only: Useful, but Not What You Think

The --append-only flag in that forced command is the second layer. Read what it does before you rely on it, because the Borg docs are blunt about it.

Append-only means Borg never overwrites or deletes committed segment data. It does not block borg delete or borg prune. Those commands still succeed from the client’s point of view. They write their changes as new transactions, and the old data stays in the segment files until someone runs borg compact. Under an append-only key, borg compact runs without any error or warning and does nothing.

An attacker with that key can therefore mark every archive as deleted, but the data is still there. Borg keeps a transactions file in append-only mode, one line per transaction with a UTC timestamp. You find the last good transaction and roll the repo back: remove the current index.N, hints.N and integrity.N files, then every segment file in data/ numbered above the last good transaction ID. The ID names the last file of a transaction, so if transaction 5 is the last good one, you delete from segment 6 up. Take a hard-link copy first (cp -al) so a typo does not make things worse.

What append-only does not do:

My pruning pattern uses two keys per host. The nightly key is append-only. A second key has the same path restriction but no --append-only, and it lives only on a trusted machine that is not the client:

authorized_keys (two keys, one repo)
command="borg serve --restrict-to-path /tank/borg/web-1 --append-only",restrict ssh-ed25519 AAAA... web-1-backup
command="borg serve --restrict-to-path /tank/borg/web-1",restrict ssh-ed25519 AAAA... web-1-admin

Once a week, from the trusted machine, look at the archive list before you compact:

Terminal window
borg list ssh://borg@nas/tank/borg/web-1/main
borg prune --keep-daily 14 --keep-weekly 8 --keep-monthly 6 \
ssh://borg@nas/tank/borg/web-1/main
borg compact ssh://borg@nas/tank/borg/web-1/main

That borg list is not decoration. If an attacker pruned everything and you compact blindly, you have made the deletion permanent. Pruning on the NAS itself also works, but then the passphrase lives on the NAS, and you lose the clean property that the server never holds it.

The live box has exactly one of four clients in append-only mode today (lab-1). The other three are not. My recommendation, and my to-do list, is all four. A repo that a compromised client can silently prune is a repo that depends on the third layer.

ZFS Snapshots: The Layer the Client Cannot Reach

Append-only lives inside Borg’s own rules, so it only protects you as far as those rules go. A snapshot below the repo does not depend on Borg at all. The client key can only talk to borg serve. It cannot run zfs destroy, and snapshots are read-only. A --restrict-to-path key can still name a path under .zfs/snapshot and read it, but it cannot change or remove anything there. --restrict-to-repository closes even that.

sanoid takes the snapshots. This is the live /etc/sanoid/sanoid.conf section for the Borg tree, plus the template:

/etc/sanoid/sanoid.conf
[tank/borg]
use_template = borg
recursive = yes
process_children_only = yes
[template_borg]
hourly = 0
daily = 14
weekly = 4
monthly = 0
autosnap = yes
autoprune = yes

recursive = yes with process_children_only = yes snapshots each tank/borg/<host> dataset and skips tank/borg itself, so new hosts are covered the moment you create their dataset. Fourteen dailies and four weeklies cost little on data that is mostly immutable segment files. The Debian sanoid timer runs every 15 minutes. If you need sanoid itself, the sanoid and syncoid post covers install and retention.

Restoring a repo from a snapshot has two routes:

Terminal window
# 1. Read-only look: every snapshot is browsable under .zfs/snapshot
ls /tank/borg/web-1/.zfs/snapshot/
# 2. Roll the whole dataset back (the newest snapshot only, without -r)
sudo zfs rollback tank/borg/web-1@autosnap_2026-11-22_00:00:02_daily

Rolling back to anything older than the newest snapshot needs -r, which destroys the snapshots in between. If you are not sure, clone the snapshot instead (zfs clone) or copy the repo out of .zfs/snapshot, and keep the original.

Two cautions before you roll back a live repo:

  1. Stop the writers. Comment out the client’s line in authorized_keys and check that no borg serve process is still running. Rolling a dataset back under an active backup leaves the repo in a state you did not plan for.
  2. Expect a warning on the client. After a rollback, the client’s local cache is newer than the repo. Borg 1.4 says the cache or security directory “is newer than repository” and calls it an attack or an unsafe state. That is correct. The Borg FAQ explains how to accept it once you know why it happened.

After the rollback, run borg check --repository-only on the NAS (it needs no passphrase), or a full borg check through the admin key, before you re-enable the client key.

Seeding the Repo and Wiring It Into borgmatic

Seed each repo from the client, against the absolute path on the NAS. Use repokey-blake2 on Borg 1.4. It uses BLAKE2b hashing, which the Borg docs say is likely faster than the plain repokey mode on CPUs without hardware SHA256:

Terminal window
borg init --encryption=repokey-blake2 \
ssh://borg@nas/tank/borg/web-1/main
# The key is stored inside the repo. Export a copy and store it elsewhere.
borg key export ssh://borg@nas/tank/borg/web-1/main /root/web-1-main.key

The passphrase and that exported key need their own backup, off the NAS and off the client. A repokey repo you cannot unlock is just an expensive hash function.

Then add the NAS as a second repository in the borgmatic config you already have. The repositories: option takes a list, and each entry has a path: and an optional label::

/etc/borgmatic/config.yaml
repositories:
- path: ssh://offsite.example/./web-1.borg
label: offsite
- path: ssh://borg@nas/tank/borg/web-1/main
label: nas

borgmatic runs each action against every listed repository, so one borgmatic create feeds both. Restore from the fast one with borgmatic extract --repository nas --archive latest. A first test backup on each host proves the key line, the path restriction and the dataset permissions all agree.

Freshness Monitoring Without the Passphrase

A backup you never check is a rumor. The tricky bit is checking a repo that the server cannot read. borg list needs the passphrase, and I do not want the passphrase on the NAS.

The server can still see files. Borg commits a new index.N file with each transaction, so the newest index.* mtime in a repo is the time of the last committed write. This is the script, run as root:

/usr/local/sbin/borg-metrics.sh
#!/bin/sh
# Writes borg repo freshness/size metrics for node-exporter's textfile collector.
# Freshness = newest index.* mtime in each repo (updated on every committed backup).
out=/var/lib/prometheus/node-exporter/borg.prom
tmp=$out.$$
# Each metric stays in one group, as the exposition format requires.
{
echo '# TYPE borg_repo_last_write_timestamp_seconds gauge'
for r in /tank/borg/*/*/; do
host=$(basename "$(dirname "$r")"); repo=$(basename "$r")
ts=$(find "$r" -maxdepth 1 -name 'index.*' -printf '%T@\n' 2>/dev/null | sort -n | tail -1 | cut -d. -f1)
[ -n "$ts" ] && echo "borg_repo_last_write_timestamp_seconds{host=\"$host\",repo=\"$repo\"} $ts"
done
echo '# TYPE borg_repo_size_bytes gauge'
for r in /tank/borg/*/*/; do
host=$(basename "$(dirname "$r")"); repo=$(basename "$r")
size=$(du -sb "$r" 2>/dev/null | cut -f1)
[ -n "$size" ] && echo "borg_repo_size_bytes{host=\"$host\",repo=\"$repo\"} $size"
done
true # an empty glob must not skip the mv
} > $tmp && mv $tmp $out

The write to a temp file followed by mv matters. node-exporter should never read a half-written .prom file. One line in /etc/cron.d runs it every five minutes:

/etc/cron.d/borg-metrics
*/5 * * * * root /usr/local/sbin/borg-metrics.sh

The alert rule fires when a repo has not seen a write in a day and a half. With nightly backups, a late or slow run stays quiet, and one missed night pages you about 12 hours after the missed run:

borg-alerts.yml
groups:
- name: borg
rules:
- alert: BorgRepoStale
expr: time() - borg_repo_last_write_timestamp_seconds > 36 * 3600
for: 15m
labels:
severity: warning
annotations:
summary: "No Borg write to {{ $labels.host }}/{{ $labels.repo }} in 36h"

For Grafana, a table or stat panel on time() - borg_repo_last_write_timestamp_seconds with unit “seconds” and amber and red thresholds at 24 and 36 hours shows every host at a glance. A bar gauge on borg_repo_size_bytes next to it shows who is growing.

The limit of this signal is easy to state. An index.N mtime proves that a transaction committed. It does not prove the archive inside is restorable. A hostile or confused client running borg prune also commits a transaction, so the repo looks fresh while its contents shrink. Pair the freshness alert with a periodic borg check from a machine that holds the passphrase, and with a test restore now and then.

Why Not BorgWarehouse

BorgWarehouse is a web UI for a central Borg server. Its README says it handles the system layer for you (user, SSH access, quota), and lists per-repository quotas, append-only mode, “no recent backup” alerts, and notifications through email, Apprise and webhooks. It also says it never asks for or stores your repository passphrase. If you are starting fresh and want clicks instead of config files, that is a good tool, and you should look at it.

I skipped it for a narrow reason. It manages the borg user’s SSH access and its own repository layout, and I wanted hand-written authorized_keys lines, per-host ZFS datasets, and sanoid snapshots per dataset. Two tools writing the same file will eventually disagree. The README also lists per-repo external storage, which may let it live on a ZFS dataset you create yourself, but I did not test that, so treat it as a question for their docs.

Where Borg 2 Stands

Borg 2.0 is still in beta. As of late November 2026, the GitHub releases page lists 2.0.0b25 (September 27, 2026) flagged as a pre-release, while the stable line is 1.4.x (1.4.5 from July 2026, with a 1.4.6rc1 out in November). Everything above targets 1.4. Revisit when 2.0 goes stable, because the repository format and several commands change.

Common Questions

Does the NAS need the Borg repository passphrase?

No. The borg serve process only moves encrypted blocks and never decrypts them. The freshness script reads file timestamps and sizes, so it needs no passphrase either. Only commands that read archive contents, such as borg list, prune and check, need it, and those run on a trusted client.

Is Borg 2 ready to use for a new backup target?

No. As of November 2026 the newest Borg 2 release is 2.0.0b25, marked as a pre-release on GitHub, while 1.4.5 is the stable release. A new target today should use Borg 1.4. Borg 2 uses a different repository format from 1.x, so moving later means a deliberate transfer, not a version bump on the same repo. Plan the new target around 1.4 and treat 2.0 as a future migration.

How do I test a restore from the local Borg target?

Run mkdir -p /tmp/restore-test, then borgmatic extract --repository nas --archive latest --path etc/hostname --destination /tmp/restore-test, and compare /tmp/restore-test/etc/hostname with the original. The --repository flag selects the NAS by its label, so the test does not touch the off-site repo. Repeat it monthly and also run borg check from a machine that holds the passphrase. A timestamp alert shows that a write happened. Only an extract proves that the data is readable.

Should ZFS compression stay on for a Borg dataset?

It barely matters. Borg encrypts on the client, so ZFS stores the blocks uncompressed either way. OpenZFS says lz4 on incompressible data is unlikely to be a bottleneck, so the default lz4 is fine.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Next Post
Backing Up a Live Game Server Safely

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts