backup_zh 4.0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.md +24 -0
  3. data/README.md +21 -0
  4. data/bin/backup_zh +5 -0
  5. data/lib/backup.rb +137 -0
  6. data/lib/backup/archive.rb +170 -0
  7. data/lib/backup/binder.rb +22 -0
  8. data/lib/backup/cleaner.rb +116 -0
  9. data/lib/backup/cli.rb +364 -0
  10. data/lib/backup/cloud_io/base.rb +41 -0
  11. data/lib/backup/cloud_io/cloud_files.rb +298 -0
  12. data/lib/backup/cloud_io/qi_niu.rb +93 -0
  13. data/lib/backup/cloud_io/s3.rb +260 -0
  14. data/lib/backup/compressor/base.rb +35 -0
  15. data/lib/backup/compressor/bzip2.rb +39 -0
  16. data/lib/backup/compressor/custom.rb +53 -0
  17. data/lib/backup/compressor/gzip.rb +74 -0
  18. data/lib/backup/config.rb +119 -0
  19. data/lib/backup/config/dsl.rb +103 -0
  20. data/lib/backup/config/helpers.rb +143 -0
  21. data/lib/backup/database/base.rb +85 -0
  22. data/lib/backup/database/mongodb.rb +186 -0
  23. data/lib/backup/database/mysql.rb +181 -0
  24. data/lib/backup/database/openldap.rb +95 -0
  25. data/lib/backup/database/postgresql.rb +133 -0
  26. data/lib/backup/database/redis.rb +179 -0
  27. data/lib/backup/database/riak.rb +82 -0
  28. data/lib/backup/encryptor/base.rb +29 -0
  29. data/lib/backup/encryptor/gpg.rb +747 -0
  30. data/lib/backup/encryptor/open_ssl.rb +72 -0
  31. data/lib/backup/errors.rb +58 -0
  32. data/lib/backup/logger.rb +199 -0
  33. data/lib/backup/logger/console.rb +51 -0
  34. data/lib/backup/logger/fog_adapter.rb +29 -0
  35. data/lib/backup/logger/logfile.rb +133 -0
  36. data/lib/backup/logger/syslog.rb +116 -0
  37. data/lib/backup/model.rb +454 -0
  38. data/lib/backup/notifier/base.rb +98 -0
  39. data/lib/backup/notifier/campfire.rb +69 -0
  40. data/lib/backup/notifier/flowdock.rb +102 -0
  41. data/lib/backup/notifier/hipchat.rb +93 -0
  42. data/lib/backup/notifier/http_post.rb +122 -0
  43. data/lib/backup/notifier/mail.rb +238 -0
  44. data/lib/backup/notifier/nagios.rb +74 -0
  45. data/lib/backup/notifier/prowl.rb +69 -0
  46. data/lib/backup/notifier/pushover.rb +80 -0
  47. data/lib/backup/notifier/slack.rb +158 -0
  48. data/lib/backup/notifier/twitter.rb +64 -0
  49. data/lib/backup/notifier/zabbix.rb +68 -0
  50. data/lib/backup/package.rb +51 -0
  51. data/lib/backup/packager.rb +101 -0
  52. data/lib/backup/pipeline.rb +124 -0
  53. data/lib/backup/splitter.rb +76 -0
  54. data/lib/backup/storage/base.rb +57 -0
  55. data/lib/backup/storage/cloud_files.rb +158 -0
  56. data/lib/backup/storage/cycler.rb +65 -0
  57. data/lib/backup/storage/dropbox.rb +236 -0
  58. data/lib/backup/storage/ftp.rb +98 -0
  59. data/lib/backup/storage/local.rb +64 -0
  60. data/lib/backup/storage/ninefold.rb +74 -0
  61. data/lib/backup/storage/qi_niu.rb +70 -0
  62. data/lib/backup/storage/rsync.rb +248 -0
  63. data/lib/backup/storage/s3.rb +154 -0
  64. data/lib/backup/storage/scp.rb +67 -0
  65. data/lib/backup/storage/sftp.rb +82 -0
  66. data/lib/backup/syncer/base.rb +70 -0
  67. data/lib/backup/syncer/cloud/base.rb +179 -0
  68. data/lib/backup/syncer/cloud/cloud_files.rb +83 -0
  69. data/lib/backup/syncer/cloud/local_file.rb +100 -0
  70. data/lib/backup/syncer/cloud/s3.rb +110 -0
  71. data/lib/backup/syncer/rsync/base.rb +48 -0
  72. data/lib/backup/syncer/rsync/local.rb +31 -0
  73. data/lib/backup/syncer/rsync/pull.rb +51 -0
  74. data/lib/backup/syncer/rsync/push.rb +205 -0
  75. data/lib/backup/template.rb +46 -0
  76. data/lib/backup/utilities.rb +224 -0
  77. data/lib/backup/version.rb +5 -0
  78. data/templates/cli/archive +28 -0
  79. data/templates/cli/compressor/bzip2 +4 -0
  80. data/templates/cli/compressor/custom +7 -0
  81. data/templates/cli/compressor/gzip +4 -0
  82. data/templates/cli/config +123 -0
  83. data/templates/cli/databases/mongodb +15 -0
  84. data/templates/cli/databases/mysql +18 -0
  85. data/templates/cli/databases/openldap +24 -0
  86. data/templates/cli/databases/postgresql +16 -0
  87. data/templates/cli/databases/redis +16 -0
  88. data/templates/cli/databases/riak +17 -0
  89. data/templates/cli/encryptor/gpg +27 -0
  90. data/templates/cli/encryptor/openssl +9 -0
  91. data/templates/cli/model +26 -0
  92. data/templates/cli/notifier/zabbix +15 -0
  93. data/templates/cli/notifiers/campfire +12 -0
  94. data/templates/cli/notifiers/flowdock +16 -0
  95. data/templates/cli/notifiers/hipchat +15 -0
  96. data/templates/cli/notifiers/http_post +32 -0
  97. data/templates/cli/notifiers/mail +21 -0
  98. data/templates/cli/notifiers/nagios +13 -0
  99. data/templates/cli/notifiers/prowl +11 -0
  100. data/templates/cli/notifiers/pushover +11 -0
  101. data/templates/cli/notifiers/slack +23 -0
  102. data/templates/cli/notifiers/twitter +13 -0
  103. data/templates/cli/splitter +7 -0
  104. data/templates/cli/storages/cloud_files +11 -0
  105. data/templates/cli/storages/dropbox +19 -0
  106. data/templates/cli/storages/ftp +12 -0
  107. data/templates/cli/storages/local +7 -0
  108. data/templates/cli/storages/ninefold +9 -0
  109. data/templates/cli/storages/qi_niu +9 -0
  110. data/templates/cli/storages/rsync +17 -0
  111. data/templates/cli/storages/s3 +14 -0
  112. data/templates/cli/storages/scp +14 -0
  113. data/templates/cli/storages/sftp +14 -0
  114. data/templates/cli/syncers/cloud_files +22 -0
  115. data/templates/cli/syncers/rsync_local +20 -0
  116. data/templates/cli/syncers/rsync_pull +28 -0
  117. data/templates/cli/syncers/rsync_push +28 -0
  118. data/templates/cli/syncers/s3 +27 -0
  119. data/templates/general/links +3 -0
  120. data/templates/general/version.erb +2 -0
  121. data/templates/notifier/mail/failure.erb +16 -0
  122. data/templates/notifier/mail/success.erb +16 -0
  123. data/templates/notifier/mail/warning.erb +16 -0
  124. data/templates/storage/dropbox/authorization_url.erb +6 -0
  125. data/templates/storage/dropbox/authorized.erb +4 -0
  126. data/templates/storage/dropbox/cache_file_written.erb +10 -0
  127. metadata +1124 -0
@@ -0,0 +1,116 @@
1
+ # encoding: utf-8
2
+
3
+ module Backup
4
+ class Logger
5
+ class Syslog
6
+ class Options
7
+ ##
8
+ # Enables logging to the system's Syslog compatible logger.
9
+ #
10
+ # This may also be enabled using +--syslog+ on the command line.
11
+ #
12
+ # If +--no-syslog+ is used on the command line, this will be
13
+ # disabled and any settings here will be ignored.
14
+ #
15
+ # @param [Boolean, nil]
16
+ # @return [Boolean, nil] Default: +false+
17
+ attr_reader :enabled
18
+
19
+ ##
20
+ # Specify the identification string to be used with Syslog.
21
+ #
22
+ # @param [String]
23
+ # @return [String] Default: 'backup'
24
+ attr_accessor :ident
25
+
26
+ ##
27
+ # Specify the options to be used with Syslog.
28
+ #
29
+ # See the Ruby Standard Library documentation for +Syslog+ for more info.
30
+ # http://rdoc.info/stdlib/syslog/Syslog.open
31
+ #
32
+ # Note that setting this to +nil+ will cause this to default
33
+ # to a setting of +Syslog::LOG_PID | Syslog::LOG_CONS+
34
+ #
35
+ # @param [Integer]
36
+ # @return [Integer] Default: +Syslog::LOG_PID+
37
+ attr_accessor :options
38
+
39
+ ##
40
+ # Specify the facility to be used with Syslog.
41
+ #
42
+ # See the Ruby Standard Library documentation for +Syslog+ for more info.
43
+ # http://rdoc.info/stdlib/syslog/Syslog.open
44
+ #
45
+ # Note that setting this to +nil+ will cause this to default
46
+ # to a setting of +Syslog::LOG_USER+
47
+ #
48
+ # @param [Integer]
49
+ # @return [Integer] Default: +Syslog::LOG_LOCAL0+
50
+ attr_accessor :facility
51
+
52
+ ##
53
+ # Specify the priority level to be used for +:info+ messages.
54
+ #
55
+ # See the Ruby Standard Library documentation for +Syslog+ for more info.
56
+ # http://rdoc.info/stdlib/syslog/Syslog.log
57
+ #
58
+ # @param [Integer]
59
+ # @return [Integer] Default: +Syslog::LOG_INFO+
60
+ attr_accessor :info
61
+
62
+ ##
63
+ # Specify the priority level to be used for +:warn+ messages.
64
+ #
65
+ # See the Ruby Standard Library documentation for +Syslog+ for more info.
66
+ # http://rdoc.info/stdlib/syslog/Syslog.log
67
+ #
68
+ # @param [Integer]
69
+ # @return [Integer] Default: +Syslog::LOG_WARNING+
70
+ attr_accessor :warn
71
+
72
+ ##
73
+ # Specify the priority level to be used for +:error+ messages.
74
+ #
75
+ # See the Ruby Standard Library documentation for +Syslog+ for more info.
76
+ # http://rdoc.info/stdlib/syslog/Syslog.log
77
+ #
78
+ # @param [Integer]
79
+ # @return [Integer] Default: +Syslog::LOG_ERR+
80
+ attr_accessor :error
81
+
82
+ def initialize
83
+ @enabled = false
84
+ @ident = 'backup'
85
+ @options = ::Syslog::LOG_PID
86
+ @facility = ::Syslog::LOG_LOCAL0
87
+ @info = ::Syslog::LOG_INFO
88
+ @warn = ::Syslog::LOG_WARNING
89
+ @error = ::Syslog::LOG_ERR
90
+ end
91
+
92
+ def enabled?
93
+ !!enabled
94
+ end
95
+
96
+ def enabled=(val)
97
+ @enabled = val unless enabled.nil?
98
+ end
99
+ end
100
+
101
+ def initialize(options)
102
+ @options = options
103
+ end
104
+
105
+ ##
106
+ # Message lines are sent without formatting (timestamp, level),
107
+ # since Syslog will provide it's own timestamp and priority.
108
+ def log(message)
109
+ level = @options.send(message.level)
110
+ ::Syslog.open(@options.ident, @options.options, @options.facility) do |s|
111
+ message.lines.each {|line| s.log(level, '%s', line) }
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,454 @@
1
+ # encoding: utf-8
2
+
3
+ module Backup
4
+ class Model
5
+ class Error < Backup::Error; end
6
+ class FatalError < Backup::FatalError; end
7
+
8
+ class << self
9
+ ##
10
+ # The Backup::Model.all class method keeps track of all the models
11
+ # that have been instantiated. It returns the @all class variable,
12
+ # which contains an array of all the models
13
+ def all
14
+ @all ||= []
15
+ end
16
+
17
+ ##
18
+ # Return an Array of Models matching the given +trigger+.
19
+ def find_by_trigger(trigger)
20
+ trigger = trigger.to_s
21
+ if trigger.include?('*')
22
+ regex = /^#{ trigger.gsub('*', '(.*)') }$/
23
+ all.select {|model| regex =~ model.trigger }
24
+ else
25
+ all.select {|model| trigger == model.trigger }
26
+ end
27
+ end
28
+
29
+ # Allows users to create preconfigured models.
30
+ def preconfigure(&block)
31
+ @preconfigure ||= block
32
+ end
33
+
34
+ private
35
+
36
+ # used for testing
37
+ def reset!
38
+ @all = @preconfigure = nil
39
+ end
40
+ end
41
+
42
+ ##
43
+ # The trigger (stored as a String) is used as an identifier
44
+ # for initializing the backup process
45
+ attr_reader :trigger
46
+
47
+ ##
48
+ # The label (stored as a String) is used for a more friendly user output
49
+ attr_reader :label
50
+
51
+ ##
52
+ # Array of configured Database objects.
53
+ attr_reader :databases
54
+
55
+ ##
56
+ # Array of configured Archive objects.
57
+ attr_reader :archives
58
+
59
+ ##
60
+ # Array of configured Notifier objects.
61
+ attr_reader :notifiers
62
+
63
+ ##
64
+ # Array of configured Storage objects.
65
+ attr_reader :storages
66
+
67
+ ##
68
+ # Array of configured Syncer objects.
69
+ attr_reader :syncers
70
+
71
+ ##
72
+ # The configured Compressor, if any.
73
+ attr_reader :compressor
74
+
75
+ ##
76
+ # The configured Encryptor, if any.
77
+ attr_reader :encryptor
78
+
79
+ ##
80
+ # The configured Splitter, if any.
81
+ attr_reader :splitter
82
+
83
+ ##
84
+ # The final backup Package this model will create.
85
+ attr_reader :package
86
+
87
+ ##
88
+ # The time when the backup initiated (in format: 2011.02.20.03.29.59)
89
+ attr_reader :time
90
+
91
+ ##
92
+ # The time when the backup initiated (as a Time object)
93
+ attr_reader :started_at
94
+
95
+ ##
96
+ # The time when the backup finished (as a Time object)
97
+ attr_reader :finished_at
98
+
99
+ ##
100
+ # Result of this model's backup process.
101
+ #
102
+ # 0 = Job was successful
103
+ # 1 = Job was successful, but issued warnings
104
+ # 2 = Job failed, additional triggers may be performed
105
+ # 3 = Job failed, additional triggers will not be performed
106
+ attr_reader :exit_status
107
+
108
+ ##
109
+ # Exception raised by either a +before+ hook or one of the model's
110
+ # procedures that caused the model to fail. An exception raised by an
111
+ # +after+ hook would not be stored here. Therefore, it is possible for
112
+ # this to be +nil+ even if #exit_status is 2 or 3.
113
+ attr_reader :exception
114
+
115
+ def initialize(trigger, label, &block)
116
+ @trigger = trigger.to_s
117
+ @label = label.to_s
118
+ @package = Package.new(self)
119
+
120
+ @databases = []
121
+ @archives = []
122
+ @storages = []
123
+ @notifiers = []
124
+ @syncers = []
125
+
126
+ instance_eval(&self.class.preconfigure) if self.class.preconfigure
127
+ instance_eval(&block) if block_given?
128
+
129
+ # trigger all defined databases to generate their #dump_filename
130
+ # so warnings may be logged if `backup perform --check` is used
131
+ databases.each {|db| db.send(:dump_filename) }
132
+
133
+ Model.all << self
134
+ end
135
+
136
+ ##
137
+ # Adds an Archive. Multiple Archives may be added to the model.
138
+ def archive(name, &block)
139
+ @archives << Archive.new(self, name, &block)
140
+ end
141
+
142
+ ##
143
+ # Adds an Database. Multiple Databases may be added to the model.
144
+ def database(name, database_id = nil, &block)
145
+ @databases << get_class_from_scope(Database, name).
146
+ new(self, database_id, &block)
147
+ end
148
+
149
+ ##
150
+ # Adds an Storage. Multiple Storages may be added to the model.
151
+ def store_with(name, storage_id = nil, &block)
152
+ @storages << get_class_from_scope(Storage, name).
153
+ new(self, storage_id, &block)
154
+ end
155
+
156
+ ##
157
+ # Adds an Syncer. Multiple Syncers may be added to the model.
158
+ def sync_with(name, syncer_id = nil, &block)
159
+ @syncers << get_class_from_scope(Syncer, name).new(syncer_id, &block)
160
+ end
161
+
162
+ ##
163
+ # Adds an Notifier. Multiple Notifiers may be added to the model.
164
+ def notify_by(name, &block)
165
+ @notifiers << get_class_from_scope(Notifier, name).new(self, &block)
166
+ end
167
+
168
+ ##
169
+ # Adds an Encryptor. Only one Encryptor may be added to the model.
170
+ # This will be used to encrypt the final backup package.
171
+ def encrypt_with(name, &block)
172
+ @encryptor = get_class_from_scope(Encryptor, name).new(&block)
173
+ end
174
+
175
+ ##
176
+ # Adds an Compressor. Only one Compressor may be added to the model.
177
+ # This will be used to compress each individual Archive and Database
178
+ # stored within the final backup package.
179
+ def compress_with(name, &block)
180
+ @compressor = get_class_from_scope(Compressor, name).new(&block)
181
+ end
182
+
183
+ ##
184
+ # Adds a Splitter to split the final backup package into multiple files.
185
+ #
186
+ # +chunk_size+ is specified in MiB and must be given as an Integer.
187
+ # +suffix_length+ controls the number of characters used in the suffix
188
+ # (and the maximum number of chunks possible).
189
+ # ie. 1 (-a, -b), 2 (-aa, -ab), 3 (-aaa, -aab)
190
+ def split_into_chunks_of(chunk_size, suffix_length = 3)
191
+ if chunk_size.is_a?(Integer) && suffix_length.is_a?(Integer)
192
+ @splitter = Splitter.new(self, chunk_size, suffix_length)
193
+ else
194
+ raise Error, <<-EOS
195
+ Invalid arguments for #split_into_chunks_of()
196
+ +chunk_size+ (and optional +suffix_length+) must be Integers.
197
+ EOS
198
+ end
199
+ end
200
+
201
+ ##
202
+ # Defines a block of code to run before the model's procedures.
203
+ #
204
+ # Warnings logged within the before hook will elevate the model's
205
+ # exit_status to 1 and cause warning notifications to be sent.
206
+ #
207
+ # Raising an exception will abort the model and cause failure notifications
208
+ # to be sent. If the exception is a StandardError, exit_status will be 2.
209
+ # If the exception is not a StandardError, exit_status will be 3.
210
+ #
211
+ # If any exception is raised, any defined +after+ hook will be skipped.
212
+ def before(&block)
213
+ @before = block if block
214
+ @before
215
+ end
216
+
217
+ ##
218
+ # Defines a block of code to run after the model's procedures.
219
+ #
220
+ # This code is ensured to run, even if the model failed, **unless** a
221
+ # +before+ hook raised an exception and aborted the model.
222
+ #
223
+ # The code block will be passed the model's current exit_status:
224
+ #
225
+ # `0`: Success, no warnings.
226
+ # `1`: Success, but warnings were logged.
227
+ # `2`: Failure, but additional models/triggers will still be processed.
228
+ # `3`: Failure, no additional models/triggers will be processed.
229
+ #
230
+ # The model's exit_status may be elevated based on the after hook's
231
+ # actions, but will never be decreased.
232
+ #
233
+ # Warnings logged within the after hook may elevate the model's
234
+ # exit_status to 1 and cause warning notifications to be sent.
235
+ #
236
+ # Raising an exception may elevate the model's exit_status and cause
237
+ # failure notifications to be sent. If the exception is a StandardError,
238
+ # the exit_status will be elevated to 2. If the exception is not a
239
+ # StandardError, the exit_status will be elevated to 3.
240
+ def after(&block)
241
+ @after = block if block
242
+ @after
243
+ end
244
+
245
+ ##
246
+ # Performs the backup process
247
+ #
248
+ # Once complete, #exit_status will indicate the result of this process.
249
+ #
250
+ # If any errors occur during the backup process, all temporary files will
251
+ # be left in place. If the error occurs before Packaging, then the
252
+ # temporary folder (tmp_path/trigger) will remain and may contain all or
253
+ # some of the configured Archives and/or Database dumps. If the error
254
+ # occurs after Packaging, but before the Storages complete, then the final
255
+ # packaged files (located in the root of tmp_path) will remain.
256
+ #
257
+ # *** Important ***
258
+ # If an error occurs and any of the above mentioned temporary files remain,
259
+ # those files *** will be removed *** before the next scheduled backup for
260
+ # the same trigger.
261
+ def perform!
262
+ @started_at = Time.now.utc
263
+ @time = package.time = started_at.strftime("%Y.%m.%d.%H.%M.%S")
264
+
265
+ log!(:started)
266
+ before_hook
267
+
268
+ procedures.each do |procedure|
269
+ procedure.is_a?(Proc) ? procedure.call : procedure.each(&:perform!)
270
+ end
271
+
272
+ syncers.each(&:perform!)
273
+
274
+ rescue Interrupt
275
+ @interrupted = true
276
+ raise
277
+
278
+ rescue Exception => err
279
+ @exception = err
280
+
281
+ ensure
282
+ unless @interrupted
283
+ set_exit_status
284
+ @finished_at = Time.now.utc
285
+ log!(:finished)
286
+ after_hook
287
+ end
288
+ end
289
+
290
+ ##
291
+ # The duration of the backup process (in format: HH:MM:SS)
292
+ def duration
293
+ return unless finished_at
294
+ elapsed_time(started_at, finished_at)
295
+ end
296
+
297
+ private
298
+
299
+ ##
300
+ # Returns an array of procedures that will be performed if any
301
+ # Archives or Databases are configured for the model.
302
+ def procedures
303
+ return [] unless databases.any? || archives.any?
304
+
305
+ [lambda { prepare! }, databases, archives,
306
+ lambda { package! }, storages, lambda { clean! }]
307
+ end
308
+
309
+ ##
310
+ # Clean any temporary files and/or package files left over
311
+ # from the last time this model/trigger was performed.
312
+ # Logs warnings if files exist and are cleaned.
313
+ def prepare!
314
+ Cleaner.prepare(self)
315
+ end
316
+
317
+ ##
318
+ # After all the databases and archives have been dumped and stored,
319
+ # these files will be bundled in to a .tar archive (uncompressed),
320
+ # which may be optionally Encrypted and/or Split into multiple "chunks".
321
+ # All information about this final archive is stored in the @package.
322
+ # Once complete, the temporary folder used during packaging is removed.
323
+ def package!
324
+ Packager.package!(self)
325
+ Cleaner.remove_packaging(self)
326
+ end
327
+
328
+ ##
329
+ # Removes the final package file(s) once all configured Storages have run.
330
+ def clean!
331
+ Cleaner.remove_package(package)
332
+ end
333
+
334
+ ##
335
+ # Returns the class/model specified by +name+ inside of +scope+.
336
+ # +scope+ should be a Class/Module.
337
+ # +name+ may be Class/Module or String representation
338
+ # of any namespace which exists under +scope+.
339
+ #
340
+ # The 'Backup::Config::DSL' namespace is stripped from +name+,
341
+ # since this is the namespace where we define module namespaces
342
+ # for use with Model's DSL methods.
343
+ #
344
+ # Examples:
345
+ # get_class_from_scope(Backup::Database, 'MySQL')
346
+ # returns the class Backup::Database::MySQL
347
+ #
348
+ # get_class_from_scope(Backup::Syncer, Backup::Config::RSync::Local)
349
+ # returns the class Backup::Syncer::RSync::Local
350
+ #
351
+ def get_class_from_scope(scope, name)
352
+ klass = scope
353
+ name = name.to_s.sub(/^Backup::Config::DSL::/, '')
354
+ name.split('::').each do |chunk|
355
+ klass = klass.const_get(chunk)
356
+ end
357
+ klass
358
+ end
359
+
360
+ ##
361
+ # Sets or updates the model's #exit_status.
362
+ def set_exit_status
363
+ @exit_status = if exception
364
+ exception.is_a?(StandardError) ? 2 : 3
365
+ else
366
+ Logger.has_warnings? ? 1 : 0
367
+ end
368
+ end
369
+
370
+ ##
371
+ # Runs the +before+ hook.
372
+ # Any exception raised will be wrapped and re-raised, where it will be
373
+ # handled by #perform the same as an exception raised while performing
374
+ # the model's #procedures. Only difference is that an exception raised
375
+ # here will prevent any +after+ hook from being run.
376
+ def before_hook
377
+ return unless before
378
+
379
+ Logger.info 'Before Hook Starting...'
380
+ before.call
381
+ Logger.info 'Before Hook Finished.'
382
+
383
+ rescue Exception => err
384
+ @before_hook_failed = true
385
+ ex = err.is_a?(StandardError) ? Error : FatalError
386
+ raise ex.wrap(err, 'Before Hook Failed!')
387
+ end
388
+
389
+ ##
390
+ # Runs the +after+ hook.
391
+ # Any exception raised here will be logged only and the model's
392
+ # #exit_status will be elevated if neccessary.
393
+ def after_hook
394
+ return unless after && !@before_hook_failed
395
+
396
+ Logger.info 'After Hook Starting...'
397
+ after.call(exit_status)
398
+ Logger.info 'After Hook Finished.'
399
+
400
+ set_exit_status # in case hook logged warnings
401
+
402
+ rescue Exception => err
403
+ fatal = !err.is_a?(StandardError)
404
+ ex = fatal ? FatalError : Error
405
+ Logger.error ex.wrap(err, 'After Hook Failed!')
406
+ # upgrade exit_status if needed
407
+ (@exit_status = fatal ? 3 : 2) unless exit_status == 3
408
+ end
409
+
410
+ ##
411
+ # Logs messages when the model starts and finishes.
412
+ #
413
+ # #exception will be set here if #exit_status is > 1,
414
+ # since log(:finished) is called before the +after+ hook.
415
+ def log!(action)
416
+ case action
417
+ when :started
418
+ Logger.info "Performing Backup for '#{ label } (#{ trigger })'!\n" +
419
+ "[ backup #{ VERSION } : #{ RUBY_DESCRIPTION } ]"
420
+
421
+ when :finished
422
+ if exit_status > 1
423
+ ex = exit_status == 2 ? Error : FatalError
424
+ err = ex.wrap(exception, "Backup for #{ label } (#{ trigger }) Failed!")
425
+ Logger.error err
426
+ Logger.error "\nBacktrace:\n\s\s" + err.backtrace.join("\n\s\s") + "\n\n"
427
+
428
+ Cleaner.warnings(self)
429
+ else
430
+ msg = "Backup for '#{ label } (#{ trigger })' "
431
+ if exit_status == 1
432
+ msg << "Completed Successfully (with Warnings) in #{ duration }"
433
+ Logger.warn msg
434
+ else
435
+ msg << "Completed Successfully in #{ duration }"
436
+ Logger.info msg
437
+ end
438
+ end
439
+ end
440
+ end
441
+
442
+ ##
443
+ # Returns a string representing the elapsed time in HH:MM:SS.
444
+ def elapsed_time(start_time, finish_time)
445
+ duration = finish_time.to_i - start_time.to_i
446
+ hours = duration / 3600
447
+ remainder = duration - (hours * 3600)
448
+ minutes = remainder / 60
449
+ seconds = remainder - (minutes * 60)
450
+ '%02d:%02d:%02d' % [hours, minutes, seconds]
451
+ end
452
+
453
+ end
454
+ end