Database Backup and Migration
Database backups and migrations are high-risk operations: one wrong target can overwrite production data. Use a repeatable workflow: export (consistent dump), compress, transfer, restore into the correct database, and verify. For WordPress, the safest default is a logical dump with mysqldump using --single-transaction (for InnoDB) so you can back up a live site without long table locks.
For live WordPress sites: mysqldump --single-transaction --quick wordpress | gzip > wordpress.sql.gz, then restore with gzip -dc wordpress.sql.gz | mysql wordpress and verify tables.
Restore steps can overwrite data. Always confirm the target host and database name before importing.
Workflow at a Glance
Prerequisites
- Credentials for a database user with dump/restore privileges.
mysqldumpandmysqlclient installed.- Enough disk space to hold the dump (plus compression overhead).
Verify tooling:
mysqldump --version
mysql --version
gzip --version
Core Commands
mysqldump [OPTIONS] -u USER -p DATABASE > dump.sql
mysql -u USER -p DATABASE < dump.sql
Key Flags (WordPress-Friendly Defaults)
| Flag | Why it matters | Example |
|---|---|---|
--single-transaction | Consistent dump for InnoDB without long locks | mysqldump --single-transaction ... |
--quick | Stream rows to reduce memory usage | mysqldump --quick ... |
--routines --triggers | Include stored routines/triggers if present | mysqldump --routines --triggers ... |
--set-gtid-purged=OFF | Avoid GTID statements (MySQL feature) | Use when needed for MySQL 5.7+ |
Most WordPress databases use InnoDB. If you have MyISAM tables, you may need different locking behavior.
Examples (Commands + Expected Output)
Successful dumps and imports often produce no output. Verify by checking the output file and running sanity queries.
Create a consistent WordPress dump (file output)
mysqldump -u wpuser -p \
--single-transaction --quick \
--routines --triggers \
wordpress > wordpress.sql
Expected output:
(no output on success)
Use case: Create a migration-ready dump.
Confirm the dump file exists and has a reasonable size
ls -lh wordpress.sql
Expected output:
-rw-r--r-- 1 root root 1.2G Mar 1 10:55 wordpress.sql
Use case: Catch empty/failed dumps early.
Compress the dump
gzip -9 wordpress.sql
Expected output:
(no output on success)
Use case: Reduce transfer/storage cost.
Dump and compress in one step
mysqldump -u wpuser -p --single-transaction --quick wordpress | gzip -9 > wordpress.sql.gz
Expected output:
(no output on success)
Use case: Faster workflow with less temporary disk usage.
Transfer the dump to another server
scp wordpress.sql.gz user@destination-vps:/tmp/
Expected output:
wordpress.sql.gz 100% 220M 12.0MB/s 00:18
Use case: Migrate the database between servers.
Restore into the target database
gzip -dc wordpress.sql.gz | mysql -u root -p wordpress
Expected output:
(no output on success)
Use case: Import into the correct database on the destination.
Verify tables exist after restore
mysql -u wpuser -p -D wordpress -e "SHOW TABLES;"
Expected output:
Tables_in_wordpress
wp_options
wp_posts
wp_postmeta
...
Use case: Fast sanity check after import.
WordPress Migration Checklist
- Confirm the correct database name and credentials on source and destination.
- Take a dump with
--single-transaction --quick. - Compress and transfer the dump.
- Create the destination database (if needed).
- Import the dump.
- Verify table count and a few key rows (
siteurl,home, users).
WordPress VPS Use Cases
| Scenario | Recommended approach | Notes |
|---|---|---|
| Pre-update backup | Dump + gzip | Keep last-known-good DB state |
| Full server migration | Dump + transfer + restore | Verify before DNS cutover |
| Disaster recovery test | Restore into staging | Proves backups are usable |
Troubleshooting
| Problem | Likely cause | Fix |
|---|---|---|
| Dump file is tiny | Wrong database or credentials | Verify DB name and run SHOW TABLES; |
| Import fails with permission errors | Privileges missing | Grant required permissions or import as admin |
| Site breaks after restore | URL mismatch | Update siteurl/home carefully; verify serialized data handling |
Best Practices
- Always keep the dump file name dated (e.g.,
wordpress-2026-03-01.sql.gz). - Use
--single-transactionfor live InnoDB workloads. - Verify backups by restoring into staging regularly.
Cheat Sheet
mysqldump -u wpuser -p --single-transaction --quick wordpress > wordpress.sql
gzip -9 wordpress.sql
mysqldump -u wpuser -p --single-transaction --quick wordpress | gzip -9 > wordpress.sql.gz
scp wordpress.sql.gz user@destination:/tmp/
gzip -dc wordpress.sql.gz | mysql -u root -p wordpress
mysql -u wpuser -p -D wordpress -e "SHOW TABLES;"