Not a developer? Go to MovableType.com

Ask an Expert

Restoring some deleted comments from backed-up MySQL database?

Asked by Jed Hartman
Posted January 12, 2014, in Featured.

Hi, all. A user of my MT instance accidentally deleted 200 legitimate comments from their blog a couple months ago; I’m now trying to restore those. I have a backup of the MySQL database (using mysqldump) from just before the deletion, so I’ve now created a text file containing all of the deleted comments, in MySQL format, as rows from the comment table.

I was planning to import that data directly into the database from the text file, but I just realized that direct interaction with the database seems likely to be a bad idea; I don’t know whether there are things like comment counts stored elsewhere that I would mess up by doing this.

So do any of you have any advice on how to get the comments back into MT?

I could copy and paste the text of each one into a comment box. The timestamps and IP addresses and such would be wrong, but I could tweak those by hand in MySQL—but I don’t know if that kind of tweaking would cause problems. And I don’t relish the idea of doing all that by hand, one comment at a time.

I imagine that what I really should do is use the MT API to add the comments in bulk, but I’ve never done anything with the API. Is there a script out there somewhere that does bulk adding of comments? Or could someone point me toward tips on how to do that? I’ve done some web searches but haven’t turned anything up.

thanks,

—Jed

Back

1 Answer

Jed Hartman

Jed Hartman on March 22, 2014, 2:28 p.m. Reply

Answering my own question, in case anyone else runs into something similar:

I’m still on MT 5.04, so I ended up writing some Perl code, loosely based on the sample code shown at (http://movabletype.org/documentation/man/MT/Comment.html), to create and insert comment records. Then I parsed the comments and metadata from MySQL format, and inserted them as comments using $comment->save (as shown on the linked-to page). I did only one or two at first, to make sure everything was working, then did the whole batch of 200. It worked pretty well.

Jed Hartman

Website: http://www.kith.org/journals/jed/

Ask An Expert