Backup and restore
Daily encrypted backup of the production MongoDB to Google Cloud Storage, retention, step-by-step restore (test of 08/10/2026: 65 seconds) and the external uptime and backup alarm on GitHub Actions.
Status: live since 08/10/2026. First backup and full restore test run the same day. The scripts live on the VPS in /opt/aladia/ops/ (described in /opt/aladia/ops/README.md); the alarm is the .github/workflows/uptime.yml workflow.
1. Where the backups are
| What | Where |
|---|---|
| Bucket | gs://aladia-prod-db-backups, project backend-prod-aladia, location EU, Standard class |
| Access | private: uniform bucket-level access, public access prevention enforced, versioning on |
| Daily backup | daily/YYYY-MM-DD/aladia-mongo-<YYYYMMDD>T<HHMM>Z.archive.gz.gpg |
| Proof of completeness | daily/YYYY-MM-DD/manifest-<YYYYMMDD>T<HHMM>Z.json: size, sha256, duration, document count per collection. Written only when the backup succeeded: an archive without a manifest must be ignored |
| Weekly backup | weekly/YYYY-MM-DD/…, server-side copy of Sunday's backup |
| Encryption key | on the VPS /root/.backup-key (600); copy in Secret Manager, secret db-backup-encryption-key in the same project. Without this key the backups are unreadable |
Format: mongodump --archive --gzip --oplog (all databases, consistent snapshot thanks to the oplog) encrypted with gpg --symmetric AES256. The stream goes from the aladia-mongo-1 container to the bucket without touching the VPS disk. Size on 08/10/2026: 352 MB (184 collections, 2,575,592 documents), 52 seconds.
When: every day at 03:30 Italian time, systemd aladia-backup.timer (Persistent=true: if the VPS was down, it runs at boot). Log: /var/log/aladia-backup.log.
Who can do what:
| Identity | Bucket permissions | Used by |
|---|---|---|
db-backup-writer@backend-prod-aladia.iam.gserviceaccount.com | create and read objects, cannot delete or overwrite them | VPS (/root/.config/aladia-backup/writer-sa.json, 600) |
db-backup-checker@backend-prod-aladia.iam.gserviceaccount.com | list objects only | GitHub Actions of the Aladia-Srl/aladia.io repo through Workload Identity Federation (pool github-actions, provider aladia-io): no stored key |
If the VPS were compromised, an attacker could add objects but not destroy existing backups: only the bucket lifecycle deletes.
2. Retention
Bucket lifecycle rules:
| Prefix | Retention |
|---|---|
daily/ | 15 days (14 daily backups always available) |
weekly/ | 57 days (8 weekly backups) |
| noncurrent versions | 7 days |
Cloud Storage default soft delete (7 days) also applies: an object deleted by hand can be recovered from the console.
3. Step-by-step restore
Time measured in the test of 08/10/2026 (352 MB backup): 65 seconds overall, of which 1–3 s to start the container and about 60 s to download, decrypt and restore with indexes. Everything runs on the VPS as root.
3.1 Restore test (does not touch production)
/opt/aladia/ops/restore-test.sh # today's backup
/opt/aladia/ops/restore-test.sh 2026-10-08 # backup of a given dayThe script reads the latest manifest of the day, starts an isolated temporary Mongo (aladia-restore-test, --network none), restores, compares sha256 and per-collection counts with the manifest and with production, then removes the container and its volume. Result on 08/10/2026: identical sha256, 184/184 collections, 2,575,592/2,575,592 documents, 0 differences.
Repeat at least once a month.
3.2 Real restore into production
Only with the Owner's approval: it replaces the current data.
- Pick the backup. List the manifests and take the archive named in the
objectfield:T=$(/opt/aladia/ops/gcs-token.sh /root/.config/aladia-backup/writer-sa.json) curl -s -H "Authorization: Bearer $T" \ "https://storage.googleapis.com/storage/v1/b/aladia-prod-db-backups/o?prefix=daily/&fields=items(name,size)" | grep manifest - Check the backup first with
restore-test.sh <day>(step 3.1). - Stop writes:
cd /opt/aladia && docker compose stop monolith. - Save the current state (so you can go back): run
/opt/aladia/ops/backup-mongo.shand note the object it creates. - Restore (overwrites the collections contained in the archive):
OBJ=daily/YYYY-MM-DD/aladia-mongo-YYYYMMDDTHHMMZ.archive.gz.gpg T=$(/opt/aladia/ops/gcs-token.sh /root/.config/aladia-backup/writer-sa.json) docker exec aladia-mongo-1 sh -c 'umask 077; printf "password: \"%s\"\n" "$MONGO_INITDB_ROOT_PASSWORD" > /tmp/.mdc.yml' curl -sS --fail -H "Authorization: Bearer $T" "https://storage.googleapis.com/aladia-prod-db-backups/$OBJ" \ | gpg --batch --decrypt --passphrase-file /root/.backup-key \ | docker exec -i aladia-mongo-1 mongorestore --host localhost:27017 -u aladia \ --authenticationDatabase admin --config /tmp/.mdc.yml \ --archive --gzip --oplogReplay --drop --nsExclude 'admin.*' docker exec aladia-mongo-1 rm -f /tmp/.mdc.yml--nsExclude 'admin.*'keeps the Mongo users currently in use. - Restart:
docker compose start monolith, then checkhttps://api.aladia.io/v2/readyz("status":"ok").
If the VPS is lost: on a new machine with Docker you need the encryption key (Secret Manager, db-backup-encryption-key) and read access to the bucket (GCP console or gcloud). Download the archive, decrypt it with gpg --batch --decrypt --passphrase-file <key> and pipe it into mongorestore --archive --gzip --oplogReplay on a fresh mongo:7.
4. Alarm
GitHub workflow Uptime e backup (.github/workflows/uptime.yml). It runs on GitHub's servers, so it works even when the VPS is down.
| Job | When | What it checks | Fails when |
|---|---|---|---|
uptime | every 5 minutes | https://api.aladia.io/v2/readyz (HTTP 200 and "status":"ok", i.e. monolith and MongoDB up) and https://app.aladia.io (2xx following redirects) | it fails twice in a row, 2 minutes apart |
backup | hourly at minute 17, plus 02:35 and 03:35 UTC | that a manifest-*.json from today or yesterday exists and, after 04:30 Italian time, today's one | the latest complete backup is 26 hours old or more, or after 04:30 Italian time today's backup is missing |
Notification: when a scheduled workflow fails, GitHub emails the user who last modified the cron in the file (Sigid). Check in GitHub, Settings → Notifications → Actions, that emails for failed workflows are on.
On top of that, when the backup job fails the workflow opens a "Backup fallito" issue (label backup-fallito, assigned to Sigid or to the user in the repo variable BACKUP_ALERT_ASSIGNEE): GitHub sends the assignment email and notifies repo watchers. Only one issue is open at a time. While it stays open, the first check of each day adds a comment that mentions the assignee (another GitHub email) and updates the title with the number of days without a backup, for example "Backup fallito: 6 giorni senza backup (ultimo 2026-11-05)"; the other checks of the same day write nothing. From 6 to 11 Nov 2026 the backup was missing for six days with a single email on the first day, which is why the reminder exists. The issue closes itself, with a comment, at the first check that finds today's backup. The timer starts at 03:30 Italian time and the backup takes about a minute: the 02:35 and 03:35 UTC checks make sure that, with summer or winter time, a failed backup is reported within about an hour (before 13/10/2026 the 26-hour check only caught it in the afternoon).
Known limits: GitHub scheduled workflows can start a few minutes late under load; scheduled workflows only run from the default branch, so the alarm is live after the merge into main.
How to verify the alarm
- That it runs: repo Actions tab → Uptime e backup: one green run every 5 minutes.
- By hand: Run workflow (
workflow_dispatch) runs both jobs. - The failing path, without touching production: on a test branch change the URL to
https://api.aladia.io/v2/readyz-testand open a PR that only changes the workflow: theuptimejob must fail after about 2 minutes. - Backup: on the VPS
tail /var/log/aladia-backup.logandsystemctl list-timers aladia-backup.timer.
5. Files on the VPS
| File | What |
|---|---|
/opt/aladia/ops/backup-mongo.sh | backup: dump, encryption, streaming upload, size check, manifest, weekly copy |
/opt/aladia/ops/restore-test.sh | restore test on a temporary container with count comparison |
/opt/aladia/ops/gcs-token.sh | OAuth token from a service account key (openssl, no gcloud); prints only the token |
/opt/aladia/ops/setup-gcp-backup.py | one-off, idempotent setup of bucket, service accounts, secret and Workload Identity Federation |
/opt/aladia/ops/README.md | description of all the files above |
/etc/systemd/system/aladia-backup.{service,timer} | schedule at 03:30 Europe/Rome |
/etc/logrotate.d/aladia-backup | monthly log rotation, 12 months |
/root/.backup-key, /root/.config/aladia-backup/writer-sa.json | encryption key and writer SA key (600, never in logs) |