views:

2082

answers:

9

I have a mysqldump backup of my mysql database consisting of all of our tables which is about 440 megs. I want to restore the contents of just one of the tables form the mysqldump. Is this possible? Theoretically, I could just cut out the section that rebuilds the table I want but I don't even know how to effectively edit a text document that size.

+4  A: 

One way or another, any process doing that will have to go through the entire text of the dump and parse it in some way. I'd just grep for

INSERT INTO `the_table_i_want`

and pipe the output into mysql. Take a look at the first table in the dump before, to make sure you're getting the INSERT's the right way.

Edit: OK, got the formatting right this time.

JCCyC
I'm dumb for not immediately thinking of grepping it. Grep saves my bacon every day, it seems. The other two suggestions were also bang-up ones and totally what I would have done without the wonders of grep available. Thank you all!
Mobius
If you think you love grep, when you learn awk you'll become its happy sex slave: http://en.wikipedia.org/wiki/Awk
JCCyC
+1  A: 

One possible way to deal with this is to restore to a temporary database, and dump just that table from the temporary database. Then use the new script.

Tim Hoolihan
+1  A: 

Most modern text editors should be able to handle a text file that size, if your system is up to it.

Anyway, I had to do that once very quickly and i didnt have time to find any tools. I set up a new MySQL instance, imported the whole backup and then spit out just the table I wanted.

Then I imported that table into the main database.

It was tedious but rather easy. Good luck.

Bryan Migliorisi
A: 

Get a decent text editor like Notepad++ or Vim (if you're already proficient with it). Search for the table name and you should be able to highlight just the CREATE, ALTER, and INSERT commands for that table. It may be easier to navigate with your keyboard rather than a mouse. And I would make sure you're on a machine with plenty or RAM so that it will not have a problem loading the entire file at once. Once you've highlighted and copied the rows you need, it would be a good idea to back up just the copied part into it's own backup file and then import it into MySQL.

Cameron
+8  A: 

You can try to use sed in order to extract only the table you want.

Let say the name of your table is mytable and the file mysql.dump is the file containing your huge dump:

$ sed -n -e '/CREATE TABLE.*mytable/,/CREATE TABLE/p' mysql.dump > mytable.dump

This will copy in the file mytable.dump what is located between CREATE TABLE mytable and the next CREATE TABLE corresponding to the next table.

You can then adjust the file mytable.dump which contains the structure of the table mytable, and the data (a list of INSERT).

uloBasEI
That's even nicer than my answer since it takes care of the CREATE TABLE too, but you should search with the backquotes so as not to get another table called "thisismytabletoo".
JCCyC
True. I was not sure if the table names in all mysql dumps are always surrounded by backquote or if "CREATE TABLE mytable" could also be possible. We can easily adapt the first regexp if we know how the dump looks like.A second problem could be if the table mytable is not unique (one in the database db1, and another one in the database db2). Both will be exported in the file mytable.dump.If the table is not unique, we can use the same sed command, first with CREATE DATABASE in order to extract only the right database. Then, use the sed command with CREATE TABLE.
uloBasEI
Yep, JCCyC. This thing did exactly what I needed with zero monkeybusiness. Thank you and uloBasEl so much!
Mobius
Sweet! Remember to add DROP TABLE at the top and remove the DROP TABLE [next table] at the bottom of the file.
Nathan
A: 

The chunks of SQL are blocked off with "Table structure for table my_table" and "Dumping data for table my_table."

You can use a Windows command line as follows to get the line numbers for the various sections. Adjust the searched string as needed.

find /n "for table `" sql.txt

The following will be returned:

---------- SQL.TXT

[4384]-- Table structure for table my_table

[4500]-- Dumping data for table my_table

[4514]-- Table structure for table some_other_table

... etc.

That gets you the line numbers you need... now, if I only knew how to use them... investigating.

Kuyenda
+1  A: 

wouldn't it be much easier to just create a test database, restore the whole database, then dump the single table to a file - then restore the data to your "production" database?

mqqn
A: 

My database's size is 96GB so it is not possible to load whole dump to temp database and export one table from it.

pls give me other solution if possible

really need help

thanks in advance

Sunil Divyeshvar
Ask your own question in your own thread if you're having problems. Mine has already been answered above by the accepted solution.
Mobius
A: 

I assume this will help: http://tinyurl.com/KzDumpSplitter

kv