← Back to Chapters

MySQL Database Backup

?️ MySQL Database Backup

? Quick Overview

Database backup in MySQL means creating a copy of your database data so it can be restored in case of data loss, corruption, or system failure. Regular backups are critical for data safety and disaster recovery.

? Key Concepts

  • Backup is usually stored as a .sql file
  • Most common tool: mysqldump
  • Backups can be full database, single database, or specific tables
  • Backups can be restored using MySQL commands

? Syntax / Theory

MySQL uses the mysqldump utility to export database structure and data into a SQL file. This file contains SQL statements like CREATE and INSERT which can recreate the database.

? Code Example(s)

? View Code Example
-- Backup a single MySQL database
mysqldump -u root -p my_database > my_database_backup.sql
? View Code Example
-- Backup all MySQL databases
mysqldump -u root -p --all-databases > all_databases_backup.sql
? View Code Example
-- Backup specific tables from a database
mysqldump -u root -p my_database users orders > tables_backup.sql

? Live Output / Explanation

What Happens?

  • MySQL asks for your password
  • A .sql file is created
  • The file contains SQL queries to recreate data
  • You can store this file safely or upload to cloud

? Tips & Best Practices

  • Always test backups by restoring them
  • Use meaningful file names with dates
  • Store backups in a different physical location
  • Automate backups using scheduled tasks

? Try It Yourself / Practice Tasks

  1. Create a sample MySQL database
  2. Insert some records
  3. Take a backup using mysqldump
  4. Delete the database
  5. Restore it from the backup file