Skip to content
Sandeep Kumar
Updated 3 min read

Sync production MySQL to your laptop, safely

Read-only users, a source-to-local DB map, structure-only tables for PII, and gzip streaming: the small Bash script I use to debug safely with real data.

Sandeep Kumar, Full-stack EngineerSandeep KumarFull-stack Engineer
On this page
  1. A read-only user, always
  2. Map production names to local names
  3. Structure only for sensitive tables
  4. Do not lock production
  5. Import into a fresh database
  6. What I would add next

Some bugs only show up with real data. A model with 40 variants. A city slug with a trailing space. A price row that is NULL when it supposedly cannot be.

For those I want production data locally, without risking production and without copying anything I should not have on a laptop. This is the shape of the script I use across several projects.

A read-only user, always

The script connects with a user that has SELECT, SHOW VIEW and TRIGGER, and nothing else. No LOCK TABLES, because the dump below never takes a lock.

That is the whole safety model and it is deliberately boring. If I point it at the wrong host, or reverse the arguments at the end of a long day, the worst it can do is read something.

Credentials come from environment variables — SRC_HOST, SRC_USER, SRC_PASSWORD, LOCAL_* — and never from the repository.

Map production names to local names

Production and local databases rarely share names, so the script keeps the mapping explicit rather than inferring it.

bash
DB_MAP=(
  "shop_db        shop_local"
  "catalog_db     catalog"
  "blog_db        blog_local"
)

Arguments are always the production names. An unknown name prints the map and exits rather than guessing, because a guess here writes to the wrong local database and you find out later.

bash
./db-sync.sh                    # every DB in the map
./db-sync.sh shop_db catalog_db # just these

Structure only for sensitive tables

Leads, SMS logs, sessions and audit trails hold phone numbers and personal data. I need their schema to run the application. I do not need their rows, and having them on a laptop is a liability with no upside.

bash
STRUCTURE_ONLY_TABLES="audit_trail lead leads lead_detail sms_log queue"
IGNORE_TABLES="logs user_session"

The dump runs twice per database. Once with --no-data for the structure-only list, once with --ignore-table for everything else. Per-database extras go in IGNORE_TABLES_<db>, so a giant cache table in one database does not affect the others.

This is the rule I would keep if I kept only one. The convenience of having real rows locally is never worth being the person who had a production lead table on a stolen laptop.

Do not lock production

bash
mariadb-dump --single-transaction --quick --skip-lock-tables \
  --routines --triggers "$SRC_DB" | gzip > "$DUMP"

Those are the MariaDB client names, which is what my machines have; on MySQL proper the commands are mysqldump and mysql and the flags are the same.

--single-transaction gives a consistent InnoDB snapshot without taking table locks. --quick streams rows instead of buffering whole tables into memory. And piping through gzip keeps the file small and the transfer short, which matters more than it sounds on a bad connection.

Import into a fresh database

bash
mariadb -e "DROP DATABASE IF EXISTS \`$LOCAL_DB\`; CREATE DATABASE \`$LOCAL_DB\`"
zcat "$DUMP" | mariadb "$LOCAL_DB"

Dropping and recreating avoids half-merged states, where an old local table survives because production renamed it and you spend twenty minutes debugging a table that no longer exists upstream.

Anything that writes while you are importing — a webhook, a retrying job — needs to be safe to replay, which is its own problem.

And the reason this script exists at all is that query plans depend on the data. An index that looks decisive against ten thousand test rows gets ignored against ten million, because the optimiser's choice turns on how selective the condition actually is. Reading a slow query plan against synthetic data tells you about synthetic data.

What I would add next

A row-count comparison after the import, printed as a table, so a silently truncated dump is obvious rather than discovered three days later.

--where filters for the very large tables, since last 90 days is almost always enough and the full history is most of the transfer time.

And a guard that refuses to run unless LOCAL_HOST is 127.0.0.1 or localhost. The read-only user makes the source safe. Nothing currently makes the destination safe, and that is the gap in this script I am least comfortable with.

Small script, large payoff. Reproducing a production bug locally in minutes rather than guessing at it from logs.

Frequently asked

How do I copy a production database without risking production?

Use a dedicated read-only account with SELECT, SHOW VIEW and TRIGGER and nothing else, and dump with --single-transaction so InnoDB gives you a consistent snapshot without taking table locks. The safety comes from the grant rather than from the care you take at the keyboard, which is the point.

What should I exclude when pulling production data to a laptop?

Anything personal you do not need to run the application: leads, contacts, sessions, SMS and email logs, audit trails. Dump those with --no-data so you keep the schema and none of the rows, and ignore outright anything huge that you will never read, such as log or cache tables.

Will mysqldump lock my production tables?

Not with --single-transaction on InnoDB, which reads from a consistent snapshot instead of locking. Add --quick so rows stream rather than buffering whole tables into memory, and pipe through gzip to keep the transfer short. Without --single-transaction the default behaviour does take locks.

Why not just use seed data locally?

Because some bugs only exist in real rows, and because the query optimiser's choices depend on data distribution. An index that looks decisive against ten thousand synthetic rows can be ignored against ten million real ones, so a plan read against seed data tells you about seed data.

Found this useful?Share on XShare on LinkedIn

Related reading