15.5. Protecting Backups from a Compromised Client

Every copy made so far is reachable with credentials that live on client1. Consider what an attacker who owns client1 - and therefore owns its backup schedule, its keys, and its passphrase - can do: the writer key authenticates to the repository account, so whatever the backup tool permits, the attacker can do, including destroying history. This section measures that reach, then builds two boundaries the compromised client cannot cross: a vault account with its own key and path, and an offline disk with no network path at all. The design goal is exact: losing client1 entirely may cost the RPO window, never the retention window.

15.5.1. Append-Only or Deletion-Restricted Access and Tool-Specific Limits

The writer account in Section 15.4.1 was built with Borg's --append-only server option. What it does and does not promise is stated by the tool itself - on client1, as root:

# borg serve --help | grep -A3 -- "--append-only"
                  [--restrict-to-repository PATH] [--append-only]
                  [--storage-quota QUOTA]

Start in server mode. This command is usually not used manually.
--
  --append-only         only allow appending to repository segment files. Note
                        that this only affects the low level structure of the
                        repository, and running `delete` or `prune` will still
                        be allowed. See :ref:`append_only_mode` in Additional

The pipeline selects the option's own description from the help text. --append-only stops a client from rewriting the repository's segment files - it preserves the bytes already written, which is a real property: a vandal cannot overwrite old chunks with garbage. It does not stop borg delete or borg prune, which mark archives deleted and free their segments through the repository's own normal mechanisms. Section 15.5.4 demonstrates exactly that with exactly the writer key, and the live listing afterwards shows the archive gone.

That limit is not a defect to work around in the same account; it is the signal that deletion authority must live somewhere the writer key cannot reach. Three placements exist, in increasing strength: a second account with its own key and path on the same server (Section 15.5.2), storage the server cannot write at all while detached (Section 15.5.3), and off-site media (Section 15.1.3). The same reasoning applies to any backup tool: restic's REST server has an append-only mode whose guarantees are the tool's to state, and a plain SFTP writer is bounded only by filesystem permissions - in every case, the question to answer is not "can the backup account write?" but "what can the backup account destroy, and who else could destroy it?"

15.5.2. Separating Backup Writers from Retention and Deletion Authority

The vault is a second copy of the repository under a second account, reachable only with the retention key that was generated alongside the writer key in Section 15.4.1. The writer account can create archives in its own path; the vault account can read and serve its own path; neither account's key opens the other's world. In production the retention key lives on a separate administrative machine, which also removes the compromised-client failure domain from key custody entirely; here it stays on client1 so one machine can exercise both roles.

Build the vault account. On backup1, as root:

# useradd -m -s /bin/bash borgadmin
# install -d -m 700 -o borgadmin -g borgadmin /home/borgadmin/.ssh /var/lib/borgadmin /var/lib/borgadmin/vault

Both commands are silent. Install the retention key's public half as the account's only authorized key - the contents of /home/borgadmin/.ssh/authorized_keys on backup1:

command="borg serve --restrict-to-path /var/lib/borgadmin/vault",restrict ssh-ed25519 <retention public key>

The placeholder stands for the entire one-line contents of /root/.ssh/id_ed25519_admin.pub copied from client1, just as the writer line in Section 15.4.1 carries the writer key's public half. Note what the forced command lacks: no --append-only, because this account's purpose is exactly the authority to prune the vault's own retention; and no --restrict-to-path pointing at the writer's repository, so the retention key cannot be used against the live repository path either. Fix ownership and mode:

# chown -R borgadmin:borgadmin /home/borgadmin/.ssh && chmod 700 /home/borgadmin/.ssh && chmod 600 /home/borgadmin/.ssh/authorized_keys

The compound is silent.

The writer key reaches only the repository; the retention key reaches the vault; the offline disk sits behind no network path.

Figure 15.4. Two authorities and one detached copy. Conceptual schematic of the accounts, keys, and paths built in this chapter.

Copy the repository into the vault. Still on backup1, as root, with no backup running - the same quiescence rule as Section 15.2.2, because Borg 1.4 has no server-side transfer command and a repository copied mid-write can be inconsistent:

# mkdir -p /var/lib/borgadmin/vault/client1 && rsync -aHAX --numeric-ids /var/lib/borgsvc/repos/client1/ /var/lib/borgadmin/vault/client1/ && chown -R borgadmin:borgadmin /var/lib/borgadmin/vault/client1

The compound is silent: mkdir prepares the directory, rsync copies the repository with ownership, ACLs, and extended attributes intact (the trailing slashes copy contents into client1/), and chown gives the vault account the tree so its forced command can read it. Run this refresh after each retention cycle, when the repository is known quiet; on a busy server, snapshot the repository's volume as in Section 15.2.2 and copy from the snapshot instead. In production, automate this with a second timer whose failure is as visible as the backup's.

Now read the vault from client1 with the retention identity. The variables around the command each do specific work:

Variable Effect
BORG_RSH use the retention key and the borgadmin account for this connection
BORG_KEY_FILE use the exported key copy at /etc/borg/client1.key instead of the location-named key in the cache
BORG_CONFIG_DIR, BORG_CACHE_DIR keep separate Borg state for the vault copy, apart from the live repository's location record and chunk cache
BORG_RELOCATED_REPO_ACCESS_IS_OK acknowledge that this repository ID is deliberately being accessed at a new location

Borg records each repository's ID together with the location it was first seen at, in the client's security directory; a repository that appears at an unexpected location is suspected of being a relocated copy - an old attack where a repository is moved so clients keep writing to it under the attacker's control - and Borg stops and asks. The vault copy is exactly such a relocated copy, made deliberately, so the acknowledgment is stated in the environment instead of confirmed interactively. On client1, as root:

# BORG_RSH='ssh -i /root/.ssh/id_ed25519_admin -o IdentitiesOnly=yes' BORG_PASSPHRASE=handbook-pass env BORG_KEY_FILE=/etc/borg/client1.key BORG_CONFIG_DIR=/root/.config/borg-vault BORG_CACHE_DIR=/root/.cache/borg-vault BORG_RELOCATED_REPO_ACCESS_IS_OK=yes borg list ssh://borgadmin@backup1/var/lib/borgadmin/vault/client1
client1-2026-11-21T002302            Sat, 2026-11-21 00:23:07 [4f5bc6329c22cfb84a31c7011ac0bd4dae95e28a92022c9b82e4ef97ec5ebf67]
client1-2026-11-20T1400              Sat, 2026-11-21 00:23:18 [e2951d848cd3bd64d1dc29baa1411cc127acfdf3483cd3de88934486bb0359e7]
client1-2026-11-20T1500              Sat, 2026-11-21 00:23:29 [34649f801e067fd57ddffd7e9f20cb5f728bd56361325ce0ba2cf9d647f7d3aa]

The vault holds the same three archives the live repository held after the prune in Section 15.4.4 - including client1-2026-11-20T1500, which is still present here. That archive matters next: Section 15.5.4 deletes it from the live repository with nothing but the writer key, and then restores it from this copy with the retention key.

15.5.3. Protecting Offline Copies, Recovery Keys, and Repository Credentials

The vault is still storage on the backup server: a compromise of backup1 itself, or of its storage, reaches both copies. The offline disk closes that gap - it is attached only during a refresh, holds a third copy plus the exported repository key, and is unmounted and left detached afterwards. Nothing on any network can reach it while it is detached; only physical access can.

The practice offline disk is backup1's /dev/vdb. Everything on it is erased. On backup1, as root:

# wipefs -a /dev/vdb && mkfs.ext4 -L offline /dev/vdb
Creating filesystem with 786432 4k blocks and 196608 inodes
Filesystem UUID: 018b907d-421f-4e7a-852f-43f423d203fc
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912

Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

Selected output; the redrawn progress counters and the version banner that mke2fs prints on standard error are omitted.

# mkdir -p /srv/offline
# mount /dev/vdb /srv/offline

Both commands are silent. Copy the vault into it, and give the exported repository key a place on the same disk:

# install -d -m 755 /srv/offline/repos /srv/offline/keys && rsync -aHAX --numeric-ids /var/lib/borgadmin/vault/client1/ /srv/offline/repos/client1/

The compound is silent. Now place the key file itself on the disk. The exported key was written in Section 15.4.1 and installed at /etc/borg/client1.key on client1; copy it to backup1 through your administrative channel - with real removable media, this is the step where the disk travels to the client, receives the key, and travels back - and restrict it to root:

# chown root:root /srv/offline/keys/client1.key && chmod 600 /srv/offline/keys/client1.key

The compound is silent; read the placement back:

# ls -l /srv/offline/keys/client1.key
-rw------- 1 root root 813 Nov 21 00:24 /srv/offline/keys/client1.key

The 813-byte size matches the export from Section 15.4.1, and the mode and root ownership keep the key readable only by this server's root account while the disk is attached. The passphrase is not copied beside it: the key and the passphrase are two secrets, and they belong in two custodies - that is the whole point of keyfile mode's split.

Detach the disk:

# umount /srv/offline

The command is silent. Verify detachment rather than assuming it:

# findmnt /srv/offline

findmnt prints a mount record when its argument is a mount point and nothing when it is not - here it prints nothing and exits 1, which is the detached state. The directory is now an ordinary empty directory on the root filesystem:

# ls -A /srv/offline

The command prints nothing: the copy exists only on the disk that was just unmounted. Remount it to prove the offline copy actually restores - the one test that makes any of this more than shelf-ware:

# mount /dev/vdb /srv/offline

The command is silent. Then restore a file from the offline repository, with the offline key, without any network or client involvement. Still on backup1, as root:

# install -d -m 755 /srv/offline-restore
# cd /srv/offline-restore

Both commands are silent; the shell now sits in the staging directory. The cd matters because borg extract writes below the current directory:

# time env BORG_PASSPHRASE=handbook-pass BORG_KEY_FILE=/srv/offline/keys/client1.key BORG_CONFIG_DIR=/root/.config/borg-offline BORG_CACHE_DIR=/root/.cache/borg-offline BORG_RELOCATED_REPO_ACCESS_IS_OK=yes borg extract --numeric-ids /srv/offline/repos/client1::client1-2026-11-20T1500 srv/appdata/reports/quarterly.txt

real    0m1.232s
user    0m0.287s
sys 0m0.048s

The repository URL here is a plain path - no ssh://, no account, no key on any network - and BORG_KEY_FILE points at the offline key copy. time reports the wall-clock cost of the restore, 1.2 seconds for one file in this environment. Read the result:

# cat /srv/offline-restore/srv/appdata/reports/quarterly.txt
quarterly report final

The restored file holds the content the archive recorded. Leave the disk detached again:

# umount /srv/offline

The command is silent. The refresh routine is therefore: mount, rsync the vault, place the key if it changed, unmount, verify with findmnt, and carry the media somewhere else. The rotation cadence is a policy decision from Section 15.1.3 - how stale may the surviving copy be after the worst day?

Custody of the remaining secrets follows the same separation logic. The passphrase file /etc/borg/passphrase stays mode 600 on client1 for the scheduled service; the passphrase itself belongs in a password manager or sealed storage reachable during a disaster, not in every backup copy. Borg's borg key export --paper form prints the key as text designed for printout, for sites whose recovery plan runs through a safe rather than a filesystem. And the offline key copy made above is precisely the artifact Section 15.7.3 needs when the client's own key cache is gone.

15.5.4. Testing Unauthorized Deletion and Restoring Without Production Credentials

A protection is only real if it fails correctly under attack. This subsection plays the attacker with exactly what a compromised client holds - the writer key and the passphrase - and then performs the recovery with exactly what the client does not hold.

First, the attack against the live repository. On client1, as root, delete an archive using the writer key - the same identity the scheduled backup uses:

# BORG_PASSPHRASE=handbook-pass borg delete --force ssh://borgsvc@backup1/var/lib/borgsvc/repos/client1::client1-2026-11-20T1500

The command prints nothing and exits 0: the archive is gone. Without --force, borg delete asks for an interactive confirmation before destroying an archive; an attacker scripting client1 does not answer prompts. Either way, --append-only on the writer account did not prevent the deletion. Confirm the loss in the live repository:

# BORG_PASSPHRASE=handbook-pass borg list ssh://borgsvc@backup1/var/lib/borgsvc/repos/client1
client1-2026-11-21T002302            Sat, 2026-11-21 00:23:07 [4f5bc6329c22cfb84a31c7011ac0bd4dae95e28a92022c9b82e4ef97ec5ebf67]
client1-2026-11-20T1400              Sat, 2026-11-21 00:23:18 [e2951d848cd3bd64d1dc29baa1411cc127acfdf3483cd3de88934486bb0359e7]

client1-2026-11-20T1500 no longer appears. The live copy of the history has lost an archive to nothing more than the client's own credentials.

Now point the same writer key at the vault and watch it fail twice. Try the vault's path through the writer account:

# BORG_PASSPHRASE=handbook-pass borg list ssh://borgsvc@backup1/var/lib/borgadmin/vault/client1
Repository path not allowed: /var/lib/borgadmin/vault/client1

The connection authenticated, the forced command ran, and Borg's server side refused: the writer account's --restrict-to-path names only /var/lib/borgsvc/repos, so a repository anywhere else is not allowed, whether or not it exists. Try the vault's account instead:

# BORG_PASSPHRASE=handbook-pass borg list ssh://borgadmin@backup1/var/lib/borgadmin/vault/client1
Connection closed by remote host. Is borg working on the server?

The writer key is not in borgadmin's authorized_keys at all, so sshd closes the connection before any Borg command runs - the error is less specific because the server deliberately reveals nothing. Both attempts exited with status 2; between them they cover both ways a client can guess at a vault: wrong path under its own account, and right account without the right key.

Finally, restore the deleted archive through the retention identity. On client1, as root, prepare a staging directory:

# install -d -m 755 /srv/recovery-from-vault
# cd /srv/recovery-from-vault

Both commands are silent. Extract the deleted archive's file from the vault with the retention key:

# BORG_RSH='ssh -i /root/.ssh/id_ed25519_admin -o IdentitiesOnly=yes' BORG_PASSPHRASE=handbook-pass env BORG_KEY_FILE=/etc/borg/client1.key BORG_CONFIG_DIR=/root/.config/borg-vault BORG_CACHE_DIR=/root/.cache/borg-vault BORG_RELOCATED_REPO_ACCESS_IS_OK=yes borg extract --list ssh://borgadmin@backup1/var/lib/borgadmin/vault/client1::client1-2026-11-20T1500 srv/appdata/reports/quarterly.txt
srv/appdata/reports/quarterly.txt

--list prints each extracted path. Verify what came back:

# cat /srv/recovery-from-vault/srv/appdata/reports/quarterly.txt && stat -c "%U:%G %a %n" /srv/recovery-from-vault/srv/appdata/reports/quarterly.txt
quarterly report final
john:www-data 640 /srv/recovery-from-vault/srv/appdata/reports/quarterly.txt

The file is byte-identical to what the deleted archive recorded, and the ownership and mode came back with it. The complete result: the attacker holding everything client1 had - keys, passphrase, schedule - could destroy the live repository copy but could not reach the vault copy, could not authenticate to the vault account, and the archive it deleted restored in seconds from the copy it never touched. The offline disk from Section 15.5.3 makes the same statement more strongly: its restore used no SSH and no account at all. A recovery that depends on nothing the compromised machine held is the property this whole section exists to buy.