Class: Backup::Adapters::Base

Inherits:
Object
  • Object
show all
Includes:
CommandHelper
Defined in:
lib/backup/adapters/base.rb

Direct Known Subclasses

Archive, Custom, MySQL, PostgreSQL, SQLite

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommandHelper

#log, #run

Constructor Details

#initialize(trigger, procedure) ⇒ Base

Initializes the Backup Process

This will first load in any prefixed settings from the Backup::Adapters::Base Then it will add it’s own settings.

First it will call the ‘perform’ method. This method is concerned with the backup, and must be implemented by derived classes! Then it will optionally encrypt the backed up file Then it will store it to the specified storage location Then it will record the data to the database Once this is all done, all the temporary files will be removed

Wrapped inside of begin/ensure/end block to ensure the deletion of any files in the tmp directory



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/backup/adapters/base.rb', line 33

def initialize(trigger, procedure)
  self.trigger                = trigger
  self.procedure              = procedure
  self.timestamp              = Time.now.strftime("%Y%m%d%H%M%S")
  self.tmp_path               = File.join(BACKUP_PATH.gsub(' ', '\ '), 'tmp', 'backup', trigger)
  self.encrypt_with_password  = procedure.attributes['encrypt_with_password']
  self.keep_backups           = procedure.attributes['keep_backups']

  self.performed_file   = "#{timestamp}.#{trigger.gsub(' ', '-')}#{performed_file_extension}"
  self.compressed_file  = "#{performed_file}.gz"
  self.encrypted_file   = "#{compressed_file}.enc"
  self.final_file       = compressed_file

  begin
    create_tmp_folder
    load_settings # if respond_to?(:load_settings)
    perform
    encrypt
    store
    record
    notify
  ensure
    remove_tmp_files
  end
end

Instance Attribute Details

#compressed_fileObject

IMPORTANT final_file must have the value of the final filename result so if a file gets compressed, then the file could look like this:

myfile.gz

and if a file afterwards gets encrypted, the file will look like:

myfile.gz.enc

It is important that, whatever the final filename of the file will be, that :final_file will contain it.



18
19
20
# File 'lib/backup/adapters/base.rb', line 18

def compressed_file
  @compressed_file
end

#encrypt_with_passwordObject

Returns the value of attribute encrypt_with_password.



7
8
9
# File 'lib/backup/adapters/base.rb', line 7

def encrypt_with_password
  @encrypt_with_password
end

#encrypted_fileObject

IMPORTANT final_file must have the value of the final filename result so if a file gets compressed, then the file could look like this:

myfile.gz

and if a file afterwards gets encrypted, the file will look like:

myfile.gz.enc

It is important that, whatever the final filename of the file will be, that :final_file will contain it.



18
19
20
# File 'lib/backup/adapters/base.rb', line 18

def encrypted_file
  @encrypted_file
end

#final_fileObject

IMPORTANT final_file must have the value of the final filename result so if a file gets compressed, then the file could look like this:

myfile.gz

and if a file afterwards gets encrypted, the file will look like:

myfile.gz.enc

It is important that, whatever the final filename of the file will be, that :final_file will contain it.



18
19
20
# File 'lib/backup/adapters/base.rb', line 18

def final_file
  @final_file
end

#keep_backupsObject

Returns the value of attribute keep_backups.



7
8
9
# File 'lib/backup/adapters/base.rb', line 7

def keep_backups
  @keep_backups
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/backup/adapters/base.rb', line 7

def options
  @options
end

#performed_fileObject

IMPORTANT final_file must have the value of the final filename result so if a file gets compressed, then the file could look like this:

myfile.gz

and if a file afterwards gets encrypted, the file will look like:

myfile.gz.enc

It is important that, whatever the final filename of the file will be, that :final_file will contain it.



18
19
20
# File 'lib/backup/adapters/base.rb', line 18

def performed_file
  @performed_file
end

#procedureObject

Returns the value of attribute procedure.



7
8
9
# File 'lib/backup/adapters/base.rb', line 7

def procedure
  @procedure
end

#timestampObject

Returns the value of attribute timestamp.



7
8
9
# File 'lib/backup/adapters/base.rb', line 7

def timestamp
  @timestamp
end

#tmp_pathObject

Returns the value of attribute tmp_path.



7
8
9
# File 'lib/backup/adapters/base.rb', line 7

def tmp_path
  @tmp_path
end

#triggerObject

Returns the value of attribute trigger.



7
8
9
# File 'lib/backup/adapters/base.rb', line 7

def trigger
  @trigger
end

Instance Method Details

#create_tmp_folderObject

Creates the temporary folder for the specified adapter



60
61
62
# File 'lib/backup/adapters/base.rb', line 60

def create_tmp_folder
  run "mkdir -p #{tmp_path}"
end

#encryptObject

Encrypts the archive file



74
75
76
77
78
79
80
# File 'lib/backup/adapters/base.rb', line 74

def encrypt
  if encrypt_with_password.is_a?(String)
    log system_messages[:encrypting]
    run "openssl enc -des-cbc -in #{File.join(tmp_path, compressed_file)} -out #{File.join(tmp_path, encrypted_file)} -k #{encrypt_with_password}"
    self.final_file = encrypted_file
  end
end

#load_settingsObject

TODO make methods in derived classes public? respond_to cannot identify private methods



65
66
# File 'lib/backup/adapters/base.rb', line 65

def load_settings
end

#notifyObject

Delivers a notification by email regarding the successfully stored backup



95
96
97
98
99
# File 'lib/backup/adapters/base.rb', line 95

def notify
  if Backup::Mail::Base.setup?
    Backup::Mail::Base.notify!(self)
  end
end

#recordObject

Records data on every individual file to the database



88
89
90
91
92
# File 'lib/backup/adapters/base.rb', line 88

def record
  record = procedure.initialize_record
  record.load_adapter(self)
  record.save
end

#remove_tmp_filesObject

Removes the files inside the temporary folder



69
70
71
# File 'lib/backup/adapters/base.rb', line 69

def remove_tmp_files
  run "rm #{File.join(tmp_path, '*')}"
end

#storeObject

Initializes the storing process



83
84
85
# File 'lib/backup/adapters/base.rb', line 83

def store
  procedure.initialize_storage(self)
end

#system_messagesObject



101
102
103
104
105
106
107
108
109
# File 'lib/backup/adapters/base.rb', line 101

def system_messages
  { :compressing  => "Compressing backup..",
    :archiving    => "Archiving backup..",
    :encrypting   => "Encrypting backup..",
    :mysqldump    => "Creating MySQL dump..",
    :pgdump       => "Creating PostgreSQL dump..",
    :sqlite       => "Copying and compressing SQLite database..",
    :commands     => "Executing commands.." }
end