Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

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!

No backup no party.

So I'll answer:

To never be in your situation again install the ultra simple and ultra powerful automysqlbackup (if you're on linux):

sudo apt-get install automysqlbackup

configure it:

sudo nano /etc/default/automysqlbackup

Then upload its content automatically to dropbox, ubuntu one or similar.

Only then live happily :)

Just to complement Kent Fredric's answer, you CAN rollback the drop database command if you are using binary logging since the database's creation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top