Class: ServerBackups::MysqlRestore

Inherits:
RestoreBase show all
Defined in:
lib/server_backups/mysql_restore.rb

Constant Summary collapse

ALL_DATABASES =
'all'
ETC_TIMEZONE =
'/etc/timezone'

Instance Attribute Summary

Attributes inherited from RestoreBase

#config, #database, #restore_point, #s3, #working_dir

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file, working_dir, restore_point, database) ⇒ MysqlRestore

Returns a new instance of MysqlRestore.



9
10
11
12
# File 'lib/server_backups/mysql_restore.rb', line 9

def initialize(config_file, working_dir, restore_point, database)
    @database = database
    super config_file, working_dir, restore_point
end

Class Method Details

.all_databases(config_file, working_dir) ⇒ Object



66
67
68
# File 'lib/server_backups/mysql_restore.rb', line 66

def all_databases(config_file, working_dir)
    MysqlBackup.new(config_file, working_dir, 'daily', 'mysql').all_databases
end

.restore(config_file, working_dir, restore_point, database) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/server_backups/mysql_restore.rb', line 57

def restore(config_file, working_dir, restore_point, database)
    return new(config_file, working_dir, restore_point, database).do_restore \
        if database != ALL_DATABASES

    all_databases(config_file, working_dir).each do |db_name|
        new(config_file, working_dir, restore_point, db_name).do_restore
    end
end

Instance Method Details

#do_restoreObject



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

def do_restore
    full_backup_file.get response_target: (restore_script_path + '.gz')
    system "gunzip #{restore_script_path}.gz"

    incremental_backups.each do |s3object|
        file = Tempfile.new('foo')
        begin
            s3object.get response_target: file
            file.close
            system config.mysqlbinlog_bin + ' ' + file.path + \
                   " --stop-datetime='#{formatted_restore_point_in_system_time_zone}'" \
                   " --database=#{database} >> " + restore_script_path
        ensure
            file.close
            file.unlink # deletes the temp file
        end
    end

    execute_script restore_script_path
end

#formatted_restore_point_in_system_time_zoneObject



30
31
32
33
# File 'lib/server_backups/mysql_restore.rb', line 30

def formatted_restore_point_in_system_time_zone
    restore_point.in_time_zone(config.system_time_zone) \
                 .strftime('%Y-%m-%d %H:%M:%S')
end

#full_backup_fileObject



14
15
16
17
# File 'lib/server_backups/mysql_restore.rb', line 14

def full_backup_file
    full_backup_prefix = File.join(config.prefix, 'mysql_backup', database)
    s3.get_ordered_collection(full_backup_prefix).full_backup_for(restore_point)
end

#incremental_backupsObject



19
20
21
22
# File 'lib/server_backups/mysql_restore.rb', line 19

def incremental_backups
    incr_backup_prefix = File.join(config.prefix, 'mysql_backup')
    s3.get_ordered_collection(incr_backup_prefix).incremental_backups_for(restore_point)
end

#restore_script_pathObject



24
25
26
# File 'lib/server_backups/mysql_restore.rb', line 24

def restore_script_path
    File.join(working_dir, "#{database}.sql")
end