Advanced MySQL Lesson 12 of 13
MySQL Backup and Restore — mysqldump, Export and Import
Back up MySQL databases with mysqldump and phpMyAdmin export, restore them with import, move a database to another computer or a web host, and import CSV data.
Hard drives fail, laptops get stolen, and someone eventually runs DELETE without a WHERE. A backup is the only real protection. MySQL makes backups easy: they're just text files full of SQL.
Backup with mysqldump
mysqldump -u root -p school > school-backup-2026-12-02.sql
This creates a .sql file containing the CREATE TABLE and INSERT statements needed to rebuild the whole database. In XAMPP on Windows, run it from C:\xampp\mysql\bin.
A peek inside the file:
CREATE TABLE `students` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(80) NOT NULL,
...
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `students` VALUES (1,'Ana Reyes',...),(2,'Ben Santos',...);
Useful options
| Command | Backs up |
|---|---|
mysqldump -u root -p school students > students.sql | One table |
mysqldump -u root -p --databases school store > two.sql | Several databases |
mysqldump -u root -p --no-data school > structure.sql | Structure only, no rows |
mysqldump -u root -p --all-databases > everything.sql | Everything on the server |
Restore
mysql -u root -p -e "CREATE DATABASE school_restored CHARACTER SET utf8mb4"
mysql -u root -p school_restored < school-backup-2026-12-02.sql
With phpMyAdmin
- Backup: select the database → Export → Quick → Format: SQL → Go. A
.sqlfile downloads. - Restore: create or select a database → Import → choose the
.sqlfile → Go.
Moving your project to another computer or host
- Export the database to a
.sqlfile. - Copy your PHP files and the
.sqlfile to the new place (flash drive, Google Drive, or upload). - On the new server, create an empty database and import the file.
- Update the database name, user and password in your app's connection file.
This is exactly how capstone projects are moved from a student's laptop to the panel's computer, or to a web host.
Importing a CSV file
Have a class list in Excel? Save it as CSV, then load it into a table:
LOAD DATA LOCAL INFILE 'C:/data/students.csv'
INTO TABLE students
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(lrn, first_name, last_name, grade_level);
IGNORE 1 LINES skips the header row. phpMyAdmin's Import tab can also read CSV files with a few clicks.
A backup routine
- Automate: schedule
mysqldumpdaily (Task Scheduler on Windows, cron on Linux). - Keep several copies: e.g., the last 7 daily backups.
- Store them elsewhere: a backup on the same disk dies with that disk. Copy it to cloud storage or another drive.
- Test restoring: a backup you've never restored might not work when you need it.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04