Skip to content

Commit dc1eef2

Browse files
committed
Add --position/-j argument to mysql_binlog_dump and add Binlog#seek method
1 parent eec600a commit dc1eef2

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

bin/mysql_binlog_dump

+10-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Usage:
2424
--checksum, -c
2525
Enable CRC32 checksums.
2626
27+
--position, -j
28+
Start the first file at a particular position.
29+
2730
--debug, -d
2831
Debug reading from the binary log, showing calls into the reader and the
2932
data bytes read. This is useful for debugging the mysql_binlog library
@@ -47,6 +50,7 @@ end
4750
@options = OpenStruct.new
4851
@options.file = nil
4952
@options.checksum = nil
53+
@options.position = nil
5054
@options.debug = false
5155
@options.tail = false
5256
@options.rotate = false
@@ -56,6 +60,7 @@ getopt_options = [
5660
[ "--help", "-?", GetoptLong::NO_ARGUMENT ],
5761
[ "--file", "-f", GetoptLong::REQUIRED_ARGUMENT ],
5862
[ "--checksum", "-c", GetoptLong::NO_ARGUMENT ],
63+
[ "--position", "-j", GetoptLong::REQUIRED_ARGUMENT ],
5964
[ "--debug", "-d", GetoptLong::NO_ARGUMENT ],
6065
[ "--tail", "-t", GetoptLong::NO_ARGUMENT ],
6166
[ "--rotate", "-r", GetoptLong::NO_ARGUMENT ],
@@ -71,6 +76,8 @@ getopt.each do |opt, arg|
7176
@options.filenames << arg
7277
when "--checksum"
7378
@options.checksum = :crc32
79+
when "--position"
80+
@options.position = arg.to_i
7481
when "--debug"
7582
@options.debug = true
7683
when "--tail"
@@ -86,7 +93,7 @@ if @options.filenames.empty?
8693
usage 1, "One or more filenames must be provided"
8794
end
8895

89-
@options.filenames.each do |filename|
96+
@options.filenames.each_with_index do |filename, i|
9097
reader = MysqlBinlog::BinlogFileReader.new(filename)
9198
if @options.debug
9299
reader = MysqlBinlog::DebuggingReader.new(reader, :data => true, :calls => true)
@@ -97,6 +104,8 @@ end
97104
reader.tail = @options.tail
98105
binlog.ignore_rotate = !@options.rotate
99106

107+
binlog.seek(@options.position) if @options.position && i.zero?
108+
100109
binlog.each_event do |event|
101110
pp event
102111
puts

lib/mysql_binlog/binlog.rb

+7
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ def rewind
7272
reader.rewind
7373
end
7474

75+
def seek(position)
76+
# Try to find and consume the format description event which is necessary for understanding
77+
# the subsequent event format; can't seek arbitrarily until we have it.
78+
read_event until @fde
79+
reader.seek(position)
80+
end
81+
7582
# Skip the remainder of this event. This can be used to skip an entire
7683
# event or merely the parts of the event this library does not understand.
7784
def skip_event(header)

0 commit comments

Comments
 (0)