Back up your server with restic
Encrypted, deduplicated backups of your VPS to a server you control: install restic, run the first backup, schedule it and test a restore.
We do not back up your server, and there are no snapshots. Your disk is a single SSD without redundancy. This guide gives you encrypted backups that you control, using restic.
restic encrypts on your server before anything is sent, so the backup location only ever stores ciphertext. You need a second place to store the copies: another server you own, or any S3-compatible storage. Never back up to the same disk.
Install and set up#
sudo apt update && sudo apt install -y restic
sudo install -m 600 /dev/null /root/.restic-pass
Put a long random passphrase in /root/.restic-pass, and store a copy in your password manager. If you lose it, the backups cannot be opened.
This example stores the repository on another Linux server over SFTP (SSH key login must already work from this server to that one):
export RESTIC_REPOSITORY=sftp:[email protected]:/srv/restic/myvps
export RESTIC_PASSWORD_FILE=/root/.restic-pass
restic init
First backup#
restic backup /etc /home /root /var/www /var/lib/docker/volumes
List what you have with restic snapshots. Later runs only send what changed.
Databases need a dump first, because copying live database files gives a broken backup:
sudo -u postgres pg_dumpall > /var/backups/pg-all.sql
mysqldump --all-databases > /var/backups/mysql-all.sql
Then include /var/backups in the backup.
Run it every night#
Create /etc/systemd/system/restic-backup.service:
[Unit]
Description=restic backup
[Service]
Type=oneshot
Environment=RESTIC_REPOSITORY=sftp:[email protected]:/srv/restic/myvps
Environment=RESTIC_PASSWORD_FILE=/root/.restic-pass
ExecStart=/usr/bin/restic backup /etc /home /root /var/www /var/backups
ExecStart=/usr/bin/restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
And /etc/systemd/system/restic-backup.timer:
[Unit]
Description=Nightly restic backup
[Timer]
OnCalendar=*-*-* 03:30:00
RandomizedDelaySec=15min
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now restic-backup.timer
systemctl list-timers restic-backup.timer
Test a restore#
A backup you never restored is a hope. Once a month:
restic check
restic restore latest --target /tmp/restore-test --include /etc
Important Keep the passphrase and the access to the backup server out of the machine they protect. If an attacker owns the server and can reach the repository, they can delete the backups too. Use a backup user that can only append, or a repository with versioning.
Related: delete a server and get credit, initial server setup.
More in this section
- Add swap space on Ubuntu 24.04Create a swap file on a small Ubuntu 24.04 VPS, make it permanent and tune swappiness so the server survives memory spikes.
- Install Docker and Compose on Ubuntu 24.04Install Docker Engine and the Compose plugin from the official repository, run it as a normal user and avoid the UFW bypass.
- Set up a UFW firewall on Ubuntu 24.04Enable UFW with safe defaults, allow only the ports you use, rate-limit SSH, restrict access by IP and remove rules.
- Create an encrypted volume with LUKSKeep sensitive files in a LUKS-encrypted volume on a single-disk VPS: create, open, close, and what it does and does not protect.
