Class: PXCBackup::MySQL
- Inherits:
-
Object
- Object
- PXCBackup::MySQL
- Defined in:
- lib/pxcbackup/mysql.rb
Instance Attribute Summary collapse
-
#datadir ⇒ Object
readonly
Returns the value of attribute datadir.
Instance Method Summary collapse
- #auth ⇒ Object
- #exec(query) ⇒ Object
- #get_status(variable) ⇒ Object
- #get_variable(variable, scope = 'GLOBAL') ⇒ Object
-
#initialize(options = {}) ⇒ MySQL
constructor
A new instance of MySQL.
- #set_variable(variable, value, scope = 'GLOBAL') ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ MySQL
Returns a new instance of MySQL.
7 8 9 10 11 12 13 |
# File 'lib/pxcbackup/mysql.rb', line 7 def initialize( = {}) @which = PathResolver.new() @username = [:mysql_user] || 'root' @password = [:mysql_pass] || '' @datadir = [:mysql_datadir] || get_variable('datadir') || '/var/lib/mysql' raise 'Could not find mysql data dir' unless File.directory?(@datadir) end |
Instance Attribute Details
#datadir ⇒ Object (readonly)
Returns the value of attribute datadir.
5 6 7 |
# File 'lib/pxcbackup/mysql.rb', line 5 def datadir @datadir end |
Instance Method Details
#auth ⇒ Object
15 16 17 |
# File 'lib/pxcbackup/mysql.rb', line 15 def auth "--user=#{@username.shellescape} --password=#{@password.shellescape}" end |
#exec(query) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/pxcbackup/mysql.rb', line 19 def exec(query) lines = `echo #{query.shellescape} | #{@which.mysql.shellescape} #{auth} 2> /dev/null`.lines.to_a return nil if lines.empty? keys = lines.shift.chomp.split("\t") rows = [] lines.each do |line| values = line.chomp.split("\t") row = {} keys.each_with_index do |val, key| row[val] = values[key] end rows << row end rows end |
#get_status(variable) ⇒ Object
45 46 47 48 |
# File 'lib/pxcbackup/mysql.rb', line 45 def get_status(variable) result = exec("SHOW STATUS LIKE '#{variable}'") result ? result.first['Value'] : nil end |
#get_variable(variable, scope = 'GLOBAL') ⇒ Object
36 37 38 39 |
# File 'lib/pxcbackup/mysql.rb', line 36 def get_variable(variable, scope = 'GLOBAL') result = exec("SHOW #{scope} VARIABLES LIKE '#{variable}'") result ? result.first['Value'] : nil end |
#set_variable(variable, value, scope = 'GLOBAL') ⇒ Object
41 42 43 |
# File 'lib/pxcbackup/mysql.rb', line 41 def set_variable(variable, value, scope = 'GLOBAL') exec("SET #{scope} #{variable}=#{value}") end |