Sub topic 4
Installing and initializing a repository
This chapter assumes that you can use the Linux command line, connect to a server with SSH keys, edit root-owned files, and understand basic systemd services and file permissions. You will initialize one encrypted repository. Choose either restic or Borg rather than initializing both for the same backup target.
Install the backup tool
On Debian or Ubuntu, install the tools with the package manager:
sudo apt update
sudo apt install -y restic borgbackup openssh-clientOn the backup server, prepare the backend you choose:
- Restic over SFTP needs an SSH account with access to the destination directory. The backup server does not need restic installed.
- Borg over SSH needs Borg installed on both the Linux server and the backup server. The SSH account needs permission to run Borg and write to the repository directory.
Test SSH access before initializing anything:
ssh [email protected]Use a dedicated backup account instead of a personal account. Restrict its access to the repository directory where possible.
Protect the repository password
Both tools encrypt repository data. The repository password is required to read or restore that data, so store it in a root-only file:
sudo install -d -m 700 /etc/restic
sudo install -m 600 /dev/null /etc/restic/password
sudoedit /etc/restic/passwordPut the password on one line in the file. Do not place it directly in a command, because command history and process listings can expose it.
| Tool | Password variable | Remote requirement |
|---|---|---|
| restic | `RESTIC_PASSWORD_FILE` | SFTP access |
| Borg | `BORG_PASSCOMMAND` | Borg on the remote host |
Initialize a restic repository
Set the repository location and password file in the current shell:
export RESTIC_REPOSITORY='sftp:[email protected]:/srv/restic/myserver'
export RESTIC_PASSWORD_FILE='/etc/restic/password'
restic init
restic snapshotsrestic init creates the repository metadata and encryption configuration. The snapshots command should initially show no snapshots. The SFTP path is remote, so the backup data does not remain only on the server being protected.
Initialize a Borg repository
Borg uses an SSH command to access the remote host. The ./ in this URL means that /srv/borg/myserver is relative to the SSH account's home directory:
export BORG_REPO='ssh://[email protected]/./borg/myserver'
export BORG_PASSCOMMAND='cat /etc/restic/password'
borg init --encryption=repokey-blake2
borg listBorg's repokey-blake2 mode stores the encryption key in the repository and protects it with the passphrase. Export a separate key backup after initialization:
borg key export :: /root/borg-key-backup.txt
sudo chmod 600 /root/borg-key-backup.txtStore this exported key with your protected recovery material. It does not replace the repository passphrase.
Initialization flow
Exercise
What must be available before running borg init over SSH, and why should the repository password not be typed directly in the command?
Show answer
Borg must be installed on the remote host, and the SSH account must be able to write to the repository directory. The password should not be typed in the command because shell history and process listings can expose it.