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.shcreates a gzip-compressed dump namedzomato-20261104T020000Z.sql.gzwhenBACKUP_TIMESTAMP=20261104T020000Zis supplied. Decompressing that file must contain themysqldumpoutput, includingINSERT INTO orders.backend/scripts/backup.shinvokesmysqldumpwith--single-transaction,--routines, and--triggers, using the configured host, therootuser, and thezomatodatabase by default. The MySQL password must not appear in the command-line arguments.backend/scripts/backup.shupdateslatest.sql.gzto a symbolic link targeting the newest successfully published dump. After backups dated20261103T020000Zand20261104T020000Z, the link must point tozomato-20261104T020000Z.sql.gz.backend/scripts/backup.shrespectsKEEP=3. After creating dumps dated20261101T020000Zthrough20261105T020000Z, only these three files must remain:zomato-20261103T020000Z.sql.gzzomato-20261104T020000Z.sql.gzzomato-20261105T020000Z.sql.gz
backend/scripts/backup.shwithKEEP=1removes older matching dumps but preserves unrelated files such asnotes.txt.backend/scripts/backup.shexits non-zero whenmysqldumpfails. After a successful backup dated20261103T020000Zfollowed by a failed backup dated20261104T020000Z, only the earlier successful dump must remain, andlatest.sql.gzmust still point to it.backend/scripts/restore-test.shcreates therestore_testdatabase, loads the decompressed latest backup into it through the MySQL client's standard input, and executesSELECT COUNT(*) FROM orders.backend/scripts/restore-test.shnever targets the livezomatodatabase for restoration or deletion. Its database cleanup must drop onlyrestore_test.backend/scripts/restore-test.shdropsrestore_testafter completing a successful restore test.backend/scripts/restore-test.shexits non-zero when the restoredorderstable contains zero rows.backend/scripts/restore-test.shexits non-zero whenBACKUP_DIR/latest.sql.gzdoes not exist.backend/compose.prod.ymldefines abackupservice withrestart: unless-stoppedand a dependency on MySQL usingcondition: service_healthy.backend/compose.prod.ymlmounts the named volumebackupsat/backupsand mounts the scripts directory into the backup service.backend/compose.prod.ymlsupplies a shell command as a list rather than a plain multi-line string. The scheduling command containssleepand invokesbackup.shin a loop.backend/compose.prod.ymldeclares thebackupsnamed volume at the top level.
Constraints
- Use POSIX
shsyntax in both scripts. The scripts are executed withsh, so do not rely on Bash-specific features. - Require
MYSQL_ROOT_PASSWORDand export it to child processes throughMYSQL_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.gzlink. - Update
latest.sql.gzonly after successful publication. - Retention must operate only on
zomato-*.sql.gzfiles and retain the newest configured number, based on filename ordering. - The restore process must never modify the live
zomatodatabase. 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-transactionavoids 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:
cd backend
make test W=7B4
./scripts/check-w7-b4.shThe 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:
docker compose -f compose.prod.yml exec backup sh /scripts/backup.sh
docker compose -f compose.prod.yml exec backup sh /scripts/restore-test.shWhen 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.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
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
Up next · Module 3 · 4 items · about 5 h 30 min
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
backup.shcreates timestamped, compressed MySQL dumps.- Failed backups preserve the previous successful dump and latest pointer.
- Retention removes only old matching dumps.
restore-test.shverifies orders in an isolated database.- Restore failures and missing backups exit non-zero.
- The Compose backup service schedules nightly runs using persistent storage.
Commands
cd backend && make test W=7B4cd backend && ./scripts/check-w7-b4.shTests
TestW7B4_BackupWritesATimestampedGzipOfTheDumpinbackend/internal/deploy/w7b4_test.goTestW7B4_BackupTakesAConsistentSnapshotOfTheZomatoDatabaseinbackend/internal/deploy/w7b4_test.goTestW7B4_BackupPointsLatestAtTheNewestDumpinbackend/internal/deploy/w7b4_test.goTestW7B4_BackupKeepsOnlyTheNewestDumpsinbackend/internal/deploy/w7b4_test.goTestW7B4_BackupNeverDeletesFilesItDidNotCreateinbackend/internal/deploy/w7b4_test.goTestW7B4_FailedDumpExitsNonZeroAndLeavesNoPartialFileinbackend/internal/deploy/w7b4_test.goTestW7B4_RestoreTestLoadsTheDumpIntoAFreshDatabaseinbackend/internal/deploy/w7b4_test.goTestW7B4_RestoreTestNeverTouchesTheLiveDatabaseinbackend/internal/deploy/w7b4_test.goTestW7B4_RestoreTestCleansUpAfterItselfinbackend/internal/deploy/w7b4_test.goTestW7B4_RestoreTestFailsWhenTheRestoredTableIsEmptyinbackend/internal/deploy/w7b4_test.goTestW7B4_RestoreTestFailsWithoutAnyBackupinbackend/internal/deploy/w7b4_test.goTestW7B4_ComposeRunsTheBackupOnAScheduleWithItsOwnVolumeinbackend/internal/deploy/w7b4_test.go