# Back up your app's database and actually test the restore

- Author: RomanMotion (https://wurk.fun/user/RomanMotion)
- Published: 2026-11-07
- Canonical (HTML): https://wurk.fun/blog/back-up-your-app-s-database-and-actually-test-the-restore
- Cover image: https://ik.imagekit.io/wurk/browser-uploads/f1c626b5c17e7f0a61dfbbe7a20d85ae7b33a20527243aeec2ee1d0a8b87562a/5b5363eb-fcf3-4c7d-a9b0-ec92aff76072/cover-backup.jpg?wurkUpload=v1.rMkHx7Co_xYWBh_7O3Bj6aI2j8oBaRnzWaCub2H-u8I

---

You shipped the app. Real people use it, they type real things into it, and all of it lives in one Postgres database or one SQLite file on one server. Then a disk dies, a host quietly changes something, or a migration your agent wrote runs one time too many, and that data is just gone.

The fix fits in an afternoon, and your coding agent can do most of it. Here's the setup worth having before you need it.

A real backup is three jobs on a schedule. A dump leaves the database, the copy leaves the server, and a restore gets tested into a throwaway database with a row count to compare. Your agent can build all three in one sitting. The one rule to enforce from day one: the agent never runs a restore against production. Ever.

## Two ways this ends

The first story is the app with no backup at all. Everything works until the night it doesn't: the disk fails, the provider freezes the account over an unpaid invoice, or the agent gets an instruction slightly wrong and drops the wrong table. There's nothing to restore, so the only question is how much of the app you rebuild from memory.

The second story is worse in a quiet way, because it feels safe. A backup job exists. It has been running for months. Nobody has ever loaded one of those files back into anything. The night it's needed, the dump turns out to be half-written, the format doesn't match the restore tool, or the script has been overwriting the same file with an empty one since the week the database moved. The file exists and the data doesn't.

A backup you've never restored is a hope, not a backup. The drill below is what turns it into one.

## The dump, scheduled and rotated

For Postgres, `pg_dump` is the tool, and the [pg_dump reference on postgresql.org](https://www.postgresql.org/docs/current/app-pgdump.html) covers every flag. The custom format is the one to use: it's compressed and it restores with `pg_restore`, which lets you pick what to restore.

```bash
pg_dump --format=custom \
  --file=/backups/app-$(date +%F).dump \
  "$DATABASE_URL"
```

For SQLite, use the CLI's `.backup` command (listed in the [sqlite3 CLI docs](https://www.sqlite.org/cli.html)). Copying the file with `cp` while your app is writing to it can give you a corrupt copy; SQLite lists that among the ways [a database file gets corrupted](https://www.sqlite.org/howtocorrupt.html).

```bash
sqlite3 /srv/app/app.db \
  ".backup '/backups/app-$(date +%F).db'"
```

Either way, schedule it. A daily run at a quiet hour is the usual choice:

```bash
0 3 * * * /usr/local/bin/dump-app.sh >> /var/log/dump.log 2>&1
```

Then rotate, or the disk fills up and the job starts failing where nobody looks:

```bash
ls -1t /backups/app-*.dump | tail -n +8 | xargs -r rm --
```

This is the kind of small, well-defined job a coding agent handles well. If you need a plan for one, there's [10% off GLM coding plans](https://z.ai/subscribe?ic=R0K78RJKNW).

## Copies that outlive the server

A dump sitting in `/backups` on the same machine is one disk failure away from nothing. The copy has to leave the server. Simplest is a scheduled pull, run from a second machine you control:

```bash
# runs on the second machine, not the app server
rsync -a -e ssh you@app-server:/backups/ /srv/app-backups/
```

A pull is better than a push here: the app server never holds credentials that could delete the off-site copies.

Object storage works too and doesn't need a second machine; most hosts have a CLI for it, and your agent can wire either one in. Keep a week of daily copies and a handful of monthly ones. The rotation line above handles the dailies; add a monthly run that skips deletion.

## The drill: restore into a throwaway

A restore drill is a script that loads the newest dump into a database that exists only for this, counts the rows, and deletes itself. On Postgres:

```bash
#!/bin/sh
# weekly restore drill. NEVER targets production.
set -e
DRILL="${1:-drill_$(date +%s)}"
case "$DRILL" in
  drill_*) ;;
  *) echo "refusing: $DRILL is not a drill database" >&2; exit 1 ;;
esac
createdb "$DRILL"
pg_restore --no-owner --dbname="$DRILL" "$(ls -1t /backups/*.dump | head -1)"
# swap in your app's real tables:
for t in users orders; do
  psql -d "$DRILL" -tAc "select '$t', count(*) from $t;"
done
dropdb "$DRILL"
```

Compare the counts with what the live app roughly holds; a table at zero, or far below yesterday, is a red flag. And you've proven the file loads, which is the fact that matters. `--no-owner` skips ownership commands that fail when the drill runs as a different user. For SQLite the drill is the same shape: restore into a file named `drill-something`, open it, check the tables and a couple of counts, delete the file.

The guard built into the script is the important part. It refuses any target name that doesn't start with `drill_`, even one passed in by hand, and that belongs in your agent's standing instructions too: a restore only ever targets a database whose name starts with `drill_`; if the target is production, the agent stops and asks you. The rule isn't about trust. It's about the late, tired session where the context is muddled and the most expensive mistake is the easiest one to make.

## Hand this to your agent

```text
Set up database backups for this app and prove they work.

1. Nightly dump of the database with pg_dump or sqlite3 .backup,
   timestamped files, custom format for Postgres.
2. Copies sent off this server after every dump.
3. Rotate: keep the newest 7 daily dumps, plus monthly ones.
4. A weekly restore drill: load the newest dump into a throwaway
   database named drill_*, count rows in the main tables, print
   the counts, then drop the drill database.
5. The drill script must refuse to run unless the target database
   name starts with "drill_". You never run a restore against the
   production database. If anything is unclear, stop and ask.
```

If you're still picking a harness for this kind of work, you can [try the zcode agent harness free](https://zcode.z.ai/invite/ALWM9JUA) and hand it the prompt above.

## The checklist

```text
[ ] Dump runs nightly, timestamped files
[ ] Copies leave the server after every dump
[ ] Old dumps rotate on their own
[ ] Restore drill runs weekly into a drill_* database
[ ] The drill prints row counts you can eyeball
[ ] Standing rule on file: restores never target production
```

The drill is the part everyone skips and the part that saves you, because it's the only step that proves the whole chain works. Run it once this week, then let the schedule keep proving it.

There's [more on our blog](https://jackgross-five.vercel.app), and if this saved you an afternoon, a small tip on the post keeps these coming.
