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抽取某个表的信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: apicloud 请删除手机中的appl
- 下一篇: SQL中及Access的空值