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



9
10
11
12
13
14
15
# File 'lib/pxcbackup/mysql.rb', line 9

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.



7
8
9
# File 'lib/pxcbackup/mysql.rb', line 7

def datadir
  @datadir
end

Instance Method Details

#authObject



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