The only copy of every customer, order and one-of-a-kind item was the live Postgres data directory, plus whatever the deploy checklist's manual pg_dump happened to have caught. That dump is good and stays, but it only runs when someone deploys: a quiet week meant the newest copy of real customer data was a week old, and nothing bounded the gap. Two services rather than one, because they are different jobs. The database is small, changes constantly and wants a logical dump — daily, gzipped, 7/4/6 daily-weekly-monthly retention. Uploads are large and append-mostly and want an archive — weekly, 56 days, mounted read-only so a backup process cannot damage the thing it is backing up. Forcing both through one tool would serve one of them badly. The dumper is pinned to postgres-backup-local:16 to match the server. pg_dump refuses to dump a server newer than itself, so a floating tag is a backup that stops working the day Postgres is upgraded — silently, because nothing reads a dump until it is needed. It depends_on the database's existing pg_isready healthcheck, which is the constraint that shaped this: a dumper is a client, and without that the first run after a NAS reboot races Postgres coming up. Both carry a staleness healthcheck rather than trusting the schedule. A regime that stopped a month ago is indistinguishable from a working one until a restore is attempted, and `find -mmin` is the cheapest thing that tells them apart. It surfaces in Portainer beside the app rather than somewhere separate to remember to look. Windows are the interval plus grace — 26 hours daily, 9 days weekly — so a late run is not a failure, and start_period covers the first cycle when nothing has been written yet. Verified rather than assumed. Both images were pulled and checked to have a shell and `find`, since a CMD-SHELL healthcheck against an image without one reports unhealthy forever. postgres-backup-local:16 ships pg_dump 16.10 against the postgres:16 server. The healthcheck expression was exercised three ways in the image itself — empty directory, fresh artifact, and one aged three days — and returns unhealthy, healthy, unhealthy. The compose file parses and the #118 drift guard still passes over it. Three things these deliberately do not cover, written into the compose file and the doc rather than left to be discovered: They run while the stack runs, so they cannot protect the stack's own teardown. Deleting the Portainer stack deletes them too. That is why the deploy checklist's manual dump stays, and README now says so where the checklist is. They write to the same volume as the data they protect. That survives a bad migration, a dropped table, a bad deploy and a stack deletion, and not the disk. Getting a copy off /volume1 is a Synology-side job and is what turns this from a convenience into a guarantee. Daily database against weekly uploads leaves a window where a restore pairs the two from different moments. An orphaned image is harmless; a row without its image is a broken thumbnail on one recent item, usually still on the admin's machine. Neither is data loss, and a synchronised snapshot is not worth the complexity to avoid it. The restore procedure leads with practising on a throwaway database, because an untested backup is a file of unknown validity and a truncated dump looks exactly like a good one until it matters. It checks row counts and the pgmigrations head — the migration check being the one most easily skipped and most likely to bite, since a dump older than the code restores a schema the app will fail against. Both open items are listed as unticked in the doc: no off-volume copy exists yet, and no restore has been performed. Until the second is done this documents an untested procedure, and it says so. Refs #147
7.1 KiB
Backup and restore
What protects production's data, what each part does and does not cover, and how to get it back. See #147.
What runs
Two services in docker-compose.prod.yml, alongside the app and the database.
| Service | What | When | Kept | Lands in |
|---|---|---|---|---|
redefined-designs-db-backup-syn |
pg_dump of redefined, gzipped |
Daily, 03:00 | 7 daily, 4 weekly, 6 monthly | /volume1/configs/redefined-designs/backups/postgres |
redefined-designs-uploads-backup-syn |
tar.gz of the uploads directory |
Weekly, Sunday 04:00 | 56 days | /volume1/configs/redefined-designs/backups/uploads |
Both prune automatically. Both report unhealthy when their newest artifact is older than their interval plus grace, so a regime that has quietly stopped shows up in Portainer's stack view beside the app rather than being discovered during a restore.
The database dumper is pinned to prodrigestivill/postgres-backup-local:16, matching the server's major version. pg_dump refuses to dump a server newer than itself, so a floating tag would be a backup that stops working the day Postgres is upgraded — silently, because nothing reads the dumps until they are needed.
What this does not protect against
Three gaps, stated plainly because a backup you are wrong about is worse than one you know the limits of.
The disk. Both artifact directories are on /volume1, the same volume as the live database and uploads. This protects against a bad migration, a dropped table, a bad deploy and a stack deletion. It does not protect against the volume failing. Getting a copy off /volume1 is a Synology-side job — Hyper Backup to another volume, an external disk, or offsite — and until that exists this is a convenience rather than a guarantee.
The stack's own teardown. These services run while the stack runs. Deleting the Portainer stack deletes them along with everything else, so they cannot cover the moment they would be most wanted. The manual pg_dump in README's deploy steps stays for exactly that reason: a routine regime and a snapshot before a risky operation are different jobs, and neither replaces the other.
The gap between the two. The database is dumped daily and uploads weekly, so a restore pairs a database from one moment with images from another. Two mismatches are possible. An image with no row is harmless — an orphaned file nobody references. A row with no image is a broken thumbnail on one recent item, and the original is usually recoverable, because an admin who added an item that recently still has the photograph. Neither is data loss, and paying for a synchronised snapshot to avoid a recoverable broken thumbnail is not worth the complexity.
Checking it is working
# Both should say (healthy). unhealthy means nothing has been written inside
# the window — the regime has stopped, and this is the whole point of the check.
sudo docker ps --filter "name=backup" --format "{{.Names}}\t{{.Status}}"
# What is actually on disk, newest last.
ls -lht /volume1/configs/redefined-designs/backups/postgres/daily | head
ls -lht /volume1/configs/redefined-designs/backups/uploads | head
A dump whose size is wildly different from its predecessors is worth opening. A dump that is a few hundred bytes is a failed dump that exited successfully.
Restoring the database
Read this before running it. --clean --if-exists is baked into the dumps, so restoring drops and recreates every table it touches. Against production that discards whatever is currently there. Take a fresh dump first, whatever state you think the database is in.
1. Practise on a throwaway database first
This is not optional ceremony. An untested backup is a file of unknown validity, and the failure modes — a truncated dump, a dump taken mid-migration — look exactly like a good one until the day it matters.
BACKUP=/volume1/configs/redefined-designs/backups/postgres/daily/redefined-<date>.sql.gz
sudo docker exec -i redefined-designs-db-syn \
psql -U redefined -d postgres -c "CREATE DATABASE restore_test"
gunzip -c "$BACKUP" | sudo docker exec -i redefined-designs-db-syn \
psql -U redefined -d restore_test
Then verify it is a real database rather than an empty one:
# Row counts against the tables that matter. Compare them with production.
sudo docker exec -i redefined-designs-db-syn psql -U redefined -d restore_test -c "
SELECT 'items' AS t, count(*) FROM items
UNION ALL SELECT 'customers', count(*) FROM customers
UNION ALL SELECT 'orders', count(*) FROM orders
UNION ALL SELECT 'checkouts', count(*) FROM checkouts;"
# And that the schema is at the same migration as the code expects.
sudo docker exec -i redefined-designs-db-syn psql -U redefined -d restore_test \
-c "SELECT name FROM pgmigrations ORDER BY id DESC LIMIT 1"
sudo docker exec -i redefined-designs-db-syn \
psql -U redefined -d postgres -c "DROP DATABASE restore_test"
The migration check is the one most easily skipped and the most likely to bite: a dump older than the current code restores a schema the app will fail against on boot. The container runs migrate.js up at start, so it may repair itself — but only forwards, and only if the migrations are still compatible.
2. Restore for real
# Stop the app so nothing writes while the schema is being replaced.
sudo docker stop redefined-designs-syn
gunzip -c "$BACKUP" | sudo docker exec -i redefined-designs-db-syn \
psql -U redefined -d redefined
sudo docker start redefined-designs-syn
# Migrations run at container start, so watch them land before serving.
sudo docker logs -f redefined-designs-syn | head -30
Restoring uploads
The archives are plain tar.gz unless BACKUP_PASSPHRASE is set in the stack.
ARCHIVE=/volume1/configs/redefined-designs/backups/uploads/uploads-<date>.tar.gz
# Look before extracting. The archive contains a `backup/uploads/` prefix.
tar -tzf "$ARCHIVE" | head
# Extract somewhere harmless first, then move what is needed into place.
mkdir -p /volume1/configs/redefined-designs/restore-tmp
tar -xzf "$ARCHIVE" -C /volume1/configs/redefined-designs/restore-tmp
Restore into the live directory by copying rather than replacing it wholesale, unless the intent really is to discard everything added since the archive was taken. Image paths are stored in the database as /uploads/<file>, so a file missing from disk is a broken image on one item rather than an error, which makes a partial restore safe and a wrong wholesale one quietly destructive.
On BACKUP_PASSPHRASE
Leave it unset unless there is a reason. If it is set, the archives are GPG-encrypted, and an archive nobody can decrypt is not a backup. A passphrase kept only in Portainer's stack variables is lost with the NAS — precisely the disaster the backups exist for. Set it only if the passphrase itself lives somewhere that survives the NAS.
Open
- An off-
/volume1copy, by whatever Synology-side mechanism is chosen, recorded here once it exists - A restore performed at least once against
restore_testand the result verified, per the section above
Neither is done. Until the second one is, this document describes an untested procedure.