Sub topic 4
Installing and initializing a repository
This chapter assumes you can use the Linux command line, log in over SSH with keys, and read a simple systemd unit. It also assumes you know the repository, snapshot and deduplication model from the previous chapter. Here you install restic or Borg, prepare the secrets they need, and create an empty encrypted repository. Nothing is backed up yet.
Installing the tools
Both tools are in the standard repositories of current distributions. Install only the one you chose, or both if you want to compare them.
# Debian and Ubuntu
sudo apt install restic borgbackup
# RHEL, Rocky and Alma (enable EPEL first)
sudo dnf install epel-release
sudo dnf install restic borgbackup
restic version
borg --versionDistribution packages can lag behind upstream by a year or more. This matters for restic, because versions before 0.14 create repositories without compression. If your package is old, download the single static binary from the restic releases page, or run sudo restic self-update on a binary you installed yourself. Do not run self-update on a package-managed binary.
Borg has a second constraint. The examples in this tutorial use Borg 1.x syntax (borg init, borg compact from 1.2). Borg 2 renames commands such as init to rcreate, and its repository format is not compatible with 1.x. Check borg --version before copying commands from a blog post.
When the backup target is reached over SSH, Borg must also be installed on the remote host, because the client starts borg serve there. restic over SFTP needs only a working SFTP subsystem on the remote side.
Prepare the secret first
A repository is encrypted from the moment it is created, so you need the passphrase before you run init. Generate a long random one and store it in a file only root can read. The backup will run unattended as root, so the file is how the tool gets the passphrase without a prompt.
sudo install -d -m 700 /etc/restic
openssl rand -base64 32 | sudo tee /etc/restic/password > /dev/null
sudo chmod 600 /etc/restic/password
sudo ls -l /etc/restic/passwordFor Borg, do the same under /etc/borg/passphrase. Copy the passphrase into your password manager now. A repository whose passphrase is lost cannot be read by anyone, including you.
Avoid putting the passphrase directly in RESTIC_PASSWORD or BORG_PASSPHRASE in a script. Environment variables of a running process can be read by other tools, and the values often end up in shell history. File-based options (RESTIC_PASSWORD_FILE, BORG_PASSCOMMAND) keep the secret out of both.
Preparing SSH access
Scheduled backups run as root, so root's SSH identity must be able to log in to the backup host without a prompt. A common failure is that the key works for your own user but root has never connected to the host, so there is no entry in /root/.ssh/known_hosts.
sudo ssh-keygen -t ed25519 -f /root/.ssh/id_backup -N ''
sudo ssh-copy-id -i /root/.ssh/id_backup.pub [email protected]
sudo ssh -i /root/.ssh/id_backup [email protected] trueThe last command also records the host key. To avoid passing -i every time, add a Host block to /root/.ssh/config that names this identity file. The next chapter covers locking the key down on the remote side.
Initializing a restic repository
restic reads the repository location and passphrase source from environment variables, so later commands do not repeat them. The repository URL uses the sftp:user@host:/path form.
export RESTIC_REPOSITORY=sftp:[email protected]:/srv/restic/myserver
export RESTIC_PASSWORD_FILE=/etc/restic/password
restic init
restic snapshotsrestic init creates the directory structure on the remote host and writes a config file plus a keys/ directory. It prints the repository ID. A new repository has no snapshots, so restic snapshots printing "No snapshots found" confirms that the passphrase works and the connection is good.
Use sudo -E or a root shell when you run these commands. If you set the variables as your own user and then run sudo restic, the variables are not passed on and restic asks for a repository.
Initializing a Borg repository
Borg takes its location from BORG_REPO and uses an ssh:// URL. In ssh://[email protected]/./borg/myserver, the /./ makes the path relative to the home directory of the remote user. A single leading slash would mean an absolute path.
export BORG_REPO=ssh://[email protected]/./borg/myserver
export BORG_PASSCOMMAND='cat /etc/borg/passphrase'
borg init --encryption=repokey-blake2
borg list
borg infoUnlike restic, Borg makes you choose an encryption mode at init time, and you cannot change it later without creating a new repository.
| Mode | Where the key lives | Trade-off |
|---|---|---|
| `repokey-blake2` | Inside the repository, protected by the passphrase | Simple. Anyone with the repository and passphrase can read it. |
| `keyfile-blake2` | In `~/.config/borg/keys` on the client | The repository alone is useless without the key file. If the client disk is lost, so is the key. |
| `authenticated-blake2` | No encryption, only integrity checks | For trusted storage where you want tamper detection but not secrecy. |
For a single server, repokey-blake2 is a sound default, provided you export the key as described in the key management chapter. The -blake2 variants use BLAKE2b and are faster than the SHA-256 variants on most 64-bit CPUs.
sudo borg key export :: /root/borg-key-backup.txtStore the exported file somewhere other than the server. A copy on the same disk does not help when the disk fails.
How the pieces fit together
Common errors
- "Fatal: unable to open config file" (restic). The path does not point at a repository, or
inithas not run. Check the URL, including whether the remote path is absolute. - "wrong password or no key found" (restic). The passphrase file differs from the one used at init. A trailing newline is fine, but a different or empty file is not.
- "Repository ... does not exist" (Borg). Often the
/./versus/mistake, which points at/borg/myserverinstead of the home directory. - "Connection closed by remote host" or "borg: command not found" (Borg). Borg is missing on the remote host or not in the SSH session's
PATH. Install it there, or setBORG_REMOTE_PATH. - A prompt for the host key or passphrase under cron or systemd. Scheduled jobs have no terminal. Run the command once manually as root to accept the host key, and use the passphrase file settings above.
- Permissions are too open on the passphrase file. restic does not complain, but any local user who can read the file can read your backups. Keep it at mode 600, owned by root.
Checking your work
Before moving on, confirm the empty repository is reachable using the same environment a scheduled job will have. Open a root shell, set only the variables above, and run the listing command. If it works without a prompt, the setup is ready for the first backup.
sudo -i
export RESTIC_REPOSITORY=sftp:[email protected]:/srv/restic/myserver
export RESTIC_PASSWORD_FILE=/etc/restic/password
restic cat configThe command prints a small JSON document with the repository ID and format version. It exercises the connection, the passphrase and the repository structure without reading any data.
Exercise
You ran borg init --encryption=repokey-blake2 on your server and the repository works. Your server's disk then fails completely. You still have the remote repository and the passphrase in your password manager, but you never exported the key. Can you restore, and what would have changed if you had used keyfile-blake2 instead?
Show answer
With repokey-blake2 the encrypted key is stored inside the repository, so the remote repository plus the passphrase is enough to restore, even though exporting the key is still good practice. With keyfile-blake2 the key would live only in ~/.config/borg/keys on the failed disk, and without an exported copy the repository would be permanently unreadable even with the correct passphrase.