1004 Words

πŸ—„οΈ Backups with BorgBackup

BorgBackup (borg) is, like restic, a backup program that encrypts everything and stores every piece of data only once (deduplication). After the first backup, a new one only adds what changed, so you can keep a snapshot of every day for years without filling the disk.

I use borg especially for backups to my own server over SSH: borg on the server side makes it fast, and the server can be locked down so that it can only receive backups.

πŸ’‘ This note is about borg 1.x (1.2, 1.4), the version in Debian, Ubuntu and Fedora. Borg 2 renames many commands and can’t read borg 1 repositories. Check with borg --version.


1. Install

sudo apt install borgbackup     # Debian/Ubuntu
sudo dnf install borgbackup     # Fedora

For a backup over SSH, install borg on the server too.

2. Create a repository

# on a USB disk
borg init --encryption=repokey-blake2 /run/media/$USER/BACKUP/borg

# on a server over SSH
borg init --encryption=repokey-blake2 ssh://me@nas.local/./backups/laptop

Borg asks for a passphrase. With repokey the encryption key is stored inside the repository, protected by that passphrase.

Export the key right away and keep it outside the backup (for example in your password manager, or printed). If the repository gets damaged, the key and the passphrase are your only way back in:

borg key export /run/media/$USER/BACKUP/borg ~/borg-key-laptop.txt
borg key export --paper /run/media/$USER/BACKUP/borg     # a version to print

⚠️ Without the passphrase and the key, nobody can read your backups, not even you.

3. Don’t type the details every time

Put the repository and the passphrase in environment variables. Save the passphrase in a file only you can read:

mkdir -p ~/.config/borg
nano ~/.config/borg/passphrase && chmod 600 ~/.config/borg/passphrase

export BORG_REPO=ssh://me@nas.local/./backups/laptop
export BORG_PASSCOMMAND="cat $HOME/.config/borg/passphrase"

With BORG_REPO set, :: means “this repository”, and ::name an archive in it.

4. Make a backup

Every backup is an archive with its own name. Placeholders like {hostname} and {now} make a unique name:

borg create --stats --progress --compression zstd,3 \
  --exclude-caches \
  --exclude "$HOME/.cache" --exclude "$HOME/Downloads" --exclude '*/node_modules' \
  ::'{hostname}-{now:%Y-%m-%d_%H:%M}' \
  "$HOME"
  • --compression zstd,3: good compression at a good speed (lz4 is faster, zstd,10 smaller).
  • --exclude-caches: skips folders that mark themselves as cache (with a CACHEDIR.TAG file).
  • --stats: at the end, how much data was new.

5. Look at your backups

borg list                                    # all archives
borg list ::laptop-2026-11-28_20:00 | less   # the files in one archive
borg info ::laptop-2026-11-28_20:00          # size, and how much was new
borg diff ::laptop-2026-11-27_20:00 laptop-2026-11-28_20:00   # what changed

6. Restore

borg extract writes the files into the current folder, so go to an empty folder first. Paths in an archive have no leading /:

mkdir -p /tmp/restore && cd /tmp/restore
borg extract ::laptop-2026-11-28_20:00 home/benjamin/Documents
borg extract --dry-run --list ::laptop-2026-11-28_20:00 home/benjamin/Documents   # only show what it would restore

Or open the backups as normal folders, and copy what you need (needs the python3-pyfuse3 or python3-llfuse package):

mkdir -p /tmp/borg && borg mount :: /tmp/borg
ls /tmp/borg                                 # one folder per archive
borg umount /tmp/borg

7. Clean up: prune and compact

prune decides which archives to keep; compact (borg 1.2 and newer) then really frees the disk space:

borg prune --list --glob-archives '{hostname}-*' \
  --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --keep-yearly 2
borg compact

Try it first with --dry-run to see what would be removed.

8. Check the repository

borg check                     # are the repository and the archives intact?
borg check --verify-data       # also read and check all data (slow, do it now and then)

9. A locked-down backup server

When your laptop backs up to a server over SSH, a thief with your laptop could also delete your backups. Prevent that by letting the backup key do only one thing: borg serve, in one folder.

On the server, in ~/.ssh/authorized_keys of the backup user, put this in front of the laptop’s key (one line):

command="borg serve --restrict-to-path /srv/borg/laptop --append-only",restrict ssh-ed25519 AAAA... laptop-backup
  • --restrict-to-path: this key can only reach this one repository.
  • --append-only: the laptop can add backups, but not delete or change old ones. Pruning then has to happen on the server itself (with a key or login that doesn’t have --append-only).
  • restrict: no shell, no port forwarding, nothing else.

Use a separate SSH key for backups (ssh-keygen -t ed25519 -f ~/.ssh/borg), without a passphrase or with an agent, so the automatic backup can run. Tell borg to use it:

export BORG_RSH="ssh -i $HOME/.ssh/borg"

10. Every day, automatically

Option A: a script and a systemd timer

~/.local/bin/borg-backup.sh:

#!/bin/bash
set -euo pipefail
export BORG_REPO=ssh://me@nas.local/./backups/laptop
export BORG_PASSCOMMAND="cat $HOME/.config/borg/passphrase"
export BORG_RSH="ssh -i $HOME/.ssh/borg"

borg create --stats --compression zstd,3 --exclude-caches \
  --exclude "$HOME/.cache" --exclude "$HOME/Downloads" \
  ::'{hostname}-{now:%Y-%m-%d_%H:%M}' "$HOME"

borg prune --glob-archives '{hostname}-*' --keep-daily 7 --keep-weekly 4 --keep-monthly 12
borg compact

(With an --append-only server, leave out the last two commands and prune on the server.)

~/.config/systemd/user/borg-backup.service:

[Unit]
Description=Borg backup of my home folder

[Service]
Type=oneshot
ExecStart=%h/.local/bin/borg-backup.sh

~/.config/systemd/user/borg-backup.timer:

[Unit]
Description=Daily borg backup

[Timer]
OnCalendar=daily
Persistent=true
RandomizedDelaySec=30m

[Install]
WantedBy=timers.target
chmod +x ~/.local/bin/borg-backup.sh
systemctl --user daemon-reload
systemctl --user enable --now borg-backup.timer
journalctl --user -u borg-backup -e        # how did the last backup go?

More about timers in systemd basics.

Option B: borgmatic

borgmatic wraps borg in one YAML configuration file: what to back up, where to, what to keep, and checks. It also comes with a systemd timer.

sudo apt install borgmatic        # or: sudo dnf install borgmatic
borgmatic config generate         # writes an example config with comments
nano ~/.config/borgmatic/config.yaml
borgmatic --verbosity 1           # create + prune + compact + check

Borg or restic?

Borgrestic
To a server over SSHβœ… fast, borg runs on the server tooβœ… over SFTP, slower
Straight to cloud storage (S3, B2)❌ (only via a mounted folder or rclone)βœ… built in
Locked-down server (append-only)βœ… with borg serveβœ… with rest-server
Compressionlz4, zstd, zlib, lzmazstd
ProgramPython, on both sidesone binary

My choice: borg for backups to my own server, restic when the backup has to go to the cloud. And whichever you use: follow the 3-2-1 rule, and test a restore now and then.