- 25+ Projects Delivered • 5 Years Experience • UK, USA & Pakistan Clients • Free SEO Audits - 25+ Projects Delivered • 5 Years Experience • UK, USA & Pakistan Clients • Free SEO Audits 

- 25+ Projects Delivered • 5 Years Experience • UK, USA & Pakistan Clients • Free SEO Audits - 25+ Projects Delivered • 5 Years Experience • UK, USA & Pakistan Clients • Free SEO Audits 

Back issues Writing

A Backup You Have Never Restored Is a Maybe

I cut a WordPress dump at a clean statement boundary and restored it. Four of seven tables came back, no posts at all, and the import returned exit code 0.

Topics

The file had been cut at a statement boundary, at 90 percent of its bytes. The import took 1.96 seconds, returned exit code 0, and printed nothing to stderr. When it stopped, the database held 4 of the 7 tables and 30,900 of 34,956 rows. All 24,000 post meta rows had arrived. Not one of the 2,000 posts they belong to had.

Cut the same file mid line and the client tells you it failed. The difference between those two runs is where the bytes happened to stop, and a disk that fills up mid dump stops wherever it stops.

Short answer

A backup is a file until you have restored it and compared the result against the source. So the monthly job is not "check the backup ran". It is: download the newest archive, import it into a scratch database, then compare row counts and checksums table by table. If a table is missing or short, the backup failed and you found out on a Tuesday instead of during an outage.

What the drill measured

I built a WordPress-shaped database in a disposable MariaDB 11.8.9 container and ran six measured runs against it, three dumps and three restores. 34,956 rows across 7 tables: 2,000 posts, 12 meta rows each, 6,000 comments, 900 options, 8 users.

Then: a full dump with a single snapshot; a restore of that dump into a scratch database; a restore of it cut mid line at 60 percent; a restore of it cut at the last complete statement; and two dumps taken while rows were being written, one table at a time and one from a snapshot.

Two things to be honest about before the numbers. The database is a fixture, not a client site, because I do not have permission to publish anyone’s content. And every duration here is local import time on one machine, with no network transfer in it, so it says what the import costs and not how long a real recovery takes end to end.

The script is blog-pipeline/tools/restore_drill.py and its output is blog-pipeline/data/restore_drill.json, both committed. You can run the same drill against your own dump. The dump client in the MariaDB image is mariadb-dump, MariaDB’s build of the same program as mysqldump. The flags here work on both.

A restore you can verify in one statement

The good restore is unglamorous. The dump took 0.13 seconds to write and came out at 6.3 MB of SQL. Restoring it into an empty database took 2.71 seconds and returned exit code 0. Every table came back with the same row count as the source, and CHECKSUM TABLE matched on 7 of 7 tables.

That last check is the one worth adopting. One statement, run against both databases, returns a checksum per table:

CHECKSUM TABLE wp_posts, wp_postmeta, wp_options, wp_users, wp_usermeta, wp_comments, wp_term_relationships;

Equal numbers mean the rows and their contents are identical. A difference means something changed, and you look at row counts to find which table. It takes seconds, it cannot be fooled by a file that is the right size, and it is the difference between checking a backup and assuming one. The two restores, side by side:

WHOLE FILECUT CLEANLYImport exit code00Tables created7 of 74 of 7Rows imported34,95630,900Posts2,0000Error outputnonenone
IMPORT EXIT CODEWhole file: 0Cut cleanly: 0TABLES CREATEDWhole file: 7 of 7Cut cleanly: 4 of 7ROWS IMPORTEDWhole file: 34,956Cut cleanly: 30,900POSTSWhole file: 2,000Cut cleanly: 0ERROR OUTPUTWhole file: noneCut cleanly: none
The same dump restored whole, and cut at the last complete statement. Both imports returned exit code 0. Counts from blog-pipeline/data/restore_drill.json.

Where the cut lands decides what you hear

Two cuts of one dump, and the client behaves differently in each:

  • Cut mid line at 60 percent: exit code 1, an error naming the statement it could not parse, 4 of 7 tables, 30,900 of 34,956 rows.
  • Cut at 90 percent, on the last complete statement: exit code 0, no error output at all, 4 of 7 tables, 30,900 rows.

The second is the one to fear. It is what a dump looks like when the write stopped between statements: a full disk, a killed process, a host that dropped the connection at the wrong moment. The import has nothing to complain about, so a restore tool that reads the exit code reports success.

Look at what it leaves behind. wp_comments and wp_options complete, all 24,000 meta rows, an empty wp_posts table, and 24,000 meta rows pointing at posts that are not there. If your check is that the import finished, this passes, because it did.

The fix is not a better plugin. It is comparing the restored row count to the source row count for every table, which is step five of the drill and the only check that catches both cuts.

Table by table loses records, one at a time

The subtler failure came from dumping with --skip-lock-tables, which reads one table at a time from whatever state it is in, while a writer inserted a post and its meta rows in one transaction every few milliseconds. That is roughly what a plugin loop does.

The dump finished in 3.93 seconds and captured 2,154 posts and 25,836 meta rows. Cross-checking, 1 post carried fewer meta rows than the 12 the fixture gives every post. The source had 0 such rows before the run and 0 after, so the hole was created by the dump, not by the fixture.

One record in 2,154. That is the shape of this failure: not a broken site, one post with a missing image or price field, found months later by a customer.

The same run with a single snapshot, --single-transaction, finished in 4.03 seconds with 2,637 posts, 31,644 meta rows and 0 incomplete posts. MySQL’s documentation for mysqldump describes the option as issuing a BEGIN statement before dumping data, which is what holds both tables at the same moment.

The docs also say something most backup advice skips: mysqldump is a logical backup and “not intended as a fast or scalable solution”. For a site of a few hundred megabytes it is fine. It is not a strategy for a store with a large database, and knowing which one you have matters more than the plugin’s star rating.

What has to be in the backup set

WordPress’s own backup documentation is specific about this, and it is worth quoting because most “backup plugin” pages are not: there are two parts, database and files, and “in a typical WordPress setup” the files do not contain the database. Both are needed to restore a site.

The same page gives the order. Back up the database first, then the files. Restore the files first, then import the database. It also names what people forget is a file: wp-config.php, which holds the database credentials and the table prefix, and .htaccess if the site has rewrite rules.

Sizes, from a read-only pass I ran on the two installs I maintain:

InstallDatabaseUploadsBackup set5 copies
MedFactor307 MB75 MB382 MB1.9 GB
ProvidaRCM409 MB459 MB868 MB4.2 GB

The documentation’s guidance is 3 to 5 recent copies in different places, so that is the column on the right at five. Five copies of the second set is 4.2 GB before the live site’s own 1.93 GB is counted, so retention is a disk decision and not a setting you can leave on defaults.

One warning about compression. My fixture compressed 25.27:1, which is not a real ratio. Its post bodies are repeated filler text. Do not use that number to decide whether a 400 MB database fits somewhere.

The backup that never ran

The most common dead backup is not corrupt. It never executed, and the schedule screen still shows the schedule.

The reason is in WordPress’s own description of WP-Cron: it “does not run constantly as the system cron does; it is only triggered on page load”. No visitors, or a page cache serving every request from HTML, means no trigger, which means the 3am job slides to whenever someone next loads a page that misses the cache.

That is why the check is the timestamp of the last completed backup, not the schedule. If the newest archive is older than the interval you set, the backup is already dead, and the fix is a real system cron entry or a host level scheduler calling the task. The monthly maintenance pass I wrote about covers where that check lives in a routine.

The prefix that turns a restore into a fresh install

Restoring a dump into a database does not guarantee WordPress will see it. wp-config.php carries $table_prefix, and the documentation says to change it if you want something other than wp_, most often when several installs share one database.

I ran that check against the restored database: the query a wp2_ prefix would issue found 0 tables, while wp_options under wp_ held 900 rows. Nothing was missing. The installation would have seen none of it.

The symptom is a site that greets you with the five minute install wizard, or a blank page, on a database that is completely intact. Before you reinstall anything, read the prefix out of the dump file’s CREATE TABLE lines and compare it to the config.

The drill and the runbook

Six steps, in this order, because each one makes the next failure cheaper to find:

01Copy the files firstUploads, plugins, theme, wp-config02Dump from one snapshotNot table by table03Fetch the archive yourselfDownload it, do not trust the badge04Import into a scratch databaseNever over the live one05Compare row countsEvery table, source against restored06Open three URLsHome, a service page, one post
01Copy the files firstUploads, plugins, theme, wp-config02Dump from one snapshotNot table by table03Fetch the archive yourselfDownload it, do not trust the badge04Import into a scratch databaseNever over the live one05Compare row countsEvery table, source against restored06Open three URLsHome, a service page, one post
The drill in the order that makes a failure visible earliest. All six steps run on a copy; the live database is not touched until step six passes.

Two files make the drill repeatable. The first is the command, written down with the flags, because nobody remembers --single-transaction at 2am. The second is the result of the last drill, with a date, so the next person knows whether the archive in front of them has ever been opened.

What goes on the one page:

  • Where the dumps live, and which one is newest
  • The host, database name and credentials location
  • The dump command with its flags
  • The restore command, with the scratch database name
  • The row count comparison, or the checksum statement
  • Who runs it, and the date of the last run

If a restore fails, the useful question is whether the files survived. A missing database with intact uploads is a content problem. A missing uploads folder with an intact database is a site full of broken images, and that is the failure that pushes an owner toward a rebuild rather than a repair. Either way, the decision is cheaper when it is made from a comparison table and not from a support ticket.

I am not going to tell you a backup plugin is a waste of money. A plugin that runs on a schedule and pushes off site is better than nothing, and most owners will never type a dump command. What it cannot do is tell you the file is good. That takes a restore, on a copy, into a database you can delete, and the row counts to prove it.

One more thing worth knowing about the stack: every backup plugin adds work to the same site it is protecting, and a site already carrying too many plugins is the one most likely to fail mid-dump. That trade is covered in what actually makes WordPress fast, and the short version is that backups are one of the few plugins worth their cost. If you are staring at a plugin list and wondering which ones are load bearing, this is the shortlist I keep.

The backup you own is the one you have restored. Everything else is a file with a hopeful name.

Request

Got a site that should be doing more?

Send me the address and one sentence about what you sell. I will tell you what I would fix first, and whether it is worth fixing at all.

Start a project Free site audit