Install Borg on Both Machines
Borg runs on the server you back up and on the backup host, which runs borg serve over SSH:
# Debian and Ubuntu
sudo apt install borgbackup
# AlmaLinux and Rocky Linux (EPEL)
sudo dnf install epel-release
sudo dnf install borgbackup
This guide uses Borg 1.x commands. Debian 12, Ubuntu 24.04, and EPEL 9 ship Borg 1.2; Debian 13, Ubuntu 26.04, and EPEL 10 ship Borg 1.4. Borg 2, still a beta release, renames several commands (borg init becomes borg repo-create) and cannot read 1.x repositories, so check borg --version before following any guide.
The repository belongs on another machine, not on the disk it protects. For the off-site copy that should sit next to it, see the 3-2-1 backup rule.
Step 1: Store the Passphrase in a File
Run the backup as root so it can read every file. Keep the passphrase in a file only root can read, never on a command line:
sudo install -m 600 /dev/null /root/.borg-passphrase
sudo nano /root/.borg-passphrase # one line: STRONG_PASSPHRASE
Then, in a root shell, tell Borg where the repository is and how to get the passphrase:
export BORG_REPO='ssh://user@your_server_ip/srv/borg/web1'
export BORG_PASSCOMMAND='cat /root/.borg-passphrase'
The ssh:// form takes an absolute path; add :2222 after the address for another SSH port. The shorter scp-style form is marked deprecated in the Borg 1.4 docs.
Step 2: Create an Encrypted Repository
borg init --encryption=repokey-blake2
borg key export :: /root/borg-web1.key
Both commands use the repository in BORG_REPO.
Without the passphrase and the key there is no way to read the backups. Copy the passphrase and the exported key to a password manager or another safe place off the server, then remove the key file from the server.
Step 3: Create an Archive
borg create --stats --compression zstd \
::'{hostname}-{now:%Y-%m-%d}' \
/etc /home /root /var/www /var/backups \
--exclude '/home/*/.cache'
Each run makes one archive named after the host and date. The first run copies everything; later runs send only new blocks. Dump databases into /var/backups first.
Step 4: List and Restore a File
borg list # archives
borg list ::web1-2026-11-28 # files in one archive
mkdir -p /tmp/restore-test && cd /tmp/restore-test
borg extract ::web1-2026-11-28 etc/nginx/nginx.conf
diff /tmp/restore-test/etc/nginx/nginx.conf /etc/nginx/nginx.conf
borg extract writes into the current directory, and paths are given without the leading slash. Always extract into a scratch directory and compare, never straight into /. That comparison is your restore test; repeat it with a larger folder each month.
Step 5: Prune Old Archives
Prune deletes archives for good. Preview it first:
borg prune --list --dry-run --keep-daily 7 --keep-weekly 4 --keep-monthly 6
borg prune --list --keep-daily 7 --keep-weekly 4 --keep-monthly 6
borg compact
Since Borg 1.2, disk space is freed only when borg compact runs. Keep one repository per server: without a filter such as --glob-archives, prune treats every archive in the repository as a candidate.
Step 6: Check the Repository
borg check
Add --verify-data to read and verify all data; it is slow, so run it monthly.
Append-Only Access
If the server is broken into, an attacker with its SSH key could delete the backups. On the backup host, restrict that key in ~/.ssh/authorized_keys:
command="borg serve --append-only --restrict-to-path /srv/borg/web1",restrict ssh-ed25519 PUBLIC_KEY
The server can then add archives, but whatever it deletes is only marked as deleted. Run prune and compact from a separate, trusted login without --append-only. Be aware that the first write from that login removes marked data for good, so check borg list for missing archives before you prune.
Schedule It with a systemd Timer
Put the two variables in /etc/borg/env with mode 600, without export:
BORG_REPO=ssh://user@your_server_ip/srv/borg/web1
BORG_PASSCOMMAND=cat /root/.borg-passphrase
Put the create command, plus prune and compact if the key is not append-only, in a root-owned script such as /usr/local/sbin/borg-backup (mode 700). Then create /etc/systemd/system/borg-backup.service and borg-backup.timer:
# borg-backup.service
[Unit]
Description=Borg backup
[Service]
Type=oneshot
EnvironmentFile=/etc/borg/env
ExecStart=/usr/local/sbin/borg-backup
# borg-backup.timer
[Timer]
OnCalendar=*-*-* 02:30:00
RandomizedDelaySec=15m
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now borg-backup.timer
Connect once by hand as root first so the host key is accepted. More on units in systemd unit files; where Borg fits in a full plan is in how to back up a Linux server.