Sub topic 13
Protecting passphrases and keys
This chapter assumes you can use the Linux command line, manage file permissions, use SSH keys, and operate systemd services. The goal is to protect two different secrets: the encryption secret for the backup repository and the credentials used to reach remote storage.
Separate the secrets
A restic repository is encrypted with a password. Restic reads it from RESTIC_PASSWORD_FILE or another configured secret source. Anyone who obtains this password can decrypt repository data, so the file must be readable only by root or the backup service account.
Borg uses an encryption key stored in the repository. The key is protected by a passphrase. Borg can read the passphrase through BORG_PASSCOMMAND, avoiding a password in the process list or shell history.
SSH authentication is separate. The backup host needs an SSH private key or another allowed method to access the remote repository. Limit that account to backup storage where possible.
| Secret | Protects | Recovery requirement |
|---|---|---|
| Restic password or Borg passphrase | Repository contents | Keep a tested offline copy |
| Borg exported key | Access to Borg data | Store separately from the server |
| SSH private key | Remote repository access | Restrict permissions and access |
A secret that exists only on the server is not a disaster-recovery plan. Store recovery material in an approved password manager, offline encrypted storage, or another protected location. Do not store the only copy inside the repository it unlocks.
Create root-only secret files
Create a restic password file with root ownership and mode 600. Mode 600 means the owner can read and write the file, while other users have no permissions.
sudo install -o root -g root -m 600 /dev/null /etc/restic/password
sudo sh -c 'printf "%s\n" "replace-with-a-long-random-password" > /etc/restic/password'For Borg, use the same pattern for the passphrase:
sudo install -o root -g root -m 600 /dev/null /etc/borg/passphrase
sudo sh -c 'printf "%s\n" "replace-with-a-long-random-passphrase" > /etc/borg/passphrase'Avoid placing secrets directly in a unit file, backup script, command argument, or shell history. A script can refer to a protected file instead:
export RESTIC_PASSWORD_FILE=/etc/restic/password
export BORG_PASSCOMMAND='cat /etc/borg/passphrase'If a dedicated service account runs the backup, make that account the file owner instead of granting broad group access.
Export and protect a Borg key
A Borg repository key can be exported for recovery. Store the export outside the server and protect it like the passphrase.
borg key export :: /root/borg-key-backup.txt
chmod 600 /root/borg-key-backup.txtThe exported key is not a replacement for the passphrase. Recovery may require both. Test that the documented key and passphrase can open a repository, but do not perform tests by changing the production repository.
Limit access to remote storage
Use a separate backup account on the remote host. Restrict its SSH key and storage permissions to the repository directory. Keep the private key readable only by the service account.
Review secret permissions and the scheduled service before enabling automation:
stat -c '%U %G %a %n' /etc/restic/password /etc/borg/passphrase
systemctl cat restic-backup.serviceExercise
Why should the repository password, Borg key export, and SSH private key be stored separately from the backup repository?
Show answer
A repository failure, accidental deletion, or compromised server could otherwise remove both the encrypted data and the material needed to recover it.