Class: NewBackup::MySqlCmds

Inherits:
Object
  • Object
show all
Includes:
Methadone::CLILogging
Defined in:
lib/new_backup/mysqlcmds.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, username, password, database, obfuscate) ⇒ MySqlCmds

Returns a new instance of MySqlCmds.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/new_backup/mysqlcmds.rb', line 29

def initialize(hostname, username, password, database, obfuscate)
  self.hostname=hostname.dup.to_s
  self.username=username.dup.to_s
  self.password=password.dup.to_s
  self.database=database.dup.to_s
  self.obfuscate_script=obfuscate.dup.to_s

  @commands =
    {:mysqldump  	=> get_command('mysqldump'),
    :mysql      	=> get_command('mysql'),
    :gzip		=> get_command('gzip')
  }

  debug "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: @commands: #{@commands}"

end

Instance Attribute Details

#databaseObject

Returns the value of attribute database.



26
27
28
# File 'lib/new_backup/mysqlcmds.rb', line 26

def database
  @database
end

#hostnameObject

Returns the value of attribute hostname.



26
27
28
# File 'lib/new_backup/mysqlcmds.rb', line 26

def hostname
  @hostname
end

#obfuscate_scriptObject

Returns the value of attribute obfuscate_script.



26
27
28
# File 'lib/new_backup/mysqlcmds.rb', line 26

def obfuscate_script
  @obfuscate_script
end

#passwordObject

Returns the value of attribute password.



26
27
28
# File 'lib/new_backup/mysqlcmds.rb', line 26

def password
  @password
end

#usernameObject

Returns the value of attribute username.



26
27
28
# File 'lib/new_backup/mysqlcmds.rb', line 26

def username
  @username
end

Instance Method Details

#dump(save_file) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/new_backup/mysqlcmds.rb', line 46

def dump(save_file)
  cmd = []
  cmd << @commands[:mysqldump]
  cmd << "--host #{self.hostname}"
  cmd << "--user=#{self.username}"
  cmd << "--password=#{self.password}" unless self.password.nil? or self.password.empty?
  cmd << self.database
  cmd << "|"
  cmd << @commands[:gzip]
  cmd << ">"
  cmd << save_file
 
  debug "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: cmd = #{cmd.join(" ")}"

  saver = RunIt.new cmd.join(" ")
  info "dumping #{self.database} to #{save_file}"
  saver.run
  raise "#{self.class}#save error: #{saver.result.exitstatus}: #{saver.error.inspect}" unless saver.result.success?
  debug "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: saver.output: #{saver.output}"
end

#obfuscateObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/new_backup/mysqlcmds.rb', line 67

def obfuscate

  cmd = []
  cmd << @commands[:mysql]
  cmd << "--host #{self.hostname}"
  cmd << "--user #{self.username}"
  cmd << "--password=#{self.password}" unless self.password.nil? or self.password.empty?
  cmd << self.database
  cmd << "<"
  cmd << self.obfuscate_script

  debug "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: cmd= #{cmd.join(" ")}"
  obfuscator = RunIt.new cmd.join(" ")
  info "Running #{self.obfuscate_script} on #{self.database}"
  obfuscator.run
  raise "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: error: #{obfuscator.result.exitstatus}: #{obfuscator.error.inspect}" unless obfuscator.result.success?
  debug "#{self.class}##{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: obfuscator.output: #{obfuscator.output}"
end