backup_checksum 3.0.23

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of backup_checksum might be problematic. Click here for more details.

Files changed (244) hide show
  1. data/.gitignore +7 -0
  2. data/.travis.yml +10 -0
  3. data/Gemfile +28 -0
  4. data/Gemfile.lock +130 -0
  5. data/Guardfile +21 -0
  6. data/LICENSE.md +24 -0
  7. data/README.md +476 -0
  8. data/backup_checksum.gemspec +32 -0
  9. data/bin/backup +11 -0
  10. data/lib/backup.rb +217 -0
  11. data/lib/backup/archive.rb +117 -0
  12. data/lib/backup/binder.rb +22 -0
  13. data/lib/backup/checksum/base.rb +44 -0
  14. data/lib/backup/checksum/shasum.rb +16 -0
  15. data/lib/backup/cleaner.rb +121 -0
  16. data/lib/backup/cli/helpers.rb +88 -0
  17. data/lib/backup/cli/utility.rb +247 -0
  18. data/lib/backup/compressor/base.rb +29 -0
  19. data/lib/backup/compressor/bzip2.rb +50 -0
  20. data/lib/backup/compressor/gzip.rb +47 -0
  21. data/lib/backup/compressor/lzma.rb +50 -0
  22. data/lib/backup/compressor/pbzip2.rb +56 -0
  23. data/lib/backup/config.rb +173 -0
  24. data/lib/backup/configuration/base.rb +15 -0
  25. data/lib/backup/configuration/checksum/base.rb +9 -0
  26. data/lib/backup/configuration/checksum/shasum.rb +9 -0
  27. data/lib/backup/configuration/compressor/base.rb +9 -0
  28. data/lib/backup/configuration/compressor/bzip2.rb +23 -0
  29. data/lib/backup/configuration/compressor/gzip.rb +23 -0
  30. data/lib/backup/configuration/compressor/lzma.rb +23 -0
  31. data/lib/backup/configuration/compressor/pbzip2.rb +28 -0
  32. data/lib/backup/configuration/database/base.rb +19 -0
  33. data/lib/backup/configuration/database/mongodb.rb +49 -0
  34. data/lib/backup/configuration/database/mysql.rb +42 -0
  35. data/lib/backup/configuration/database/postgresql.rb +41 -0
  36. data/lib/backup/configuration/database/redis.rb +39 -0
  37. data/lib/backup/configuration/database/riak.rb +29 -0
  38. data/lib/backup/configuration/encryptor/base.rb +9 -0
  39. data/lib/backup/configuration/encryptor/gpg.rb +17 -0
  40. data/lib/backup/configuration/encryptor/open_ssl.rb +32 -0
  41. data/lib/backup/configuration/helpers.rb +52 -0
  42. data/lib/backup/configuration/notifier/base.rb +28 -0
  43. data/lib/backup/configuration/notifier/campfire.rb +25 -0
  44. data/lib/backup/configuration/notifier/hipchat.rb +41 -0
  45. data/lib/backup/configuration/notifier/mail.rb +112 -0
  46. data/lib/backup/configuration/notifier/presently.rb +25 -0
  47. data/lib/backup/configuration/notifier/prowl.rb +23 -0
  48. data/lib/backup/configuration/notifier/twitter.rb +21 -0
  49. data/lib/backup/configuration/storage/base.rb +18 -0
  50. data/lib/backup/configuration/storage/cloudfiles.rb +25 -0
  51. data/lib/backup/configuration/storage/dropbox.rb +58 -0
  52. data/lib/backup/configuration/storage/ftp.rb +29 -0
  53. data/lib/backup/configuration/storage/local.rb +17 -0
  54. data/lib/backup/configuration/storage/ninefold.rb +20 -0
  55. data/lib/backup/configuration/storage/rsync.rb +29 -0
  56. data/lib/backup/configuration/storage/s3.rb +25 -0
  57. data/lib/backup/configuration/storage/scp.rb +25 -0
  58. data/lib/backup/configuration/storage/sftp.rb +25 -0
  59. data/lib/backup/configuration/syncer/base.rb +10 -0
  60. data/lib/backup/configuration/syncer/cloud.rb +23 -0
  61. data/lib/backup/configuration/syncer/cloud_files.rb +30 -0
  62. data/lib/backup/configuration/syncer/rsync/base.rb +28 -0
  63. data/lib/backup/configuration/syncer/rsync/local.rb +11 -0
  64. data/lib/backup/configuration/syncer/rsync/pull.rb +11 -0
  65. data/lib/backup/configuration/syncer/rsync/push.rb +31 -0
  66. data/lib/backup/configuration/syncer/s3.rb +23 -0
  67. data/lib/backup/database/base.rb +59 -0
  68. data/lib/backup/database/mongodb.rb +232 -0
  69. data/lib/backup/database/mysql.rb +163 -0
  70. data/lib/backup/database/postgresql.rb +146 -0
  71. data/lib/backup/database/redis.rb +139 -0
  72. data/lib/backup/database/riak.rb +69 -0
  73. data/lib/backup/dependency.rb +114 -0
  74. data/lib/backup/encryptor/base.rb +29 -0
  75. data/lib/backup/encryptor/gpg.rb +80 -0
  76. data/lib/backup/encryptor/open_ssl.rb +72 -0
  77. data/lib/backup/errors.rb +124 -0
  78. data/lib/backup/logger.rb +152 -0
  79. data/lib/backup/model.rb +386 -0
  80. data/lib/backup/notifier/base.rb +81 -0
  81. data/lib/backup/notifier/campfire.rb +168 -0
  82. data/lib/backup/notifier/hipchat.rb +99 -0
  83. data/lib/backup/notifier/mail.rb +206 -0
  84. data/lib/backup/notifier/presently.rb +88 -0
  85. data/lib/backup/notifier/prowl.rb +65 -0
  86. data/lib/backup/notifier/twitter.rb +70 -0
  87. data/lib/backup/package.rb +51 -0
  88. data/lib/backup/packager.rb +108 -0
  89. data/lib/backup/pipeline.rb +107 -0
  90. data/lib/backup/splitter.rb +75 -0
  91. data/lib/backup/storage/base.rb +119 -0
  92. data/lib/backup/storage/cloudfiles.rb +87 -0
  93. data/lib/backup/storage/cycler.rb +117 -0
  94. data/lib/backup/storage/dropbox.rb +181 -0
  95. data/lib/backup/storage/ftp.rb +119 -0
  96. data/lib/backup/storage/local.rb +82 -0
  97. data/lib/backup/storage/ninefold.rb +116 -0
  98. data/lib/backup/storage/rsync.rb +149 -0
  99. data/lib/backup/storage/s3.rb +94 -0
  100. data/lib/backup/storage/scp.rb +99 -0
  101. data/lib/backup/storage/sftp.rb +108 -0
  102. data/lib/backup/syncer/base.rb +42 -0
  103. data/lib/backup/syncer/cloud.rb +190 -0
  104. data/lib/backup/syncer/cloud_files.rb +56 -0
  105. data/lib/backup/syncer/rsync/base.rb +52 -0
  106. data/lib/backup/syncer/rsync/local.rb +53 -0
  107. data/lib/backup/syncer/rsync/pull.rb +38 -0
  108. data/lib/backup/syncer/rsync/push.rb +113 -0
  109. data/lib/backup/syncer/s3.rb +47 -0
  110. data/lib/backup/template.rb +46 -0
  111. data/lib/backup/version.rb +43 -0
  112. data/spec/archive_spec.rb +335 -0
  113. data/spec/cleaner_spec.rb +304 -0
  114. data/spec/cli/helpers_spec.rb +176 -0
  115. data/spec/cli/utility_spec.rb +363 -0
  116. data/spec/compressor/base_spec.rb +31 -0
  117. data/spec/compressor/bzip2_spec.rb +83 -0
  118. data/spec/compressor/gzip_spec.rb +83 -0
  119. data/spec/compressor/lzma_spec.rb +83 -0
  120. data/spec/compressor/pbzip2_spec.rb +124 -0
  121. data/spec/config_spec.rb +321 -0
  122. data/spec/configuration/base_spec.rb +35 -0
  123. data/spec/configuration/compressor/bzip2_spec.rb +29 -0
  124. data/spec/configuration/compressor/gzip_spec.rb +29 -0
  125. data/spec/configuration/compressor/lzma_spec.rb +29 -0
  126. data/spec/configuration/compressor/pbzip2_spec.rb +32 -0
  127. data/spec/configuration/database/base_spec.rb +17 -0
  128. data/spec/configuration/database/mongodb_spec.rb +56 -0
  129. data/spec/configuration/database/mysql_spec.rb +53 -0
  130. data/spec/configuration/database/postgresql_spec.rb +53 -0
  131. data/spec/configuration/database/redis_spec.rb +50 -0
  132. data/spec/configuration/database/riak_spec.rb +35 -0
  133. data/spec/configuration/encryptor/gpg_spec.rb +26 -0
  134. data/spec/configuration/encryptor/open_ssl_spec.rb +35 -0
  135. data/spec/configuration/notifier/base_spec.rb +32 -0
  136. data/spec/configuration/notifier/campfire_spec.rb +32 -0
  137. data/spec/configuration/notifier/hipchat_spec.rb +44 -0
  138. data/spec/configuration/notifier/mail_spec.rb +71 -0
  139. data/spec/configuration/notifier/presently_spec.rb +35 -0
  140. data/spec/configuration/notifier/prowl_spec.rb +29 -0
  141. data/spec/configuration/notifier/twitter_spec.rb +35 -0
  142. data/spec/configuration/storage/cloudfiles_spec.rb +41 -0
  143. data/spec/configuration/storage/dropbox_spec.rb +38 -0
  144. data/spec/configuration/storage/ftp_spec.rb +44 -0
  145. data/spec/configuration/storage/local_spec.rb +29 -0
  146. data/spec/configuration/storage/ninefold_spec.rb +32 -0
  147. data/spec/configuration/storage/rsync_spec.rb +41 -0
  148. data/spec/configuration/storage/s3_spec.rb +38 -0
  149. data/spec/configuration/storage/scp_spec.rb +41 -0
  150. data/spec/configuration/storage/sftp_spec.rb +41 -0
  151. data/spec/configuration/syncer/cloud_files_spec.rb +44 -0
  152. data/spec/configuration/syncer/rsync/base_spec.rb +33 -0
  153. data/spec/configuration/syncer/rsync/local_spec.rb +10 -0
  154. data/spec/configuration/syncer/rsync/pull_spec.rb +10 -0
  155. data/spec/configuration/syncer/rsync/push_spec.rb +43 -0
  156. data/spec/configuration/syncer/s3_spec.rb +38 -0
  157. data/spec/database/base_spec.rb +54 -0
  158. data/spec/database/mongodb_spec.rb +428 -0
  159. data/spec/database/mysql_spec.rb +335 -0
  160. data/spec/database/postgresql_spec.rb +278 -0
  161. data/spec/database/redis_spec.rb +260 -0
  162. data/spec/database/riak_spec.rb +108 -0
  163. data/spec/dependency_spec.rb +49 -0
  164. data/spec/encryptor/base_spec.rb +30 -0
  165. data/spec/encryptor/gpg_spec.rb +134 -0
  166. data/spec/encryptor/open_ssl_spec.rb +129 -0
  167. data/spec/errors_spec.rb +306 -0
  168. data/spec/logger_spec.rb +363 -0
  169. data/spec/model_spec.rb +649 -0
  170. data/spec/notifier/base_spec.rb +89 -0
  171. data/spec/notifier/campfire_spec.rb +199 -0
  172. data/spec/notifier/hipchat_spec.rb +188 -0
  173. data/spec/notifier/mail_spec.rb +280 -0
  174. data/spec/notifier/presently_spec.rb +181 -0
  175. data/spec/notifier/prowl_spec.rb +117 -0
  176. data/spec/notifier/twitter_spec.rb +132 -0
  177. data/spec/package_spec.rb +61 -0
  178. data/spec/packager_spec.rb +225 -0
  179. data/spec/pipeline_spec.rb +257 -0
  180. data/spec/spec_helper.rb +59 -0
  181. data/spec/splitter_spec.rb +120 -0
  182. data/spec/storage/base_spec.rb +160 -0
  183. data/spec/storage/cloudfiles_spec.rb +230 -0
  184. data/spec/storage/cycler_spec.rb +239 -0
  185. data/spec/storage/dropbox_spec.rb +370 -0
  186. data/spec/storage/ftp_spec.rb +247 -0
  187. data/spec/storage/local_spec.rb +235 -0
  188. data/spec/storage/ninefold_spec.rb +319 -0
  189. data/spec/storage/rsync_spec.rb +345 -0
  190. data/spec/storage/s3_spec.rb +221 -0
  191. data/spec/storage/scp_spec.rb +209 -0
  192. data/spec/storage/sftp_spec.rb +220 -0
  193. data/spec/syncer/base_spec.rb +22 -0
  194. data/spec/syncer/cloud_files_spec.rb +192 -0
  195. data/spec/syncer/rsync/base_spec.rb +118 -0
  196. data/spec/syncer/rsync/local_spec.rb +121 -0
  197. data/spec/syncer/rsync/pull_spec.rb +90 -0
  198. data/spec/syncer/rsync/push_spec.rb +327 -0
  199. data/spec/syncer/s3_spec.rb +192 -0
  200. data/spec/version_spec.rb +21 -0
  201. data/templates/cli/utility/archive +25 -0
  202. data/templates/cli/utility/compressor/bzip2 +7 -0
  203. data/templates/cli/utility/compressor/gzip +7 -0
  204. data/templates/cli/utility/compressor/lzma +7 -0
  205. data/templates/cli/utility/compressor/pbzip2 +7 -0
  206. data/templates/cli/utility/config +31 -0
  207. data/templates/cli/utility/database/mongodb +18 -0
  208. data/templates/cli/utility/database/mysql +21 -0
  209. data/templates/cli/utility/database/postgresql +17 -0
  210. data/templates/cli/utility/database/redis +16 -0
  211. data/templates/cli/utility/database/riak +11 -0
  212. data/templates/cli/utility/encryptor/gpg +12 -0
  213. data/templates/cli/utility/encryptor/openssl +9 -0
  214. data/templates/cli/utility/model.erb +23 -0
  215. data/templates/cli/utility/notifier/campfire +12 -0
  216. data/templates/cli/utility/notifier/hipchat +15 -0
  217. data/templates/cli/utility/notifier/mail +22 -0
  218. data/templates/cli/utility/notifier/presently +13 -0
  219. data/templates/cli/utility/notifier/prowl +11 -0
  220. data/templates/cli/utility/notifier/twitter +13 -0
  221. data/templates/cli/utility/splitter +7 -0
  222. data/templates/cli/utility/storage/cloud_files +22 -0
  223. data/templates/cli/utility/storage/dropbox +20 -0
  224. data/templates/cli/utility/storage/ftp +12 -0
  225. data/templates/cli/utility/storage/local +7 -0
  226. data/templates/cli/utility/storage/ninefold +9 -0
  227. data/templates/cli/utility/storage/rsync +11 -0
  228. data/templates/cli/utility/storage/s3 +19 -0
  229. data/templates/cli/utility/storage/scp +11 -0
  230. data/templates/cli/utility/storage/sftp +11 -0
  231. data/templates/cli/utility/syncer/cloud_files +48 -0
  232. data/templates/cli/utility/syncer/rsync_local +12 -0
  233. data/templates/cli/utility/syncer/rsync_pull +17 -0
  234. data/templates/cli/utility/syncer/rsync_push +17 -0
  235. data/templates/cli/utility/syncer/s3 +45 -0
  236. data/templates/general/links +11 -0
  237. data/templates/general/version.erb +2 -0
  238. data/templates/notifier/mail/failure.erb +9 -0
  239. data/templates/notifier/mail/success.erb +7 -0
  240. data/templates/notifier/mail/warning.erb +9 -0
  241. data/templates/storage/dropbox/authorization_url.erb +6 -0
  242. data/templates/storage/dropbox/authorized.erb +4 -0
  243. data/templates/storage/dropbox/cache_file_written.erb +10 -0
  244. metadata +311 -0
@@ -0,0 +1,168 @@
1
+ # encoding: utf-8
2
+
3
+ ##
4
+ # If the Ruby version of this process is 1.8.x or less
5
+ # then use the JSON gem. Otherwise if the current process is running
6
+ # Ruby 1.9.x or later then it is built in and we can load it from the Ruby core lib
7
+ if RUBY_VERSION < '1.9.0'
8
+ Backup::Dependency.load('json')
9
+ else
10
+ require 'json'
11
+ end
12
+
13
+ ##
14
+ # Load the HTTParty library from the gem
15
+ Backup::Dependency.load('httparty')
16
+
17
+ module Backup
18
+ module Notifier
19
+ class Campfire < Base
20
+
21
+ ##
22
+ # Campfire api authentication token
23
+ attr_accessor :api_token
24
+
25
+ ##
26
+ # Campfire account's subdomain
27
+ attr_accessor :subdomain
28
+
29
+ ##
30
+ # Campfire account's room id
31
+ attr_accessor :room_id
32
+
33
+ def initialize(model, &block)
34
+ super(model)
35
+
36
+ instance_eval(&block) if block_given?
37
+ end
38
+
39
+ private
40
+
41
+ ##
42
+ # Notify the user of the backup operation results.
43
+ # `status` indicates one of the following:
44
+ #
45
+ # `:success`
46
+ # : The backup completed successfully.
47
+ # : Notification will be sent if `on_success` was set to `true`
48
+ #
49
+ # `:warning`
50
+ # : The backup completed successfully, but warnings were logged
51
+ # : Notification will be sent, including a copy of the current
52
+ # : backup log, if `on_warning` was set to `true`
53
+ #
54
+ # `:failure`
55
+ # : The backup operation failed.
56
+ # : Notification will be sent, including the Exception which caused
57
+ # : the failure, the Exception's backtrace, a copy of the current
58
+ # : backup log and other information if `on_failure` was set to `true`
59
+ #
60
+ def notify!(status)
61
+ name = case status
62
+ when :success then 'Success'
63
+ when :warning then 'Warning'
64
+ when :failure then 'Failure'
65
+ end
66
+ message = "[Backup::%s] #{@model.label} (#{@model.trigger})" % name
67
+ send_message(message)
68
+ end
69
+
70
+ ##
71
+ # Creates a new Campfire::Interface object and passes in the
72
+ # campfire clients "room_id", "subdomain" and "api_token". Using this object
73
+ # the provided "message" will be sent to the desired Campfire chat room
74
+ def send_message(message)
75
+ room = Interface.room(room_id, subdomain, api_token)
76
+ room.message(message)
77
+ end
78
+
79
+ ##
80
+ # The Campfire::Interface acts as the Interface for the Campfire class.
81
+ # It uses the HTTParty library and the Campfire::Room class to communicate
82
+ # with the Campfire rooms. HTTParty provides the Campfire::Interface with the methods
83
+ # necessary to communicate (inside the HTTParty module) such as the class methods:
84
+ # * post
85
+ # * base_uri
86
+ # * basic_auth
87
+ class Interface
88
+ include HTTParty
89
+
90
+ ##
91
+ # We communicate using the JSON data format
92
+ headers 'Content-Type' => 'application/json'
93
+
94
+ ##
95
+ # Instantiates a new Campfire::Room object with
96
+ # the provided arguments and returns this object
97
+ def self.room(room_id, subdomain, api_token)
98
+ Room.new(room_id, subdomain, api_token)
99
+ end
100
+ end
101
+
102
+ ##
103
+ # The Campfire::Room acts as a model for an actual room on the Campfire service.
104
+ # And it uses the Campfire::Interface's (HTTParty) class methods to communicate based
105
+ # on the provided parameters (room_id, subdomain and api_token)
106
+ class Room
107
+
108
+ ##
109
+ # Campfire api authentication api_token
110
+ attr_accessor :api_token
111
+
112
+ ##
113
+ # Campfire account's subdomain
114
+ attr_accessor :subdomain
115
+
116
+ ##
117
+ # Campfire account's room id
118
+ attr_accessor :room_id
119
+
120
+ ##
121
+ # Instantiates a new Campfire::Room object and sets all the
122
+ # necessary arguments (@room_id, @subdomain, @api_token)
123
+ def initialize(room_id, subdomain, api_token)
124
+ @room_id = room_id
125
+ @subdomain = subdomain
126
+ @api_token = api_token
127
+ end
128
+
129
+ ##
130
+ # Wrapper method for the #send_message (private) method
131
+ def message(message)
132
+ send_message(message)
133
+ end
134
+
135
+ private
136
+
137
+ ##
138
+ # Takes a "message" as argument, the "type" defaults to "Textmessage".
139
+ # This method builds up a POST request with the necessary params (serialized to JSON format)
140
+ # and sends it to the Campfire service in order to submit the message
141
+ def send_message(message, type = 'Textmessage')
142
+ post 'speak', :body => {
143
+ :message => {
144
+ :body => message,
145
+ :type => type
146
+ }
147
+ }.to_json
148
+ end
149
+
150
+ ##
151
+ # Builds/sets up the Campfire::Interface attributes and submits
152
+ # the POST request that was built in the #send_message (private) method
153
+ def post(action, options = {})
154
+ Interface.base_uri("https://#{subdomain}.campfirenow.com")
155
+ Interface.basic_auth(api_token, 'x')
156
+ Interface.post(room_url_for(action), options)
157
+ end
158
+
159
+ ##
160
+ # Returns the url for the specified room (in JSON format)
161
+ def room_url_for(action)
162
+ "/room/#{room_id}/#{action}.json"
163
+ end
164
+ end
165
+
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,99 @@
1
+ # encoding: utf-8
2
+
3
+ if RUBY_VERSION < '1.9.0'
4
+ Backup::Dependency.load('json')
5
+ else
6
+ require 'json'
7
+ end
8
+
9
+ # Load HTTParty
10
+ Backup::Dependency.load('hipchat')
11
+
12
+ module Backup
13
+ module Notifier
14
+ class Hipchat < Base
15
+
16
+ ##
17
+ # The Hipchat API token
18
+ attr_accessor :token
19
+
20
+ ##
21
+ # Who the notification should appear from
22
+ attr_accessor :from
23
+
24
+ ##
25
+ # The rooms that should be notified
26
+ attr_accessor :rooms_notified
27
+
28
+ ##
29
+ # Notify users in the room
30
+ attr_accessor :notify_users
31
+
32
+ ##
33
+ # The background color of a success message.
34
+ # One of :yellow, :red, :green, :purple, or :random. (default: yellow)
35
+ attr_accessor :success_color
36
+
37
+ ##
38
+ # The background color of a warning message.
39
+ # One of :yellow, :red, :green, :purple, or :random. (default: yellow)
40
+ attr_accessor :warning_color
41
+
42
+ ##
43
+ # The background color of an error message.
44
+ # One of :yellow, :red, :green, :purple, or :random. (default: yellow)
45
+ attr_accessor :failure_color
46
+
47
+ def initialize(model, &block)
48
+ super(model)
49
+
50
+ @notify_users ||= false
51
+ @rooms_notified ||= []
52
+ @success_color ||= 'yellow'
53
+ @warning_color ||= 'yellow'
54
+ @failure_color ||= 'yellow'
55
+
56
+ instance_eval(&block) if block_given?
57
+ end
58
+
59
+ private
60
+
61
+ ##
62
+ # Notify the user of the backup operation results.
63
+ # `status` indicates one of the following:
64
+ #
65
+ # `:success`
66
+ # : The backup completed successfully.
67
+ # : Notification will be sent if `on_success` was set to `true`
68
+ #
69
+ # `:warning`
70
+ # : The backup completed successfully, but warnings were logged
71
+ # : Notification will be sent, including a copy of the current
72
+ # : backup log, if `on_warning` was set to `true`
73
+ #
74
+ # `:failure`
75
+ # : The backup operation failed.
76
+ # : Notification will be sent, including the Exception which caused
77
+ # : the failure, the Exception's backtrace, a copy of the current
78
+ # : backup log and other information if `on_failure` was set to `true`
79
+ #
80
+ def notify!(status)
81
+ name, color = case status
82
+ when :success then ['Success', success_color]
83
+ when :warning then ['Warning', warning_color]
84
+ when :failure then ['Failure', failure_color]
85
+ end
86
+ message = "[Backup::%s] #{@model.label} (#{@model.trigger})" % name
87
+ send_message(message, color)
88
+ end
89
+
90
+ def send_message(msg, color)
91
+ client = HipChat::Client.new(token)
92
+ [rooms_notified].flatten.each do |room|
93
+ client[room].send(from, msg, :color => color, :notify => notify_users)
94
+ end
95
+ end
96
+
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,206 @@
1
+ # encoding: utf-8
2
+
3
+ ##
4
+ # Only load the Mail gem and Erb library when using Mail notifications
5
+ Backup::Dependency.load('mail')
6
+
7
+ module Backup
8
+ module Notifier
9
+ class Mail < Base
10
+
11
+ ##
12
+ # Mail delivery method to be used by the Mail gem.
13
+ # Supported methods:
14
+ #
15
+ # `:smtp` [::Mail::SMTP] (default)
16
+ # : Settings used only by this method:
17
+ # : `address`, `port`, `domain`, `user_name`, `password`
18
+ # : `authentication`, `enable_starttls_auto`, `openssl_verify_mode`
19
+ #
20
+ # `:sendmail` [::Mail::Sendmail]
21
+ # : Settings used only by this method:
22
+ # : `sendmail`, `sendmail_args`
23
+ #
24
+ # `:exim` [::Mail::Exim]
25
+ # : Settings used only by this method:
26
+ # : `exim`, `exim_args`
27
+ #
28
+ # `:file` [::Mail::FileDelivery]
29
+ # : Settings used only by this method:
30
+ # : `mail_folder`
31
+ #
32
+ attr_accessor :delivery_method
33
+
34
+ ##
35
+ # Sender and Receiver email addresses
36
+ # Examples:
37
+ # sender - my.email.address@gmail.com
38
+ # receiver - your.email.address@gmail.com
39
+ attr_accessor :from, :to
40
+
41
+ ##
42
+ # The address to use
43
+ # Example: smtp.gmail.com
44
+ attr_accessor :address
45
+
46
+ ##
47
+ # The port to connect to
48
+ # Example: 587
49
+ attr_accessor :port
50
+
51
+ ##
52
+ # Your domain (if applicable)
53
+ # Example: mydomain.com
54
+ attr_accessor :domain
55
+
56
+ ##
57
+ # Username and Password (sender email's credentials)
58
+ # Examples:
59
+ # user_name - meskyanichi
60
+ # password - my_secret_password
61
+ attr_accessor :user_name, :password
62
+
63
+ ##
64
+ # Authentication type
65
+ # Example: plain
66
+ attr_accessor :authentication
67
+
68
+ ##
69
+ # Automatically set TLS
70
+ # Example: true
71
+ attr_accessor :enable_starttls_auto
72
+
73
+ ##
74
+ # OpenSSL Verify Mode
75
+ # Example: none - Only use this option for a self-signed and/or wildcard certificate
76
+ attr_accessor :openssl_verify_mode
77
+
78
+ ##
79
+ # When using the `:sendmail` `delivery_method` option,
80
+ # this may be used to specify the absolute path to `sendmail` (if needed)
81
+ # Example: '/usr/sbin/sendmail'
82
+ attr_accessor :sendmail
83
+
84
+ ##
85
+ # Optional arguments to pass to `sendmail`
86
+ # Note that this will override the defaults set by the Mail gem (currently: '-i -t')
87
+ # So, if set here, be sure to set all the arguments you require.
88
+ # Example: '-i -t -X/tmp/traffic.log'
89
+ attr_accessor :sendmail_args
90
+
91
+ ##
92
+ # When using the `:exim` `delivery_method` option,
93
+ # this may be used to specify the absolute path to `exim` (if needed)
94
+ # Example: '/usr/sbin/exim'
95
+ attr_accessor :exim
96
+
97
+ ##
98
+ # Optional arguments to pass to `exim`
99
+ # Note that this will override the defaults set by the Mail gem (currently: '-i -t')
100
+ # So, if set here, be sure to set all the arguments you require.
101
+ # Example: '-i -t -X/tmp/traffic.log'
102
+ attr_accessor :exim_args
103
+
104
+ ##
105
+ # Folder where mail will be kept when using the `:file` `delivery_method` option.
106
+ # Default location is '$HOME/Backup/emails'
107
+ # Example: '/tmp/test-mails'
108
+ attr_accessor :mail_folder
109
+
110
+ def initialize(model, &block)
111
+ super(model)
112
+
113
+ instance_eval(&block) if block_given?
114
+ end
115
+
116
+ private
117
+
118
+ ##
119
+ # Notify the user of the backup operation results.
120
+ # `status` indicates one of the following:
121
+ #
122
+ # `:success`
123
+ # : The backup completed successfully.
124
+ # : Notification will be sent if `on_success` was set to `true`
125
+ #
126
+ # `:warning`
127
+ # : The backup completed successfully, but warnings were logged
128
+ # : Notification will be sent, including a copy of the current
129
+ # : backup log, if `on_warning` was set to `true`
130
+ #
131
+ # `:failure`
132
+ # : The backup operation failed.
133
+ # : Notification will be sent, including the Exception which caused
134
+ # : the failure, the Exception's backtrace, a copy of the current
135
+ # : backup log and other information if `on_failure` was set to `true`
136
+ #
137
+ def notify!(status)
138
+ name, send_log =
139
+ case status
140
+ when :success then [ 'Success', false ]
141
+ when :warning then [ 'Warning', true ]
142
+ when :failure then [ 'Failure', true ]
143
+ end
144
+
145
+ email = new_email
146
+ email.subject = "[Backup::%s] #{@model.label} (#{@model.trigger})" % name
147
+ email.body = @template.result('notifier/mail/%s.erb' % status.to_s)
148
+
149
+ if send_log
150
+ email.convert_to_multipart
151
+ email.attachments["#{@model.time}.#{@model.trigger}.log"] = {
152
+ :mime_type => 'text/plain;',
153
+ :content => Logger.messages.join("\n")
154
+ }
155
+ end
156
+
157
+ email.deliver!
158
+ end
159
+
160
+ ##
161
+ # Configures the Mail gem by setting the defaults.
162
+ # Creates and returns a new email, based on the @delivery_method used.
163
+ def new_email
164
+ method = %w{ smtp sendmail exim file test }.
165
+ index(@delivery_method.to_s) ? @delivery_method.to_s : 'smtp'
166
+
167
+ options =
168
+ case method
169
+ when 'smtp'
170
+ { :address => @address,
171
+ :port => @port,
172
+ :domain => @domain,
173
+ :user_name => @user_name,
174
+ :password => @password,
175
+ :authentication => @authentication,
176
+ :enable_starttls_auto => @enable_starttls_auto,
177
+ :openssl_verify_mode => @openssl_verify_mode }
178
+ when 'sendmail'
179
+ opts = {}
180
+ opts.merge!(:location => File.expand_path(@sendmail)) if @sendmail
181
+ opts.merge!(:arguments => @sendmail_args) if @sendmail_args
182
+ opts
183
+ when 'exim'
184
+ opts = {}
185
+ opts.merge!(:location => File.expand_path(@exim)) if @exim
186
+ opts.merge!(:arguments => @exim_args) if @exim_args
187
+ opts
188
+ when 'file'
189
+ @mail_folder ||= File.join(Config.root_path, 'emails')
190
+ { :location => File.expand_path(@mail_folder) }
191
+ when 'test' then {}
192
+ end
193
+
194
+ ::Mail.defaults do
195
+ delivery_method method.to_sym, options
196
+ end
197
+
198
+ email = ::Mail.new
199
+ email.to = @to
200
+ email.from = @from
201
+ email
202
+ end
203
+
204
+ end
205
+ end
206
+ end