Skip to content

Commit 909e908

Browse files
committed
v_1.0
处理mysqlbinlog的导出文件,从中筛选出指定表有关的语句
1 parent d3bb1b0 commit 909e908

File tree

2 files changed

+222
-2
lines changed

2 files changed

+222
-2
lines changed

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,33 @@
1-
# MySQL_Binlog_Table_Filter
2-
created because mysqlbinlog can not filter table
1+
MySQL_Binlog_Table_Filter
2+
-----
3+
4+
####Created because mysqlbinlog can not filter table
5+
6+
###示例 / Example
7+
./myfiler.pl --tables hello,hi --enable-drop --enable-truncate --src src.sql
8+
9+
###参数
10+
--tables <tablenames> 筛选要导出的表名,使用英文逗号","分隔多个表名
11+
--enable-drop 允许DROP语句,可选,默认不允许
12+
--enable-truncate 允许TRUNCATE语句,可选,默认不允许
13+
--src <exported sql file> 从指定文件读取
14+
15+
###操作指南
16+
1. 用mysqlbinlog将binlog文件导出成sql文件,使用-d操作符指定数据库。
17+
2. 使用myfilter.pl处理导出的数据库文件,导出结果默认输出到标准输出,可以重定向到一个新的sql文件。
18+
3. 导入处理后的sql文件
19+
20+
###Parameters
21+
--tables <tablenames> export tables in tablenames, deliminate by ","
22+
--enable-drop enable DROP, optional, default disabled
23+
--enable-truncate enable TRUNCATE, optional, default disabled
24+
--src <exported sql file> read from sql file
25+
26+
###How To
27+
1. Export binlog file using mysqlbing with -d operator to export the exact database.
28+
2. Use myfilter.pl to parse exported sql file. Result will be printed to stdout, you can redirect the export to a new sql file.
29+
3. Import the parsed sql file into your database.
30+
31+
32+
###Help
33+
Any question please mail to njutczd+gmail.com

myfilter.pl

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
#!/usr/bin/perl
2+
# MySQL_Binlog_Table_Filter
3+
# By Chen.Zhidong
4+
# njutczd+gmail.com
5+
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
#
19+
use strict;
20+
use Getopt::Long;
21+
22+
my $version = "1.0";
23+
24+
my %opt = (
25+
"tables"=>"",
26+
"enable-drop"=>0,
27+
"enable-truncate"=>0,
28+
"src"=>"",
29+
);
30+
31+
GetOptions(\%opt,
32+
"tables=s",
33+
"enable-drop",
34+
"enable-truncate",
35+
"src=s",
36+
"help"
37+
) || die usage();
38+
39+
sub usage {
40+
print "\n".
41+
" MySQL Exported Binlog Filter $version\n".
42+
" Options:\n".
43+
" --tables <tablenames> export tables in tablenames, deliminate by ","\n".
44+
" --enable-drop enable DROP, optional, default disabled\n".
45+
" --enable-truncate enable TRUNCATE, optional, default disabled\n".
46+
" --src <exported sql file> read from sql file\n".
47+
" --help print help message\n".
48+
"\n".
49+
" Example:\n".
50+
" $0 --tables hello,hi --enable-drop --enable-truncate --src src.sql\n".
51+
"\n";
52+
exit;
53+
}
54+
55+
if(defined $opt{'help'} && $opt{'help'}) { usage(); }
56+
57+
if($opt{'tables'} eq "" || $opt{'src'} eq "")
58+
{
59+
usage();
60+
}
61+
62+
open(FILE,"<",$opt{'src'}) || die "can not open file $opt{'src'}\n";
63+
64+
my @tables=split(/,/,$opt{'tables'});
65+
66+
my $block="";
67+
my $line="";
68+
my $end_log_pos=0;
69+
my $matchfilter=0;
70+
71+
while($line=<FILE>)
72+
{
73+
if($line ne "")
74+
{
75+
if($line =~ /^\/\*/)
76+
{
77+
#do nothing
78+
}
79+
elsif($line =~ /^#\d+.+end_log_pos (\d+) .*/)
80+
{
81+
#determin end_log_pos
82+
$end_log_pos=$1;
83+
$block.=$line;
84+
}
85+
elsif($line =~ /# at (\d+)/)
86+
{
87+
if($end_log_pos == $1 && $matchfilter)
88+
{
89+
#meet end_log_pos and print if table matches
90+
$block.=$line;
91+
print $block;
92+
}
93+
#clean variables
94+
$block="";
95+
$end_log_pos=0;
96+
$matchfilter=0;
97+
}
98+
else
99+
{
100+
if($line =~ /^ *update ([a-z_]+) .+/i)
101+
{
102+
#update
103+
if ($1 ~~ @tables)
104+
{
105+
$block.=$line;
106+
$matchfilter=1;
107+
}
108+
}
109+
elsif($line =~ /^ *insert into `?([a-z_]+)`? .+/i)
110+
{
111+
#insert
112+
if ($1 ~~ @tables)
113+
{
114+
$block.=$line;
115+
$matchfilter=1;
116+
}
117+
}
118+
elsif($line =~ /^ *delete from `?([a-z_]+)`? .+/i)
119+
{
120+
#delete
121+
if ($1 ~~ @tables)
122+
{
123+
$block.=$line;
124+
$matchfilter=1;
125+
}
126+
}
127+
elsif($opt{'enable-drop'} && $line =~ /^ *drop table `?([a-z_]+)`?.*/i)
128+
{
129+
#drop
130+
if ($1 ~~ @tables)
131+
{
132+
$block.=$line;
133+
$matchfilter=1;
134+
}
135+
}
136+
elsif($opt{'enable-truncate'} && $line =~ /^ *truncate table `?([a-z_]+)`?.*/i)
137+
{
138+
#truncate
139+
if ($1 ~~ @tables)
140+
{
141+
$block.=$line;
142+
$matchfilter=1;
143+
}
144+
}
145+
elsif($line =~ /^ *alter table `?([a-z_]+)`? .+/i)
146+
{
147+
#alter
148+
if ($1 ~~ @tables)
149+
{
150+
$block.=$line;
151+
$matchfilter=1;
152+
}
153+
}
154+
elsif($line =~ /^ *create table `?([a-z_]+)`? .+/)
155+
{
156+
#create table
157+
if ($1 ~~ @tables)
158+
{
159+
$block.=$line;
160+
$matchfilter=1;
161+
}
162+
}
163+
elsif($line =~ /^ *create (unique index|index) .+ on `?([a-z_]+)`? .+/i)
164+
{
165+
#create index
166+
if ($1 ~~ @tables)
167+
{
168+
$block.=$line;
169+
$matchfilter=1;
170+
}
171+
}
172+
elsif($line =~/^ *create view .+ from `?([a-z_]+)`? .+/i)
173+
{
174+
#create view
175+
if ($1 ~~ @tables)
176+
{
177+
$block.=$line;
178+
$matchfilter=1;
179+
}
180+
}
181+
else
182+
{
183+
$block.=$line;
184+
}
185+
}
186+
}
187+
}
188+
189+
close FILE;

0 commit comments

Comments
 (0)