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

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

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

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:

  1. Append-only mode. The server can write new copies but not delete old ones. For borg, borg serve --append-only on the backup server; for restic, rest-server with --append-only. The backup server itself handles rotation.
  2. A restricted SSH key. The key the server uses to reach the storage must not give a shell. In the storage's authorized_keys it 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.
  3. Or the other way round: the backup server pulls data itself. The working server then has no access to the storage at all.
  4. 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.

You don't have a backup until you have restored from it. Once a month or a quarter, restore the server or database onto a test machine and check that the site comes up. Time it while you're at it: that's how long the outage will last in a real emergency.

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.