Class: Backup::MySQLBackup

Inherits:
Object
  • Object
show all
Defined in:
lib/backup/mysql_backup.rb

Overview

MySQLBackup creates a dump file of a MySQL database with mysqldump and returns the dump file.

Instance Method Summary collapse

Constructor Details

#initialize(database, user, password) ⇒ MySQLBackup

database is the database to be backed up with the user that has the credentials to access the database with the provided password



11
12
13
14
15
# File 'lib/backup/mysql_backup.rb', line 11

def initialize(database, user, password)
  @database = database
  @user = user
  @password = password
end

Instance Method Details

#backup(backup_folder = "./") ⇒ Object

Creates a MySQL dump file and returns the file



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/backup/mysql_backup.rb', line 36

def backup(backup_folder="./")
  Dir.mkdir backup_folder unless File.exists? backup_folder

  timestamp = Time.now.strftime('%Y%m%d-%H%M%S')
  backup_file = backup_folder + @database + '_' + timestamp + '.sql'
  mysqldump = "mysqldump -u#{@user} -p#{@password} #{@database}"

  backup_command = "#{mysqldump} > #{backup_file}"
  stdout_str, stderr_str, status = Open3.capture3(backup_command)
  unless status.exitstatus == 0
    File.delete backup_file if File.exists? backup_file
    STDERR.puts "There was a problem running mysqldump"
    STDERR.puts "--> #{backup_command}"
    STDERR.puts stderr_str
    exit(1)
  end

  backup_file

end

#compress(backup_file) ⇒ Object

Compresses the MySQL dump file. If an error occurs when compressing the file an error message is printed and the application terminates



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/backup/mysql_backup.rb', line 19

def compress(backup_file)
  tar_file = backup_file + '.tar.gz'
  tar_command = "tar cfz #{tar_file} #{backup_file}"
  stdout_str, stderr_str, status = Open3.capture3(tar_command)
  unless status.exitstatus == 0
    File.delete tar_file if File.exists? tar_file
    STDERR.puts "There was a problem compressing the backup file"
    STDERR.puts "-->#{tar_command}"
    STDERR.puts stderr_str
    exit(2)
  end

  status.exitstatus

end