Security and hardening · Advanced · 24 min
Protect backups from ransomware with append-only and immutable copies
By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · rest-server 0.15.0 · restic 0.20.0
You will run rest-server 0.15 in append-only mode so a compromised web server can add backups but never delete them, prune from a separate admin machine, test restores, and plan an S3 Object Lock copy offsite.
On this page
Ransomware crews rarely start by encrypting files. They start by finding the backups, because a company that can restore does not pay. On a typical server the backup job runs as root with credentials that can write, overwrite and delete the backup repository. Whoever takes root on that server inherits the same rights, and a single restic forget --keep-last 1 --prune or rm -rf wipes months of history before the encryption begins.
The fix is to make sure the machine that sends backups cannot destroy them. In this tutorial you put a restic repository behind rest-server 0.15.0 in append-only mode on a separate backup host. The web server gets its own login that can add and read snapshots, and nothing else. Deleting old snapshots happens only from an admin laptop that reaches the backup host over SSH. You then attack your own setup from the web server to prove the deletes are refused, run a restore drill, and plan a third copy in an S3 bucket with Object Lock in compliance mode, which even the account owner cannot delete early.
This tutorial is about the attacker, not about restic basics. If you have not used restic before, work through Back up a server to an encrypted repository with restic first.
Step 1 · Decide what each machine is allowed to destroy
A common target is the 3-2-1-1-0 rule:
- 3 copies of the data: the live data and two backups.
- 2 different kinds of storage, so one failure mode does not take both.
- 1 copy offsite, away from the building, the cloud region or the provider account.
- 1 copy that is offline or immutable, so no login can change it for a set time.
- 0 errors when you test a restore.
Write the attack you are defending against in one sentence: "An attacker has root on the web server, with every file and credential on it." Then, for each place a backup lives, ask what that attacker can do with what is on the web server.
In the layout above:
- The web server holds a rest-server login and the repository password. With them an attacker can read your backups (so keep treating the backups as sensitive) and add junk snapshots, but cannot delete or replace anything.
- The backup host accepts no SSH logins from the web server. An attacker would need a second, separate break-in to reach it.
- The admin laptop holds the only SSH key for the backup host. Keep it for administration, protect the key with a passphrase, and turn on full-disk encryption.
- The offsite bucket is locked by the storage provider. Even with the backup host and the cloud credentials, nobody can delete a locked version before its date.
If the attacker could also reach the backup host, append-only mode would not help, so the separation of logins matters as much as the flag.
For teams in Nigeria and across Africa where the backup host and the offsite copy cross a metered or prepaid link, plan the first full upload for a cheap or unlimited window. After that, restic uploads only changed chunks, which is usually a small fraction of the data.
Check it worked
You have a short table, in your incident plan or a notes file, with one row per backup location (live data, backup host, offsite) and two columns: "who can write" and "who can delete". The web server appears in no "who can delete" cell.
Step 2 · Install restic and rest-server
restic 0.20.0 is the current release. Install it from the project's release binary on both the web server and the backup host, checking the SHA-256 sum before installing. Ubuntu's archive has an older restic; the release binary keeps both machines on the same version.
On the web server and on the backup host:
sudo apt-get update
sudo apt-get install -y curl bzip2 ca-certificates openssl
mkdir -p ~/restic-install
cd ~/restic-install
ARCH=$(dpkg --print-architecture)
curl -fsSLO "https://github.com/restic/restic/releases/download/v0.20.0/restic_0.20.0_linux_${ARCH}.bz2"
curl -fsSLO https://github.com/restic/restic/releases/download/v0.20.0/SHA256SUMS
sha256sum --check --ignore-missing SHA256SUMS
bunzip2 "restic_0.20.0_linux_${ARCH}.bz2"
sudo install -m 755 "restic_0.20.0_linux_${ARCH}" /usr/local/bin/restic
cd ~
rm -rf ~/restic-install
restic versionThe checksum line must end in OK. If it says FAILED, stop and download again.
rest-server is the small HTTP server from the restic project that serves a repository over HTTPS. Version 0.15.0 (released 23 November 2026) ships as a tarball per platform with a SHA256SUMS file. You also need htpasswd from apache2-utils to create its logins.
On the backup host only:
sudo apt-get install -y apache2-utils
mkdir -p ~/rest-server-install
cd ~/rest-server-install
ARCH=$(dpkg --print-architecture)
curl -fsSLO "https://github.com/restic/rest-server/releases/download/v0.15.0/rest-server_0.15.0_linux_${ARCH}.tar.gz"
curl -fsSLO https://github.com/restic/rest-server/releases/download/v0.15.0/SHA256SUMS
sha256sum --check --ignore-missing SHA256SUMS
tar xzf "rest-server_0.15.0_linux_${ARCH}.tar.gz"
sudo install -m 755 "rest-server_0.15.0_linux_${ARCH}/rest-server" /usr/local/bin/rest-server
cd ~
rm -rf ~/rest-server-install
rest-server --versionBoth binaries sit in /usr/local/bin and get no updates from apt, including security fixes. Watch the two projects' GitHub releases pages. To upgrade restic, run sudo restic self-update. To upgrade rest-server, repeat the block above with the new version number, then restart the service.
Check it worked
restic version prints a line starting restic 0.20.0 compiled with go. rest-server --version prints something like rest-server 0.15.0 compiled with go1.26.x on linux/amd64.
Step 3 · Run rest-server in append-only mode over HTTPS
rest-server runs as its own system user, with no shell and no home directory you would log in to. It owns the data folder; nothing else on the backup host needs to write there.
On the backup host:
sudo useradd --system --home-dir /srv/rest-server --no-create-home --shell /usr/sbin/nologin rest-server
sudo install -d -m 700 -o rest-server -g rest-server /srv/rest-server
sudo install -d -m 750 -o root -g rest-server /etc/rest-serverBasic authentication over plain HTTP would send the web server's password in clear text with every request, so serve HTTPS. On a private network a self-signed certificate works, because you hand its public half to the web server and restic checks against that file only. Replace backup.example.internal with your backup host's name in this command and every later one. The key is written with a private umask from the start, then opened to the rest-server group:
sudo sh -c 'umask 077; openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes -days 825 -subj "/CN=backup.example.internal" -addext "subjectAltName=DNS:backup.example.internal" -keyout /etc/rest-server/tls.key -out /etc/rest-server/tls.crt'
sudo chgrp rest-server /etc/rest-server/tls.key
sudo chmod 640 /etc/rest-server/tls.key
sudo chmod 644 /etc/rest-server/tls.crtThe certificate is valid for 825 days. Put the renewal date in your calendar now.
Next, give the web server its own credentials. Two secrets live on the web server: the rest-server login password (which only opens the HTTP door) and the repository password (which decrypts the data). Generate both into private files.
On the web server:
sudo install -d -m 700 /root/restic
sudo install -m 600 /dev/null /root/restic/repo-password
head -c 32 /dev/urandom | base64 | sudo tee /root/restic/repo-password > /dev/null
sudo install -m 600 /dev/null /root/restic/rest-password
head -c 24 /dev/urandom | base64 | sudo tee /root/restic/rest-password > /dev/nullPrint them once, save both in your password manager under clear names, then clear the terminal. The admin laptop will need the repository password later; the rest-server password goes into the backup host in a moment.
sudo cat /root/restic/repo-password /root/restic/rest-passwordBack on the backup host, create the login file private first, then add the user web1. htpasswd asks for the password twice; paste the rest-server password (the second line you printed). -B stores it as a bcrypt hash, which rest-server accepts.
sudo install -m 640 -o root -g rest-server /dev/null /etc/rest-server/htpasswdsudo htpasswd -B /etc/rest-server/htpasswd web1Now the service. The flags that matter for ransomware:
--append-onlylets clients read and add files, and refuses every delete except on lock files. Replacing the repository'sconfigfile is refused too.--private-reposlets the userweb1reach only/web1/, so a second server you add later cannot touch this one's repository.--max-sizecaps the total size of everything under--path. An attacker cannot delete your backups, but could try to fill the disk with junk; this stops the upload instead. 100 GiB is shown; size it to your data with room for growth.
Write the unit file. The sandboxing lines follow the project's example unit, trimmed for a server that opens its own port:
sudo tee /etc/systemd/system/rest-server.service > /dev/null <<'EOF'
[Unit]
Description=restic rest-server (append-only)
After=network-online.target
Wants=network-online.target
[Service]
User=rest-server
Group=rest-server
ExecStart=/usr/local/bin/rest-server --path /srv/rest-server --listen :8000 --append-only --private-repos --max-size 107374182400 --htpasswd-file /etc/rest-server/htpasswd --tls --tls-cert /etc/rest-server/tls.crt --tls-key /etc/rest-server/tls.key
Restart=always
RestartSec=5
UMask=077
ReadWritePaths=/srv/rest-server
NoNewPrivileges=yes
CapabilityBoundingSet=
PrivateTmp=yes
PrivateDevices=yes
ProtectSystem=strict
ProtectHome=yes
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectControlGroups=yes
RestrictSUIDSGID=yes
LockPersonality=yes
MemoryDenyWriteExecute=yes
SystemCallArchitectures=native
SystemCallFilter=@system-service
[Install]
WantedBy=multi-user.target
EOFsudo systemctl daemon-reload
sudo systemctl enable --now rest-server
sudo systemctl status rest-server --no-pagerAllow port 8000 from the web server only. Replace 203.0.113.10 with your web server's address. If ufw is new to you, read Set up a UFW firewall on Ubuntu 26.04 first, so you do not lock yourself out of SSH:
sudo ufw allow from 203.0.113.10 to any port 8000 proto tcpCheck the server's ~/.ssh/authorized_keys files and /etc/ssh/sshd_config while you are here: the backup host should accept SSH keys from the admin laptop and nothing from the web server.
Check it worked
systemctl status rest-server shows active (running). Ask for the repository without a password:
curl -s -o /dev/null -w '%{http_code}\n' --cacert /etc/rest-server/tls.crt https://backup.example.internal:8000/web1/It prints 401: TLS works and the server wants a login.
Step 4 · Back up the web server to its private repository
The web server needs the backup host's certificate so restic can check it is talking to the right machine. Copy it across from your admin laptop (replace both host names):
scp backup.example.internal:/etc/rest-server/tls.crt .
scp tls.crt web.example.internal:On the web server, move it next to the other restic files:
sudo install -m 644 ~/tls.crt /root/restic/backup-ca.crt
rm ~/tls.crtrestic reads the rest-server login from RESTIC_REST_USERNAME and RESTIC_REST_PASSWORD, and the certificate from RESTIC_CACERT. Put them in a wrapper so the password never sits in the repository URL, on a command line or in shell history. The wrapper itself holds no secrets; it reads them from the root-only files at run time:
sudo tee /usr/local/sbin/restic-backup > /dev/null <<'EOF'
#!/bin/sh
# restic for this server's append-only repository on the backup host.
export RESTIC_REPOSITORY=rest:https://backup.example.internal:8000/web1/
export RESTIC_REST_USERNAME=web1
RESTIC_REST_PASSWORD=$(cat /root/restic/rest-password)
export RESTIC_REST_PASSWORD
export RESTIC_PASSWORD_FILE=/root/restic/repo-password
export RESTIC_CACERT=/root/restic/backup-ca.crt
exec /usr/local/bin/restic "$@"
EOF
sudo chmod 755 /usr/local/sbin/restic-backup
sudo restic-backup initinit is allowed in append-only mode because it only creates files. restic will not run init on an existing repository, and rest-server would refuse to replace config anyway.
Create a small folder to practise on, so the drill never touches real site data, and take two snapshots. The second one is tagged drill; you will use it to test deletes:
sudo mkdir -p /srv/drill-site
echo '<h1>Drill site</h1>' | sudo tee /srv/drill-site/index.html > /dev/null
echo 'order 1001, paid' | sudo tee /srv/drill-site/orders.csv > /dev/null
sudo restic-backup backup /etc /srv/drill-site
sudo restic-backup backup /etc /srv/drill-site --tag drillIn daily use, back up your real paths (and a database dump, as in Back up and restore PostgreSQL with pg_dump) on a timer, as in Automate and test restic backups with a systemd timer, with restic-backup in place of the local wrapper.
Check it worked
sudo restic-backup snapshotsThe table lists two snapshots of /etc and /srv/drill-site from this host, the newer one with the tag drill.
Step 5 · Attack your own backups from the web server
Now act as the attacker who has root on the web server. The first thing they would try is restic's own clean-up, keeping only the newest snapshot and deleting the rest:
sudo restic-backup forget --keep-last 1 --prunerestic lists the snapshot it wants to remove, then prints unable to remove snapshot/<id> from the repository and exits with an error. It never reaches the prune.
A careful attacker would skip restic and talk to the server directly. Try to delete the repository's config file, without which nothing in the repository can be opened. The login is fed to curl on standard input, so it does not show in the process list:
sudo sh -c 'printf "user = \"web1:%s\"\n" "$(cat /root/restic/rest-password)" | curl -s -o /dev/null -w "%{http_code}\n" -K - --cacert /root/restic/backup-ca.crt -X DELETE https://backup.example.internal:8000/web1/config'It prints 403. rest-server answers every DELETE that is not on a lock file with 403 Forbidden.
Append-only mode does not stop everything. The attacker can still read every backup with the repository password, so rotate both passwords after an incident (see Rotate a leaked secret without downtime). They can also add fake snapshots, which matters for the next step.
Check it worked
sudo restic-backup snapshotsBoth snapshots are still listed.
Step 6 · Prune from the admin laptop with keep-within
Old snapshots still need removing, or the backup host fills up. rest-server stores repositories in the same layout as restic's local backend, so the admin can run restic directly against /srv/rest-server/web1 on the backup host, as the rest-server user, bypassing the HTTP server and its append-only rule. The web server has no way in to do the same.
From your admin laptop, open a session on the backup host:
ssh backup.example.internalIn that session, list the snapshots. restic asks for the repository password; paste it from your password manager. --no-cache stops restic writing a cache on the backup host.
sudo -u rest-server restic --no-cache -r /srv/rest-server/web1 snapshotsRemove the drill snapshot by its ID from the first column (replace <drill-snapshot-id>), and prune the data only it used:
sudo -u rest-server restic --no-cache -r /srv/rest-server/web1 forget <drill-snapshot-id> --pruneFor routine clean-up, choose the policy with the attacker in mind. A policy such as --keep-daily 7 --keep-weekly 4 keeps the newest snapshot in each day or week. An attacker on the web server can create empty snapshots with restic backup --time set just after each of your real ones; the next prune would then keep the fakes and delete your real data. --keep-within 30d keeps every snapshot from the last 30 days, real and fake alike, so nothing real is lost and the extra snapshots stand out when you look:
sudo -u rest-server restic --no-cache -r /srv/rest-server/web1 forget --keep-within 30d --pruneRead the snapshot list before every prune. More snapshots than your timer would make, odd times, or snapshots with almost no data are a sign the web server is compromised: stop, keep everything, and follow your incident plan. Pruning by hand once a month is a reasonable cadence for a small team; do not schedule it on the web server, which would need the delete rights you have just taken away.
Check it worked
The forget with the ID prints removed snapshot/<id> and a prune summary ending in done. The --keep-within 30d run lists one snapshot under keep and removes nothing. Back on the web server:
sudo restic-backup snapshotsOne snapshot is left, with no drill tag.
Step 7 · Prove you can restore
The 0 in 3-2-1-1-0 is about evidence: a backup you have never restored is a guess. Check part of the stored data and restore the drill folder from the web server, which can still read everything:
sudo restic-backup check --read-data-subset=10%
sudo restic-backup restore latest --target /tmp/restore-drill --include /srv/drill-site
sudo diff -r /srv/drill-site /tmp/restore-drill/srv/drill-site && echo "restore matches"
sudo rm -rf /tmp/restore-drill--read-data-subset=10% downloads and checks a random tenth of the stored data. To cover the whole repository over a year without large monthly downloads, run the check monthly with the month number as the group, so January checks part 1 of 12 and December part 12:
sudo restic-backup check --read-data-subset=$(date +%-m)/12Twice a year, do a full drill on a spare machine: restore the latest snapshot of one of your production servers, start the application from it, and time how long it took. That number goes in your incident response plan.
Check it worked
check ends with no errors were found. diff prints nothing and the next line prints restore matches.
Step 8 · Add an immutable offsite copy with S3 Object Lock
The backup host protects you from a compromised web server. It does not protect you from someone who gets into the backup host itself, from a fire, or from losing the provider account. The last copy goes offsite into storage that refuses deletes for a fixed time, whoever asks.
In Amazon S3 that is Object Lock. It needs versioning, which it turns on for you. In compliance mode, AWS states that no user, including the account's root user, can overwrite or delete a locked object version, change its mode, or shorten its retention before the retain-until date. In governance mode, users with a special permission can still bypass the lock, which is useful for testing but not what you want against an attacker.
Compliance mode cannot be undone for objects already written. Try it first in a throwaway bucket with "Days":1, and choose your real retention (30 days is shown) knowing you will pay to store every locked version for that long.
On your admin laptop, with the AWS CLI signed in to the account that will hold the copy (replace <bucket-name> and the region):
aws s3api create-bucket --bucket <bucket-name> --region eu-west-2 --create-bucket-configuration LocationConstraint=eu-west-2 --object-lock-enabled-for-bucket
aws s3api put-object-lock-configuration --bucket <bucket-name> --object-lock-configuration '{"ObjectLockEnabled":"Enabled","Rule":{"DefaultRetention":{"Mode":"COMPLIANCE","Days":30}}}'
aws s3api get-object-lock-configuration --bucket <bucket-name>Create an IAM user for the copy with access to this bucket only, and its access key. Every object restic writes now gets a 30-day lock by default.
Run the copy on the backup host, started by you from the admin laptop, so the cloud credentials never exist on the web server. Create the credentials file private first, then open it with sudoedit:
sudo install -m 600 -o rest-server -g rest-server /dev/null /etc/rest-server/offsite.env
sudoedit /etc/rest-server/offsite.envPut these lines in it, replacing the placeholders with the IAM user's key and your bucket:
AWS_ACCESS_KEY_ID=replace-with-the-access-key-id
AWS_SECRET_ACCESS_KEY=replace-with-the-secret-access-key
RESTIC_REPOSITORY=s3:s3.eu-west-2.amazonaws.com/<bucket-name>/web1
RESTIC_FROM_REPOSITORY=/srv/rest-server/web1Create the offsite repository with the same chunker settings, so data deduplicates across both, then copy the snapshots. restic asks for the source repository's password and, for init, a new password for the offsite one (store it in your password manager too):
sudo -u rest-server sh -c 'set -a; . /etc/rest-server/offsite.env; set +a; restic --no-cache init --from-repo "$RESTIC_FROM_REPOSITORY" --copy-chunker-params'
sudo -u rest-server sh -c 'set -a; . /etc/rest-server/offsite.env; set +a; restic --no-cache copy --from-repo "$RESTIC_FROM_REPOSITORY"'Run the copy line after each monthly prune, or on a timer on the backup host. Later copies only upload new chunks.
What happens when someone deletes from the bucket: restic and most tools send a simple DELETE, which S3 accepts by adding a delete marker on top. The locked version underneath stays until its date, so the data can be brought back by restoring earlier object versions. A DELETE aimed at a specific locked version is refused with 403. Two consequences to plan for:
- Prune on the offsite repository only rarely. Pruned files still cost storage until their lock expires.
- Add an S3 lifecycle rule that removes noncurrent versions a few days after the retention period, or storage grows for ever.
AWS notes that the only way to remove compliance-locked data early is to close the AWS account, so lock the account down too: MFA on the root user and no long-lived root keys. Other S3-compatible providers offer Object Lock with their own rules; read their documentation for compliance mode before relying on it.
Check it worked
get-object-lock-configuration prints "ObjectLockEnabled": "Enabled" with "Mode": "COMPLIANCE" and your number of days. After the copy, restic snapshots against the offsite repository lists the same snapshots as the backup host. In the S3 console, an object under web1/data/ shows a retention mode of Compliance and a retain-until date.
Next steps
- Put the restore time from your drill, and who holds the admin laptop and the offsite passwords, into Write an incident response plan for a small team.
- Schedule the web server's backups and a weekly
checkwith Automate and test restic backups with a systemd timer. - Add a consistent database dump to what you back up with Back up and restore PostgreSQL with pg_dump.
- After any suspected break-in, change the rest-server and repository passwords with Rotate a leaked secret without downtime.
Related tutorials
- Write custom Fail2ban jails and filters for your own appsYou will have Fail2ban 1.1 banning repeated failed sign-ins to your own app and to an nginx basic-auth area, with filters you tested against sample logs, your own addresses allow-listed, and longer bans for repeat offenders.
- Validate file uploads before you store or serve themYou will take a FastAPI image upload route that trusts everything the browser sends, prove its flaws with pytest, and rebuild it with size limits, type checks from the file's own bytes, Pillow re-encoding, random storage names and safe download headers.
- Use SSH certificates instead of copying keys to every serverYou will run a small SSH certificate authority with ssh-keygen, log in to an Ubuntu 26.04 server with an eight-hour user certificate, give the server a host certificate so clients stop asking about fingerprints, and revoke a certificate the server then refuses.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials