Class: Backup::Model

Inherits:
Object
  • Object
show all
Includes:
CLI
Defined in:
lib/backup/model.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CLI

#mkdir, #raise_if_command_not_found!, #rm, #run, #utility

Constructor Details

#initialize(trigger, label, &block) ⇒ Model

Takes a trigger, label and the configuration block and instantiates the model. The TIME (time of execution) gets stored in the @time attribute. After the instance has evaluated the configuration block and properly set the configuration for the model, it will append the newly created “model” instance to the @all class variable (Array) so it can be accessed by Backup::Finder and any other location



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/backup/model.rb', line 97

def initialize(trigger, label, &block)
  @trigger     = trigger
  @label       = label
  @time        = TIME

  @databases   = Array.new
  @archives    = Array.new
  @encryptors  = Array.new
  @compressors = Array.new
  @storages    = Array.new
  @notifiers   = Array.new
  @syncers     = Array.new

  instance_eval(&block)
  Backup::Model.all << self
end

Class Attribute Details

.allObject

The Backup::Model.all class method keeps track of all the models that have been instantiated. It returns the @all class variable, which contains an array of all the models



53
54
55
# File 'lib/backup/model.rb', line 53

def all
  @all
end

.currentObject

Contains the currently-in-use model. This attribute should get set by Backup::Finder. Use Backup::Model.current to retrieve the actual data of the model



64
65
66
# File 'lib/backup/model.rb', line 64

def current
  @current
end

.extensionObject

Contains the current file extension (this changes from time to time after a file gets compressed or encrypted so we can keep track of the correct file when new extensions get appended to the current file name)



59
60
61
# File 'lib/backup/model.rb', line 59

def extension
  @extension
end

Instance Attribute Details

#archivesObject

The archives attr_accessor holds an array of archive objects



22
23
24
# File 'lib/backup/model.rb', line 22

def archives
  @archives
end

#compressorsObject

The compressors attr_accessor holds an array of compressor objects



30
31
32
# File 'lib/backup/model.rb', line 30

def compressors
  @compressors
end

#databasesObject

The databases attribute holds an array of database objects



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

def databases
  @databases
end

#encryptorsObject

The encryptors attr_accessor holds an array of encryptor objects



26
27
28
# File 'lib/backup/model.rb', line 26

def encryptors
  @encryptors
end

#labelObject

The label is used for a more friendly user output



14
15
16
# File 'lib/backup/model.rb', line 14

def label
  @label
end

#notifiersObject

The notifiers attr_accessor holds an array of notifier objects



34
35
36
# File 'lib/backup/model.rb', line 34

def notifiers
  @notifiers
end

#storagesObject

The storages attribute holds an array of storage objects



38
39
40
# File 'lib/backup/model.rb', line 38

def storages
  @storages
end

#syncersObject

The syncers attribute holds an array of syncer objects



42
43
44
# File 'lib/backup/model.rb', line 42

def syncers
  @syncers
end

#timeObject

The time when the backup initiated (in format: 2011.02.20.03.29.59)



46
47
48
# File 'lib/backup/model.rb', line 46

def time
  @time
end

#triggerObject

The trigger is used as an identifier for initializing the backup process



10
11
12
# File 'lib/backup/model.rb', line 10

def trigger
  @trigger
end

Class Method Details

.fileObject

Returns the full path to the current file (including the current extension). To just return the filename and extension without the path, use File.basename(Backup::Model.file)



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

def file
  File.join(TMP_PATH, "#{ TIME }.#{ TRIGGER }.#{ Backup::Model.extension }")
end

.tmp_pathObject

Returns the temporary trigger path of the current model e.g. /Users/Michael/tmp/backup/my_trigger



76
77
78
# File 'lib/backup/model.rb', line 76

def tmp_path
  File.join(TMP_PATH, TRIGGER)
end

Instance Method Details

#archive(name, &block) ⇒ Object

Adds an archive to the array of archives to store during the backup process



126
127
128
# File 'lib/backup/model.rb', line 126

def archive(name, &block)
  @archives << Backup::Archive.new(name, &block)
end

#compress_with(name, &block) ⇒ Object

Adds a compressor to the array of compressors to use during the backup process



142
143
144
145
146
# File 'lib/backup/model.rb', line 142

def compress_with(name, &block)
  @compressors << Backup::Compressor.const_get(
    last_constant(name)
  ).new(&block)
end

#database(database, &block) ⇒ Object

Adds a database to the array of databases to dump during the backup process



117
118
119
120
121
# File 'lib/backup/model.rb', line 117

def database(database, &block)
  @databases << Backup::Database.const_get(
    last_constant(database)
  ).new(&block)
end

#encrypt_with(name, &block) ⇒ Object

Adds an encryptor to the array of encryptors to use during the backup process



133
134
135
136
137
# File 'lib/backup/model.rb', line 133

def encrypt_with(name, &block)
  @encryptors << Backup::Encryptor.const_get(
    last_constant(name)
  ).new(&block)
end

#notify_by(name, &block) ⇒ Object

Adds a notifier to the array of notifiers to use during the backup process



151
152
153
154
155
# File 'lib/backup/model.rb', line 151

def notify_by(name, &block)
  @notifiers << Backup::Notifier.const_get(
    last_constant(name)
  ).new(&block)
end

#perform!Object

Performs the backup process

Databases

Runs all (if any) database objects to dump the databases

Archives

Runs all (if any) archive objects to package all their paths in to a single tar file and places it in the backup folder

Package

After all the database dumps and archives are placed inside the folder, it’ll make a single .tar package (archive) out of it

Encryption

Optionally encrypts the packaged file with one or more encryptors

Compression

Optionally compresses the packaged file with one or more compressors

Storages

Runs all (if any) storage objects to store the backups to remote locations and (if configured) it’ll cycle the files on the remote location to limit the amount of backups stored on each individual location

Syncers

Runs all (if any) sync objects to store the backups to remote locations. A Syncer does not go through the process of packaging, compressing, encrypting backups. A Syncer directly transfers data from the filesystem to the remote location

Notifiers

Runs all (if any) notifier objects when a backup proces finished with or without any errors.

Cleaning

After the whole backup process finishes, it’ll go ahead and remove any temporary file that it produced. If an exception(error) is raised during this process which breaks the process, it’ll always ensure it removes the temporary files regardless to avoid mass consumption of storage space on the machine



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/backup/model.rb', line 214

def perform!
  begin
    if databases.any? or archives.any?
      databases.each   { |d| d.perform! }
      archives.each    { |a| a.perform! }
      package!
      compressors.each { |c| c.perform! }
      encryptors.each  { |e| e.perform! }
      storages.each    { |s| s.perform! }
      clean!
    end

    syncers.each   { |s| s.perform!       }
    notifiers.each { |n| n.perform!(self) }
  rescue => exception
    clean!
    notifiers.each   { |n| n.perform!(self, exception) }
    show_exception!(exception)
  end
end

#store_with(storage, &block) ⇒ Object

Adds a storage method to the array of storage methods to use during the backup process



160
161
162
163
164
# File 'lib/backup/model.rb', line 160

def store_with(storage, &block)
  @storages << Backup::Storage.const_get(
    last_constant(storage)
  ).new(&block)
end

#sync_with(syncer, &block) ⇒ Object

Adds a syncer method to the array of syncer methods to use during the backup process



169
170
171
172
173
# File 'lib/backup/model.rb', line 169

def sync_with(syncer, &block)
  @syncers << Backup::Syncer.const_get(
    last_constant(syncer)
  ).new(&block)
end