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

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

二、从binlog中提取某一个表的信息,并进行恢复

The second step was to restore the data from the time of the backup

(which was about 10 hours ago) up to the point of the crash. The binary

log was already spread across two files at that time. So I had to

extract all the data manipulating statements for the database holding

the crashed table from those two binlog files to a text file.

mysqlbinlog --database=db_name --start-position=102655866 mysql1-bin.000312 > restore.sql

mysqlbinlog --database=db_name mysql1-bin.000313 >> restore.sql

The start-position is of course the position of the binlog at the

time of the backup. Now I could do a search for all statements affecting

the crashed table and feed them to mysql again.

grep -B3 -w table_name restore.sql egrep -v '^--$' > restore_table.sql

emacs restore_table.sql

mysql db_name < restore_table.sql

As I knew that all those statements didn't contain any newlines I used

a simple approach with grep (the -B3 giving me the lines with the meta

information just before the actual statement), quickly checked the

resulting file in a text editor (where I deleted the ALTER TABLE

statement, too, to not have the crash happen again) and ran the queries.

That's it. The table was now in exactly the same state as it was before the crash.

mysqlbinlog mysql-bin.012001>a.sql

grep -B3 -w tblauction a.sql >re.sql

就查找出了关于表tblauction的相关DML操作语句

mysql binlog 恢复指定表_Mysql用全备恢复指定表mysqlbinlog抽取某个表的信息相关推荐

  1. mysql里面integer默认宽度_MySQL中关于数据类型指定宽度之后的情况

    概述 MySQL有很多种数据类型,最常用的就是int,char,varchar,这些类型在创建表的时候都可以指定该字段的宽度,方法是在类型后面加一个括号,括号中写宽度就可以了. 但是,在指定宽度之后, ...

  2. mysql 堆表_Mysql聚集索引和非聚集索引(堆组织表和索引组织表)

    Mysql聚集索引和非聚集索引(堆组织表和索引组织表) 1.堆组织表(HOT)和索引组织表(IOT)有什么区别? myisam使用的堆组织表(Heap Organize Table, HOT),没有聚 ...

  3. MySQL一般读作什么_MySQL入门必做练习题50题(一) 创建表

    练习题介绍: 对于一个学校信息管理系统中的数据库,有如下4张表: 学生表:student(学号,学生姓名,出生年月,性别) 成绩表:score(学号,课程号,成绩) 课程表:course(课程号,课程 ...

  4. mysql行锁索引问题_Mysql锁机制--索引失效导致行锁变表锁

    =============== Tips:在阅读本文前,最好先阅读 这篇(Mysql锁机制--行锁)文章~ 在上篇文章中,我们看到InnoDB默认的行锁可以使得操作不同行时不会产生相互影响.不会阻塞, ...

  5. mysql update锁表_MySQL执行update语句是锁行还是锁表分析

    我们在数据库执行update语句的时候,到底是锁表还是锁行?这里直接用MySQL上例子测试下. 一.环境准备 1.新建一个表create table test_update( id BIGINTnot ...

  6. mysql 跳过一个事物_MYSQL GTID跳过指定事务

    主库删除了ttt表,从库上没有ttt表,出现了报错. [root@mysqlstu2:demo]10:49:52>show slave status\G ******************** ...

  7. mysql 怎么设置ip地址_Mysql如何设置用户指定ip地址操作数据库

    Mysql设置用户指定ip地址操作数据库的方法: 语法: grant priv_type on mysql.user to 'user'@'host' identified by 'password' ...

  8. mysql年月分表_MySQL之按月拆分主表并按月分表写入数据提高数据查询速度

    使用场景: 主表数据量特别大,为了提高查询的速度,可以考虑按月进行分表,要求就是当月的数据到当月表查询,上月的数据到上月表查询,当天的数据到主表来查询.这样在一定程度上也是提高了数据的查询速度 过程演 ...

  9. mysql非聚集索引区间查询_mysql的聚集索引和非聚集索引,回表查询,索引覆盖,最左前缀原则略解...

    什么是聚集索引和非聚集索引 我们知道 Mysql 底层是用 B+ 树来存储索引的,且数据都存在叶子节点.对于 InnoDB 来说,它的主键索引和行记录是存储在一起的,因此叫做聚集索引(clustere ...

最新文章

  1. 来谈谈Servlet~~
  2. html怎么让方块自动旋转,纯CSS3做的的3D旋转方块
  3. ubuntu 10.10开机自启动svn服务
  4. boost::hana::one用法的测试程序
  5. visual studio报错:error C4996: ‘scanf‘
  6. android骰子游戏代码_真神器!不用手写一行代码就能做网站~
  7. 自动驾驶车辆转向控制(通过支持转角控制的EPS实现角速度控制)
  8. Angular问题02 创建模块失败、 angular-cli名称问题、升级angular-cli
  9. 数据挖掘:特征提取——PCA与LDA
  10. 专题四——线段树、树状数组
  11. PphpStorm常用操作整理
  12. android 主流分辨率是多少,android手机常用分辨率有哪些?
  13. 开启虚拟机电脑自动重启的解决
  14. Vultr与阿里云结合自动换IP的解决方案
  15. SpringMVC上传图片报400
  16. C. Two Shuffled Sequences
  17. 为什么不建议轻行量玩频
  18. HDU 6194 后缀数组+单调栈
  19. vue--实现学生信息管理案例
  20. ccf python写题随手记

热门文章

  1. 前端学习(1727):前端系列javascript之需求分析
  2. 实例27:python
  3. gateway请求拦截_一种网关对用户请求进行统一拦截判断是否放行的方法与流程...
  4. python学习笔记--理解生成器
  5. android image设置adjustviewbounds_探索 Android 平台的 CameraX
  6. 二维数组排序 行与列分别升序_6个经典排序技巧,尤其是最后一个,绝对的个性化...
  7. php 5.4 aws,使用 Amazon EC2 管理 AWS SDK for PHP 实例 - 适用于 PHP 的 AWS 开发工具包
  8. decimal类型对象里面定义什么类型_奥斯塔罗 单身开启桃花雷达 现阶段的我适合什么类型的对象?...
  9. Problem Collection II 构造
  10. OI回忆录——一个过气OIer的制杖历程