免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2572 | 回复: 0

Mysql恢复到指定表 [复制链接]

论坛徽章:
0
发表于 2010-02-23 11:38 |显示全部楼层

Mysql恢复到指定表
2009年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 test;
+----------+
| count(*) |
+----------+
|  1568394 |
+----------+
1 row in set (0.00 sec)
OK,表已经恢复了。贴下这个脚本的代码:
cat restore_table.pl
#!/usr/bin/perl -w
use strict;
use Getopt::Std;

my %opts;
getopt('ft',\%opts);
my $file=$opts{f};
my $tag=$opts{t};
my $pattern1="Table structure for table `$tag`";
my $pattern2="Dumping data for table `$tag`";

my $pattern3="40000 ALTER TABLE `$tag` DISABLE KEYS";
my $pattern4="40000 ALTER TABLE `$tag` ENABLE KEYS";

my $print=0;
open FD,$file;
while(){
        my $content=$_;
        $print=1 if $content =~ $pattern1;
        print if $print ==  1;
        last if($content =~ $pattern2);
}
$print=0;

while(){
        my $content=$_;
        $print=1 if $content =~ $pattern3;
        print if $print ==  1;
        last if($content =~ $pattern4);
}

close FD
声明一下,此脚本未经过严格测试,使用出现任何问题本人不负责任。


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/111930/showart_2184899.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP