Chapter 10 · Lesson 10 of 10
Server security

Backups against ransomware (pull-based, offsite, append-only)

A ransomware-resistant backup is a copy the compromised server cannot reach, change or delete, kept with separate credentials and proven by a test restore.

By the end you will

Design a backup setup that an attacker with root on your server cannot erase, using pull, offsite, immutable storage and tested restores.

Time
25 min
Level
Intermediate
Check
4 questions
You need
  • A server with data you care about
  • A second machine or storage account for backups

Ransomware and plain sabotage both work the same way: get access to a server, then destroy the data and everything that looks like a backup. If the server can write to your backups, then whoever controls the server can erase them. The design goal is a copy that a fully compromised server cannot reach, change or delete. That needs a different shape of backup, not more backups of the same shape. This lesson assumes you know the 3-2-1 rule.

How it works#

Think about what an attacker with root on your server can do. They can read every file on it, including config files that hold backup credentials. They can use any mounted disk or network share. They can run the same backup tool you use, with the same keys, and tell it to delete. So a safe backup design removes those paths:

  • Direction. The backup host pulls from the server. The server holds no credentials for it.
  • Location. At least one copy is offsite, on a different account and network.
  • Immutability. The copy refuses deletes or overwrites for a retention period, or accepts only appends.
  • Separate credentials. The key the server uses, if it pushes at all, can add data but cannot delete or reconfigure.
  • History. Many versions are kept, so a file encrypted yesterday can be restored from last week.
  • Proof. Restores are tested, because encrypted or empty backups look fine until you need them.

Step 1: Decide what must survive#

List the data that cannot be recreated: databases, uploaded files, config, secret files and the Compose files that describe your services. Code in git and container images can be rebuilt, so they matter less. Databases need a consistent dump, not a copy of live files, see back up a Postgres database in Docker and Postgres backup and restore with pg_dump. Remember that backups of secrets are secrets too: manage secrets on servers and containers.

Step 2: Prefer pull over push#

On the backup machine, not the server, run the job:

bash
rsync -a -e "ssh -i ~/.ssh/backup_key" \
  backup@203.0.113.10:/srv/backups/ /backups/server1/current/

The backup machine holds the SSH key. On the server, create a restricted backup account that can only read the export folder, and restrict its key in authorized_keys to a forced command or read-only access. The server has no way to log in to the backup machine at all. Read about the tools in copy files with scp and rsync. Be careful with --delete options on the destination: a pull that mirrors an emptied source will empty your backup too. Keep dated snapshots instead of one mirror.

Step 3: Keep versions#

A single mirror copies damage along with the data. Use a tool that keeps history and deduplicates, such as restic or borg, or filesystem snapshots on the backup machine. Retention should reach back past the time an attack could go unnoticed. Choose the number of versions from how long you could take to notice a problem, and what space allows.

Step 4: Add an offsite, immutable copy#

Send a second copy to storage in another location. Look for two features in whatever you use, and check them in the provider’s documentation rather than assuming:

  • Append-only mode, where the credential in use can add new backup data but not delete or alter existing data.
  • Object lock or retention, where objects cannot be deleted or overwritten until the retention period ends, even with an admin key.

Give the server (or the backup host) a credential that holds only the add permission. Keep the delete and admin credentials somewhere else, such as a password manager on your own machine, and do not store them on the server. Then test it: try to delete a test object with the server’s key. It must fail. If it succeeds, the protection is not real.

Step 5: Test restores on a schedule#

Restore to a fresh machine or folder and run the application against it. The routine is in test a backup by restoring it. Check three things: files open, the database starts and has recent rows, and you know the steps and the time it takes. Write the steps down and keep them off the server, since the server may be the thing that is gone. Also keep the encryption keys and repository passwords for your backups somewhere separate from the backups.

Step 6: Watch the backup job#

A job that quietly stops running is a common failure. Have it report success or failure to something outside the server, and alert when no success arrives in the expected window. A sudden drop in backup size is also a sign: an encrypted or emptied source produces a strangely small or strangely different backup. See monitor uptime simply for a simple pattern.

Check it worked#

Run this checklist and answer each with evidence, not belief:

  1. Log in to the production server as root. Can you reach the backup host or storage with delete rights? The answer must be no.
  2. Try to delete a recent backup object using the credentials stored on the server. It must fail.
  3. Restore last week’s version into an empty folder. It must open correctly.
  4. Show the date of the last successful job. It must be recent.

If any answer is wrong, fix that before relying on the setup.

Edge cases and gotchas#

  • Mounted backup disks. A disk or share mounted on the server is writable by an attacker, the same as any other folder. Unmount it after the job or do not mount it.
  • Synced folders are not backups. File sync tools copy deletions and encryption to every device.
  • Snapshots on the same host. A snapshot lives on the same machine and may be removed by root. It helps against mistakes, not against intruders.
  • Backup software with write access. If the tool’s own config holds an admin key, one stolen file undoes everything. Use a limited key for routine runs.
  • Pruning jobs. Retention cleanup needs delete rights. Run it from the backup host, not from the server.
  • Costs and limits. Check your provider’s pricing and retention terms yourself before you rely on them. This lesson makes no claims about any vendor.

Troubleshooting#

SymptomLikely causeFix
Permission denied (publickey) on the pullKey not installed for the backup userAdd the backup host’s public key to authorized_keys
rsync: connection unexpectedly closedForced command or path mismatchCheck the restricted command and the path
Backup suddenly tinySource was emptied or encryptedStop pruning, keep older versions, investigate
Restore fails with wrong password or config not foundRepository key lostStore keys separately and test restoring from them
Delete test succeeds with the server’s keyCredential is too powerfulCreate an add-only key and rotate the old one
Job runs but nobody notices failuresNo external alertReport to an outside monitor
Disk full on the backup hostRetention too longReview retention, prune from the backup host

Quick reference#

output
Pull from the backup host, not push from the server
Keep dated versions, not one mirror
One offsite copy with append-only or object lock
Server credential can add, cannot delete
Test a restore on a schedule
Keep keys and restore steps off the server
bash
rsync -a -e "ssh -i ~/.ssh/backup_key" backup@203.0.113.10:/srv/backups/ /backups/server1/current/

Next#

Continue with Environment variables and secrets or the Server basics checklist.

Check what you learned4 questions
1A server pushes backups to a bucket using an access key stored on the server. What is the main risk?
2Which design keeps credentials for the backup machine off the production server?
3What does an immutable or object-lock copy protect against?
4Before you delete old backups to save space, what should you confirm?

Frequently asked questions

Why can ransomware delete my backups?

If the server can write to the backup location with stored credentials, an attacker who gets root on the server can use the same access to delete or encrypt the backups. A backup that the server can modify is only as safe as the server.

What is a pull-based backup?

The backup machine connects to the server and copies data, instead of the server pushing to the backup. The server holds no credentials for the backup machine, so a compromised server cannot reach it.

What is object lock or append-only storage?

These are storage features that refuse deletes and overwrites for a set retention period, or that allow only new data to be added. Check how your storage provider implements them and test that a delete really fails.

How do I know my backup works?

Restore it to a separate machine and check that the application starts and the data is complete. A backup that was never restored is a hope, not a backup.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios