Can I restore a single table from a full mysql mys

2019-01-01 14:16发布

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.

19条回答
临风纵饮
2楼-- · 2019-01-01 14:35

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.

查看更多
刘海飞了
3楼-- · 2019-01-01 14:36
  1. Backup

    $ mysqldump -A | gzip > mysqldump-A.gz
    
  2. Restore single table

    $ mysql -e "truncate TABLE_NAME" DB_NAME
    $ zgrep ^"INSERT INTO \`TABLE_NAME" mysqldump-A.gz | mysql DB_NAME
    
查看更多
骚的不知所云
4楼-- · 2019-01-01 14:38

I used a modified version of uloBasEI's sed command. It includes the preceding DROP command, and reads until mysql is done dumping data to your table (UNLOCK). Worked for me (re)importing wp_users to a bunch of Wordpress sites.

sed -n -e '/DROP TABLE.*`mytable`/,/UNLOCK TABLES/p' mydump.sql > tabledump.sql
查看更多
千与千寻千般痛.
5楼-- · 2019-01-01 14:38

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.

查看更多
有味是清欢
6楼-- · 2019-01-01 14:39

Back in '08 I had a need to do this too. I wrote a Perl script that'll do it, and it's now my method of choice. Also summarized how to do it in awk or how to restore elsewhere and extract. Recently I added this sed method to the list as well. You can find the script and the other methods here: http://blog.tsheets.com/2008/tips-tricks/extract-a-single-table-from-a-mysqldump-file.html

查看更多
萌妹纸的霸气范
7楼-- · 2019-01-01 14:39

The 'sed' solutions mentioned earlier are nice but as mentioned not 100% secure

  • You may have INSERT commands with data containing: ... CREATE TABLE...(whatever)...mytable...

  • or even the exact string "CREATE TABLE `mytable`;" if you are storing DML commands for instance!

(and if the table is huge you don't want to check that manually)

I would verify the exact syntax of the dump version used, and have a more restrictive pattern search:

Avoid ".*" and use "^" to ensure we start at the begining of the line. And I'd prefer to grab the initial 'DROP'

All in all, this works better for me:

sed -n -e '/^DROP TABLE IF EXISTS \`mytable\`;/,/^UNLOCK TABLES;/p' mysql.dump > mytable.dump
查看更多
登录 后发表回答