Wednesday, December 23, 2009

Extracting a single table from a huge mysqldump file

Someday you may find yourself in a situation where you want to restore a single table of your MySQL database. Hopefully this is in a lab situation or on a personal development box somewhere – but even if it is production you need not fear. There are plenty of articles about restoring your entire database, but not a lot about restoring a single table. What if your coding compadre’s mistakenly change the field for every row in a table on you, or maybe you want to alter all of the data in a table but want an easy way to ‘reset’ things so that you can tweak and try again? If you aren’t taking table level backups restoring that single table can be rather difficult. You’ll locate your MySQL dump, and realize that it is huge (even when zipped), and you don’t want to wait around to restore your entire DB in order to recover one measly table. So what to do? The file is too large to open in a text editor and do anything meaningful with it – it would take way too much time. This is where a little bit of ‘awk‘ magic can save your day. Using awk, you can extract a block of text from the MySQL dump that pertains to the table you want to work with.
First, you have to know where in your mysqldump output you want to begin your extraction, and where you want to end it. The key here is finding something unique at the beginning and ending of the block that won’t be found anywhere else.
A sample mysqldump contains something like the following:

--
-- Table structure for table `abc`
--
. . .
DROP TABLE IF EXISTS `abc`;
CREATE TABLE `abc` (
. . .
LOCK TABLES `abc` WRITE;
INSERT INTO `abc` VALUES (1,0,'2
. . .
UNLOCK TABLES;
. . .
--
-- Table structure for table `xyz`
--
As you can see, we have a line with the comment “Table structure for table `abc`”, then all of the dropping, creating, and inserting for the table, and then another comment for the next table. These two lines are perfect for grabbing all of the operations pertinent to our one table.
To extract the dump for a single table from an entire database dump, run the following from a command prompt:
$ awk ‘/Table structure for table .abc./,/Table structure for table .xyz./{print}’ mydumpfile.sql > /tmp/extracted_table_abc.sql
The awk command is very powerful – the above command searches through the dump file, and as soon as it matches a line containing the first search string (denoted by the first set of slashes), it prints that line and every subsequent line until it encounters a line containing the second search string (denoted by the second set of slashes). FYI, the periods surrounding the table names above are wildcard characters.
Now the extracted_table.sql file contains the SQL to restore your table! One final thing: There are usually various parameters at the top of your mysqldump file that you may need to set before restoring your table, depending on the complexity of your database (i.e. disabling foreign key checks.)
To restore your table, you’d run:
$ mysql -u user -ppwd mydb < /tmp/extracted_table_abc.sql
Alternate Method:
A second option is to restore your data into a temporary database (assuming you have the disk space), extract the table you’re interested in to it’s own dump file, and then restore that dump file to your original database.
Create a new database, name it something easy to distinguish from your production one, i.e. fakedb.
Restore your data to the fakedb with a command like this:
$ mysql -u user -ppassword fakedb <>
From fakedb, grab the data you want from your target table:
mysql> select * from targettable into outfile “/tmp/mytablebackup.bak”;
On the production db, where you have your undesired data, clear it all out with:
mysql> delete from baddatatable;
Import the good stuff back into it:
mysql> load data infile “/tmp/mytablebackup.bak” into table baddatatable;
Enjoy J

No comments:

Post a Comment