日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

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

發布時間:2023/12/10 数据库 64 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql binlog 恢复指定表_Mysql用全备恢复指定表mysqlbinlog抽取某个表的信息 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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抽取某个表的信息的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。