Every server will eventually lose data: a failed disk, a mistaken rm, a broken update, ransomware, or an account cancelled by accident. Hosting providers — Server.HK included — make clear that customers are responsible for their own backups. The good news is that a reliable, automated, encrypted, off-site backup takes about half an hour to set up with restic.
Why restic
| Feature | Why it matters |
|---|---|
| Encryption by default | Backups stored elsewhere cannot be read without your password |
| Deduplication | Only changed data is stored; daily backups take little space |
| Many back-ends | Another server over SFTP, S3-compatible storage, Backblaze B2, local disks and more |
| Snapshots | Restore any file from any day you kept |
| Single binary | Easy to install anywhere |
The 3-2-1 rule
Keep 3 copies of your data, on 2 different types of storage, with 1 copy off-site. For a server, that usually means: the live data, a backup on another server or cloud storage in a different location, and ideally a second backup location or provider.
A backup on the same server or disk is not a backup against disk failure or compromise.
Step 1: Install
sudo apt install restic # Ubuntu / Debian
sudo dnf install restic # AlmaLinux / Rocky (EPEL)
sudo restic self-update # get the latest version
restic version
Step 2: Choose where backups go
Option A: another server over SFTP
A second VPS or dedicated server in another location (for example, back up a Hong Kong server to Japan or the US). Set up an SSH key for the backup user (SSH keys) and restrict it on the target if you like. Repository URL:
sftp:backup@198.51.100.30:/srv/restic/web1
Option B: S3-compatible object storage
Most cloud object storage services speak the S3 API:
s3:https://s3.example-provider.com/my-bucket/web1
with AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY set.
Option C: Backblaze B2
b2:my-bucket:web1
with B2_ACCOUNT_ID and B2_ACCOUNT_KEY.
Step 3: Credentials file
Keep settings in one root-only file:
sudo mkdir -p /etc/restic
sudo nano /etc/restic/env
export RESTIC_REPOSITORY="sftp:backup@198.51.100.30:/srv/restic/web1"
export RESTIC_PASSWORD_FILE="/etc/restic/password"
# For S3:
# export AWS_ACCESS_KEY_ID="..."
# export AWS_SECRET_ACCESS_KEY="..."
openssl rand -base64 32 | sudo tee /etc/restic/password > /dev/null
sudo chmod 600 /etc/restic/env /etc/restic/password
Store a copy of the repository password somewhere safe outside the server (a password manager). Without it, the backups cannot be decrypted — by you or anyone else.
Step 4: Initialise the repository
sudo bash -c 'source /etc/restic/env && restic init'
Step 5: Back up databases first
Files of a running database are not a consistent backup. Dump the database to a file, then let restic back up the dump:
sudo mkdir -p /var/backups/db
sudo mariadb-dump --single-transaction --routines --all-databases | gzip > /var/backups/db/all.sql.gz
Details for MySQL/MariaDB, including per-database dumps and users: backing up MySQL and MariaDB. For PostgreSQL use pg_dumpall or pg_dump; for Docker apps, dump from the container (Docker Compose backups).
Step 6: The backup script
/usr/local/bin/restic-backup.sh:
#!/usr/bin/env bash
set -euo pipefail
source /etc/restic/env
# 1. Database dump
mkdir -p /var/backups/db
mariadb-dump --single-transaction --routines --all-databases | gzip > /var/backups/db/all.sql.gz
# 2. Back up files
restic backup \
/etc \
/var/www \
/var/backups/db \
/home \
/root \
--exclude-caches \
--exclude '/var/www/*/cache' \
--exclude '*.tmp' \
--tag daily
# 3. Retention: keep 7 daily, 4 weekly, 6 monthly
restic forget --prune --keep-daily 7 --keep-weekly 4 --keep-monthly 6
# 4. Verify a sample of the data
restic check --read-data-subset=2%
# 5. Heartbeat (optional)
# curl -fsS -m 10 https://status.example.com/api/push/XXXX?status=up > /dev/null
sudo chmod 700 /usr/local/bin/restic-backup.sh
sudo /usr/local/bin/restic-backup.sh
The first run uploads everything; later runs only send changes.
What to include
| Include | Why |
|---|---|
/etc |
All configuration (Nginx, PHP, SSH, cron) |
/var/www or app directories |
Code and uploads |
| Database dumps | Your data |
/home, /root |
Scripts, keys, user files |
| Docker volumes / bind mounts | App data |
Skip what can be reinstalled: /usr, /var/lib/docker/overlay2, caches, node_modules.
Step 7: Schedule it
With a systemd timer (logs in the journal, catches up after downtime):
/etc/systemd/system/restic-backup.service:
[Unit]
Description=restic backup
[Service]
Type=oneshot
ExecStart=/usr/local/bin/restic-backup.sh
Nice=19
IOSchedulingClass=idle
/etc/systemd/system/restic-backup.timer:
[Unit]
Description=Daily restic backup
[Timer]
OnCalendar=*-*-* 03:15
RandomizedDelaySec=15m
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now restic-backup.timer
systemctl list-timers restic*
sudo journalctl -u restic-backup -n 50
Or use cron with output redirected to a log.
Step 8: Restore — and test it
A backup you have never restored is a hope, not a backup.
source /etc/restic/env
restic snapshots
restic ls latest /var/www/example.com | head
restic restore latest --target /tmp/restore --include /var/www/example.com
restic mount /mnt/restic # browse all snapshots as folders (needs FUSE)
Restore a single file to compare, and at least once a quarter do a full test restore onto a spare server: restore files, import the database dump, start the site. Write down the steps — that document is your disaster recovery plan.
Protect backups from ransomware
If an attacker gets root on the server, they can read /etc/restic/env and delete the backups too. Mitigations:
- Use storage with object lock / immutability or versioning (many S3-compatible services offer it).
- On an SFTP target, run
rest-server --append-onlyinstead of plain SFTP, so the client can add but not delete snapshots; runforget --prunefrom the backup server itself. - Keep a second, independent backup.
Monitoring
- Heartbeat from the script to Uptime Kuma or a similar service.
restic snapshotsshould show a new snapshot every day — check weekly.- Watch repository size growth:
restic stats --mode raw-data.