From dc1eef28509b03c37aa4fbc02aa25d522531638b Mon Sep 17 00:00:00 2001 From: Jeremy Cole Date: Wed, 2 Aug 2023 16:11:58 -0700 Subject: [PATCH] Add --position/-j argument to mysql_binlog_dump and add Binlog#seek method --- bin/mysql_binlog_dump | 11 ++++++++++- lib/mysql_binlog/binlog.rb | 7 +++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/bin/mysql_binlog_dump b/bin/mysql_binlog_dump index 08f72d9..2638a80 100755 --- a/bin/mysql_binlog_dump +++ b/bin/mysql_binlog_dump @@ -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 @@ -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 @@ -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 ], @@ -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" @@ -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) @@ -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 diff --git a/lib/mysql_binlog/binlog.rb b/lib/mysql_binlog/binlog.rb index 953aa75..f361fcf 100644 --- a/lib/mysql_binlog/binlog.rb +++ b/lib/mysql_binlog/binlog.rb @@ -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)