The 3-2-1 backup rule for servers, and how to test your restores
Backups fail in boring ways: nothing was copied, the copy was on the same disk, or nobody knew how to restore. The 3-2-1 rule and a restore test fix most of that.
The 3-2-1 rule
Keep 3 copies of your data, on 2 different kinds of storage, with 1 copy off-site. For a server, that usually means: the live data, a local snapshot or dump, and an encrypted copy in separate storage that the server cannot delete.
What to back up
- Databases, as consistent dumps rather than raw files
- Site files and uploads
- Configuration: web server, PHP, cron, firewall
- Application secrets and encryption keys, stored separately and securely
- Anything that is expensive to recreate by hand
Encrypt before it leaves the server
Encrypt with a public key on the server, and keep the private key somewhere else. If the server is compromised, the attacker cannot read old backups.
gpg --import backup-public.asc mysqldump --single-transaction appdb | gzip | gpg --encrypt -r [email protected] > appdb-$(date +%F).sql.gz.gpg
A simple nightly job
A small script run from cron is enough to start. This example dumps, encrypts and ships with rclone, then writes a status file you can monitor:
#!/bin/bash set -euo pipefail D=$(date +%F) mysqldump --single-transaction appdb | gzip | gpg --encrypt -r [email protected] > /var/backups/appdb-$D.sql.gz.gpg rclone copy /var/backups/appdb-$D.sql.gz.gpg offsite:backups/ echo "{\"state\":\"ok\",\"date\":\"$D\"}" > /var/lib/backup-status.json
30 2 * * * root /usr/local/sbin/nightly-backup
Retention
Keep several generations. A common pattern is 14 daily copies, 8 weekly and 6 monthly, so you can recover from a problem you notice late. Make sure old copies are deleted automatically, or storage will quietly fill up.
Test the restore
Until you have restored a backup, you do not know it works. Do this at least every quarter:
- Download the newest backup to a scratch machine
- Decrypt it with the private key
- Import the database into a scratch database
- Compare table and row counts with production
- Open the restored site and click through it
- Write down how long it took
Alert on silence
A backup job that stops running makes no noise. Have it ping a monitor or write a status file that something checks, and alert when it is missing or old.
Questions
Are provider snapshots enough?
They are a good extra layer, but they live in the same account as the server. Keep an encrypted copy somewhere an attacker with your cloud login cannot reach.
How often should I back up?
Decide how much data you can afford to lose. Nightly is common. Busy shops may need database backups every few hours.
More guides
Ubuntu server hardening checklist
A step-by-step Ubuntu server hardening checklist: key-only SSH, UFW firewall, automatic security updates, fail2ban and how to verify each step.
Read the guide →How to migrate a website to a new server without downtime
A practical website migration plan: inventory, copy with rsync and mysqldump, test before DNS changes, lower the TTL, switch at a quiet hour, keep a rollback.
Read the guide →SPF, DKIM and DMARC explained, with examples
What SPF, DKIM and DMARC do, how to write the DNS records, how to check them with dig, how to roll out DMARC safely and the mistakes that break email delivery.
Read the guide →Want this done for you?
We set up and check all of this on client servers, with a backup first and a written record.