HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

MySQL Backup & Restore

Data is the most valuable asset in any application. Hardware fails, accidental deletes happen, and deployments sometimes go wrong. A reliable backup strategy ensures that no matter what goes wrong, you can recover your data and get back up and running quickly.

MySQL provides a powerful command-line tool called mysqldump that exports a database — or any part of it — into a plain SQL file. That file can then be used to restore the database at any point in the future.


Types of MySQL Backups

🗄️
Full Database Backup

Exports all tables, data, and structure from one database into a single SQL file.

📋
Single Table Backup

Exports only one specific table — useful for protecting critical data before a risky update.

🌐
All Databases Backup

Exports every database on the entire MySQL server into one combined SQL file.

📊
Structure Only

Exports just the table definitions (CREATE TABLE statements) without any row data.


What is mysqldump?

mysqldump is a command-line utility that comes bundled with MySQL. It connects to the MySQL server, reads the requested database(s), and writes a series of SQL statements — CREATE TABLE, INSERT INTO, and others — into a .sql file. Running that file later recreates the database exactly as it was at the time of the backup.

All mysqldump commands are run from your operating system's terminal (Command Prompt, PowerShell, or Bash) — not from inside the MySQL console.


Backing Up a Single Database

The most common backup command exports one complete database:

# Syntax
$ mysqldump -u username -p database_name > backup_file.sql

# Example — back up the 'shop_db' database
$ mysqldump -u root -p shop_db > shop_db_backup.sql

After running this command, MySQL will prompt you for the user's password. Once entered, the file shop_db_backup.sql will be created in your current directory containing the complete database.

Naming Tip: Include a date in your backup filename so you always know when it was taken — for example: shop_db_2026_05_04.sql. This makes it easy to find the right backup when you need it.

Backing Up Specific Tables

If you only need to back up certain tables from a database, list them after the database name:

# Back up only the 'orders' and 'customers' tables from shop_db
$ mysqldump -u root -p shop_db orders customers > orders_customers_backup.sql

Backing Up All Databases

To create a complete server-wide backup — all databases at once — use the --all-databases flag:

# Back up every database on the server
$ mysqldump -u root -p --all-databases > all_databases_backup.sql
Note: The --all-databases backup includes the mysql system database, which contains user accounts and privileges. This makes it a useful disaster recovery backup for the entire server.

Structure-Only Backup (No Data)

Sometimes you only want the table definitions — perhaps to recreate the schema on a new server without copying the data. Use --no-data:

# Export table structure only, no row data
$ mysqldump -u root -p --no-data shop_db > shop_db_structure.sql

Data-Only Backup (No Structure)

Conversely, if the tables already exist on the target server and you only want to import the data rows, use --no-create-info:

# Export row data only, no CREATE TABLE statements
$ mysqldump -u root -p --no-create-info shop_db > shop_db_data_only.sql

Restoring a Database

Restoring from a backup is done with the mysql command — the standard MySQL client. You pipe the SQL file into it and MySQL replays all the statements inside:

# Syntax
$ mysql -u username -p database_name < backup_file.sql

# Example — restore shop_db from backup
$ mysql -u root -p shop_db < shop_db_backup.sql
Before Restoring: The target database must already exist. If you are restoring to a brand new server, create the database first:
CREATE DATABASE shop_db;
Then run the restore command above.

Restoring an All-Databases Backup

If your backup was created with --all-databases, you do not need to specify a database name — the SQL file already contains CREATE DATABASE and USE statements:

$ mysql -u root -p < all_databases_backup.sql

Useful mysqldump Options

Here is a reference of commonly used options:

Option What it does
--all-databases Dumps all databases on the server
--no-data Exports table structure only — no row data
--no-create-info Exports row data only — no CREATE TABLE statements
--single-transaction Takes a consistent snapshot without locking tables (InnoDB only — recommended)
--routines Includes stored procedures and stored functions in the dump
--triggers Includes triggers (included by default)
--compress Compresses data transferred between client and server
--result-file=file.sql Alternative to > — writes output directly to a file

Recommended Production Backup Command

For InnoDB databases in production, this command produces a clean, consistent backup without locking your tables:

$ mysqldump -u root -p --single-transaction --routines --triggers shop_db > shop_db_2026_05_04.sql

Automating Backups

Running backups manually every day is easy to forget. On Linux or macOS you can automate backups using a cron job. On Windows you can use Task Scheduler.

Here is an example Linux cron entry that backs up shop_db every day at 2:00 AM:

# Open crontab editor
$ crontab -e

# Add this line (runs daily at 02:00)
0 2 * * * mysqldump -u root -pYourPassword --single-transaction shop_db > /backups/shop_db_$(date +\%F).sql
Security Note: Embedding a password directly in a cron job or shell script is a security risk. Consider storing credentials in MySQL's option file (~/.my.cnf) with restricted file permissions (chmod 600) and using -p without a password value in the command.

Key Points to Remember

  • mysqldump exports a database into a plain SQL file that can be used to recreate it.
  • Run mysqldump from the terminal — not from inside the MySQL console.
  • Use > to write the backup to a file and < to restore from one.
  • The target database must exist before you restore into it.
  • Use --single-transaction for InnoDB databases to get a consistent backup without locking tables.
  • Include the date in your backup filename and store backups in a separate, secure location.
  • Test your backups by restoring them occasionally — an untested backup is not a backup.