Chapter 19: Backup & Restore

This is the chapter that saves your life when something goes wrong! No matter how good your code or how careful you are, accidents happen:

  • Server crashes
  • Wrong DELETE without WHERE
  • Hacker attack
  • Disk failure

That’s why backups are non-negotiable in any real database. Today we’ll learn how to:

  • Create full backups using mysqldump (the most common tool)
  • Restore from backup
  • Basics of point-in-time recovery (recover to any exact moment)

Let’s pretend our my_coaching database is our live coaching institute system — we never want to lose student enrollments, fees, or personal data!

1. mysqldump Utility – The King of MySQL Backups

mysqldump is a command-line tool that comes with MySQL. It creates a .sql file containing:

  • CREATE DATABASE / CREATE TABLE statements
  • All INSERT statements with your data
  • Views, procedures, triggers, etc.

Important options you must know:

Option What it does When to use
–databases dbname Include CREATE DATABASE statement Always (for full restore)
–all-databases Backup every database on the server For full server backup
–routines Include stored procedures & functions Almost always
–triggers Include triggers Almost always
–events Include scheduled events If you use events
–no-data Only structure (no data) For schema-only backup
–single-transaction Consistent backup without locking tables For InnoDB tables (recommended!)
–quick Faster for large tables Always with big data

Basic full backup command (recommended):

Bash
  • Enter root password when prompted
  • File saved as my_coaching_backup_2026-01-24.sql
  • –single-transaction = no table locks, consistent snapshot

Backup only data (no CREATE statements):

Bash

Backup specific tables:

Bash

Best practice – Automate daily backups (cron job example – Linux/macOS):

Bash

2. Restoring from Backup – Bringing Data Back to Life

Two main ways to restore:

Method 1 – Restore entire backup file

Bash
  • This runs all CREATE + INSERT statements
  • If database already exists → error! (unless you drop it first)

Safe way – Drop & recreate database first:

Bash

Inside mysql:

SQL

Then:

Bash

Method 2 – Restore only specific tables from backup If backup contains multiple tables:

Bash

Important:

  • Always test restore on a test server first!
  • Check file size & contents before restoring
  • Use –force if you want to ignore errors:
Bash

3. Point-in-Time Recovery (PITR) Basics

(Recover to any exact moment – advanced but very powerful)

Full backup + binary logs = PITR

Binary logs record every change (INSERT/UPDATE/DELETE) after the backup.

Step-by-step PITR:

  1. Enable binary logging (in my.cnf / my.ini):
ini

Restart MySQL.

  1. Take full backup with binlog position
Bash

The file will contain a line like:

SQL
  1. Later – to recover to a specific time:
  • Restore full backup first
  • Then apply binlogs from that point:
Bash

Or recover to exact time:

Bash

Best practice: Use tools like Percona XtraBackup or MySQL Enterprise Backup for hot backups + PITR in production.

That’s it for Chapter 19! 🎉 You now know how to protect your data and recover from disasters — one of the most important skills any database professional must have!

Homework for today (do it right now – very important!) Run these commands and paste the output of the final SELECT:

SQL

Paste the student_count from the last SELECT — I’ll check if restore worked!

This concludes our full MySQL Tutorial series! 🎉 You’ve gone from zero to professional-level MySQL knowledge — congratulations!

If you want:

  • Practice projects
  • Advanced topics (partitioning, replication, performance tuning)
  • Interview questions

Just tell me — I’m always here for you! 😊

What would you like to do next?

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *