# 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.

URL: https://levelrail.com/learn/security/backups-against-ransomware/
Updated: 2026-11-27

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](https://levelrail.com/learn/deploy-operate/backups-and-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](https://levelrail.com/learn/deploy-operate/back-up-a-postgres-database-in-docker/)) and [Postgres backup and restore with pg_dump](https://levelrail.com/learn/databases/postgres-backup-and-restore-with-pg-dump/)). Remember that backups of secrets are secrets too: [manage secrets on servers and containers](https://levelrail.com/learn/security/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](https://levelrail.com/learn/server-basics/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](https://levelrail.com/learn/deploy-operate/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](https://levelrail.com/learn/deploy-operate/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

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| `Permission denied (publickey)` on the pull | Key not installed for the backup user | Add the backup host's public key to `authorized_keys` |
| `rsync: connection unexpectedly closed` | Forced command or path mismatch | Check the restricted command and the path |
| Backup suddenly tiny | Source was emptied or encrypted | Stop pruning, keep older versions, investigate |
| Restore fails with `wrong password` or `config not found` | Repository key lost | Store keys separately and test restoring from them |
| Delete test succeeds with the server's key | Credential is too powerful | Create an add-only key and rotate the old one |
| Job runs but nobody notices failures | No external alert | Report to an outside monitor |
| Disk full on the backup host | Retention too long | Review retention, prune from the backup host |

## Quick reference

```text
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/
```

## Related lessons

- [Backups and the 3-2-1 rule](https://levelrail.com/learn/deploy-operate/backups-and-the-3-2-1-rule/)): the foundation this lesson hardens.
- [Test a backup by restoring it](https://levelrail.com/learn/deploy-operate/test-a-backup-by-restoring-it/)): prove the copy works.
- [Copy files with scp and rsync](https://levelrail.com/learn/server-basics/copy-files-with-scp-and-rsync/)): the transfer tools for a pull job.
- [Manage secrets on servers and containers](https://levelrail.com/learn/security/manage-secrets-on-servers-and-containers/)): protect keys and secret files in backups.
- [Audit a Linux server](https://levelrail.com/learn/security/audit-a-linux-server/)): check who can log in and what is scheduled.

## Next

Continue with [Environment variables and secrets](https://levelrail.com/learn/deploy-operate/environment-variables-and-secrets/)) or the [Server basics checklist](https://levelrail.com/learn/server-basics/initial-server-setup-checklist/)).

## 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

- [Docker docs, volumes backup and restore](https://docs.docker.com/engine/storage/volumes/#back-up-restore-or-migrate-data-volumes)
- [Ubuntu Server docs, backups](https://ubuntu.com/server/docs/backups-introduction)
