Class: ServerBackups::BackupBase

Inherits:
Object
  • Object
show all
Defined in:
lib/server_backups/backup_base.rb

Direct Known Subclasses

MysqlBackup, WebsiteBackup

Constant Summary collapse

BACKUP_TYPES =
%i[incremental daily weekly monthly].freeze
TIMESTAMP_FORMAT =
'%Y-%m-%dT%H00.UTC%z'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file, working_directory, backup_type) ⇒ BackupBase

Returns a new instance of BackupBase.



13
14
15
16
17
18
19
20
21
# File 'lib/server_backups/backup_base.rb', line 13

def initialize(config_file, working_directory, backup_type)
    @working_directory = working_directory
    @config = Config.new(config_file)
    Time.zone = config.time_zone
    @logger = config.logger
    @backup_type = backup_type.to_sym
    logger.debug "Initialized #{backup_type} #{self.class.name.demodulize.titleize}, " \
                 "prefix: '#{s3_prefix}'"
end

Instance Attribute Details

#backup_typeObject (readonly)

Returns the value of attribute backup_type.



7
8
9
# File 'lib/server_backups/backup_base.rb', line 7

def backup_type
  @backup_type
end

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/server_backups/backup_base.rb', line 7

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'lib/server_backups/backup_base.rb', line 7

def logger
  @logger
end

#s3Object (readonly)

Returns the value of attribute s3.



7
8
9
# File 'lib/server_backups/backup_base.rb', line 7

def s3
  @s3
end

#working_directoryObject (readonly)

Returns the value of attribute working_directory.



7
8
9
# File 'lib/server_backups/backup_base.rb', line 7

def working_directory
  @working_directory
end

Class Method Details

.daily(config_file, working_directory) ⇒ Object



28
29
30
# File 'lib/server_backups/backup_base.rb', line 28

def daily(config_file, working_directory)
    new(config_file, working_directory, :daily)
end

.incremental(config_file, working_directory) ⇒ Object



40
41
42
# File 'lib/server_backups/backup_base.rb', line 40

def incremental(config_file, working_directory)
    new(config_file, working_directory, :incremental)
end

.monthly(config_file, working_directory) ⇒ Object



36
37
38
# File 'lib/server_backups/backup_base.rb', line 36

def monthly(config_file, working_directory)
    new(config_file, working_directory, :monthly)
end

.weekly(config_file, working_directory) ⇒ Object



32
33
34
# File 'lib/server_backups/backup_base.rb', line 32

def weekly(config_file, working_directory)
    new(config_file, working_directory, :weekly)
end

Instance Method Details

#backup_filenameObject



45
46
47
# File 'lib/server_backups/backup_base.rb', line 45

def backup_filename
    "#{self.class.name.demodulize.underscore}.#{backup_type}.#{timestamp}.tgz"
end

#backup_pathObject



83
84
85
# File 'lib/server_backups/backup_base.rb', line 83

def backup_path
    File.join(working_directory, backup_filename)
end

#backup_s3_keyObject



69
70
71
# File 'lib/server_backups/backup_base.rb', line 69

def backup_s3_key
    File.join(s3_prefix, File.basename(backup_filename))
end

#do_backupObject



87
88
89
90
91
92
93
# File 'lib/server_backups/backup_base.rb', line 87

def do_backup
    load_resources
    take_backup
    store_backup
    # verify_backup
    remove_old_backups
end

#incremental?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/server_backups/backup_base.rb', line 23

def incremental?
    backup_type == :incremental
end

#load_resourcesObject



99
100
101
102
# File 'lib/server_backups/backup_base.rb', line 99

def load_resources
    # @mysql = Mys3ql::Mysql.new self.config
    @s3 = S3.new config
end

#remove_old_backupsObject



79
80
81
# File 'lib/server_backups/backup_base.rb', line 79

def remove_old_backups
    s3.delete_files_not_newer_than s3_prefix, config.get_retention_threshold(backup_type)
end

#s3_prefixObject



64
65
66
67
# File 'lib/server_backups/backup_base.rb', line 64

def s3_prefix
    File.join(config.prefix, self.class.name.demodulize.underscore,
              backup_type.to_s, '/')
end

#store_backupObject



73
74
75
76
77
# File 'lib/server_backups/backup_base.rb', line 73

def store_backup
    logger.info 'Upload file'
    @uploaded_file = s3.save backup_path, backup_s3_key
    logger.info 'Finished uploading file.'
end

#take_backupObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/server_backups/backup_base.rb', line 53

def take_backup
    logger.info "Creating #{backup_type} #{title} #{create_archive_command}"

    system create_archive_command
    unless last_command_succeeded?
        raise BackupCreationError.new("Received #{$CHILD_STATUS} from tar command.",
                                      self.class, backup_type)
    end
    logger.debug "Backup exited with #{$CHILD_STATUS}"
end

#timestampObject



95
96
97
# File 'lib/server_backups/backup_base.rb', line 95

def timestamp
    Time.zone.now.strftime(TIMESTAMP_FORMAT)
end

#titleObject



49
50
51
# File 'lib/server_backups/backup_base.rb', line 49

def title
    self.class.name.demodulize.titleize
end