Class: Backup::Model
- Inherits:
-
Object
- Object
- Backup::Model
- Defined in:
- lib/backup/model.rb
Defined Under Namespace
Classes: Error, FatalError
Instance Attribute Summary collapse
-
#archives ⇒ Object
readonly
Array of configured Archive objects.
-
#compressor ⇒ Object
readonly
The configured Compressor, if any.
-
#databases ⇒ Object
readonly
Array of configured Database objects.
-
#encryptor ⇒ Object
readonly
The configured Encryptor, if any.
-
#exception ⇒ Object
readonly
Exception raised by either a
before
hook or one of the model’s procedures that caused the model to fail. -
#exit_status ⇒ Object
readonly
Result of this model’s backup process.
-
#finished_at ⇒ Object
readonly
The time when the backup finished (as a Time object).
-
#label ⇒ Object
readonly
The label (stored as a String) is used for a more friendly user output.
-
#notifiers ⇒ Object
readonly
Array of configured Notifier objects.
-
#package ⇒ Object
readonly
The final backup Package this model will create.
-
#splitter ⇒ Object
readonly
The configured Splitter, if any.
-
#started_at ⇒ Object
readonly
The time when the backup initiated (as a Time object).
-
#storages ⇒ Object
readonly
Array of configured Storage objects.
-
#syncers ⇒ Object
readonly
Array of configured Syncer objects.
-
#time ⇒ Object
readonly
The time when the backup initiated (in format: 2011.02.20.03.29.59).
-
#trigger ⇒ Object
readonly
The trigger (stored as a String) is used as an identifier for initializing the backup process.
Class Method Summary collapse
-
.all ⇒ Object
The Backup::Model.all class method keeps track of all the models that have been instantiated.
-
.find_by_trigger(trigger) ⇒ Object
Return an Array of Models matching the given
trigger
. -
.preconfigure(&block) ⇒ Object
Allows users to create preconfigured models.
Instance Method Summary collapse
-
#after(&block) ⇒ Object
Defines a block of code to run after the model’s procedures.
-
#archive(name, &block) ⇒ Object
Adds an Archive.
-
#before(&block) ⇒ Object
Defines a block of code to run before the model’s procedures.
-
#compress_with(name, &block) ⇒ Object
Adds an Compressor.
-
#database(name, database_id = nil, &block) ⇒ Object
Adds an Database.
-
#duration ⇒ Object
The duration of the backup process (in format: HH:MM:SS).
-
#encrypt_with(name, &block) ⇒ Object
Adds an Encryptor.
-
#initialize(trigger, label, &block) ⇒ Model
constructor
A new instance of Model.
-
#notify_by(name, &block) ⇒ Object
Adds an Notifier.
-
#perform! ⇒ Object
Performs the backup process.
-
#remote_archive(name, &block) ⇒ Object
Adds a Remote Archive.
-
#remote_data(name, &block) ⇒ Object
Adds a Remote data Archive.
-
#split_into_chunks_of(chunk_size, suffix_length = 3) ⇒ Object
Adds a Splitter to split the final backup package into multiple files.
-
#store_with(name, storage_id = nil, &block) ⇒ Object
Adds an Storage.
-
#sync_with(name, syncer_id = nil, &block) ⇒ Object
Adds an Syncer.
Constructor Details
#initialize(trigger, label, &block) ⇒ Model
Returns a new instance of Model.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/backup/model.rb', line 115 def initialize(trigger, label, &block) @trigger = trigger.to_s @label = label.to_s @package = Package.new(self) @databases = [] @archives = [] @storages = [] @notifiers = [] @syncers = [] instance_eval(&self.class.preconfigure) if self.class.preconfigure instance_eval(&block) if block_given? # trigger all defined databases to generate their #dump_filename # so warnings may be logged if `backup perform --check` is used databases.each {|db| db.send(:dump_filename) } Model.all << self end |
Instance Attribute Details
#archives ⇒ Object (readonly)
Array of configured Archive objects.
57 58 59 |
# File 'lib/backup/model.rb', line 57 def archives @archives end |
#compressor ⇒ Object (readonly)
The configured Compressor, if any.
73 74 75 |
# File 'lib/backup/model.rb', line 73 def compressor @compressor end |
#databases ⇒ Object (readonly)
Array of configured Database objects.
53 54 55 |
# File 'lib/backup/model.rb', line 53 def databases @databases end |
#encryptor ⇒ Object (readonly)
The configured Encryptor, if any.
77 78 79 |
# File 'lib/backup/model.rb', line 77 def encryptor @encryptor end |
#exception ⇒ Object (readonly)
Exception raised by either a before
hook or one of the model’s procedures that caused the model to fail. An exception raised by an after
hook would not be stored here. Therefore, it is possible for this to be nil
even if #exit_status is 2 or 3.
113 114 115 |
# File 'lib/backup/model.rb', line 113 def exception @exception end |
#exit_status ⇒ Object (readonly)
Result of this model’s backup process.
0 = Job was successful 1 = Job was successful, but issued warnings 2 = Job failed, additional triggers may be performed 3 = Job failed, additional triggers will not be performed
106 107 108 |
# File 'lib/backup/model.rb', line 106 def exit_status @exit_status end |
#finished_at ⇒ Object (readonly)
The time when the backup finished (as a Time object)
97 98 99 |
# File 'lib/backup/model.rb', line 97 def finished_at @finished_at end |
#label ⇒ Object (readonly)
The label (stored as a String) is used for a more friendly user output
49 50 51 |
# File 'lib/backup/model.rb', line 49 def label @label end |
#notifiers ⇒ Object (readonly)
Array of configured Notifier objects.
61 62 63 |
# File 'lib/backup/model.rb', line 61 def notifiers @notifiers end |
#package ⇒ Object (readonly)
The final backup Package this model will create.
85 86 87 |
# File 'lib/backup/model.rb', line 85 def package @package end |
#splitter ⇒ Object (readonly)
The configured Splitter, if any.
81 82 83 |
# File 'lib/backup/model.rb', line 81 def splitter @splitter end |
#started_at ⇒ Object (readonly)
The time when the backup initiated (as a Time object)
93 94 95 |
# File 'lib/backup/model.rb', line 93 def started_at @started_at end |
#storages ⇒ Object (readonly)
Array of configured Storage objects.
65 66 67 |
# File 'lib/backup/model.rb', line 65 def storages @storages end |
#syncers ⇒ Object (readonly)
Array of configured Syncer objects.
69 70 71 |
# File 'lib/backup/model.rb', line 69 def syncers @syncers end |
#time ⇒ Object (readonly)
The time when the backup initiated (in format: 2011.02.20.03.29.59)
89 90 91 |
# File 'lib/backup/model.rb', line 89 def time @time end |
#trigger ⇒ Object (readonly)
The trigger (stored as a String) is used as an identifier for initializing the backup process
45 46 47 |
# File 'lib/backup/model.rb', line 45 def trigger @trigger end |
Class Method Details
.all ⇒ Object
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
13 14 15 |
# File 'lib/backup/model.rb', line 13 def all @all ||= [] end |
.find_by_trigger(trigger) ⇒ Object
Return an Array of Models matching the given trigger
.
19 20 21 22 23 24 25 26 27 |
# File 'lib/backup/model.rb', line 19 def find_by_trigger(trigger) trigger = trigger.to_s if trigger.include?('*') regex = /^#{ trigger.gsub('*', '(.*)') }$/ all.select {|model| regex =~ model.trigger } else all.select {|model| trigger == model.trigger } end end |
.preconfigure(&block) ⇒ Object
Allows users to create preconfigured models.
30 31 32 |
# File 'lib/backup/model.rb', line 30 def preconfigure(&block) @preconfigure ||= block end |
Instance Method Details
#after(&block) ⇒ Object
Defines a block of code to run after the model’s procedures.
This code is ensured to run, even if the model failed, unless a before
hook raised an exception and aborted the model.
The code block will be passed the model’s current exit_status:
‘0`: Success, no warnings. `1`: Success, but warnings were logged. `2`: Failure, but additional models/triggers will still be processed. `3`: Failure, no additional models/triggers will be processed.
The model’s exit_status may be elevated based on the after hook’s actions, but will never be decreased.
Warnings logged within the after hook may elevate the model’s exit_status to 1 and cause warning notifications to be sent.
Raising an exception may elevate the model’s exit_status and cause failure notifications to be sent. If the exception is a StandardError, the exit_status will be elevated to 2. If the exception is not a StandardError, the exit_status will be elevated to 3.
253 254 255 256 |
# File 'lib/backup/model.rb', line 253 def after(&block) @after = block if block @after end |
#archive(name, &block) ⇒ Object
Adds an Archive. Multiple Archives may be added to the model.
138 139 140 |
# File 'lib/backup/model.rb', line 138 def archive(name, &block) @archives << Archive.new(self, name, &block) end |
#before(&block) ⇒ Object
Defines a block of code to run before the model’s procedures.
Warnings logged within the before hook will elevate the model’s exit_status to 1 and cause warning notifications to be sent.
Raising an exception will abort the model and cause failure notifications to be sent. If the exception is a StandardError, exit_status will be 2. If the exception is not a StandardError, exit_status will be 3.
If any exception is raised, any defined after
hook will be skipped.
225 226 227 228 |
# File 'lib/backup/model.rb', line 225 def before(&block) @before = block if block @before end |
#compress_with(name, &block) ⇒ Object
Adds an Compressor. Only one Compressor may be added to the model. This will be used to compress each individual Archive and Database stored within the final backup package.
192 193 194 |
# File 'lib/backup/model.rb', line 192 def compress_with(name, &block) @compressor = get_class_from_scope(Compressor, name).new(&block) end |
#database(name, database_id = nil, &block) ⇒ Object
Adds an Database. Multiple Databases may be added to the model.
157 158 159 160 |
# File 'lib/backup/model.rb', line 157 def database(name, database_id = nil, &block) @databases << get_class_from_scope(Database, name). new(self, database_id, &block) end |
#duration ⇒ Object
The duration of the backup process (in format: HH:MM:SS)
305 306 307 308 |
# File 'lib/backup/model.rb', line 305 def duration return unless finished_at elapsed_time(started_at, finished_at) end |
#encrypt_with(name, &block) ⇒ Object
Adds an Encryptor. Only one Encryptor may be added to the model. This will be used to encrypt the final backup package.
184 185 186 |
# File 'lib/backup/model.rb', line 184 def encrypt_with(name, &block) @encryptor = get_class_from_scope(Encryptor, name).new(&block) end |
#notify_by(name, &block) ⇒ Object
Adds an Notifier. Multiple Notifiers may be added to the model.
177 178 179 |
# File 'lib/backup/model.rb', line 177 def notify_by(name, &block) @notifiers << get_class_from_scope(Notifier, name).new(self, &block) end |
#perform! ⇒ Object
Performs the backup process
Once complete, #exit_status will indicate the result of this process.
If any errors occur during the backup process, all temporary files will be left in place. If the error occurs before Packaging, then the temporary folder (tmp_path/trigger) will remain and may contain all or some of the configured Archives and/or Database dumps. If the error occurs after Packaging, but before the Storages complete, then the final packaged files (located in the root of tmp_path) will remain.
*** Important *** If an error occurs and any of the above mentioned temporary files remain, those files *** will be removed *** before the next scheduled backup for the same trigger.
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/backup/model.rb', line 274 def perform! @started_at = Time.now.utc @time = package.time = started_at.strftime("%Y.%m.%d.%H.%M.%S") log!(:started) before_hook procedures.each do |procedure| procedure.is_a?(Proc) ? procedure.call : procedure.each(&:perform!) end syncers.each(&:perform!) rescue Interrupt @interrupted = true raise rescue Exception => err @exception = err ensure unless @interrupted set_exit_status @finished_at = Time.now.utc log!(:finished) after_hook end end |
#remote_archive(name, &block) ⇒ Object
Adds a Remote Archive. Multiple archives may be added to the model.
144 145 146 |
# File 'lib/backup/model.rb', line 144 def remote_archive(name, &block) @archives << RemoteArchive.new(self, name, &block) end |
#remote_data(name, &block) ⇒ Object
Adds a Remote data Archive
150 151 152 |
# File 'lib/backup/model.rb', line 150 def remote_data(name, &block) @archives << RemoteData.new(self, name, &block) end |
#split_into_chunks_of(chunk_size, suffix_length = 3) ⇒ Object
Adds a Splitter to split the final backup package into multiple files.
chunk_size
is specified in MiB and must be given as an Integer. suffix_length
controls the number of characters used in the suffix (and the maximum number of chunks possible). ie. 1 (-a, -b), 2 (-aa, -ab), 3 (-aaa, -aab)
203 204 205 206 207 208 209 210 211 212 |
# File 'lib/backup/model.rb', line 203 def split_into_chunks_of(chunk_size, suffix_length = 3) if chunk_size.is_a?(Integer) && suffix_length.is_a?(Integer) @splitter = Splitter.new(self, chunk_size, suffix_length) else raise Error, <<-EOS Invalid arguments for #split_into_chunks_of() +chunk_size+ (and optional +suffix_length+) must be Integers. EOS end end |
#store_with(name, storage_id = nil, &block) ⇒ Object
Adds an Storage. Multiple Storages may be added to the model.
164 165 166 167 |
# File 'lib/backup/model.rb', line 164 def store_with(name, storage_id = nil, &block) @storages << get_class_from_scope(Storage, name). new(self, storage_id, &block) end |
#sync_with(name, syncer_id = nil, &block) ⇒ Object
Adds an Syncer. Multiple Syncers may be added to the model.
171 172 173 |
# File 'lib/backup/model.rb', line 171 def sync_with(name, syncer_id = nil, &block) @syncers << get_class_from_scope(Syncer, name).new(syncer_id, &block) end |