Class: PXCBackup::MySQL

Inherits:
Object
  • Object
show all
Defined in:
lib/pxcbackup/mysql.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  @which = PathResolver.new(options)
  @username = options[:mysql_user] || 'root'
  @password = options[:mysql_pass] || ''
  @datadir = options[:mysql_datadir] || get_variable('datadir') || '/var/lib/mysql'
  raise 'Could not find mysql data dir' unless File.directory?(@datadir)
end

Instance Attribute Details

#datadirObject (readonly)

Returns the value of attribute datadir.



5
6
7
# File 'lib/pxcbackup/mysql.rb', line 5

def datadir
  @datadir
end

Instance Method Details

#authObject



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