Class: Toolshed::Databases::Mysql::Backup

Inherits:
Object
  • Object
show all
Includes:
Password
Defined in:
lib/toolshed/databases/mysql/backup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Password

#password_from_config

Constructor Details

#initialize(options = nil) ⇒ Backup

Returns a new instance of Backup.



14
15
16
17
18
19
20
21
22
# File 'lib/toolshed/databases/mysql/backup.rb', line 14

def initialize(options = nil)
  options ||= {}
  @host = options[:host]
  @name = options[:name]
  @path = options[:path]
  @password = options[:password]
  @username = options[:username]
  @wait_time = options[:wait_time] || 120
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



12
13
14
# File 'lib/toolshed/databases/mysql/backup.rb', line 12

def host
  @host
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/toolshed/databases/mysql/backup.rb', line 12

def name
  @name
end

#passwordObject (readonly)

Returns the value of attribute password.



12
13
14
# File 'lib/toolshed/databases/mysql/backup.rb', line 12

def password
  @password
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/toolshed/databases/mysql/backup.rb', line 12

def path
  @path
end

#usernameObject (readonly)

Returns the value of attribute username.



12
13
14
# File 'lib/toolshed/databases/mysql/backup.rb', line 12

def username
  @username
end

#wait_timeObject (readonly)

Returns the value of attribute wait_time.



12
13
14
# File 'lib/toolshed/databases/mysql/backup.rb', line 12

def wait_time
  @wait_time
end

Instance Method Details

#create_pathObject



24
25
26
# File 'lib/toolshed/databases/mysql/backup.rb', line 24

def create_path
  FileUtils.mkdir_p(File.dirname(path))
end

#executeObject

Raises:

  • (TypeError)


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/toolshed/databases/mysql/backup.rb', line 28

def execute
  raise TypeError, "Wait time passed in is not a number #{wait_time}" unless wait_time.is_a?(Fixnum)
  Toolshed.logger.info "Starting execution of mysqldump -h #{host} -u #{username} #{hidden_password_param} #{name} > #{path}."
  create_path
  results = Toolshed::Base.wait_for_command("mysqldump -h #{host} -u #{username} #{password_param} #{name} > #{path}", wait_time)
  unless results[:stderr].is_a?(NilClass) || results[:stderr].empty?
    error_message = results[:stderr].join(' ')
    Toolshed.logger.fatal error_message
    raise Toolshed::PermissionsException, error_message
  end
  Toolshed.logger.info results[:stdout].join(' ') unless results[:stdout].is_a?(NilClass)
  Toolshed.logger.info 'mysqldump has completed.'
end

#hidden_password_paramObject



46
47
48
# File 'lib/toolshed/databases/mysql/backup.rb', line 46

def hidden_password_param
  password_param.empty? ? '' : '-p *******'
end

#password_paramObject



42
43
44
# File 'lib/toolshed/databases/mysql/backup.rb', line 42

def password_param
  password.nil? || password.empty?  ? '' : "-p#{password_from_config(password)}"
end