Backup & Restore
Learn why regular backups matter and how to back up and restore a MySQL database using mysqldump.
Introduction
No matter how carefully you design a database, things can still go wrong — someone runs a DELETE without a WHERE clause, a hard drive fails, or a bug corrupts data. A recent backup is the difference between a minor inconvenience and a catastrophic, unrecoverable loss.
This lesson covers mysqldump, MySQL's standard command-line backup tool, how to restore a database from a backup file, and how to automate the whole process so it happens without you needing to remember.
- Why regular backups are essential for any real database.
- How to create a full backup with the mysqldump tool.
- How to restore a database from a backup file.
- How to automate backups on a schedule.
Why Backups Matter
A backup is simply a saved copy of your data, taken at a specific point in time, that you can use to restore your database if something goes wrong. Without one, any data-destroying mistake or failure is permanent.
Accidental Data Loss
A mistaken DELETE, DROP, or UPDATE without a proper WHERE clause can wipe out data instantly.
Hardware Failure
Disks and servers eventually fail, sometimes without warning.
Data Corruption
Software bugs, power outages, or crashes can corrupt data files.
Peace of Mind
A recent backup means any disaster becomes recoverable, not permanent.
If you have never taken a backup of a production database, you are one mistake away from losing everything in it, permanently. Treat backups as a required part of running any real database, not an optional extra.
Creating a Backup with mysqldump
mysqldump is a command-line tool, bundled with MySQL, that exports an entire database — its table structures and all its data — into a single SQL file full of CREATE TABLE and INSERT statements.
It is run from your terminal (not inside a MySQL SQL session), like this:
mysqldump -u root -p shop > shop_backup.sqlHere, -u root specifies the username, -p prompts you for the password, shop is the name of the database to back up, and > shop_backup.sql redirects the output into a file instead of printing it to the screen.
-- MySQL dump 10.13
DROP TABLE IF EXISTS `products`;
CREATE TABLE `products` (
`id` int NOT NULL,
`name` varchar(100) NOT NULL,
`price` decimal(10,2) NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `products` VALUES (1,'Wireless Mouse',19.99),(2,'USB-C Cable',9.50);The resulting file is plain, readable SQL — you can open it in a text editor, and it can be replayed against any MySQL server to fully recreate the database from scratch.
Restoring from a Backup
Restoring simply means running the backup file's SQL statements back against a MySQL server. You do this from the terminal using the mysql command-line client, with the backup file redirected in as input.
mysql -u root -p shop < shop_backup.sqlNote the direction of the redirect: an export uses > (output goes into the file), while a restore uses < (the file feeds into the command as input). The target database (shop, here) must already exist before you restore into it.
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS shop;"
mysql -u root -p shop < shop_backup.sqlA restore replays DROP TABLE and CREATE TABLE statements from the backup, so restoring into a database that already has tables with the same names will destroy their current contents. Always double-check which database you are restoring into.
Automating Backups
Manually remembering to run mysqldump every day is unreliable — automation is what actually keeps you safe. Two common approaches are using an OS-level scheduler, or a MySQL Event you learned about in an earlier lesson.
On Linux or macOS, a cron job can run mysqldump automatically every night:
# Run every day at 2:00 AM
0 2 * * * mysqldump -u root -pYOURPASSWORD shop > /backups/shop_$(date +\%F).sqlAlternatively, since mysqldump itself runs outside MySQL, an internal MySQL Event cannot invoke it directly — but Events are still useful for scheduling in-database maintenance (like archiving old rows) that complements an OS-level backup schedule.
Do not overwrite the same backup file every night. Keep several recent backups (for example, one per day for the last week) so that even a backup taken after data was already silently corrupted is not your only option.
Common Mistakes
- Never taking a backup until after something has already gone wrong.
- Storing backups only on the same server as the live database — if that server fails, the backups are lost too.
- Restoring a backup without checking which database it will overwrite.
- Forgetting to test that a backup file can actually be restored successfully before you need it in an emergency.
Best Practices
- Automate backups on a regular schedule rather than relying on memory.
- Store backup files on separate storage or a different machine from the live database.
- Periodically test restoring a backup to confirm it actually works.
- Keep several recent backups, not just the single most recent one.
Frequently Asked Questions
Is mysqldump run inside a MySQL session?
No, it is a separate command-line tool run directly from your terminal or shell, not from within a mysql> prompt.
Can I back up just one table instead of the whole database?
Yes — you can pass the table name after the database name, for example: mysqldump -u root -p shop products > products_backup.sql.
What happens if the target database does not exist when restoring?
The restore command will fail. You need to create the database first, typically with CREATE DATABASE, before running the restore.
Is a mysqldump backup human-readable?
Yes, it produces plain SQL text that you can open and read in any text editor.
Are there other backup methods besides mysqldump?
Yes, tools like MySQL Enterprise Backup or Percona XtraBackup exist for very large databases, but mysqldump remains the standard, widely available starting point.
Key Takeaways
- Regular backups protect against accidental data loss, hardware failure, and corruption.
- mysqldump exports a full database's structure and data into a single SQL file.
- Restoring means replaying that SQL file with the mysql command-line client.
- Backups should be automated with a scheduler like cron rather than done manually.
- Backups should be stored separately from the live database and tested periodically.
Summary
Backups are not optional for any database that matters — they are the safety net that turns a disaster into a minor inconvenience.
In this lesson, you learned why backups matter, how to create one with mysqldump, how to restore from it, and how to automate the process. Next, you will learn how to manage the users who are allowed to connect to your MySQL server in the first place.
- You understand why regular backups are essential.
- You can create a full backup using mysqldump.
- You can restore a database from a backup file.
- You are ready to learn about managing MySQL users.