Skip to content

Feature/71 db backup restore - #99

Merged
anis022 merged 7 commits into
developfrom
feature/71-db-backup-restore
Dec 4, 2026
Merged

anis022 merged 7 commits into
developfrom
feature/71-db-backup-restore

Conversation

@maayjayy

@maayjayy maayjayy commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #71

Overview

First of two PRs for #71 (database backup and restore). As agreed with Anis, this PR does everything that can be done without Azure. The Azure deployment (daily schedule, real storage, alert, restore test of a production backup) will come in a second PR once #12 is done.

The backup script (infra/db-backup/backup.sh) dumps the database with pg_dump, counts the rows of every table, encrypts everything with age and a public key, computes a checksum and uploads both files to Blob Storage. Then it deletes the backups older than the retention. The purge only runs after a successful upload, so a failing job never deletes the last good backups.

The restore script (infra/db-backup/restore.sh) downloads a backup, checks its checksum, decrypts it, then restores it in a single transaction and checks that every table has the same number of rows as when it was backed up. It refuses a database that is not empty unless --confirm is given, and it only touches the target database after all the other checks passed.

Both scripts run in a Docker image (infra/db-backup/Dockerfile) and read their settings from environment variables only. Procedures, settings, measured time and the Azure plan are in docs/azure/backup-restore.md.

What this PR covers from the acceptance criteria:

  • no credentials in the repo or the image
  • row counts match the source after a restore
  • a corrupted file is detected and the target database is not touched
  • the restore refuses a non-empty database without --confirm
  • the procedures and the restore time are documented, linked from the README

Partly done:

  • the purge works and is tested, the real retention rule will be set in Azure
  • failures are in the logs (FAILED line and exit code 1), the alert comes with the Azure part
  • I restored a backup of a test database with generated data, not a real production backup yet

Not done here: the daily schedule, it needs the Container Apps Job from the second PR.

Testing

39 new integration tests in tests/MoissonMontreal.Tests/DbBackup/, run with dotnet test. They use Testcontainers to start PostgreSQL, Azurite (Blob Storage emulator) and the backup image itself, so they test the same image that will run in Azure.

dotnet test --filter "FullyQualifiedName~MoissonMontreal.Tests.DbBackup"

The first run downloads the Azurite image and builds the backup image, so it takes a few minutes. After that, the 39 tests take about 15 seconds.

  • The source database is built from the migrations and filled with Fixtures/BackupTestData.sql (10,000 receipts, 50,000 receipt lines, 39 MB).
  • A restored database is compared to the source value by value (every row, index and constraint), plus control totals (issued receipts total, receipts per status, last receipt number), every migration present, and the API /health on the restored database.
  • Refusals: damaged backup, damaged with recalculated checksum, incomplete, wrong key, non-empty database, restore failing halfway (nothing changed), missing or wrong settings.
  • I also broke the scripts on purpose 5 ways (no confirm check, no single transaction, purge everything, no checksum check, ignore row counts): each one makes at least one test fail.

Full test suite: 789 passed, 0 failed, 0 warnings. Measured: backup 0.4 s, restore 0.6 s for 39 MB.

Screenshots / Screencasts

N/A, no frontend changes.

Checklist

  • Code is neat, readable, and works
  • Code is commented where appropriate and well-documented
  • Commit messages follow our guidelines
  • Issue number is linked
  • Branch is linked
  • Reviewers are assigned (one of your tech leads)

Notes

  • Second PR (after [Issue]: Azure resources and first manual deployment #12): Container Apps Job, storage account, alert, restore test of a real backup with its measured time. This PR says "Part of [Issue]: Database backup and restore test #71" so the issue stays open.
  • Container Apps as in the PRD. The image will go to Azure Container Registry, as in the PRD and the stack guide, not GitHub Container Registry as the issue says.
  • Retention: 30 days, a temporary value (BACKUP_RETENTION_DAYS) until Moisson Montréal confirms it.
  • The production age key will be generated in the second PR and added to Vaultwarden before the first backup. It will need to be handed over to Moisson Montréal with the other keys.
  • New folder infra/db-backup/, next to the infra/ folder of PR feat(infra): add Azure App Service and PostgreSQL manual deployment #58. Both PRs add a section at the end of README.md, so the second one merged will have a small conflict: keep both sections.
  • Added Testcontainers.Azurite (same version as Testcontainers.PostgreSql) and Azure.Storage.Blobs to the test project, for the Blob Storage emulator.

@maayjayy maayjayy added this to the Sprint 3 milestone Dec 2, 2026
@maayjayy maayjayy self-assigned this Dec 2, 2026
@maayjayy maayjayy linked an issue Dec 2, 2026 that may be closed by this pull request
9 of 13 tasks
/// </summary>
public async Task WriteFileAsync(string path, byte[] content)
{
await ShellAsync($"mkdir -p {Path.GetDirectoryName(path)}");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Path.GetDirectoryName(path) returns a Windows path on Windows (\tmp\local), so the mkdir inside the container fails. 4 backup tests fail with dotnet test on Windows (Restore_RestoresALocalFile and 3 others). I'll push a fix that builds the Linux path by hand.

@anis022
anis022 removed the request for review from lauriechammah December 4, 2026 06:31
Build the folder of a container path at the last '/' instead of with
Path.GetDirectoryName, which gives backslashes on Windows, and send
shell commands with \n line endings, since a Windows checkout gives the
test files \r\n line endings that bash refuses.

@anis022 anis022 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed the fix. There was a second Windows issue too: multi-line shell commands in the tests get \r\n line endings on a Windows checkout and bash refuses them, so ShellAsync now converts them to \n. All 39 backup tests pass on Windows now.

@anis022
anis022 merged commit 5ef280d into develop Dec 4, 2026
2 checks passed
@anis022
anis022 deleted the feature/71-db-backup-restore branch December 4, 2026 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Issue]: Database backup and restore test

2 participants