Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#mysql_locked(&block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'bin/ec2-consistent-snapshot-rb', line 52

def mysql_locked(&block)
  mysql = nil
  
  if $opts[:mysql]
    require 'mysql'
    mysql = Mysql::new($opts[:mysql_host], $opts[:mysql_username], $opts[:mysql_password])
    mysql.query("SET SQL_LOG_BIN=0")
    mysql.query("FLUSH LOCAL TABLES")
    mysql.query("FLUSH LOCAL TABLES WITH READ LOCK")
    
    def query_result_string(mysql, query)
      result = mysql.query(query)
      string = ""
      if result.num_rows() > 0
        result.fetch_row.each_with_index do |value, i|
          string << "#{result.fetch_field_direct(i).name}: #{value}\n"
        end
      end
      string
    end
    
    if $opts[:mysql_slave_status_file]
      File.open($opts[:mysql_slave_status_file], "w") {|f| f.puts query_result_string(mysql, "SHOW SLAVE STATUS"); f.fsync }
    end
    
    if $opts[:mysql_master_status_file]
      File.open($opts[:mysql_master_status_file], "w") {|f| f.puts query_result_string(mysql, "SHOW MASTER STATUS"); f.fsync }
    end
    
    mysql.query("SET SQL_LOG_BIN=1")
  end
  
  begin
    yield
  ensure
    mysql.real_query("UNLOCK TABLES") if mysql
  end
end

#xfs_frozen(&block) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'bin/ec2-consistent-snapshot-rb', line 91

def xfs_frozen(&block)
  system('xfs_freeze', '-f', $opts[:xfs_filesystem]) if $opts[:xfs_filesystem]
  
  begin
    yield
  ensure
    system('xfs_freeze', '-u', $opts[:xfs_filesystem]) if $opts[:xfs_filesystem]
  end
end