Nightly mysqldump and one restore test

You build a nightly MySQL backup service that creates timestamped compressed dumps of zomato and verifies recovery in an isolated restore_test database. The service retains the newest configured backups and preserves the latest successful dump when a backup fails.

Ticket

Build a nightly MySQL backup workflow for the production Compose stack. Create backend/scripts/backup.sh to produce timestamped, compressed dumps of the zomato database, maintain a latest.sql.gz pointer, and prune old dumps. Create backend/scripts/restore-test.sh to restore the latest dump into an isolated database, verify its orders table, and clean up afterward.

A backup file is not proof of recoverability. The food-delivery service needs a tested recovery path that does not put live orders at risk. Extend backend/compose.prod.yml with a dedicated backup service that schedules the backup script at 02:00 UTC and stores its output in a persistent named volume.

Acceptance criteria

  • backend/scripts/backup.sh creates a gzip-compressed dump named zomato-20261104T020000Z.sql.gz when BACKUP_TIMESTAMP=20261104T020000Z is supplied. Decompressing that file must contain the mysqldump output, including INSERT INTO orders.
  • backend/scripts/backup.sh invokes mysqldump with --single-transaction, --routines, and --triggers, using the configured host, the root user, and the zomato database by default. The MySQL password must not appear in the command-line arguments.
  • backend/scripts/backup.sh updates latest.sql.gz to a symbolic link targeting the newest successfully published dump. After backups dated 20261103T020000Z and 20261104T020000Z, the link must point to zomato-20261104T020000Z.sql.gz.
  • backend/scripts/backup.sh respects KEEP=3. After creating dumps dated 20261101T020000Z through 20261105T020000Z, only these three files must remain:
    • zomato-20261103T020000Z.sql.gz
    • zomato-20261104T020000Z.sql.gz
    • zomato-20261105T020000Z.sql.gz
  • backend/scripts/backup.sh with KEEP=1 removes older matching dumps but preserves unrelated files such as notes.txt.
  • backend/scripts/backup.sh exits non-zero when mysqldump fails. After a successful backup dated 20261103T020000Z followed by a failed backup dated 20261104T020000Z, only the earlier successful dump must remain, and latest.sql.gz must still point to it.
  • backend/scripts/restore-test.sh creates the restore_test database, loads the decompressed latest backup into it through the MySQL client's standard input, and executes SELECT COUNT(*) FROM orders.
  • backend/scripts/restore-test.sh never targets the live zomato database for restoration or deletion. Its database cleanup must drop only restore_test.
  • backend/scripts/restore-test.sh drops restore_test after completing a successful restore test.
  • backend/scripts/restore-test.sh exits non-zero when the restored orders table contains zero rows.
  • backend/scripts/restore-test.sh exits non-zero when BACKUP_DIR/latest.sql.gz does not exist.
  • backend/compose.prod.yml defines a backup service with restart: unless-stopped and a dependency on MySQL using condition: service_healthy.
  • backend/compose.prod.yml mounts the named volume backups at /backups and mounts the scripts directory into the backup service.
  • backend/compose.prod.yml supplies a shell command as a list rather than a plain multi-line string. The scheduling command contains sleep and invokes backup.sh in a loop.
  • backend/compose.prod.yml declares the backups named volume at the top level.

Constraints

  • Use POSIX sh syntax in both scripts. The scripts are executed with sh, so do not rely on Bash-specific features.
  • Require MYSQL_ROOT_PASSWORD and export it to child processes through MYSQL_PWD. Never place the password directly in command-line arguments.
  • Use temporary files before publishing a backup. A failed dump or compression must return non-zero, leave no partial published backup, and preserve the previous latest.sql.gz link.
  • Update latest.sql.gz only after successful publication.
  • Retention must operate only on zomato-*.sql.gz files and retain the newest configured number, based on filename ordering.
  • The restore process must never modify the live zomato database. Clean up the temporary restore database on exit, including failure paths.
  • Escape dollar signs as $$ inside Compose commands so the container shell receives them.
  • Do not modify the provided tests.

Design direction

Keep the backup and recovery responsibilities in separate scripts.

In backend/scripts/backup.sh, handle configuration validation, dump creation, compression, publication, pointer replacement, and retention. Use temporary files inside BACKUP_DIR so successful renames publish complete files. A relative symlink keeps the latest pointer independent of the volume's absolute mount path.

In backend/scripts/restore-test.sh, separate database preparation, import, verification, and cleanup. Use a cleanup trap so the temporary database is removed when a command fails. Keep the restore target configurable, but prevent it from being set to the live zomato database.

Extend backend/compose.prod.yml with a dedicated backup service. Use mysql:8.4 because it includes both mysqldump and the MySQL client. Mount the backups volume independently from the MySQL data volume. Mount the scripts read-only and use /bin/sh -c for the scheduler.

Think about three trade-offs:

  • --single-transaction avoids a long table lock for InnoDB tables, but concurrent schema changes can still affect dump consistency.
  • Atomic publication protects the previous successful backup, while retention controls disk usage. Perform retention only after successful publication.
  • A local named volume survives container recreation, but not every server failure. External backup storage is a separate production concern.

Any layout that meets the contract passes.

How to check your work

Run the tests first, then start the production stack check from the same directory:

bash
cd backend
make test W=7B4
./scripts/check-w7-b4.sh

The Go tests verify the backup scripts, restore behaviour, retention, and Compose scheduling. The helper exercises the workflow against the production Compose stack, restores a real dump into restore_test, and cleans up afterward.

You can also invoke the scripts directly inside the running backup container:

bash
docker compose -f compose.prod.yml exec backup sh /scripts/backup.sh
docker compose -f compose.prod.yml exec backup sh /scripts/restore-test.sh

When a test fails, read the first failing assertion. It identifies the backup file, database operation, cleanup behaviour, or Compose property that does not match the contract.

Finished Exercise 7.7?

Single-server production deployment

3 items

  • 7.5HTTPS health and migration gatesnot completed
  • 7.6Production HTTPS Compose stacknot completed
  • 7.7Nightly mysqldump and one restore testnot completed

Production app testing

Up next · Module 3 · 4 items · about 5 h 30 min

Start module 3

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. backup.sh creates timestamped, compressed MySQL dumps.
  2. Failed backups preserve the previous successful dump and latest pointer.
  3. Retention removes only old matching dumps.
  4. restore-test.sh verifies orders in an isolated database.
  5. Restore failures and missing backups exit non-zero.
  6. The Compose backup service schedules nightly runs using persistent storage.

Commands

terminal
cd backend && make test W=7B4
terminal
cd backend && ./scripts/check-w7-b4.sh

Tests

  • TestW7B4_BackupWritesATimestampedGzipOfTheDump in backend/internal/deploy/w7b4_test.go
  • TestW7B4_BackupTakesAConsistentSnapshotOfTheZomatoDatabase in backend/internal/deploy/w7b4_test.go
  • TestW7B4_BackupPointsLatestAtTheNewestDump in backend/internal/deploy/w7b4_test.go
  • TestW7B4_BackupKeepsOnlyTheNewestDumps in backend/internal/deploy/w7b4_test.go
  • TestW7B4_BackupNeverDeletesFilesItDidNotCreate in backend/internal/deploy/w7b4_test.go
  • TestW7B4_FailedDumpExitsNonZeroAndLeavesNoPartialFile in backend/internal/deploy/w7b4_test.go
  • TestW7B4_RestoreTestLoadsTheDumpIntoAFreshDatabase in backend/internal/deploy/w7b4_test.go
  • TestW7B4_RestoreTestNeverTouchesTheLiveDatabase in backend/internal/deploy/w7b4_test.go
  • TestW7B4_RestoreTestCleansUpAfterItself in backend/internal/deploy/w7b4_test.go
  • TestW7B4_RestoreTestFailsWhenTheRestoredTableIsEmpty in backend/internal/deploy/w7b4_test.go
  • TestW7B4_RestoreTestFailsWithoutAnyBackup in backend/internal/deploy/w7b4_test.go
  • TestW7B4_ComposeRunsTheBackupOnAScheduleWithItsOwnVolume in backend/internal/deploy/w7b4_test.go