Class: Siba::Source::Mysql::Db

Inherits:
Object
  • Object
show all
Includes:
FilePlug, LoggerPlug
Defined in:
lib/siba-source-mysql/db.rb

Constant Summary collapse

HIDE_PASSWORD_TEXT =
"****p7d****"
BACKUP_FILE_NAME =
"mysql_dump"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Db

Returns a new instance of Db.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/siba-source-mysql/db.rb', line 13

def initialize(options)
  @options = options

  if !tables.nil? && !tables.empty? && 
    (databases.nil? || (!databases.nil? && databases.size != 1))
    raise Siba::CheckError, "When 'tables' option is set there must be a single database specified in 'databases' option."
  end

  Siba::Source::Mysql::Db.check_spaces_in_arrays databases, 'databases'
  Siba::Source::Mysql::Db.check_spaces_in_arrays tables, 'tables'
  Siba::Source::Mysql::Db.check_spaces_in_arrays ignore_tables, 'ignore_tables'

  check_installed
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/siba-source-mysql/db.rb', line 135

def method_missing(meth, *args, &block)
  if method_defined? meth
    options[meth]
  else
    super
  end
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/siba-source-mysql/db.rb', line 11

def options
  @options
end

Class Method Details

.check_spaces_in_arrays(array, option_name) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/siba-source-mysql/db.rb', line 121

def self.check_spaces_in_arrays(array, option_name)
  unless array.nil? || array.empty?
    array.each do |value|
      value.strip!
      if value.gsub(/[,;]/," ").split(" ").size > 1  
        raise Siba::CheckError, "'#{option_name}' value can not contain spaces or commas. If you need to specify multiple values please use YAML array sytax instead:
- one
- two
- three"
      end
    end
  end
end

.escape_for_shell(str) ⇒ Object



117
118
119
# File 'lib/siba-source-mysql/db.rb', line 117

def self.escape_for_shell(str)
  str.gsub "\"", "\\\""
end

.format_mysql_parameter(name, val) ⇒ Object



111
112
113
114
115
# File 'lib/siba-source-mysql/db.rb', line 111

def self.format_mysql_parameter(name, val)
  val = HIDE_PASSWORD_TEXT if name == :password
  val = escape_for_shell val
  %(--#{name.to_s}="#{val}")
end

Instance Method Details

#backup(dest_dir) ⇒ Object

Raises:

  • (Siba::Error)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/siba-source-mysql/db.rb', line 36

def backup(dest_dir)        
  unless Siba::FileHelper.dir_empty? dest_dir
    raise Siba::Error, "Failed to backup MySQL: output directory is not empty: #{dest_dir}"
  end

  path_to_backup = File.join dest_dir, BACKUP_FILE_NAME
  command_without_password = %(mysqldump #{get_mysqldump_params} --routines --result-file="#{path_to_backup}")
  command = command_without_password
  unless password.nil?
    command = command_without_password.gsub HIDE_PASSWORD_TEXT, password
  end
  logger.debug command_without_password
  output = siba_file.run_shell command, "failed to backup MySQL: #{command_without_password}"
  raise Siba::Error, "failed to backup MySQL: #{output}" if output =~ /ERROR:/

  unless siba_file.file_file? path_to_backup
    raise Siba::Error, "Failed to backup MySQL: backup file was not created"
  end
end

#check_installedObject

Raises:

  • (Siba::Error)


29
30
31
32
33
34
# File 'lib/siba-source-mysql/db.rb', line 29

def check_installed
  msg =  "utility is not found. Please make sure MySQL is installed and its bin directory is added to your PATH."
  raise Siba::Error, "'mysqldump' #{msg}" unless siba_file.shell_ok? "mysqldump --help"
  raise Siba::Error, "'mysql' #{msg}" unless siba_file.shell_ok? "mysql --help"
  logger.debug "Mysql backup utilities verified"
end

#db_and_table_namesObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/siba-source-mysql/db.rb', line 155

def db_and_table_names
  names = []
  unless databases.nil? || databases.empty?
    names << "DB#{databases.size > 1 ? "s": ""}: #{databases.join(", ")}" 
  else
    names << "all databases" 
  end

  unless tables.nil? || tables.empty?
    names << "table#{tables.size > 1 ? "s": ""}: #{tables.join(", ")}"
  end
  out = names.join(", ")
  out = " (#{out})" unless out.empty?
  out
end

#get_mysql_paramsObject



101
102
103
104
105
106
107
108
109
# File 'lib/siba-source-mysql/db.rb', line 101

def get_mysql_params
  params = []
  LOGIN_PARAMETERS.each do |name|
    val = options[name]
    next if val.nil?
    params << Siba::Source::Mysql::Db.format_mysql_parameter(name, val)
  end
  params.join " "
end

#get_mysqldump_paramsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/siba-source-mysql/db.rb', line 72

def get_mysqldump_params
  params = []
  OPTION_NAMES.each do |name|
    val = options[name]
    next if val.nil? && name != :databases
    if MULTIPLE_CHOISES.include? name
      case name
      when :databases
        if val.nil? || val.empty?
          params << "--all-databases" 
        else
          params << "--databases #{val.join(" ")}"  
        end
      when :tables
        params << "--tables #{val.join(" ")}"  
      when :ignore_tables
        val.each do |ignore_table|
          params << %(--ignore-table=#{ignore_table})
        end
      end
    elsif name == :custom_parameters
      params << val
    else
      params << Siba::Source::Mysql::Db.format_mysql_parameter(name, val)
    end
  end
  params.join " "
end

#method_defined?(meth) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/siba-source-mysql/db.rb', line 151

def method_defined?(meth)
  OPTION_NAMES.include? meth.to_sym
end

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
146
147
148
149
# File 'lib/siba-source-mysql/db.rb', line 143

def respond_to?(meth)
  if method_defined? meth
    true
  else
    super
  end
end

#restore(from_dir) ⇒ Object

Raises:

  • (Siba::Error)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/siba-source-mysql/db.rb', line 56

def restore(from_dir)
  path_to_backup = File.join from_dir, BACKUP_FILE_NAME
  unless siba_file.file_file? path_to_backup
    raise Siba::Error, "Failed to restore MySQL: backup file does not exist: #{path_to_backup}"
  end

  command_without_password = %(mysql -e "source #{path_to_backup}" --silent #{get_mysql_params})
  command = command_without_password
  unless password.nil?
    command = command_without_password.gsub HIDE_PASSWORD_TEXT, password
  end
  logger.debug command_without_password
  output = siba_file.run_shell command, "failed to restore MySQL: #{command_without_password}"
  raise Siba::Error, "Failed to restore MySQL: #{output}" if output =~ /ERROR/
end