Sub topic 13
Protecting passphrases and keys
The encryption on a backup repository only protects you if you control two things: who can read the secret, and whether you can still get it back after the server is gone. This chapter assumes you have already initialized a repository and run the backup script from the earlier chapters. It covers where the secrets live, how to store them, how to back them up, and how to limit what a compromised server can do to your backups.
What you are protecting
Both tools encrypt data with a random master key. They never use your passphrase directly. The passphrase only unlocks a small key record that contains the master key.
- restic stores one or more key files in the repository's
keys/directory. Each file holds the master key, encrypted with one password. - Borg stores the key either inside the repository (
repokeymodes) or on the client machine (keyfilemodes). The passphrase protects that key.
This design has three consequences:
- Losing the passphrase, or the key in a
keyfilerepository, makes the data unrecoverable. No reset procedure exists. - Changing a passphrase is cheap. The data is not re-encrypted, only the key record is rewritten.
- Anyone who has both the key material and the passphrase can read every snapshot. That includes snapshots from before you changed the passphrase, if they kept a copy of the old key record.
| Aspect | restic | Borg 1.x |
|---|---|---|
| Where the key lives | Always in the repository (`keys/`) | In the repository (`repokey-*`) or on the client (`keyfile-*`) |
| Multiple passwords | Yes, one key file per password | One passphrase per key |
| Add or remove a password | `restic key add`, `restic key remove` | Not supported |
| Change a password | `restic key passwd` | `borg key change-passphrase` |
| Extra key backup needed | The password only | The exported key in `keyfile` mode. Recommended in `repokey` mode too |
Storing the passphrase on the server
An unattended backup needs the passphrase without a human typing it. The usual choice is a file readable only by root, which the first examples in this tutorial already use (RESTIC_PASSWORD_FILE, BORG_PASSCOMMAND). Create it with a random value and restrictive permissions from the start:
install -d -m 700 /etc/restic
( umask 077; openssl rand -base64 32 > /etc/restic/password )
ls -l /etc/restic/passwordThe umask 077 runs in a subshell so the file is never briefly world-readable. The ls output should show -rw------- owned by root.
Avoid these ways of passing the secret:
RESTIC_PASSWORDorBORG_PASSPHRASEset in a script or unit file. The value ends up in shell history, insystemctl showoutput, and in/proc/<pid>/environfor the running process, which is readable by the same user.--password-commandor--password-filepaths inside world-readable scripts. The path is harmless, but the file it points to must not be.- A passphrase committed to a configuration management repository in plain text. Use your tool's encrypted-secret feature instead.
Using systemd credentials
If you run backups from a systemd service, LoadCredential= copies a file into a private, root-only directory for the lifetime of the service. The secret then stays out of the service's environment. This needs a reasonably recent systemd (v247 or later). Add a drop-in:
# /etc/systemd/system/restic-backup.service.d/credentials.conf
[Service]
LoadCredential=restic-password:/etc/restic/passwordThen point restic at the credential inside the script, replacing the RESTIC_PASSWORD_FILE line:
export RESTIC_PASSWORD_FILE="${CREDENTIALS_DIRECTORY}/restic-password"Run systemctl daemon-reload after adding the drop-in. This script now works only when started by the service. For manual runs, set RESTIC_PASSWORD_FILE to the original path.
Backing up the secret itself
The passphrase and the key are the single point of failure for your backups. If the server burns down and the only copy of the passphrase was in /etc/restic/password on that server, the off-site repository is useless.
Keep the passphrase in at least one place that does not depend on the server:
- A password manager with shared or emergency access.
- A printed copy in a safe or a safe-deposit box.
Do not store the passphrase inside the repository's own backup, and do not rely on a backup of /etc to carry it. That would put the key to the repository inside the repository.
Borg: export the key
With Borg, the key export matters even in repokey mode, because a damaged repository can take its embedded key with it. In keyfile mode the export is mandatory, since the only copy is in ~/.config/borg/keys on the client.
borg key export :: /root/borg-key-backup.txt
borg key export --paper :: /root/borg-key-paper.txtThe first command writes a text file. The second writes a format meant for printing and retyping by hand. Move the export off the server, then delete the local copy if the server is not trusted to hold it. The exported key is still encrypted by the passphrase. Anyone who gets both the export and the passphrase can read the repository, so store them in different places.
To restore a key onto a new machine:
borg key import :: /path/to/borg-key-backup.txtrestic: use a second key
Because restic allows several keys, you can give each person or machine its own password and revoke one without disturbing the others:
restic key list
restic key add
restic key passwd
restic key remove 5f3a1c9dkey add prompts for the new password. key passwd changes the password of the key you are currently using. key remove takes the key ID shown in key list. restic refuses to remove the key you are currently authenticated with, so you cannot lock yourself out by accident. After removing a key, anyone who holds a copy of that old key file and its password can still decrypt the master key. Removing a key from the repository does not rotate the master key. If a password was compromised, create a new repository and back up into it.
Limiting what a compromised server can do
The backup client holds the passphrase, so a compromised server gives an attacker the ability to read your backups. It should not also give them the ability to delete them. Ransomware that finds backup credentials will usually try to wipe the repository first.
The defense is an append-only repository: the client can add data but cannot delete or overwrite it. Pruning is then done from a separate, trusted machine.
Borg: restrict the SSH key
On the backup host, constrain the client's SSH key in ~/.ssh/authorized_keys so it can only run borg serve against one path, in append-only mode:
command="borg serve --restrict-to-path /home/backup/borg --append-only",restrict ssh-ed25519 AAAAC3Nza...example myserver-backupThe restrict option disables shell access, port forwarding and agent forwarding. With --append-only, a borg prune from the client appears to succeed but does not remove data permanently. Run the real prune and compact from an admin machine with a different SSH key.
restic: use rest-server
restic over plain SFTP cannot enforce append-only mode, because SFTP gives the client general file access. For append-only, run rest-server on the backup host with the --append-only flag and use a rest: repository URL from the client. Alternatively, use object storage with versioning or object lock and a write-only credential. Check the details for your provider, since the permissions needed differ.
Keep credentials separate
- Use one SSH key per client, so you can revoke a single server.
- Use a dedicated
backupaccount on the storage host with no sudo rights and no access to other data. - Do not reuse the backup passphrase for anything else.
How the pieces fit together
Common mistakes
- Only the repository is backed up, not the passphrase. Test this by asking whether someone could restore if the server vanished right now.
- Passphrase file readable by a service user. A web application running as
www-datashould not be able to read/etc/restic/password. Check withsudo -u www-data cat /etc/restic/password, which should fail. - Borg
keyfilemode with no key export. Replacing the client machine then locks you out permanently. - Treating
key removeas key rotation. It revokes one password, but the master key stays the same. - Testing restores only from the original server. That server already has the key, so the test does not prove you can recover from scratch. The recovery drill chapter covers this.
Exercise
Your Borg repository was created with --encryption=keyfile-blake2 and the server's disk fails. You have the repository on the backup host and the passphrase in your password manager, but you never ran borg key export. Can you restore, and what should you have done?
Show answer
No. In keyfile mode the key exists only in ~/.config/borg/keys on the client, so the lost disk took the only copy. The passphrase alone cannot decrypt the repository. You should have run borg key export (and --paper for a printable version), stored the export off the server, and kept it separate from the passphrase. Using a repokey mode would also have kept a copy inside the repository, though a key export is still advisable.
Exercise
Why is chmod 600 /etc/restic/password not enough to stop an attacker who gets root on the server from deleting your backups, and what two measures reduce that risk?
Show answer
Root can read the passphrase and the SSH key, so the attacker can run any restic or Borg command against the repository, including deleting data. Two measures help. First, make the repository append-only (a borg serve --append-only forced command, or rest-server --append-only). Second, run prune and compact from a separate admin machine with different credentials.