Guides / Process

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

beginnerUpdated 26 Nov 2026817 words

After a break-in or ransomware, backups are how you get the data back without trusting the compromised server. They help only when the attacker could not delete or encrypt them, and when a restore has worked before. The examples use restic for files and pg_dump for PostgreSQL.

What to back up

What Examples
Data databases, uploaded files, object storage buckets, mail
Configuration what you need to rebuild the server, ideally as code in a repository (Ansible, cloud-init, compose files); if it exists only on the server, /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

Leave out what you can rebuild: the operating system, packages, container images from a registry.

Keep copies the attacker cannot reach

The 3-2-1 rule asks for three copies on two kinds of storage, one of them off-site. Against an attacker, at least one copy must be out of reach of the server itself. If the server holds the credentials to delete its backups, an attacker with root on the server holds them too. Three ways around that:

Append-only access

The backup tool on the server may add snapshots but not remove them. restic with rest-server --append-only, and Borg with borg serve --append-only in the authorized_keys of the backup host, both work this way. A different, trusted machine prunes the old snapshots.

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 in compliance mode keeps each object version for a fixed time, even when a client deletes it. Bucket versioning alone keeps the old version after a delete, but a client that may delete versions can still remove it.

Keep a copy of the backup password outside the server and the backup storage, for example in the password manager of the team. restic and Borg cannot read a repository without it.

Back up files with restic

restic encrypts and deduplicates, and writes to 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, or a monitoring check that alerts when the last snapshot is older than a day).

Remove old snapshots (from the trusted machine when the server has append-only access):

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

Back up PostgreSQL

A copy of the data directory of a running database is inconsistent. Use pg_dump, or for large databases physical backups with pg_basebackup and WAL archiving:

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. Check the repository

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

The second command reads and verifies a random tenth of the stored data, a different part each time.

2. Restore files

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

3. Load the database dump

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. Rebuild a whole server

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

After an incident

  • Restore from a snapshot taken before the compromise. Attackers sometimes wait before they act, so go by the first sign of the attack, not by the day you noticed it.
  • 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 Vulnpocalypse collector reports the packages and settings of a server once a day, and the digest lists what to fix, most urgent first.

Start free