Mysql恢复到指定表

Leave a Comment2009年05月27日 作者: 大头刚 

恢复使用Mysqldump工具备份的数据,有个不方便的地方,就是在恢复的时候不能指定恢复到表,例如我备份了整个数据库,有N个表,但是我只是一个表需要恢复,如果我把这个数据库都恢复的话,岂不耗时耗力。
基于这个原因,和同事一起用perl写了个指定恢复到表的脚本。举例说明:
先把test库全库备份到指定文件,在把test表drop。

/usr/local/mysql/bin/mysqldump -u -p test > /tmp/test.sql
mysql> use test;
mysql> select count(*) from test;
+———-+
| count(*) |
+———-+
| 1568394 |
+———-+
1 row in set (0.00 sec)
 
mysql> drop table test;
Query OK, 0 rows affected (0.06 sec)
 
mysql> quit

从全库的备份中挖出test表的备份,在将其恢复。

perl restore_table.pl -f /tmp/test.sql -t test > /tmp/test.sql
/usr/local/mysql/bin/mysql -u -p -D test < /tmp/test.sql
mysql> use test;
Database changed
mysql> select count(*) from [...]