views:

3695

answers:

3

How can I restore a mysql database that was dropped using a "drop database" command? I have access to binary logs which should make this type of rollback possible.

+4  A: 

If you don't have a backup of the database, you're out of luck. Droping a database is permanent.

wbowers
Really? I thought the logs logged everything. Can you explain in a bit more depth?
Orion Edwards
+5  A: 

Documentation Sucks. It alludes to DROP DATABASE being recoverable, but only in odd conditions i'm not familiar with http://dev.mysql.com/doc/refman/5.0/en/binary-log.html

According to Docs, binlogs are just a sequence of commands executed based on a given reference point. So that when you did "DROP DATABASE", instead of going "Oh, hes droppping the database, we should back up now just in case" it merely wrote a "DROP DATABASE" to the last binlog. Recovery is not as simple as playing the tape backwards.

What you need to do is recover the database from a last-known-good, and apply the binlogs that happened between that recover point and the DROP command.

http://dev.mysql.com/doc/refman/5.0/en/recovery-from-backups.html

How one determines which binlogs to use tho, unclear.

There is nothing better than having full file system backups. And you should at least have these to fall back to.

Kent Fredric
+1  A: 

Assuming you had a backup, the binary log holds the stuff that has happened since that backup. Using the mysqlbinlog utility you could do something like:

mysqlbinlog the_log_file > update.sql

Though I think you might have to edit that file to remove anything you didn't want to execute again (like the drop database statement).

Good luck!

itsmatt