Time tracking blog

 
  • All
  • |
  • CEO
  • |
  • Fun
  • |
  • Geeks Behind TSheets
  • |
  • Marketing
  • |
  • News
  • |
  • Products & Features

5
Jun

MySQL – Restoring a Single Table from a Huge mysqldump File

Posted by Jared in Tips & Tricks
Tagged: , ,

 

NOTE: A newer article has been written detailing some additional methods for doing this. Read it here

Warning: the content which is to follow contains a lot of acronyms and jargon that is only meant for the technically strong and the very computer-literate. If you do not speak ‘command-line’ and don’t understand the meaning of words such as ‘mysql’ or ‘linux’ or ‘awk’, then my condolences – and you can just skip along to the other fare on our blog. You might enjoy the 38 seconds post or seeing a cougar grabbing a Bull Elk by the jugular.

I thought it was high time that we provide some material that would aid and assist our fellow system admins on the ‘net. When you’re going through the daily grind of taming all of those ones and zeros that just want to be free – anything that can save you a little bit of time and frustration is always welcome.

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 `test1`
--
. . .
DROP TABLE IF EXISTS `test1`;
CREATE TABLE `test1` ( . . .
LOCK TABLES `test1` WRITE;
INSERT INTO `test1` VALUES (1,0,'2 . . .
UNLOCK TABLES;
. . .
--
-- Table structure for table `test2`
--

As you can see, we have a line with the comment “Table structure for table `test1`”, 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 .test1./,/Table structure for table .test2./{print}’ mydumpfile.sql > /tmp/extracted_table.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 -ppassword mydb < /tmp/extracted_table.sql

Voila! – you’re back in business.

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 < mydumpfile.sql

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;

Now you can rinse and repeat if you want to keep trying whatever your initial operation was until you perfect it. Make your changes, delete everything, load it back in, redeux.

Hope this is useful.

AddThis Social Bookmark Button

13 Responses to “MySQL – Restoring a Single Table from a Huge mysqldump File”

  1. det Says:

    awk cannot open a dump file as large as 2.6GB

  2. dba Says:

    I am currently using the above mentioned command going through a 112GB mysqldump file. My requested data extraction file will approximately be a 35GB file, so far it has written 8GB worth of data. It’s working like a charm so far.

    Thanks Jared for the tip.

  3. Jared Says:

    @det: I’m not sure what the limitations are of awk when it comes to large files, and what might factor in to those limitations. When I get a chance, I’ll try some testing with a larger data set to see if I run into any similar problems.

    @dba: Thanks! I’m glad it was useful for you, and glad to see that you don’t seem to be experiencing any size limitations.

  4. Wim De Saegher Says:

    And let’s hope there is not one table (say, blogposts, like the table behind this blog) with the line “Table structure for table .test1.” in a textfield/varchar.. otherwise you’d get some real ugly SQL.

    Always check your data! You might want to improve the regex to start at the beginning of a line, commentdashes included, and end at the line as well, for increased security, but even then.. you might encounter a database with \n in the field. Again, always check for user-submitted data :)

  5. Darren Says:

    tried it with a 43GB dump and it couldn’t make it … I wonder if “dba” was using any other version of awk like gawk.

    @jared: have you had time to test it out with a big dump?

  6. Jared Says:

    Darren,

    I wrote another post that gives you access to a perl script that I use now for extracting a table. I’ve tested that with a pretty large dump and it worked fine. It wasn’t as large as what you’re talking about but I think it should work fine, it will just take a while. The link to the other article is right at the top of this one. Check it out and let me know if you have any questions.

    Thanks!
    Jared

  7. Darren Says:

    Hi Jared,

    Thanks for your followup on this issue … I have to say the perl script MUCH better than both other options, especially since the the mysql import (third option) is just not feasable for large dumps, and the awk (second option) (which didn’t work for me) would not always work for every table (eg last table) so is not good for automation …. the perl script you created above is GREAT for automation and can do more stuff too like the –listTables which is quite helpful indeed.

    Cheers,
    Darren

  8. Jared Says:

    Excellent, glad to hear it worked well for you!

  9. [mysql] backup/restore a single table | SysAdmin.MD Says:

    [...] to get or restore a single table from a large MySQL dump file you can use the following methods: Using AWK Using Ruby Using [...]

  10. Milda Charette Says:

    Hey. I got a 502 gateway error earlier today when I tried to access this page. Anyone else had the problem?

  11. Jared Says:

    Hi Milda, I’m not having any problems – which page in particular are you referring to?

  12. kedar Says:

    Have a look at this script:

    http://kedar.nitty-witty.com/blog/mydumpsplitter-extract-tables-from-mysql-dump-shell-script/

  13. Jared Says:

    Thanks for sharing Kedar. You might also like looking at the newer post I wrote referenced at the top of this article.

    Thanks!
    Jared

Leave a Reply