How to test a database backup: restore the dump in Docker on your Mac

A backup you have never restored from is a hope, not a backup. The easiest way to test one is a temporary database in Docker on your own Mac, far from the production server.

What you need

Docker Desktop or OrbStack on the Mac and a dump file, such as mysql-shop.sql.gz. If the dump is compressed with zstd, install it with Homebrew (brew install zstd) and use zstd -dc instead of gunzip -c.

MySQL and MariaDB

Start a temporary server. Use the same major version as production, or newer:

docker run -d --name restore-test -e MYSQL_ROOT_PASSWORD=test mysql:8.4
until docker exec restore-test mysqladmin ping -h 127.0.0.1 -uroot -ptest --silent; do sleep 2; done

The second line waits until the server in the container finishes its first-run setup and starts accepting connections.

Create the database and load the dump:

docker exec restore-test mysql -uroot -ptest -e 'CREATE DATABASE shop'
gunzip -c mysql-shop.sql.gz | docker exec -i restore-test mysql -uroot -ptest shop

The -i flag matters here: it is how the dump gets into the container. If loading fails on lines with DEFINER=`shop`@`localhost`, strip them on the fly:

gunzip -c mysql-shop.sql.gz | sed -E 's/DEFINER=`[^`]+`@`[^`]+`//g' \
  | docker exec -i restore-test mysql -uroot -ptest shop

PostgreSQL

docker run -d --name pg-test -e POSTGRES_PASSWORD=test postgres:16
until docker exec pg-test pg_isready -h 127.0.0.1 -U postgres -q; do sleep 1; done
docker exec pg-test createdb -U postgres crm
gunzip -c pgsql-crm.sql.gz | docker exec -i pg-test psql -X -q -1 -v ON_ERROR_STOP=1 -U postgres -d crm

The image version must be no older than the pg_dump that made the dump. If loading stops at role "crm" does not exist, create the role (docker exec pg-test createuser -U postgres crm) or take dumps with --no-owner --no-privileges.

Compare the tables

Tables in the dump:

gunzip -c mysql-shop.sql.gz | grep -c '^CREATE TABLE'

And in the restored database:

docker exec restore-test mysql -uroot -ptest -N -e \
  "select count(*) from information_schema.tables where table_schema='shop' and table_type='BASE TABLE'"
docker exec pg-test psql -U postgres -d crm -Atc \
  "select count(*) from information_schema.tables where table_schema not in ('pg_catalog','information_schema') and table_type='BASE TABLE'"

The numbers should match. Then open a couple of key tables and look at the latest rows: orders, users, posts.

Clean up

docker rm -f restore-test pg-test

The containers go away with their data, and only the dump stays on the Mac.

Why not on the server

Restoring over a live database wipes real data if anything goes wrong. Test backups where a mistake costs nothing. Put a database back on the server only after such a test, and by hand.