People remember backups when a disk dies, a server gets encrypted or someone runs the wrong command. That is when it turns out the copy was on the same server, the database in it is corrupt, and nobody saved the key to the encrypted archive. Here is how to set up backups that actually save you.
The 3-2-1 rule
- 3 copies of the data: the working one and two backups;
- on 2 different media or systems, not two folders on one disk;
- 1 copy somewhere else: another data center, another provider, the office.
Two more digits are often added today: 1 copy that can't be changed or deleted from the working server, and 0 errors when you test a restore. The first protects against ransomware, the second against a surprise at the worst possible moment.
Disk snapshots at your hosting provider are a handy extra, not a replacement: they sit with the same provider, are tied to the same account and are often kept for only a few days.
What to back up
- Data: site files, user uploads, mail, documents.
- Databases, as a separate dump (see below).
- Settings: all of
/etc, cron jobs, systemd units, nginx and application configs. - Secrets: certificates and keys (
/etc/letsencrypt),.envfiles, encrypted. - The package list (
dpkg --get-selectionsorrpm -qa), to rebuild the system quickly. - Docker volumes, if services run in containers: images can be pulled again, data can't.
Databases: dumps only
A file copy of a running database is almost always corrupt: the database keeps writing to the files while they are copied. Make a dump, a consistent snapshot of the data:
mysqldump --single-transaction --routines --triggers --all-databases | gzip > /backup/mysql-$(date +%F).sql.gz pg_dump -Fc -f /backup/app-$(date +%F).dump app
--single-transaction makes a consistent copy of InnoDB tables without locking the site. A backup user only needs read rights; there is no need to dump as root. MySQL and MariaDB hardening helps put MySQL and MariaDB permissions in order.
What to copy with
- rsync copies changes quickly. But it is a mirror, not history: a file deleted or corrupted an hour ago will be deleted or corrupted in the copy too.
- borg and restic keep many versions, and each block of data is stored only once (deduplication), so months of daily copies take little space. Both encrypt data before sending it.
restic -r sftp:backup@backup.example.com:/srv/restic/web1 backup /etc /var/www /backup restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
The last command is rotation: keep 7 daily, 4 weekly and 6 monthly copies and delete the rest.
Protection against ransomware
If the working server can delete its own backups, so can an intruder who gets root on it. So:
- Append-only mode. The server can write new copies but not delete old ones. For borg,
borg serve --append-onlyon the backup server; for restic, rest-server with--append-only. The backup server itself handles rotation. - A restricted SSH key. The key the server uses to reach the storage must not give a shell. In the storage's
authorized_keysit is allowed a single command:restrict,command="borg serve --append-only --restrict-to-path /srv/borg/web1" ssh-ed25519 AAAA.... SSH: config and keys builds such a line. - Or the other way round: the backup server pulls data itself. The working server then has no access to the storage at all.
- Object storage with versioning or object lock: even a deleted file can be recovered.
Encryption and the key
Copies that leave the server must be encrypted. But an encrypted copy without its key is useless, and the key usually lives on the very server that burned down. Store the repository password separately: in a password manager and on paper in a safe. So that nobody holds the whole key, you can split it among several people with Secret sharing: any two of three, for example, can restore it. Individual files can be encrypted with a password before sending in Text and file encryption.
Schedule and monitoring
Run backups with cron or a systemd timer; schedule syntax is covered in the article on cron. A timer is handier: every run lands in the journal, and a run missed while the machine was off happens when it starts.
A backup that quietly stopped working is worse than none, because you believe you have copies. Set up an alert if no copy was made for more than a day: a check on the age of the latest snapshot or a service that waits for a signal after each successful run.
For network equipment, scheduled copies are set up by MikroTik backups and Cisco backups, and the integrity of downloaded archives can be verified with Checksums.