Guides / Process

Backups that survive an attack, and how to test the restore

beginnerUpdated 26 Nov 2026806 words

A backup is only as good as the last restore that worked. For security, backups have one more job: after a break-in or ransomware, they are how you get your data back without trusting the compromised server. That only works if the attacker could not delete or encrypt them too. Copies that the server cannot delete and a restore test every month cover both jobs.

What to back up

  • Data: databases, uploaded files, object storage buckets, mail.
  • Configuration: everything needed to rebuild the server, ideally as code in a repository (Ansible, cloud-init, compose files). If it only exists on the server, back up /etc and the application directories.
  • Secrets, separately and encrypted: you need them to rebuild, and they must not sit in plain text next to the data.

You do not need to back up what you can rebuild: the operating system, packages, container images that come from a registry.

Keep copies the attacker cannot reach

The usual rule is 3-2-1: three copies, on two kinds of storage, one of them off-site. For attacks, add one more: at least one copy that the server itself cannot delete or overwrite.

If the server holds the credentials to delete its backups, an attacker with root on the server holds them too. Ways to avoid that:

  • Append-only access: the backup tool on the server may add new snapshots but not remove old ones. restic with rest-server --append-only and Borg with borg serve --append-only in the authorized_keys of the backup host both do this. Pruning old snapshots then runs from a different, trusted machine.
  • Pull backups: a backup server connects to the production server and pulls the data. The production server does not hold credentials for the backup server.
  • Object lock: S3-compatible storage with object lock (compliance mode) or bucket versioning keeps old versions for a fixed time even if a client deletes them.

Keep the encryption key or password of the backups somewhere other than the backup storage and the server, for example in the password manager of the team. A backup you cannot decrypt is worth nothing.

Back up files with restic

restic encrypts and deduplicates, and works with local disks, SFTP, S3 and its own REST server. On Debian 12 the package is restic 0.14.0:

sudo apt install restic
export RESTIC_REPOSITORY=sftp:backup@backup.example.com:/srv/restic/web1
export RESTIC_PASSWORD_FILE=/root/.restic-pass     # mode 600
restic init
restic backup /etc /srv/app /var/backups/postgres
restic snapshots

Run the backup from a systemd timer or cron, and make the job report failures (an e-mail, a monitoring check that alerts when the last snapshot is older than a day).

Remove old snapshots, from a trusted machine when the server only has append-only access:

restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune

Back up PostgreSQL

Copying the data directory of a running database gives an inconsistent copy. Use pg_dump (or physical backups with pg_basebackup and WAL archiving for large databases):

pg_dump -U postgres -Fc -f /var/backups/postgres/app-$(date -u +%F).dump app

The custom format (-Fc) is compressed and lets pg_restore pick single tables. With a database in Docker, run the same command with docker exec <container> pg_dump ... and write the output outside the container.

The monthly restore test

Once a month, and after every change to the backup setup, restore into a throwaway place and check the result. Write the date and outcome in your log.

1. The repository is intact:

restic check
restic check --read-data-subset=10%
no errors were found

The second command reads and verifies a tenth of the stored data, so a month of tests covers a good share of it.

2. Files come back:

restic restore latest --target /tmp/restore-test
diff -r /tmp/restore-test/etc/nginx /etc/nginx && echo same

3. The database dump loads:

pg_restore --list app.dump | head
createdb -U postgres app_restore_test
pg_restore -U postgres -d app_restore_test app.dump
psql -U postgres -At app_restore_test -c 'select count(*) from orders'
dropdb -U postgres app_restore_test

Compare the row count with production. A dump that pg_restore cannot load fails here, not on the day you need it.

4. Once or twice a year, rebuild a whole server from configuration and backups on a new VM, and time it. That number is your real recovery time.

After an incident

  • Restore from a snapshot taken before the compromise. Attackers sometimes wait before they act, so look at when the first sign appeared, not when you noticed.
  • Restore onto a new server, never onto the compromised one.
  • Rotate the backup credentials if the server had them.

Sources

Check your servers every day

The agent of Patchbrief reports the packages and settings of a server once a day; the digest names what to fix first.

Start free