backedup 5.0.0.beta.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +19 -0
  3. data/README.md +33 -0
  4. data/bin/backedup +5 -0
  5. data/bin/docker_test +24 -0
  6. data/lib/backup/archive.rb +169 -0
  7. data/lib/backup/binder.rb +18 -0
  8. data/lib/backup/cleaner.rb +112 -0
  9. data/lib/backup/cli.rb +370 -0
  10. data/lib/backup/cloud_io/base.rb +38 -0
  11. data/lib/backup/cloud_io/cloud_files.rb +296 -0
  12. data/lib/backup/cloud_io/gcs.rb +121 -0
  13. data/lib/backup/cloud_io/s3.rb +253 -0
  14. data/lib/backup/cloud_io/swift.rb +96 -0
  15. data/lib/backup/compressor/base.rb +32 -0
  16. data/lib/backup/compressor/bzip2.rb +35 -0
  17. data/lib/backup/compressor/custom.rb +49 -0
  18. data/lib/backup/compressor/gzip.rb +73 -0
  19. data/lib/backup/compressor/pbzip2.rb +45 -0
  20. data/lib/backup/config/dsl.rb +102 -0
  21. data/lib/backup/config/helpers.rb +137 -0
  22. data/lib/backup/config.rb +118 -0
  23. data/lib/backup/database/base.rb +86 -0
  24. data/lib/backup/database/mongodb.rb +186 -0
  25. data/lib/backup/database/mysql.rb +191 -0
  26. data/lib/backup/database/openldap.rb +93 -0
  27. data/lib/backup/database/postgresql.rb +164 -0
  28. data/lib/backup/database/redis.rb +176 -0
  29. data/lib/backup/database/riak.rb +79 -0
  30. data/lib/backup/database/sqlite.rb +55 -0
  31. data/lib/backup/encryptor/base.rb +27 -0
  32. data/lib/backup/encryptor/gpg.rb +737 -0
  33. data/lib/backup/encryptor/open_ssl.rb +74 -0
  34. data/lib/backup/errors.rb +53 -0
  35. data/lib/backup/logger/console.rb +48 -0
  36. data/lib/backup/logger/fog_adapter.rb +25 -0
  37. data/lib/backup/logger/logfile.rb +131 -0
  38. data/lib/backup/logger/syslog.rb +114 -0
  39. data/lib/backup/logger.rb +197 -0
  40. data/lib/backup/model.rb +472 -0
  41. data/lib/backup/notifier/base.rb +126 -0
  42. data/lib/backup/notifier/campfire.rb +61 -0
  43. data/lib/backup/notifier/command.rb +99 -0
  44. data/lib/backup/notifier/datadog.rb +104 -0
  45. data/lib/backup/notifier/flowdock.rb +99 -0
  46. data/lib/backup/notifier/hipchat.rb +116 -0
  47. data/lib/backup/notifier/http_post.rb +114 -0
  48. data/lib/backup/notifier/mail.rb +232 -0
  49. data/lib/backup/notifier/nagios.rb +65 -0
  50. data/lib/backup/notifier/pagerduty.rb +79 -0
  51. data/lib/backup/notifier/prowl.rb +68 -0
  52. data/lib/backup/notifier/pushover.rb +71 -0
  53. data/lib/backup/notifier/ses.rb +123 -0
  54. data/lib/backup/notifier/slack.rb +147 -0
  55. data/lib/backup/notifier/twitter.rb +55 -0
  56. data/lib/backup/notifier/zabbix.rb +60 -0
  57. data/lib/backup/package.rb +51 -0
  58. data/lib/backup/packager.rb +106 -0
  59. data/lib/backup/pipeline.rb +120 -0
  60. data/lib/backup/splitter.rb +73 -0
  61. data/lib/backup/storage/base.rb +66 -0
  62. data/lib/backup/storage/cloud_files.rb +156 -0
  63. data/lib/backup/storage/cycler.rb +70 -0
  64. data/lib/backup/storage/dropbox.rb +206 -0
  65. data/lib/backup/storage/ftp.rb +116 -0
  66. data/lib/backup/storage/gcs.rb +93 -0
  67. data/lib/backup/storage/local.rb +61 -0
  68. data/lib/backup/storage/qiniu.rb +65 -0
  69. data/lib/backup/storage/rsync.rb +246 -0
  70. data/lib/backup/storage/s3.rb +155 -0
  71. data/lib/backup/storage/scp.rb +65 -0
  72. data/lib/backup/storage/sftp.rb +80 -0
  73. data/lib/backup/storage/swift.rb +124 -0
  74. data/lib/backup/storage/webdav.rb +102 -0
  75. data/lib/backup/syncer/base.rb +67 -0
  76. data/lib/backup/syncer/cloud/base.rb +176 -0
  77. data/lib/backup/syncer/cloud/cloud_files.rb +81 -0
  78. data/lib/backup/syncer/cloud/local_file.rb +97 -0
  79. data/lib/backup/syncer/cloud/s3.rb +109 -0
  80. data/lib/backup/syncer/rsync/base.rb +50 -0
  81. data/lib/backup/syncer/rsync/local.rb +27 -0
  82. data/lib/backup/syncer/rsync/pull.rb +47 -0
  83. data/lib/backup/syncer/rsync/push.rb +201 -0
  84. data/lib/backup/template.rb +41 -0
  85. data/lib/backup/utilities.rb +234 -0
  86. data/lib/backup/version.rb +3 -0
  87. data/lib/backup.rb +145 -0
  88. data/templates/cli/archive +28 -0
  89. data/templates/cli/compressor/bzip2 +4 -0
  90. data/templates/cli/compressor/custom +7 -0
  91. data/templates/cli/compressor/gzip +4 -0
  92. data/templates/cli/config +123 -0
  93. data/templates/cli/databases/mongodb +15 -0
  94. data/templates/cli/databases/mysql +18 -0
  95. data/templates/cli/databases/openldap +24 -0
  96. data/templates/cli/databases/postgresql +16 -0
  97. data/templates/cli/databases/redis +16 -0
  98. data/templates/cli/databases/riak +17 -0
  99. data/templates/cli/databases/sqlite +11 -0
  100. data/templates/cli/encryptor/gpg +27 -0
  101. data/templates/cli/encryptor/openssl +9 -0
  102. data/templates/cli/model +26 -0
  103. data/templates/cli/notifier/zabbix +15 -0
  104. data/templates/cli/notifiers/campfire +12 -0
  105. data/templates/cli/notifiers/command +32 -0
  106. data/templates/cli/notifiers/datadog +57 -0
  107. data/templates/cli/notifiers/flowdock +16 -0
  108. data/templates/cli/notifiers/hipchat +16 -0
  109. data/templates/cli/notifiers/http_post +32 -0
  110. data/templates/cli/notifiers/mail +24 -0
  111. data/templates/cli/notifiers/nagios +13 -0
  112. data/templates/cli/notifiers/pagerduty +12 -0
  113. data/templates/cli/notifiers/prowl +11 -0
  114. data/templates/cli/notifiers/pushover +11 -0
  115. data/templates/cli/notifiers/ses +15 -0
  116. data/templates/cli/notifiers/slack +22 -0
  117. data/templates/cli/notifiers/twitter +13 -0
  118. data/templates/cli/splitter +7 -0
  119. data/templates/cli/storages/cloud_files +11 -0
  120. data/templates/cli/storages/dropbox +20 -0
  121. data/templates/cli/storages/ftp +13 -0
  122. data/templates/cli/storages/gcs +8 -0
  123. data/templates/cli/storages/local +8 -0
  124. data/templates/cli/storages/qiniu +12 -0
  125. data/templates/cli/storages/rsync +17 -0
  126. data/templates/cli/storages/s3 +16 -0
  127. data/templates/cli/storages/scp +15 -0
  128. data/templates/cli/storages/sftp +15 -0
  129. data/templates/cli/storages/swift +19 -0
  130. data/templates/cli/storages/webdav +13 -0
  131. data/templates/cli/syncers/cloud_files +22 -0
  132. data/templates/cli/syncers/rsync_local +20 -0
  133. data/templates/cli/syncers/rsync_pull +28 -0
  134. data/templates/cli/syncers/rsync_push +28 -0
  135. data/templates/cli/syncers/s3 +27 -0
  136. data/templates/general/links +3 -0
  137. data/templates/general/version.erb +2 -0
  138. data/templates/notifier/mail/failure.erb +16 -0
  139. data/templates/notifier/mail/success.erb +16 -0
  140. data/templates/notifier/mail/warning.erb +16 -0
  141. data/templates/storage/dropbox/authorization_url.erb +6 -0
  142. data/templates/storage/dropbox/authorized.erb +4 -0
  143. data/templates/storage/dropbox/cache_file_written.erb +10 -0
  144. metadata +1255 -0
@@ -0,0 +1,232 @@
1
+ require "mail"
2
+
3
+ module Backup
4
+ module Notifier
5
+ class Mail < Base
6
+ ##
7
+ # Mail delivery method to be used by the Mail gem.
8
+ #
9
+ # Supported methods:
10
+ #
11
+ # [:smtp - ::Mail::SMTP (default)]
12
+ # Settings used by this method:
13
+ # {#address}, {#port}, {#domain}, {#user_name}, {#password},
14
+ # {#authentication}, {#encryption}, {#openssl_verify_mode}
15
+ #
16
+ # [:sendmail - ::Mail::Sendmail]
17
+ # Settings used by this method:
18
+ # {#sendmail_args}
19
+ #
20
+ # [:exim - ::Mail::Exim]
21
+ # Settings used by this method:
22
+ # {#exim_args}
23
+ #
24
+ # [:file - ::Mail::FileDelivery]
25
+ # Settings used by this method:
26
+ # {#mail_folder}
27
+ #
28
+ attr_accessor :delivery_method
29
+
30
+ ##
31
+ # Sender Email Address
32
+ attr_accessor :from
33
+
34
+ ##
35
+ # Receiver Email Address
36
+ attr_accessor :to
37
+
38
+ ##
39
+ # CC receiver Email Address
40
+ attr_accessor :cc
41
+
42
+ ##
43
+ # BCC receiver Email Address
44
+ attr_accessor :bcc
45
+
46
+ ##
47
+ # Set reply to email address
48
+ attr_accessor :reply_to
49
+
50
+ ##
51
+ # SMTP Server Address
52
+ attr_accessor :address
53
+
54
+ ##
55
+ # SMTP Server Port
56
+ attr_accessor :port
57
+
58
+ ##
59
+ # Your domain (if applicable)
60
+ attr_accessor :domain
61
+
62
+ ##
63
+ # SMTP Server Username (sender email's credentials)
64
+ attr_accessor :user_name
65
+
66
+ ##
67
+ # SMTP Server Password (sender email's credentials)
68
+ attr_accessor :password
69
+
70
+ ##
71
+ # Authentication type
72
+ #
73
+ # Acceptable values: +:plain+, +:login+, +:cram_md5+
74
+ attr_accessor :authentication
75
+
76
+ ##
77
+ # Set the method of encryption to be used for the +SMTP+ connection.
78
+ #
79
+ # [:starttls (default)]
80
+ # Use +STARTTLS+ to upgrade the connection to a +SSL/TLS+ connection.
81
+ #
82
+ # [:tls or :ssl]
83
+ # Use a +SSL/TLS+ connection.
84
+ #
85
+ # [:none]
86
+ # No encryption will be used.
87
+ attr_accessor :encryption
88
+
89
+ ##
90
+ # OpenSSL Verify Mode
91
+ #
92
+ # Valid modes: +:none+, +:peer+, +:client_once+, +:fail_if_no_peer_cert+
93
+ # See +OpenSSL::SSL+ for details.
94
+ #
95
+ # Use +:none+ for a self-signed and/or wildcard certificate
96
+ attr_accessor :openssl_verify_mode
97
+
98
+ ##
99
+ # Optional arguments to pass to `sendmail`
100
+ #
101
+ # Note that this will override the defaults set by the Mail gem
102
+ # (currently: '-i'). So, if set here, be sure to set all the arguments
103
+ # you require.
104
+ #
105
+ # Example: '-i -X/tmp/traffic.log'
106
+ attr_accessor :sendmail_args
107
+
108
+ ##
109
+ # Optional arguments to pass to `exim`
110
+ #
111
+ # Note that this will override the defaults set by the Mail gem
112
+ # (currently: '-i -t') So, if set here, be sure to set all the arguments
113
+ # you require.
114
+ #
115
+ # Example: '-i -t -X/tmp/traffic.log'
116
+ attr_accessor :exim_args
117
+
118
+ ##
119
+ # Folder where mail will be kept when using the `:file` `delivery_method`.
120
+ #
121
+ # Default location is '$HOME/Backup/emails'
122
+ attr_accessor :mail_folder
123
+
124
+ ##
125
+ # Array of statuses for which the log file should be attached.
126
+ #
127
+ # Available statuses are: `:success`, `:warning` and `:failure`.
128
+ # Default: [:warning, :failure]
129
+ attr_accessor :send_log_on
130
+
131
+ def initialize(model, &block)
132
+ super
133
+ instance_eval(&block) if block_given?
134
+
135
+ @send_log_on ||= [:warning, :failure]
136
+ @encryption ||= :starttls
137
+ end
138
+
139
+ private
140
+
141
+ ##
142
+ # Notify the user of the backup operation results.
143
+ #
144
+ # `status` indicates one of the following:
145
+ #
146
+ # `:success`
147
+ # : The backup completed successfully.
148
+ # : Notification will be sent if `on_success` is `true`.
149
+ #
150
+ # `:warning`
151
+ # : The backup completed successfully, but warnings were logged.
152
+ # : Notification will be sent, including a copy of the current
153
+ # : backup log, if `on_warning` or `on_success` is `true`.
154
+ #
155
+ # `:failure`
156
+ # : The backup operation failed.
157
+ # : Notification will be sent, including a copy of the current
158
+ # : backup log, if `on_failure` is `true`.
159
+ #
160
+ def notify!(status)
161
+ email = new_email
162
+ email.subject = message.call(model, status: status_data_for(status))
163
+
164
+ send_log = send_log_on.include?(status)
165
+ template = Backup::Template.new(model: model, send_log: send_log)
166
+ email.body = template.result(sprintf("notifier/mail/%s.erb", status.to_s))
167
+
168
+ if send_log
169
+ email.convert_to_multipart
170
+ email.attachments["#{model.time}.#{model.trigger}.log"] = {
171
+ mime_type: "text/plain;",
172
+ content: Logger.messages.map(&:formatted_lines).flatten.join("\n")
173
+ }
174
+ end
175
+
176
+ email.deliver! # raise error if unsuccessful
177
+ end
178
+
179
+ ##
180
+ # Configures the Mail gem by setting the defaults.
181
+ # Creates and returns a new email, based on the @delivery_method used.
182
+ def new_email
183
+ method = %w[smtp sendmail exim file test]
184
+ .index(@delivery_method.to_s) ? @delivery_method.to_s : "smtp"
185
+
186
+ options =
187
+ case method
188
+ when "smtp"
189
+ opts = {
190
+ address: @address,
191
+ port: @port,
192
+ user_name: @user_name,
193
+ password: @password,
194
+ authentication: @authentication,
195
+ enable_starttls_auto: @encryption == :starttls,
196
+ openssl_verify_mode: @openssl_verify_mode,
197
+ ssl: @encryption == :ssl,
198
+ tls: @encryption == :tls
199
+ }
200
+
201
+ # Don't override default domain setting if domain not applicable.
202
+ # ref https://github.com/mikel/mail/blob/2.6.3/lib/mail/network/delivery_methods/smtp.rb#L82
203
+ opts[:domain] = @domain if @domain
204
+ opts
205
+ when "sendmail"
206
+ opts = {}
207
+ opts[:location] = utility(:sendmail)
208
+ opts[:arguments] = @sendmail_args if @sendmail_args
209
+ opts
210
+ when "exim"
211
+ opts = {}
212
+ opts[:location] = utility(:exim)
213
+ opts[:arguments] = @exim_args if @exim_args
214
+ opts
215
+ when "file"
216
+ @mail_folder ||= File.join(Config.root_path, "emails")
217
+ { location: File.expand_path(@mail_folder) }
218
+ when "test" then {}
219
+ end
220
+
221
+ email = ::Mail.new
222
+ email.delivery_method method.to_sym, options
223
+ email.to = to
224
+ email.from = from
225
+ email.cc = cc
226
+ email.bcc = bcc
227
+ email.reply_to = reply_to
228
+ email
229
+ end
230
+ end
231
+ end
232
+ end
@@ -0,0 +1,65 @@
1
+ module Backup
2
+ module Notifier
3
+ class Nagios < Base
4
+ ##
5
+ # Host of Nagios server to notify on backup completion.
6
+ attr_accessor :nagios_host
7
+
8
+ ##
9
+ # Port of Nagios server to notify on backup completion.
10
+ attr_accessor :nagios_port
11
+
12
+ ##
13
+ # Nagios nrpe configuration file.
14
+ attr_accessor :send_nsca_cfg
15
+
16
+ ##
17
+ # Name of the Nagios service for the backup check.
18
+ attr_accessor :service_name
19
+
20
+ ##
21
+ # Host name in Nagios for the backup check.
22
+ attr_accessor :service_host
23
+
24
+ def initialize(model, &block)
25
+ super
26
+ instance_eval(&block) if block_given?
27
+
28
+ @nagios_host ||= Config.hostname
29
+ @nagios_port ||= 5667
30
+ @send_nsca_cfg ||= "/etc/nagios/send_nsca.cfg"
31
+ @service_name ||= "Backup #{model.trigger}"
32
+ @service_host ||= Config.hostname
33
+ end
34
+
35
+ private
36
+
37
+ ##
38
+ # Notify the user of the backup operation results.
39
+ #
40
+ # `status` indicates one of the following:
41
+ #
42
+ # `:success`
43
+ # : The backup completed successfully.
44
+ # : Notification will be sent if `on_success` is `true`.
45
+ #
46
+ # `:warning`
47
+ # : The backup completed successfully, but warnings were logged.
48
+ # : Notification will be sent if `on_warning` or `on_success` is `true`.
49
+ #
50
+ # `:failure`
51
+ # : The backup operation failed.
52
+ # : Notification will be sent if `on_warning` or `on_success` is `true`.
53
+ #
54
+ def notify!(status)
55
+ send_message(message.call(model, status: status_data_for(status)))
56
+ end
57
+
58
+ def send_message(message)
59
+ cmd = "#{utility(:send_nsca)} -H '#{nagios_host}' -p '#{nagios_port}' -c '#{send_nsca_cfg}'"
60
+ msg = [service_host, service_name, model.exit_status, message].join("\t")
61
+ run("echo '#{msg}' | #{cmd}")
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,79 @@
1
+ require "pagerduty"
2
+
3
+ module Backup
4
+ module Notifier
5
+ class PagerDuty < Base
6
+ ##
7
+ # PagerDuty Service API Key. Should be a 32 character hex string.
8
+ attr_accessor :service_key
9
+
10
+ ##
11
+ # Determines if a backup with a warning should resolve an incident rather
12
+ # than trigger one.
13
+ #
14
+ # Defaults to false.
15
+ attr_accessor :resolve_on_warning
16
+
17
+ def initialize(mode, &block)
18
+ super
19
+ instance_eval(&block) if block_given?
20
+
21
+ @resolve_on_warning ||= false
22
+ end
23
+
24
+ private
25
+
26
+ ##
27
+ # Trigger or resolve a PagerDuty incident for this model
28
+ #
29
+ # `status` indicates one of the following:
30
+ #
31
+ # `:success`
32
+ # : The backup completed successfully.
33
+ # : The incident will be resolved if `on_success` is `true`.
34
+ #
35
+ # `:warning`
36
+ # : The backup completed successfully, but warnings were logged.
37
+ # : An incident will be triggered if `on_warning` or `on_success` is `true`.
38
+ #
39
+ # `:failure`
40
+ # : The backup operation failed.
41
+ # : An incident will be triggered if `on_failure` is `true`.
42
+ #
43
+ def notify!(status)
44
+ incident_description = "Backup - #{model.label}"
45
+ incident_key = "backup/#{model.trigger}"
46
+ incident_details = {
47
+ incident_key: incident_key,
48
+ details: {
49
+ trigger: model.trigger,
50
+ label: model.label,
51
+ started_at: model.started_at,
52
+ finished_at: model.finished_at,
53
+ duration: model.duration,
54
+ status: status,
55
+ exception: model.exception
56
+ }
57
+ }
58
+
59
+ event_type = case status
60
+ when :success then :resolve
61
+ when :warning then resolve_on_warning ? :resolve : :trigger
62
+ when :failure then :trigger
63
+ end
64
+
65
+ case event_type
66
+ when :trigger
67
+ pagerduty.trigger(incident_description, incident_details)
68
+ when :resolve
69
+ incident = pagerduty.get_incident(incident_key)
70
+ incident.resolve(incident_description, incident_details)
71
+ end
72
+ end
73
+
74
+ def pagerduty
75
+ @pagerduty ||= Pagerduty.new(service_key)
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,68 @@
1
+ require "uri"
2
+
3
+ module Backup
4
+ module Notifier
5
+ class Prowl < Base
6
+ ##
7
+ # Application name
8
+ # Tell something like your server name. Example: "Server1 Backup"
9
+ attr_accessor :application
10
+
11
+ ##
12
+ # API-Key
13
+ # Create a Prowl account and request an API key on prowlapp.com.
14
+ attr_accessor :api_key
15
+
16
+ def initialize(model, &block)
17
+ @message =
18
+ lambda do |m, _|
19
+ "#{m.label} (#{m.trigger})"
20
+ end
21
+
22
+ super
23
+
24
+ instance_eval(&block) if block_given?
25
+ end
26
+
27
+ private
28
+
29
+ ##
30
+ # Notify the user of the backup operation results.
31
+ #
32
+ # `status` indicates one of the following:
33
+ #
34
+ # `:success`
35
+ # : The backup completed successfully.
36
+ # : Notification will be sent if `on_success` is `true`.
37
+ #
38
+ # `:warning`
39
+ # : The backup completed successfully, but warnings were logged.
40
+ # : Notification will be sent if `on_warning` or `on_success` is `true`.
41
+ #
42
+ # `:failure`
43
+ # : The backup operation failed.
44
+ # : Notification will be sent if `on_warning` or `on_success` is `true`.
45
+ #
46
+ def notify!(status)
47
+ send_message(status)
48
+ end
49
+
50
+ def send_message(status)
51
+ uri = "https://api.prowlapp.com/publicapi/add"
52
+ status_data = status_data_for(status)
53
+ data = {
54
+ application: application,
55
+ apikey: api_key,
56
+ event: status_data[:message],
57
+ description: message.call(model, status: status_data)
58
+ }
59
+ options = {
60
+ headers: { "Content-Type" => "application/x-www-form-urlencoded" },
61
+ body: URI.encode_www_form(data)
62
+ }
63
+ options[:expects] = 200 # raise error if unsuccessful
64
+ Excon.post(uri, options)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,71 @@
1
+ require "uri"
2
+
3
+ module Backup
4
+ module Notifier
5
+ class Pushover < Base
6
+ ##
7
+ # The API User Token
8
+ attr_accessor :user
9
+
10
+ ##
11
+ # The API Application Token
12
+ attr_accessor :token
13
+
14
+ ##
15
+ # The user's device identifier to sent the message directly to,
16
+ # rather than all of the user's devices
17
+ attr_accessor :device
18
+
19
+ ##
20
+ # The message title
21
+ attr_accessor :title
22
+
23
+ ##
24
+ # The priority of the notification
25
+ attr_accessor :priority
26
+
27
+ def initialize(model, &block)
28
+ super
29
+ instance_eval(&block) if block_given?
30
+ end
31
+
32
+ private
33
+
34
+ ##
35
+ # Notify the user of the backup operation results.
36
+ #
37
+ # `status` indicates one of the following:
38
+ #
39
+ # `:success`
40
+ # : The backup completed successfully.
41
+ # : Notification will be sent if `on_success` is `true`.
42
+ #
43
+ # `:warning`
44
+ # : The backup completed successfully, but warnings were logged.
45
+ # : Notification will be sent if `on_warning` or `on_success` is `true`.
46
+ #
47
+ # `:failure`
48
+ # : The backup operation failed.
49
+ # : Notification will be sent if `on_warning` or `on_success` is `true`.
50
+ #
51
+ def notify!(status)
52
+ send_message(message.call(model, status: status_data_for(status)))
53
+ end
54
+
55
+ def send_message(message)
56
+ uri = "https://api.pushover.net/1/messages.json"
57
+ data = { user: user, token: token, message: message }
58
+ [:device, :title, :priority].each do |param|
59
+ val = send(param)
60
+ data.merge!(param => val) if val
61
+ end
62
+ options = {
63
+ headers: { "Content-Type" => "application/x-www-form-urlencoded" },
64
+ body: URI.encode_www_form(data)
65
+ }
66
+ options[:expects] = 200 # raise error if unsuccessful
67
+ Excon.post(uri, options)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,123 @@
1
+ require "aws-sdk"
2
+ require "mail"
3
+
4
+ module Backup
5
+ module Notifier
6
+ class Ses < Base
7
+ ##
8
+ # Amazon Simple Email Service (SES) Credentials
9
+ attr_accessor :access_key_id, :secret_access_key, :use_iam_profile
10
+
11
+ ##
12
+ # SES Region
13
+ attr_accessor :region
14
+
15
+ ##
16
+ # Sender Email Address
17
+ attr_accessor :from
18
+
19
+ ##
20
+ # Receiver Email Address
21
+ attr_accessor :to
22
+
23
+ ##
24
+ # CC receiver Email Address
25
+ attr_accessor :cc
26
+
27
+ ##
28
+ # BCC receiver Email Address
29
+ attr_accessor :bcc
30
+
31
+ ##
32
+ # Set reply to email address
33
+ attr_accessor :reply_to
34
+
35
+ def initialize(model, &block)
36
+ super
37
+ instance_eval(&block) if block_given?
38
+
39
+ @region ||= "eu-west-1"
40
+ @send_log_on ||= [:warning, :failure]
41
+ end
42
+
43
+ ##
44
+ # Array of statuses for which the log file should be attached.
45
+ #
46
+ # Available statuses are: `:success`, `:warning` and `:failure`.
47
+ # Default: [:warning, :failure]
48
+ attr_accessor :send_log_on
49
+
50
+ private
51
+
52
+ def client
53
+ credentials = if use_iam_profile
54
+ Aws::InstanceProfileCredentials.new
55
+ else
56
+ Aws::Credentials.new(access_key_id, secret_access_key)
57
+ end
58
+
59
+ Aws::SES::Client.new(
60
+ region: region,
61
+ credentials: credentials
62
+ )
63
+ end
64
+
65
+ ##
66
+ # Notify the user of the backup operation results.
67
+ #
68
+ # `status` indicates one of the following:
69
+ #
70
+ # `:success`
71
+ # : The backup completed successfully.
72
+ # : Notification will be sent if `on_success` is `true`.
73
+ #
74
+ # `:warning`
75
+ # : The backup completed successfully, but warnings were logged.
76
+ # : Notification will be sent, including a copy of the current
77
+ # : backup log, if `on_warning` or `on_success` is `true`.
78
+ #
79
+ # `:failure`
80
+ # : The backup operation failed.
81
+ # : Notification will be sent, including a copy of the current
82
+ # : backup log, if `on_failure` is `true`.
83
+ #
84
+ def notify!(status)
85
+ email = ::Mail.new
86
+ email.to = to
87
+ email.from = from
88
+ email.cc = cc
89
+ email.bcc = bcc
90
+ email.reply_to = reply_to
91
+ email.subject = message.call(model, status: status_data_for(status))
92
+
93
+ # By default, the `mail` gem doesn't include BCC in raw output, which is
94
+ # needed for SES to send to those addresses.
95
+ email[:bcc].include_in_headers = true
96
+
97
+ send_log = send_log_on.include?(status)
98
+ template = Backup::Template.new(model: model, send_log: send_log)
99
+ email.body = template.result(sprintf("notifier/mail/%s.erb", status.to_s))
100
+
101
+ if send_log
102
+ email.convert_to_multipart
103
+ email.attachments["#{model.time}.#{model.trigger}.log"] = {
104
+ mime_type: "text/plain;",
105
+ content: Logger.messages.map(&:formatted_lines).flatten.join("\n")
106
+ }
107
+ end
108
+
109
+ send_opts = {
110
+ raw_message: {
111
+ data: email.to_s
112
+ }
113
+ }
114
+
115
+ if email.respond_to?(:destinations)
116
+ send_opts[:destinations] = email.destinations
117
+ end
118
+
119
+ client.send_raw_email(send_opts)
120
+ end
121
+ end
122
+ end
123
+ end