Security

Backups for a small production server that you can restore

What to back up on a single-server app, how to send an encrypted copy off the server with pg_dump and restic, what the provider's backups add, and the restore drill that proves it works. Every command tested.

TL;DR

  • Back up the data you can't recreate (a database dump made with the database's own tool, uploaded files and the configuration that runs the app), and keep secrets in a password manager, not only in the backup.
  • Copy it off the server every night, encrypted: restic sends the dump and the app folder to an S3-compatible bucket and keeps 7 daily, 4 weekly and 6 monthly snapshots.
  • Add the provider's server backups as a second layer: Hetzner (opens in a new tab) charges 20% of the server's price for 7 daily copies; DigitalOcean (opens in a new tab)'s basic plans 20% for weekly or 30% for daily.
  • Restore the latest snapshot into a throwaway database every month, and send a heartbeat ping after each successful run to a service that alerts you when the ping is missing.
On this page

A VPS backup strategy for a server that runs one production app comes down to four things: dump the database every night with its own tool, send that dump and the app's files encrypted to storage outside the server, keep the provider's server backups as a second layer, and restore a backup on a schedule to prove it works. We ran everything below on 11 November 2026 on Ubuntu 24.04 with PostgreSQL (opens in a new tab) 18.6 in Docker (opens in a new tab) Compose and restic 0.16.4, against a local S3-compatible server standing in for the bucket.

It is the fourth step of our checklist for hardening an Ubuntu 24.04 server: hardening keeps people out; backups are what you have left when something gets through anyway, or when the cause is a bad migration or a mistyped command.

What to back up

What a single-server app needs to come back
WhatWhyHow
The databaseYour users' data; the one thing you can't rebuildA dump with the database's own tool (pg_dump, mysqldump, mongodump)
Uploaded filesImages, documents, anything users sentThe folder or volume that holds them, as files
The app's configurationcompose.yaml, the proxy's config, scripts, cron or timer filesThe app's folder, for example /srv/app
SecretsDatabase passwords, API keys, the backup's own passwordA password manager and the encrypted backup

What you don't need: the operating system, Docker itself, or your app's image. Those come back from a fresh server, Docker's install steps and your registry. Your code is in git (opens in a new tab).

How to back up a database on your VPS

Never copy a running database's files. PostgreSQL's documentation says the server "must be shut down in order to get a usable backup" at file level, unless you take an atomic snapshot of the whole data directory. A dump has no such problem: pg_dump "makes consistent exports even if the database is being used concurrently" and doesn't block readers or writers.

Know the limit of nightly dumps. The same page notes that "except in simple cases, pg_dump is generally not the right choice for taking regular backups of production databases". A small app with one database is the simple case, as long as you accept that a restore goes back to the last dump, up to a day. If losing a day of writes is not acceptable, you need continuous WAL archiving (point-in-time recovery, in PostgreSQL's backup chapter) or a managed database that does it for you.

For a PostgreSQL server with several databases, make a backup for each one. Record any cluster-wide roles or tablespaces your restore needs. A single database dump leaves those out, and PostgreSQL's global-object export covers them separately. During the restore drill, connect with the same type of account the app uses and check that it can read the restored data.

The 3-2-1 rule as a VPS backup strategy

CISA's guidance for small businesses describes the 3-2-1 rule: three copies of important data, on two different types of storage, one of them off-site. For a single server that maps to: the live database, an encrypted copy in a bucket at another provider or region, and the provider's own backups of the server. The point of the "1" is that the off-site copy shouldn't share a fate with the server: not the same machine, ideally not the same account.

1. Create a bucket outside the server

Any S3-compatible object storage works. Two examples, as their pricing pages listed them on 11 November 2026:

If the server is at one of them, put the bucket at the other (or at least in another region). Create a bucket for this app, and an access key for this server only.

2. Set up restic

restic encrypts everything on the server before it uploads, keeps deduplicated snapshots, and talks to S3-compatible storage directly. Ubuntu 24.04 packages version 0.16.4; newer releases are on restic's GitHub (opens in a new tab) page.

sudo apt install restic
sudo install -d -m 700 /etc/restic

Create /etc/restic/env, readable only by root, with your own values:

RESTIC_REPOSITORY=s3:https://<your-endpoint>/<your-bucket>
RESTIC_PASSWORD_FILE=/etc/restic/password
RESTIC_CACHE_DIR=/var/cache/restic
AWS_ACCESS_KEY_ID=<your-access-key-id>
AWS_SECRET_ACCESS_KEY=<your-secret-access-key>
HC_PING_URL=https://hc-ping.com/<your-check-uuid>

The repository address is the bucket's endpoint and name in restic's s3:https://server/bucket form. RESTIC_CACHE_DIR matters because a systemd service has no home folder; without it restic warned in our test that it couldn't open its cache. HC_PING_URL is for the alert in step 4.

Generate the repository password, lock both files down, and create the repository:

head -c 32 /dev/urandom | base64 | sudo tee /etc/restic/password >/dev/null
sudo chmod 600 /etc/restic/env /etc/restic/password
sudo sh -c 'set -a; . /etc/restic/env; restic init'

Important

Copy the content of /etc/restic/password into your password manager now. restic's documentation: "If you lose it, you won't be able to access data stored in the repository." A backup whose password was only on the lost server is no backup.

3. The backup script

/usr/local/sbin/backup-app dumps the database through Docker Compose, then backs up the dump and the app's folder, then thins out old snapshots:

#!/bin/sh
# Dump the database, then send the dump and the app folder to the restic repository.
set -eu
umask 077
mkdir -p /var/backups/app
cd /srv/app
docker compose exec -T db pg_dump -U app -Fc app > /var/backups/app/app.dump.tmp
mv /var/backups/app/app.dump.tmp /var/backups/app/app.dump
restic backup --tag app /var/backups/app /srv/app
restic forget --tag app --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune

Make it executable with sudo chmod 700 /usr/local/sbin/backup-app. Notes on each part:

  • -Fc writes PostgreSQL's custom format: compressed, and restorable with pg_restore, which can also restore single tables.
  • The .tmp file means a failed dump never replaces the last good one. In our test, with the database stopped, the script failed at the dump and the previous app.dump stayed as it was.
  • umask 077 keeps the dump readable by root only.
  • /srv/app holds the Compose file and config. If uploads live in a named Docker volume, add its folder: docker volume inspect <volume> --format '{{.Mountpoint}}' prints it (/var/lib/docker/volumes/<volume>/_data). Files there are safe to copy while the app runs; database files are not.
  • forget --prune keeps the last snapshot of each of the last 7 days, 4 weeks and 6 months, and deletes the rest from the bucket.

pg_dump dumps one database. Roles and other cluster-wide objects need pg_dumpall --globals-only; with the official Docker image, the role is recreated from the container's environment, so most Compose apps don't need it.

4. Run it every night, and know when it didn't

A systemd timer runs the script and catches up after a reboot. /etc/systemd/system/backup-app.service:

[Unit]
Description=Back up the app database and files with restic
Wants=network-online.target
After=network-online.target docker.service

[Service]
Type=oneshot
EnvironmentFile=/etc/restic/env
ExecStart=/usr/local/sbin/backup-app
ExecStartPost=/usr/bin/curl -fsS -m 10 --retry 5 -o /dev/null ${HC_PING_URL}

/etc/systemd/system/backup-app.timer:

[Unit]
Description=Daily app backup

[Timer]
OnCalendar=*-*-* 03:00:00
RandomizedDelaySec=15m
Persistent=true

[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now backup-app.timer
sudo systemctl start backup-app.service   # one run now
journalctl -u backup-app.service -n 20

ExecStartPost runs only when the script succeeded. That makes it a heartbeat: point HC_PING_URL at a service such as Healthchecks.io (its free Hobbyist plan monitors 20 jobs on 11 November 2026; it is also open source and can be self-hosted), set the expected period to one day, and it alerts you when a ping is missing. In our test the successful run sent one ping, and the run with the database stopped failed and sent none.

5. Add the provider's backups

The provider's backups copy the whole server disk on their own schedule. They are the fastest way back from a broken server, and they cost little next to the server:

Neither includes attached volumes, and both live in the same account as the server, so they are the second layer, not the off-site copy. A disk copied while the database writes is crash-consistent at best; the dump in your bucket is the copy you can trust for the data.

6. The restore drill

Do this once now, then every month. It restores the latest backup into a throwaway database, never touching production:

sudo -i
set -a; . /etc/restic/env; set +a
restic snapshots --compact
restic restore latest --tag app --target /tmp/restore-test

docker run -d --name restore-test -e POSTGRES_PASSWORD=drill \
  -e POSTGRES_USER=app -e POSTGRES_DB=app postgres:18
until docker exec restore-test pg_isready -h 127.0.0.1 -U app -q; do sleep 1; done
docker exec -i restore-test pg_restore -U app -d app --no-owner \
  < /tmp/restore-test/var/backups/app/app.dump

docker exec restore-test psql -U app -d app -tAc "SELECT count(*), sum(total) FROM orders"
cd /srv/app && docker compose exec -T db psql -U app -d app -tAc "SELECT count(*), sum(total) FROM orders"

docker rm -f restore-test && rm -rf /tmp/restore-test

Use a query that fits your own tables. In our drill both lines printed 1000|51934.75: the restored copy matched production. pg_isready -h 127.0.0.1 waits for the real server; the image's first-start setup briefly runs a temporary one on the socket only. Run restic check now and then too: it verifies the repository's structure ("no errors were found" in our test).

When you need the real thing, for example after a bad migration, restore the dump over the live database:

restic restore latest --tag app --target /tmp/recover --include /var/backups/app
cd /srv/app
docker compose exec -T db pg_restore -U app -d app --clean --if-exists < /tmp/recover/var/backups/app/app.dump

We tested this by dropping the table in production and restoring: the 1,000 rows came back. --clean --if-exists drops each object before recreating it. Everything written since the dump is gone, so stop the app first if it might write in between.

For a lost server, the order is: a new server with Docker, set up again with secure SSH and a firewall; restic with the repository password and bucket key from your password manager; your app's folder from restic restore; docker compose up -d db for the database alone; pg_restore as above; then the rest of the app. Write those steps down with your real names while you do the drill.

Doing this with OpsCrew

OpsCrew, an open-source tool that deploys apps to servers you own, has snapshots built in: npx opscrew snapshot copies every database (with its own dump tool, while it runs), cache and the app's files; snapshot schedule on takes them daily or weekly; and snapshot offsite on encrypts each one on the server to your age key (the server holds only its public half) and uploads it to a bucket of its own at Cloudflare (opens in a new tab) R2, Hetzner or DigitalOcean. npx opscrew restore <id> puts the data back after a typed confirmation, keeping a copy of the data as it was just before, and --from offsite restores from the bucket after a server is lost. On servers it creates, snapshot --server takes the provider's whole-server snapshot and shows its price first. The scheduled check opens a GitHub issue when a scheduled snapshot or its upload fails.

Next on the checklist

Next on the Ubuntu server hardening checklist: monitoring a small server, so you hear about a full disk before your users do. If your app came from an AI builder, the production checklist for vibe-coded apps covers the app-side checks.

Key facts

FactValueSource
Hetzner Cloud backups20% of the server's price; 7 daily backup slotsA flat monthly price; Hetzner's backups overview adds that backups and snapshots do not include attached Volumes.Hetzner Cloud billing FAQ (opens in a new tab)Verified on
Hetzner Cloud snapshots0.0143 EUR per GB per month, excl. VATBilled on the compressed size, as Hetzner's billing FAQ explains.Hetzner Cloud, Regular Performance (opens in a new tab)Verified on
DigitalOcean Droplet backupsWeekly: 20% of the Droplet's cost; daily: 30%Basic backup plans. Usage-based plans are billed per GiB instead. Backups are crash-consistent and leave out volumes, as DigitalOcean's backup features page says.DigitalOcean Backups pricing (opens in a new tab)Verified on
DigitalOcean Droplet snapshots0.06 USD per GB per monthDigitalOcean Snapshots pricing (opens in a new tab)Verified on
Versions in this article's testsUbuntu 24.04.5 LTS, PostgreSQL 18.6 (postgres:18 image), restic 0.16.4 (Ubuntu's package), Docker Engine 29.9.3Tested on 11 November 2026; the bucket was a local S3-compatible server (rclone serve s3), not a cloud provider's.restic documentation, Preparing a new repository (opens in a new tab)Verified on

Frequently asked questions

Are Hetzner or DigitalOcean backups enough on their own?

They are a good second layer, not the only one: they live in the same provider account as the server, so a lost account or a mistaken deletion can take both, and they restore the whole server rather than one database. Keep a database dump outside the provider too.

How often should I test restoring a backup?

At least once a month, and after every change to the backup script or the database version. Restore into a throwaway container, never over production, and compare something you can count, such as row counts in your main tables, with the live database.

Sources

  1. Back Up Business Data (opens in a new tab), CISA, accessed on
  2. pg_dump (opens in a new tab), PostgreSQL Global Development Group, accessed on
  3. pg_restore (opens in a new tab), PostgreSQL Global Development Group, accessed on
  4. File System Level Backup (opens in a new tab), PostgreSQL Global Development Group, accessed on
  5. Preparing a new repository (opens in a new tab), restic, accessed on
  6. Removing backup snapshots (opens in a new tab), restic, accessed on
  7. Restoring from backup (opens in a new tab), restic, accessed on
  8. Backups and Snapshots overview (opens in a new tab), Hetzner, accessed on
  9. Hetzner Cloud billing FAQ (opens in a new tab), Hetzner, accessed on
  10. Hetzner Cloud, Regular Performance (opens in a new tab), Hetzner, accessed on
  11. Hetzner Object Storage (opens in a new tab), Hetzner, accessed on
  12. Backups pricing (opens in a new tab), DigitalOcean, accessed on
  13. Backups features (opens in a new tab), DigitalOcean, accessed on
  14. Snapshots pricing (opens in a new tab), DigitalOcean, accessed on
  15. Spaces pricing (opens in a new tab), DigitalOcean, accessed on
  16. Healthchecks.io pricing (opens in a new tab), Healthchecks.io, accessed on
  17. Healthchecks source code (opens in a new tab), Healthchecks.io, accessed on
  18. pg_dumpall and cluster-wide objects (opens in a new tab), PostgreSQL, accessed on
Edit this article

Put OpsCrew to work on your app.

Run npx opscrew init in your app's repository. It checks your tools and tokens first, for free.