Sub topic 5
Sending backups off the server
This chapter assumes you can log in over SSH with keys, edit authorized_keys, and set environment variables in a shell script or systemd unit. It also assumes you have already initialized a repository, as in the previous chapter.
A backup stored on the server it protects fails with that server. A disk failure, a ransomware infection, an accidental rm -rf, or a compromised root account can destroy both at once. The 3-2-1 rule asks for at least one copy off-site. This chapter covers how to send repositories to a remote host over SSH or to object storage, and how to limit what a stolen server credential can do.
Choosing a transport
restic and Borg support different remote targets.
| Target | restic | Borg 1.x |
|---|---|---|
| Another Linux host over SSH | `sftp:` backend | `ssh://` URL, needs Borg installed on the remote |
| S3 or S3-compatible storage | Built in (`s3:`) | Not supported directly |
| Other cloud storage | Built in for several, plus `rclone:` | Use a Borg hosting service, or a remote host running Borg |
| Append-only protection | `rest-server --append-only` | `borg serve --append-only` |
Borg talks to a remote borg serve process, so the remote side needs Borg installed. restic's SFTP backend needs only an SSH server with the SFTP subsystem enabled.
Preparing an SSH backup host
Create a dedicated unprivileged user on the backup host. Never use root, and never use a personal account.
# On the backup host
sudo adduser --disabled-password --gecos "" backup
sudo mkdir -p /home/backup/borg
sudo chown backup:backup /home/backup/borgOn the server being backed up, generate a key that belongs only to this job. It has no passphrase because a timer must be able to use it unattended. The restrictions in the next section make that acceptable.
ssh-keygen -t ed25519 -N "" -C "backup@myserver" -f /root/.ssh/backup_ed25519
ssh-copy-id -i /root/.ssh/backup_ed25519.pub [email protected]Pinning the host key
An unattended job cannot answer "Are you sure you want to continue connecting?". It hangs or fails. Record the host key once, and compare its fingerprint with the one shown on the backup host:
ssh-keyscan -t ed25519 backup.example.com >> /root/.ssh/known_hosts
ssh-keygen -lf /root/.ssh/known_hostsPinning also protects you from an attacker who redirects the connection to a fake host.
Using SSH with Borg
Borg reads the SSH command from BORG_RSH. Name the key explicitly so the job does not depend on which user or HOME it runs under:
export BORG_RSH='ssh -i /root/.ssh/backup_ed25519 -o BatchMode=yes'
export BORG_REPO=ssh://[email protected]/./borg/myserver
export BORG_PASSCOMMAND='cat /etc/borg/passphrase'
borg init --encryption=repokey-blake2BatchMode=yes makes SSH fail immediately instead of prompting.
The path in the URL matters. A path starting with /./ is relative to the remote user's home directory, so this repository lives at /home/backup/borg/myserver. A path starting with a single / is absolute.
Restricting the key on the backup host
Without restrictions, anyone holding the server's private key gets a shell on the backup host. Put the restriction in ~backup/.ssh/authorized_keys on the backup host:
command="borg serve --restrict-to-path /home/backup/borg/myserver --append-only",restrict ssh-ed25519 AAAA...key... backup@myserverEach part does one job:
command=forces this one command whatever the client asks for.--restrict-to-pathconfines Borg to one repository.--append-onlymakes the repository keep deleted data until an administrator cleans it up.restrictturns off port forwarding, a TTY, agent forwarding and other SSH features.
Append-only mode has a cost. borg prune run through this key only marks data for deletion, so the repository keeps growing. To really prune, run borg prune and borg compact from a trusted machine, such as the backup host itself, using a separate key without --append-only. Before doing that, check that the repository does not contain unwanted recent changes from a compromised server. Pruning commits those changes permanently.
Using SSH with restic
restic's SFTP backend runs ssh and asks for the sftp subsystem. The simplest setup is an entry in /root/.ssh/config on the server:
Host backup
HostName backup.example.com
User backup
IdentityFile /root/.ssh/backup_ed25519
IdentitiesOnly yes
BatchMode yesThen use the alias in the repository URL:
export RESTIC_REPOSITORY=sftp:backup:/srv/restic/myserver
export RESTIC_PASSWORD_FILE=/etc/restic/password
restic initHere /srv/restic/myserver is an absolute path on the backup host, because it follows the colon with a leading slash. A path without the leading slash is relative to the remote user's home directory.
Plain SFTP cannot offer append-only mode. You can jail the user with ForceCommand internal-sftp and ChrootDirectory, but the user can still delete files inside the jail. If an attacker who controls the server must not be able to erase history, use restic's rest-server with --append-only on the backup host. Then point restic at it with rest:https://backup.example.com:8000/myserver/. The same maintenance trade-off as with Borg applies: pruning must happen from a separate, trusted path.
Using object storage with restic
restic can write directly to S3 or any S3-compatible service such as MinIO or Backblaze B2's S3 API. You supply credentials through environment variables:
export AWS_ACCESS_KEY_ID=AKIAEXAMPLE
export AWS_SECRET_ACCESS_KEY=example-secret
export RESTIC_REPOSITORY=s3:s3.eu-central-1.amazonaws.com/example-backup-bucket/myserver
export RESTIC_PASSWORD_FILE=/etc/restic/password
restic init
restic backup /etc /home /var/wwwFor a non-AWS service, include the full endpoint, for example s3:https://minio.example.com/example-backup-bucket/myserver.
Do not put these variables in the script. Put them in a root-only environment file, such as /etc/restic/env with mode 0600, and load it in the service with EnvironmentFile=/etc/restic/env.
Give the access key the least privilege that works. restic needs to list, read, write and delete objects in that one bucket or prefix. Delete permission is needed for forget --prune. If you want a server that cannot delete, use a separate key for backups and a trusted second key for pruning, or enable bucket versioning so overwritten and deleted objects can be recovered.
Borg 1.x has no S3 backend. You can run Borg against a host that has Borg installed, or use a hosting service built for Borg. Mounting a bucket as a filesystem is possible, but it is slow and fragile, so avoid it for the primary repository. Check the Borg 2 documentation for its newer storage options, since that version was still evolving when this was written.
Common problems
- The job hangs or exits with "Host key verification failed". The host key is not in
known_hostsfor the user running the job. Runssh-keyscanas shown above, as the same user. - "subsystem request failed" with restic. The backup host's
sshd_configlacksSubsystem sftp ..., or the forced command inauthorized_keysblocks it. Aborg serveforced command cannot be used for restic. - "Remote: Permission denied" from Borg. The URL path is outside
--restrict-to-path, or you mixed up/./relativeand/absolutepaths. - Works by hand but not from systemd. A timer runs with a different environment. Name keys with
-iorIdentityFile, and use absolute paths for everything. - A slow link saturates. Use
restic backup --limit-upload 5000orborg create --upload-ratelimit 5000. Both take KiB per second.
Exercise
A Borg repository is at ssh://[email protected]/./borg/myserver, and the key in authorized_keys uses --restrict-to-path /home/backup/borg/myserver --append-only. The nightly job runs borg prune --keep-daily 7, but disk usage on the backup host keeps rising. Why, and how do you reclaim the space safely?
Show answer
In append-only mode, prune and delete only record the removal, and the data stays on disk until the repository is cleaned up. The restricted key cannot do that on purpose. To reclaim space, first check the repository for unexpected recent changes. Then run borg prune and borg compact from a trusted host, such as the backup host itself, using a key or session that is not append-only.