Extracting single databases from MySQL dump

27 Mar 2011

If you've ever used mysqldump (or PHPMyAdmin) to make a dump of every database on a host to a single file, you'll know how much of a pain it is to then go through that file to see which databases it contains. A while ago I dumped all the databases on my old desktop so I could import them on my MacBook. I did a straight dump of all the databases in one go, then when it came to importing them I decided I'd probably like to go through and sort them out, deleting any I didn't want. The file came in at around 300MB and so choked any GUI text editor I tried to open it in. Vim coped with it fine, but it was a chore to go through the thousands of lines just to see what was going on. So I wrote a small python script that parses the file, displays a list of the databases it contains and allows extraction of single databases into separate files.

SQL extractor

Simply pass it the path of the SQL dump, choose a number from the list and it'll be written out to a new file in the current working directory under the name (database).sql.

Get the script here or at Github.

Comments