Restore selected posts from a backup

Feb 14, 2014

Recently at work we had a situation where we needed to roll back to the previous day’s backup of our WordPress site to undo some changes that went awry. However, since posts had been added and modified since that time, we also needed to restore those posts (and associated meta records) after we rolled back. Here’s one way to do that.

1. Clone the database in its current state

First of all, we need to clone the database in its current state so that we don’t lose any work that was done since yesterday’s backup. Then later we can compare this clone to yesterday’s backup to determine which posts to re-import.

The IGNORE keyword suppresses any duplicate-key errors that would cause the statement to abort. See the documentation on INSERT Syntax for details.

5. Get associated meta records too

We’re almost there. Now we have the posts that were modified since the rollback, but we also need to get the associated post meta records. First select them to make sure we know what we’ll be inserting. Here I’m selecting everything from both tables, so I can see which posts I’m dealing with: