Created
November 30, 2011 19:59
-
-
Save seeflanigan/1410551 to your computer and use it in GitHub Desktop.
Filter binlogs, selectively replicate queries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require "rubygems" | |
| require "bundler/setup" | |
| require "sequel" | |
| # DB Config | |
| options = { | |
| :database => "development", | |
| :host => "localhost", | |
| :user => "root", | |
| :socket => "/tmp/mysql.sock"} | |
| DB = Sequel.mysql2(options) | |
| TABLES = %w{foos bars} | |
| def tables_regex | |
| @tables_regex ||= TABLES.join("|") | |
| end | |
| def query_regexes | |
| @query_regexes ||= [ | |
| /\/\*.*\*\/ INSERT INTO `(#{tables_regex})` \((.*)\) VALUES\((.*)\)/, | |
| /\/\*.*\*\/ UPDATE `(#{tables_regex})` SET (.*)/] | |
| end | |
| def binlog_filenames | |
| Dir.glob("/usr/local/var/mysql/*binlog*.0*") | |
| end | |
| def read_lines_from(filename) | |
| `mysqlbinlog #{filename}`.split("\n") | |
| end | |
| def read_binlog_lines | |
| binlog_filenames.map {|f| read_lines_from(f) }.flatten | |
| end | |
| def get_matches_from(log_lines) | |
| log_lines.select {|l| query_regexes.any? {|r| l.match(r)}} | |
| end | |
| def execute_query(q) | |
| begin | |
| DB.run(q) | |
| rescue => e | |
| logger.warn(e) | |
| end | |
| end | |
| def execute_query_statements(queries) | |
| queries.each {|q| execute_query(q) } | |
| end | |
| execute_query_statements(get_matches_from(read_binlog_lines)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment