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.
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.
./db-sync.sh # every DB in the map
./db-sync.sh shop_db catalog_db # just theseStructure 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.
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
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
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.