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
9 10 11 12 13 14 15 |
# File 'lib/pxcbackup/mysql.rb', line 9 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.
7 8 9 |
# File 'lib/pxcbackup/mysql.rb', line 7 def datadir @datadir end |
Instance Method Details
#auth ⇒ Object
17 18 19 |
# File 'lib/pxcbackup/mysql.rb', line 17 def auth "--user=#{@username.shellescape} --password=#{@password.shellescape}" end |
#exec(query) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/pxcbackup/mysql.rb', line 21 def exec(query) output = Command.run("echo #{query.shellescape} | #{@which.mysql.shellescape} #{auth}", true) lines = output[:stdout].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
48 49 50 51 |
# File 'lib/pxcbackup/mysql.rb', line 48 def get_status(variable) result = exec("SHOW STATUS LIKE '#{variable}'") result ? result.first['Value'] : nil end |
#get_variable(variable, scope = 'GLOBAL') ⇒ Object
39 40 41 42 |
# File 'lib/pxcbackup/mysql.rb', line 39 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
44 45 46 |
# File 'lib/pxcbackup/mysql.rb', line 44 def set_variable(variable, value, scope = 'GLOBAL') exec("SET #{scope} #{variable}=#{value}") end |