CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Backup & Restore / mysqldump

mysqldump

Back up MySQL databases as SQL

Intermediatebackupdatabasemysql

mysqldump connects to a MySQL or MariaDB server and writes a database out as SQL statements (CREATE TABLE, INSERT) that rebuild it when replayed with the mysql client. It's the simplest logical backup, perfect for small and medium databases, migrations and copies to staging.

Syntaxmysqldump [OPTIONS] DB [TABLE...] | --databases DB... | --all-databases

Watch it run

Before a risky migration you take a quick copy of the shop database on an Ubuntu server.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
sudo mysqldump --single-transaction --routines --events shop > shop.sql

On Ubuntu the MySQL root account uses socket authentication, so sudo is enough and no password is needed. The dump goes to stdout, redirected into a file.

Anatomy of a command

$
commandmysqldump

The MySQL client program that reads schema and rows and prints them as SQL.

How it works

mysqldump is an ordinary client. For each table it runs SHOW CREATE TABLE and then SELECT * FROM the table, streaming rows one by one (--quick, part of the default --opt) and printing them as multi-row INSERT statements. Consistency is the part to understand. By default it runs LOCK TABLES ... READ for each database, so writes block for the whole dump. --single-transaction instead sets REPEATABLE READ and runs START TRANSACTION WITH CONSISTENT SNAPSHOT: InnoDB's MVCC then shows the dump the data as of that instant while the application keeps writing. That only works for transactional tables (not MyISAM), and DDL such as ALTER TABLE during the dump breaks it. --source-data briefly takes FLUSH TABLES WITH READ LOCK to record the binary log position, which is what you need for point-in-time recovery. Restoring means replaying all the SQL on one thread and rebuilding every index, so a 200 GB database can take many hours; physical tools (Percona XtraBackup, MySQL Shell dump utilities, cloud snapshots) are used at that size.

Options you'll actually use

--single-transactionConsistent InnoDB snapshot without blocking writes
--routines / -RInclude stored procedures and functions
--events / -EInclude event scheduler events
--databases / -BDump several databases, with CREATE DATABASE and USE
--all-databases / -AEvery database, including the mysql system schema
--no-data / -dSchema only
--no-create-info / -tData only
--where='cond'Dump only matching rows
--source-data=2Record the binlog file and position as a comment (PITR)
--no-tablespacesSkip tablespace info; avoids needing the PROCESS privilege
--set-gtid-purged=OFFDon't write GTID_PURGED (when loading into a server with its own GTIDs)
--defaults-extra-file=FRead user and password from a protected option file
--skip-extended-insertOne INSERT per row: bigger, but diff-friendly

Common mistakes

mysqldump without --single-transaction on a busy InnoDB database

The default locks tables for reading, so every write waits until the dump finishes.

mysqldump --single-transaction --routines --events db
mysqldump -u backup -pSecret123 in a cron line

The password is visible in ps output, process accounting and crontab backups.

--defaults-extra-file=~/.backup.cnf (mode 600)
mysqldump db | gzip > file in a script without pipefail

The pipeline reports gzip's success even when mysqldump failed, leaving a tiny useless file.

set -o pipefail, check the exit code and the file size
Relying on mysqldump for a 500 GB database

The dump takes hours and the restore takes far longer, which blows any recovery time objective.

Physical backups (XtraBackup, MySQL Shell dumpInstance with parallel load) or volume/RDS snapshots plus binlogs

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Write the command that dumps only the schema (no rows) of database 'shop' into schema.sql. (The sandbox has no MySQL server.)

  2. 2

    Write a cron-safe backup line that dumps 'shop' consistently with routines, compresses it with a date in the name, reads credentials from ~/.backup.cnf, and fails if mysqldump fails.

  3. 3

    Write the commands to restore /backup/shop-2026-10-28.sql.gz into a new database called shop_copy.

  4. 4

    Create an option file for credentials in your home directory with the right permissions.

Related commands

pg_dumpBack up a PostgreSQL database consistentlygzipCompress and decompress single files with gzipcrontabSchedule recurring jobs with cronbash: exit codesExit codes, $?, set -euo pipefail and trap
PreviouslastNextpg_dump
Linux / Backup & Restore / mysqldump

mysqldump

Back up MySQL databases as SQL

Intermediatebackupdatabasemysql

mysqldump connects to a MySQL or MariaDB server and writes a database out as SQL statements (CREATE TABLE, INSERT) that rebuild it when replayed with the mysql client. It's the simplest logical backup, perfect for small and medium databases, migrations and copies to staging.

Syntaxmysqldump [OPTIONS] DB [TABLE...] | --databases DB... | --all-databases

Watch it run

Before a risky migration you take a quick copy of the shop database on an Ubuntu server.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
sudo mysqldump --single-transaction --routines --events shop > shop.sql

On Ubuntu the MySQL root account uses socket authentication, so sudo is enough and no password is needed. The dump goes to stdout, redirected into a file.

Anatomy of a command

$
commandmysqldump

The MySQL client program that reads schema and rows and prints them as SQL.

How it works

mysqldump is an ordinary client. For each table it runs SHOW CREATE TABLE and then SELECT * FROM the table, streaming rows one by one (--quick, part of the default --opt) and printing them as multi-row INSERT statements. Consistency is the part to understand. By default it runs LOCK TABLES ... READ for each database, so writes block for the whole dump. --single-transaction instead sets REPEATABLE READ and runs START TRANSACTION WITH CONSISTENT SNAPSHOT: InnoDB's MVCC then shows the dump the data as of that instant while the application keeps writing. That only works for transactional tables (not MyISAM), and DDL such as ALTER TABLE during the dump breaks it. --source-data briefly takes FLUSH TABLES WITH READ LOCK to record the binary log position, which is what you need for point-in-time recovery. Restoring means replaying all the SQL on one thread and rebuilding every index, so a 200 GB database can take many hours; physical tools (Percona XtraBackup, MySQL Shell dump utilities, cloud snapshots) are used at that size.

Options you'll actually use

--single-transactionConsistent InnoDB snapshot without blocking writes
--routines / -RInclude stored procedures and functions
--events / -EInclude event scheduler events
--databases / -BDump several databases, with CREATE DATABASE and USE
--all-databases / -AEvery database, including the mysql system schema
--no-data / -dSchema only
--no-create-info / -tData only
--where='cond'Dump only matching rows
--source-data=2Record the binlog file and position as a comment (PITR)
--no-tablespacesSkip tablespace info; avoids needing the PROCESS privilege
--set-gtid-purged=OFFDon't write GTID_PURGED (when loading into a server with its own GTIDs)
--defaults-extra-file=FRead user and password from a protected option file
--skip-extended-insertOne INSERT per row: bigger, but diff-friendly

Common mistakes

mysqldump without --single-transaction on a busy InnoDB database

The default locks tables for reading, so every write waits until the dump finishes.

mysqldump --single-transaction --routines --events db
mysqldump -u backup -pSecret123 in a cron line

The password is visible in ps output, process accounting and crontab backups.

--defaults-extra-file=~/.backup.cnf (mode 600)
mysqldump db | gzip > file in a script without pipefail

The pipeline reports gzip's success even when mysqldump failed, leaving a tiny useless file.

set -o pipefail, check the exit code and the file size
Relying on mysqldump for a 500 GB database

The dump takes hours and the restore takes far longer, which blows any recovery time objective.

Physical backups (XtraBackup, MySQL Shell dumpInstance with parallel load) or volume/RDS snapshots plus binlogs

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Write the command that dumps only the schema (no rows) of database 'shop' into schema.sql. (The sandbox has no MySQL server.)

  2. 2

    Write a cron-safe backup line that dumps 'shop' consistently with routines, compresses it with a date in the name, reads credentials from ~/.backup.cnf, and fails if mysqldump fails.

  3. 3

    Write the commands to restore /backup/shop-2026-10-28.sql.gz into a new database called shop_copy.

  4. 4

    Create an option file for credentials in your home directory with the right permissions.

Related commands

pg_dumpBack up a PostgreSQL database consistentlygzipCompress and decompress single files with gzipcrontabSchedule recurring jobs with cronbash: exit codesExit codes, $?, set -euo pipefail and trap
PreviouslastNextpg_dump