Skip to content

Commit

Permalink
Add --position/-j argument to mysql_binlog_dump and add Binlog#seek m…
Browse files Browse the repository at this point in the history
…ethod
  • Loading branch information
jeremycole committed Aug 3, 2023
1 parent eec600a commit dc1eef2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
11 changes: 10 additions & 1 deletion bin/mysql_binlog_dump
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ Usage:
--checksum, -c
Enable CRC32 checksums.
--position, -j
Start the first file at a particular position.
--debug, -d
Debug reading from the binary log, showing calls into the reader and the
data bytes read. This is useful for debugging the mysql_binlog library
Expand All @@ -47,6 +50,7 @@ end
@options = OpenStruct.new
@options.file = nil
@options.checksum = nil
@options.position = nil
@options.debug = false
@options.tail = false
@options.rotate = false
Expand All @@ -56,6 +60,7 @@ getopt_options = [
[ "--help", "-?", GetoptLong::NO_ARGUMENT ],
[ "--file", "-f", GetoptLong::REQUIRED_ARGUMENT ],
[ "--checksum", "-c", GetoptLong::NO_ARGUMENT ],
[ "--position", "-j", GetoptLong::REQUIRED_ARGUMENT ],
[ "--debug", "-d", GetoptLong::NO_ARGUMENT ],
[ "--tail", "-t", GetoptLong::NO_ARGUMENT ],
[ "--rotate", "-r", GetoptLong::NO_ARGUMENT ],
Expand All @@ -71,6 +76,8 @@ getopt.each do |opt, arg|
@options.filenames << arg
when "--checksum"
@options.checksum = :crc32
when "--position"
@options.position = arg.to_i
when "--debug"
@options.debug = true
when "--tail"
Expand All @@ -86,7 +93,7 @@ if @options.filenames.empty?
usage 1, "One or more filenames must be provided"
end

@options.filenames.each do |filename|
@options.filenames.each_with_index do |filename, i|
reader = MysqlBinlog::BinlogFileReader.new(filename)
if @options.debug
reader = MysqlBinlog::DebuggingReader.new(reader, :data => true, :calls => true)
Expand All @@ -97,6 +104,8 @@ end
reader.tail = @options.tail
binlog.ignore_rotate = !@options.rotate

binlog.seek(@options.position) if @options.position && i.zero?

binlog.each_event do |event|
pp event
puts
Expand Down
7 changes: 7 additions & 0 deletions lib/mysql_binlog/binlog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ def rewind
reader.rewind
end

def seek(position)
# Try to find and consume the format description event which is necessary for understanding
# the subsequent event format; can't seek arbitrarily until we have it.
read_event until @fde
reader.seek(position)
end

# Skip the remainder of this event. This can be used to skip an entire
# event or merely the parts of the event this library does not understand.
def skip_event(header)
Expand Down

0 comments on commit dc1eef2

Please sign in to comment.