lib/flydata/compatibility_check.rb in flydata-0.2.22 vs lib/flydata/compatibility_check.rb in flydata-0.2.23

- old
+ new

@@ -73,10 +73,11 @@ class MysqlCompatibilityError < StandardError end SELECT_QUERY_TMPLT = "SELECT %s" + BINLOG_RETENTION_HOURS = 24 #def initialize(de_hash, dump_dir=nil) def initialize(dp_hash, de_hash, options={}) super @db_opts = [:host, :port, :username, :password, :database].inject({}) {|h, sym| h[sym] = de_hash[sym.to_s]; h} @@ -167,10 +168,23 @@ end raise MysqlCompatibilityError, "These system variable(s) are not the correct value: #{error_explanation}\n Please change these system variables for FlyData Sync to run correctly" end end + def check_mysql_binlog_retention + client = Mysql2::Client.new(@db_opts) + begin + if is_rds?(@db_opts[:host]) + run_rds_retention_check(client) + else + run_mysql_retention_check(client) + end + ensure + client.close + end + end + def check_writing_permissions write_errors = [] paths_to_check = ["~/.flydata"] paths_to_check << @dump_dir unless @dump_dir.to_s.empty? paths_to_check.each do |path| @@ -180,9 +194,51 @@ end unless write_errors.empty? error_dir = write_errors.join(", ") raise MysqlCompatibilityError, "We cannot access the directories: #{error_dir}" end + end + + def run_mysql_retention_check(mysql_client) + expire_logs_days_limit = BINLOG_RETENTION_HOURS / 24 + sel_query = SELECT_QUERY_TMPLT % '@@expire_logs_days' + result = mysql_client.query(sel_query) + if result.first["@@expire_logs_days"]!=0 and result.first["@@expire_logs_days"] <= expire_logs_days_limit + raise MysqlCompatibilityError, "Binary log retention is too short\n " + + " We recommend the system variable '@@expire_logs_days' to be either set to 0 or at least #{expire_logs_days_limit} days" + end + end + + def run_rds_retention_check(mysql_client) + sql_query = "call mysql.rds_show_configuration;" + + begin + result = mysql_client.query(sql_query) + if result.first["name"]=="binlog retention hours" + if result.first["value"].nil? or result.first["value"].to_i <= BINLOG_RETENTION_HOURS + raise MysqlCompatibilityError, "Binary log retention is too short\n" + + " We recommend setting RDS binlog retention to be at least #{BINLOG_RETENTION_HOURS} hours. To do this, run this on your RDS MySQL database:\n" + + " $> call mysql.rds_set_configuration('binlog retention hours', 94);" + end + end + rescue Mysql2::Error => e + if e.message =~ /command denied to user/ + log_warn_stderr("[WARNING]Cannot verify RDS retention period on currend MySQL user account.\n" + + "To see retention period, please run this on your RDS:\n" + + " $> call mysql.rds_show_configuration;\n" + + "Please verify that the hours is not nil and is at least #{BINLOG_RETENTION_HOURS} hours\n" + + "To set binlog retention hours, you can run this on your RDS:\n" + + " $> call mysql.rds_set_configuration('binlog retention hours', #{BINLOG_RETENTION_HOURS});\n" + ) + else + raise e + end + end + + end + + def is_rds?(hostname) + hostname.match(/rds.amazonaws.com$/) != nil end end end