backup_zh 4.0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.md +24 -0
- data/README.md +21 -0
- data/bin/backup_zh +5 -0
- data/lib/backup.rb +137 -0
- data/lib/backup/archive.rb +170 -0
- data/lib/backup/binder.rb +22 -0
- data/lib/backup/cleaner.rb +116 -0
- data/lib/backup/cli.rb +364 -0
- data/lib/backup/cloud_io/base.rb +41 -0
- data/lib/backup/cloud_io/cloud_files.rb +298 -0
- data/lib/backup/cloud_io/qi_niu.rb +93 -0
- data/lib/backup/cloud_io/s3.rb +260 -0
- data/lib/backup/compressor/base.rb +35 -0
- data/lib/backup/compressor/bzip2.rb +39 -0
- data/lib/backup/compressor/custom.rb +53 -0
- data/lib/backup/compressor/gzip.rb +74 -0
- data/lib/backup/config.rb +119 -0
- data/lib/backup/config/dsl.rb +103 -0
- data/lib/backup/config/helpers.rb +143 -0
- data/lib/backup/database/base.rb +85 -0
- data/lib/backup/database/mongodb.rb +186 -0
- data/lib/backup/database/mysql.rb +181 -0
- data/lib/backup/database/openldap.rb +95 -0
- data/lib/backup/database/postgresql.rb +133 -0
- data/lib/backup/database/redis.rb +179 -0
- data/lib/backup/database/riak.rb +82 -0
- data/lib/backup/encryptor/base.rb +29 -0
- data/lib/backup/encryptor/gpg.rb +747 -0
- data/lib/backup/encryptor/open_ssl.rb +72 -0
- data/lib/backup/errors.rb +58 -0
- data/lib/backup/logger.rb +199 -0
- data/lib/backup/logger/console.rb +51 -0
- data/lib/backup/logger/fog_adapter.rb +29 -0
- data/lib/backup/logger/logfile.rb +133 -0
- data/lib/backup/logger/syslog.rb +116 -0
- data/lib/backup/model.rb +454 -0
- data/lib/backup/notifier/base.rb +98 -0
- data/lib/backup/notifier/campfire.rb +69 -0
- data/lib/backup/notifier/flowdock.rb +102 -0
- data/lib/backup/notifier/hipchat.rb +93 -0
- data/lib/backup/notifier/http_post.rb +122 -0
- data/lib/backup/notifier/mail.rb +238 -0
- data/lib/backup/notifier/nagios.rb +74 -0
- data/lib/backup/notifier/prowl.rb +69 -0
- data/lib/backup/notifier/pushover.rb +80 -0
- data/lib/backup/notifier/slack.rb +158 -0
- data/lib/backup/notifier/twitter.rb +64 -0
- data/lib/backup/notifier/zabbix.rb +68 -0
- data/lib/backup/package.rb +51 -0
- data/lib/backup/packager.rb +101 -0
- data/lib/backup/pipeline.rb +124 -0
- data/lib/backup/splitter.rb +76 -0
- data/lib/backup/storage/base.rb +57 -0
- data/lib/backup/storage/cloud_files.rb +158 -0
- data/lib/backup/storage/cycler.rb +65 -0
- data/lib/backup/storage/dropbox.rb +236 -0
- data/lib/backup/storage/ftp.rb +98 -0
- data/lib/backup/storage/local.rb +64 -0
- data/lib/backup/storage/ninefold.rb +74 -0
- data/lib/backup/storage/qi_niu.rb +70 -0
- data/lib/backup/storage/rsync.rb +248 -0
- data/lib/backup/storage/s3.rb +154 -0
- data/lib/backup/storage/scp.rb +67 -0
- data/lib/backup/storage/sftp.rb +82 -0
- data/lib/backup/syncer/base.rb +70 -0
- data/lib/backup/syncer/cloud/base.rb +179 -0
- data/lib/backup/syncer/cloud/cloud_files.rb +83 -0
- data/lib/backup/syncer/cloud/local_file.rb +100 -0
- data/lib/backup/syncer/cloud/s3.rb +110 -0
- data/lib/backup/syncer/rsync/base.rb +48 -0
- data/lib/backup/syncer/rsync/local.rb +31 -0
- data/lib/backup/syncer/rsync/pull.rb +51 -0
- data/lib/backup/syncer/rsync/push.rb +205 -0
- data/lib/backup/template.rb +46 -0
- data/lib/backup/utilities.rb +224 -0
- data/lib/backup/version.rb +5 -0
- data/templates/cli/archive +28 -0
- data/templates/cli/compressor/bzip2 +4 -0
- data/templates/cli/compressor/custom +7 -0
- data/templates/cli/compressor/gzip +4 -0
- data/templates/cli/config +123 -0
- data/templates/cli/databases/mongodb +15 -0
- data/templates/cli/databases/mysql +18 -0
- data/templates/cli/databases/openldap +24 -0
- data/templates/cli/databases/postgresql +16 -0
- data/templates/cli/databases/redis +16 -0
- data/templates/cli/databases/riak +17 -0
- data/templates/cli/encryptor/gpg +27 -0
- data/templates/cli/encryptor/openssl +9 -0
- data/templates/cli/model +26 -0
- data/templates/cli/notifier/zabbix +15 -0
- data/templates/cli/notifiers/campfire +12 -0
- data/templates/cli/notifiers/flowdock +16 -0
- data/templates/cli/notifiers/hipchat +15 -0
- data/templates/cli/notifiers/http_post +32 -0
- data/templates/cli/notifiers/mail +21 -0
- data/templates/cli/notifiers/nagios +13 -0
- data/templates/cli/notifiers/prowl +11 -0
- data/templates/cli/notifiers/pushover +11 -0
- data/templates/cli/notifiers/slack +23 -0
- data/templates/cli/notifiers/twitter +13 -0
- data/templates/cli/splitter +7 -0
- data/templates/cli/storages/cloud_files +11 -0
- data/templates/cli/storages/dropbox +19 -0
- data/templates/cli/storages/ftp +12 -0
- data/templates/cli/storages/local +7 -0
- data/templates/cli/storages/ninefold +9 -0
- data/templates/cli/storages/qi_niu +9 -0
- data/templates/cli/storages/rsync +17 -0
- data/templates/cli/storages/s3 +14 -0
- data/templates/cli/storages/scp +14 -0
- data/templates/cli/storages/sftp +14 -0
- data/templates/cli/syncers/cloud_files +22 -0
- data/templates/cli/syncers/rsync_local +20 -0
- data/templates/cli/syncers/rsync_pull +28 -0
- data/templates/cli/syncers/rsync_push +28 -0
- data/templates/cli/syncers/s3 +27 -0
- data/templates/general/links +3 -0
- data/templates/general/version.erb +2 -0
- data/templates/notifier/mail/failure.erb +16 -0
- data/templates/notifier/mail/success.erb +16 -0
- data/templates/notifier/mail/warning.erb +16 -0
- data/templates/storage/dropbox/authorization_url.erb +6 -0
- data/templates/storage/dropbox/authorized.erb +4 -0
- data/templates/storage/dropbox/cache_file_written.erb +10 -0
- metadata +1124 -0
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require 'mail'
|
|
3
|
+
|
|
4
|
+
module Backup
|
|
5
|
+
module Notifier
|
|
6
|
+
class Mail < Base
|
|
7
|
+
|
|
8
|
+
##
|
|
9
|
+
# Mail delivery method to be used by the Mail gem.
|
|
10
|
+
#
|
|
11
|
+
# Supported methods:
|
|
12
|
+
#
|
|
13
|
+
# [:smtp - ::Mail::SMTP (default)]
|
|
14
|
+
# Settings used by this method:
|
|
15
|
+
# {#address}, {#port}, {#domain}, {#user_name}, {#password},
|
|
16
|
+
# {#authentication}, {#encryption}, {#openssl_verify_mode}
|
|
17
|
+
#
|
|
18
|
+
# [:sendmail - ::Mail::Sendmail]
|
|
19
|
+
# Settings used by this method:
|
|
20
|
+
# {#sendmail_args}
|
|
21
|
+
#
|
|
22
|
+
# [:exim - ::Mail::Exim]
|
|
23
|
+
# Settings used by this method:
|
|
24
|
+
# {#exim_args}
|
|
25
|
+
#
|
|
26
|
+
# [:file - ::Mail::FileDelivery]
|
|
27
|
+
# Settings used by this method:
|
|
28
|
+
# {#mail_folder}
|
|
29
|
+
#
|
|
30
|
+
attr_accessor :delivery_method
|
|
31
|
+
|
|
32
|
+
##
|
|
33
|
+
# Sender Email Address
|
|
34
|
+
attr_accessor :from
|
|
35
|
+
|
|
36
|
+
##
|
|
37
|
+
# Receiver Email Address
|
|
38
|
+
attr_accessor :to
|
|
39
|
+
|
|
40
|
+
##
|
|
41
|
+
# SMTP Server Address
|
|
42
|
+
attr_accessor :address
|
|
43
|
+
|
|
44
|
+
##
|
|
45
|
+
# SMTP Server Port
|
|
46
|
+
attr_accessor :port
|
|
47
|
+
|
|
48
|
+
##
|
|
49
|
+
# Your domain (if applicable)
|
|
50
|
+
attr_accessor :domain
|
|
51
|
+
|
|
52
|
+
##
|
|
53
|
+
# SMTP Server Username (sender email's credentials)
|
|
54
|
+
attr_accessor :user_name
|
|
55
|
+
|
|
56
|
+
##
|
|
57
|
+
# SMTP Server Password (sender email's credentials)
|
|
58
|
+
attr_accessor :password
|
|
59
|
+
|
|
60
|
+
##
|
|
61
|
+
# Authentication type
|
|
62
|
+
#
|
|
63
|
+
# Acceptable values: +:plain+, +:login+, +:cram_md5+
|
|
64
|
+
attr_accessor :authentication
|
|
65
|
+
|
|
66
|
+
##
|
|
67
|
+
# Set the method of encryption to be used for the +SMTP+ connection.
|
|
68
|
+
#
|
|
69
|
+
# [:starttls (default)]
|
|
70
|
+
# Use +STARTTLS+ to upgrade the connection to a +SSL/TLS+ connection.
|
|
71
|
+
#
|
|
72
|
+
# [:tls or :ssl]
|
|
73
|
+
# Use a +SSL/TLS+ connection.
|
|
74
|
+
#
|
|
75
|
+
# [:none]
|
|
76
|
+
# No encryption will be used.
|
|
77
|
+
attr_accessor :encryption
|
|
78
|
+
|
|
79
|
+
##
|
|
80
|
+
# OpenSSL Verify Mode
|
|
81
|
+
#
|
|
82
|
+
# Valid modes: +:none+, +:peer+, +:client_once+, +:fail_if_no_peer_cert+
|
|
83
|
+
# See +OpenSSL::SSL+ for details.
|
|
84
|
+
#
|
|
85
|
+
# Use +:none+ for a self-signed and/or wildcard certificate
|
|
86
|
+
attr_accessor :openssl_verify_mode
|
|
87
|
+
|
|
88
|
+
##
|
|
89
|
+
# Optional arguments to pass to `sendmail`
|
|
90
|
+
#
|
|
91
|
+
# Note that this will override the defaults set by the Mail gem
|
|
92
|
+
# (currently: '-i'). So, if set here, be sure to set all the arguments
|
|
93
|
+
# you require.
|
|
94
|
+
#
|
|
95
|
+
# Example: '-i -X/tmp/traffic.log'
|
|
96
|
+
attr_accessor :sendmail_args
|
|
97
|
+
|
|
98
|
+
##
|
|
99
|
+
# Optional arguments to pass to `exim`
|
|
100
|
+
#
|
|
101
|
+
# Note that this will override the defaults set by the Mail gem
|
|
102
|
+
# (currently: '-i -t') So, if set here, be sure to set all the arguments
|
|
103
|
+
# you require.
|
|
104
|
+
#
|
|
105
|
+
# Example: '-i -t -X/tmp/traffic.log'
|
|
106
|
+
attr_accessor :exim_args
|
|
107
|
+
|
|
108
|
+
##
|
|
109
|
+
# Folder where mail will be kept when using the `:file` `delivery_method`.
|
|
110
|
+
#
|
|
111
|
+
# Default location is '$HOME/Backup/emails'
|
|
112
|
+
attr_accessor :mail_folder
|
|
113
|
+
|
|
114
|
+
##
|
|
115
|
+
# Array of statuses for which the log file should be attached.
|
|
116
|
+
#
|
|
117
|
+
# Available statuses are: `:success`, `:warning` and `:failure`.
|
|
118
|
+
# Default: [:warning, :failure]
|
|
119
|
+
attr_accessor :send_log_on
|
|
120
|
+
|
|
121
|
+
def initialize(model, &block)
|
|
122
|
+
super
|
|
123
|
+
instance_eval(&block) if block_given?
|
|
124
|
+
|
|
125
|
+
@send_log_on ||= [:warning, :failure]
|
|
126
|
+
@encryption ||= :starttls
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
private
|
|
130
|
+
|
|
131
|
+
##
|
|
132
|
+
# Notify the user of the backup operation results.
|
|
133
|
+
#
|
|
134
|
+
# `status` indicates one of the following:
|
|
135
|
+
#
|
|
136
|
+
# `:success`
|
|
137
|
+
# : The backup completed successfully.
|
|
138
|
+
# : Notification will be sent if `on_success` is `true`.
|
|
139
|
+
#
|
|
140
|
+
# `:warning`
|
|
141
|
+
# : The backup completed successfully, but warnings were logged.
|
|
142
|
+
# : Notification will be sent, including a copy of the current
|
|
143
|
+
# : backup log, if `on_warning` or `on_success` is `true`.
|
|
144
|
+
#
|
|
145
|
+
# `:failure`
|
|
146
|
+
# : The backup operation failed.
|
|
147
|
+
# : Notification will be sent, including a copy of the current
|
|
148
|
+
# : backup log, if `on_failure` is `true`.
|
|
149
|
+
#
|
|
150
|
+
def notify!(status)
|
|
151
|
+
tag = case status
|
|
152
|
+
when :success then '[Backup::Success]'
|
|
153
|
+
when :warning then '[Backup::Warning]'
|
|
154
|
+
when :failure then '[Backup::Failure]'
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
email = new_email
|
|
158
|
+
email.subject = "#{ tag } #{ model.label } (#{ model.trigger })"
|
|
159
|
+
|
|
160
|
+
send_log = send_log_on.include?(status)
|
|
161
|
+
template = Backup::Template.new({ :model => model, :send_log => send_log })
|
|
162
|
+
email.body = template.result('notifier/mail/%s.erb' % status.to_s)
|
|
163
|
+
|
|
164
|
+
if send_log
|
|
165
|
+
email.convert_to_multipart
|
|
166
|
+
email.attachments["#{ model.time }.#{ model.trigger }.log"] = {
|
|
167
|
+
:mime_type => 'text/plain;',
|
|
168
|
+
:content => Logger.messages.map(&:formatted_lines).flatten.join("\n")
|
|
169
|
+
}
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
email.deliver! # raise error if unsuccessful
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
##
|
|
176
|
+
# Configures the Mail gem by setting the defaults.
|
|
177
|
+
# Creates and returns a new email, based on the @delivery_method used.
|
|
178
|
+
def new_email
|
|
179
|
+
method = %w{ smtp sendmail exim file test }.
|
|
180
|
+
index(@delivery_method.to_s) ? @delivery_method.to_s : 'smtp'
|
|
181
|
+
|
|
182
|
+
options =
|
|
183
|
+
case method
|
|
184
|
+
when 'smtp'
|
|
185
|
+
{ :address => @address,
|
|
186
|
+
:port => @port,
|
|
187
|
+
:domain => @domain,
|
|
188
|
+
:user_name => @user_name,
|
|
189
|
+
:password => @password,
|
|
190
|
+
:authentication => @authentication,
|
|
191
|
+
:enable_starttls_auto => @encryption == :starttls,
|
|
192
|
+
:openssl_verify_mode => @openssl_verify_mode,
|
|
193
|
+
:ssl => @encryption == :ssl,
|
|
194
|
+
:tls => @encryption == :tls
|
|
195
|
+
}
|
|
196
|
+
when 'sendmail'
|
|
197
|
+
opts = {}
|
|
198
|
+
opts.merge!(:location => utility(:sendmail))
|
|
199
|
+
opts.merge!(:arguments => @sendmail_args) if @sendmail_args
|
|
200
|
+
opts
|
|
201
|
+
when 'exim'
|
|
202
|
+
opts = {}
|
|
203
|
+
opts.merge!(:location => utility(:exim))
|
|
204
|
+
opts.merge!(:arguments => @exim_args) if @exim_args
|
|
205
|
+
opts
|
|
206
|
+
when 'file'
|
|
207
|
+
@mail_folder ||= File.join(Config.root_path, 'emails')
|
|
208
|
+
{ :location => File.expand_path(@mail_folder) }
|
|
209
|
+
when 'test' then {}
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
::Mail.defaults do
|
|
213
|
+
delivery_method method.to_sym, options
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
email = ::Mail.new
|
|
217
|
+
email.to = @to
|
|
218
|
+
email.from = @from
|
|
219
|
+
email
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# Patch mail v2.5.4 Exim delivery method
|
|
227
|
+
# https://github.com/meskyanichi/backup/issues/446
|
|
228
|
+
# https://github.com/mikel/mail/pull/546
|
|
229
|
+
module Mail
|
|
230
|
+
class Exim
|
|
231
|
+
def self.call(path, arguments, destinations, encoded_message)
|
|
232
|
+
popen "#{path} #{arguments}" do |io|
|
|
233
|
+
io.puts encoded_message.to_lf
|
|
234
|
+
io.flush
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
end
|
|
238
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Backup
|
|
4
|
+
module Notifier
|
|
5
|
+
class Nagios < Base
|
|
6
|
+
|
|
7
|
+
##
|
|
8
|
+
# Host of Nagios server to notify on backup completion.
|
|
9
|
+
attr_accessor :nagios_host
|
|
10
|
+
|
|
11
|
+
##
|
|
12
|
+
# Port of Nagios server to notify on backup completion.
|
|
13
|
+
attr_accessor :nagios_port
|
|
14
|
+
|
|
15
|
+
##
|
|
16
|
+
# Nagios nrpe configuration file.
|
|
17
|
+
attr_accessor :send_nsca_cfg
|
|
18
|
+
|
|
19
|
+
##
|
|
20
|
+
# Name of the Nagios service for the backup check.
|
|
21
|
+
attr_accessor :service_name
|
|
22
|
+
|
|
23
|
+
##
|
|
24
|
+
# Host name in Nagios for the backup check.
|
|
25
|
+
attr_accessor :service_host
|
|
26
|
+
|
|
27
|
+
def initialize(model, &block)
|
|
28
|
+
super
|
|
29
|
+
instance_eval(&block) if block_given?
|
|
30
|
+
|
|
31
|
+
@nagios_host ||= Config.hostname
|
|
32
|
+
@nagios_port ||= 5667
|
|
33
|
+
@send_nsca_cfg||= "/etc/nagios/send_nsca.cfg"
|
|
34
|
+
@service_name ||= "Backup #{ model.trigger }"
|
|
35
|
+
@service_host ||= Config.hostname
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
##
|
|
41
|
+
# Notify the user of the backup operation results.
|
|
42
|
+
#
|
|
43
|
+
# `status` indicates one of the following:
|
|
44
|
+
#
|
|
45
|
+
# `:success`
|
|
46
|
+
# : The backup completed successfully.
|
|
47
|
+
# : Notification will be sent if `on_success` is `true`.
|
|
48
|
+
#
|
|
49
|
+
# `:warning`
|
|
50
|
+
# : The backup completed successfully, but warnings were logged.
|
|
51
|
+
# : Notification will be sent if `on_warning` or `on_success` is `true`.
|
|
52
|
+
#
|
|
53
|
+
# `:failure`
|
|
54
|
+
# : The backup operation failed.
|
|
55
|
+
# : Notification will be sent if `on_warning` or `on_success` is `true`.
|
|
56
|
+
#
|
|
57
|
+
def notify!(status)
|
|
58
|
+
message = case status
|
|
59
|
+
when :success then 'Completed Successfully'
|
|
60
|
+
when :warning then 'Completed Successfully (with Warnings)'
|
|
61
|
+
when :failure then 'Failed'
|
|
62
|
+
end
|
|
63
|
+
send_message("#{ message } in #{ model.duration }")
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def send_message(message)
|
|
67
|
+
cmd = "#{ utility(:send_nsca) } -H '#{ nagios_host }' -p '#{ nagios_port }' -c '#{ send_nsca_cfg }'"
|
|
68
|
+
msg = [service_host, service_name, model.exit_status, message].join("\t")
|
|
69
|
+
run("echo '#{ msg }' | #{ cmd }")
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require 'uri'
|
|
3
|
+
|
|
4
|
+
module Backup
|
|
5
|
+
module Notifier
|
|
6
|
+
class Prowl < Base
|
|
7
|
+
|
|
8
|
+
##
|
|
9
|
+
# Application name
|
|
10
|
+
# Tell something like your server name. Example: "Server1 Backup"
|
|
11
|
+
attr_accessor :application
|
|
12
|
+
|
|
13
|
+
##
|
|
14
|
+
# API-Key
|
|
15
|
+
# Create a Prowl account and request an API key on prowlapp.com.
|
|
16
|
+
attr_accessor :api_key
|
|
17
|
+
|
|
18
|
+
def initialize(model, &block)
|
|
19
|
+
super
|
|
20
|
+
instance_eval(&block) if block_given?
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
##
|
|
26
|
+
# Notify the user of the backup operation results.
|
|
27
|
+
#
|
|
28
|
+
# `status` indicates one of the following:
|
|
29
|
+
#
|
|
30
|
+
# `:success`
|
|
31
|
+
# : The backup completed successfully.
|
|
32
|
+
# : Notification will be sent if `on_success` is `true`.
|
|
33
|
+
#
|
|
34
|
+
# `:warning`
|
|
35
|
+
# : The backup completed successfully, but warnings were logged.
|
|
36
|
+
# : Notification will be sent if `on_warning` or `on_success` is `true`.
|
|
37
|
+
#
|
|
38
|
+
# `:failure`
|
|
39
|
+
# : The backup operation failed.
|
|
40
|
+
# : Notification will be sent if `on_warning` or `on_success` is `true`.
|
|
41
|
+
#
|
|
42
|
+
def notify!(status)
|
|
43
|
+
tag = case status
|
|
44
|
+
when :success then '[Backup::Success]'
|
|
45
|
+
when :warning then '[Backup::Warning]'
|
|
46
|
+
when :failure then '[Backup::Failure]'
|
|
47
|
+
end
|
|
48
|
+
send_message(tag)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def send_message(message)
|
|
52
|
+
uri = 'https://api.prowlapp.com/publicapi/add'
|
|
53
|
+
data = {
|
|
54
|
+
:application => application,
|
|
55
|
+
:apikey => api_key,
|
|
56
|
+
:event => message,
|
|
57
|
+
:description => "#{ model.label } (#{ model.trigger })"
|
|
58
|
+
}
|
|
59
|
+
options = {
|
|
60
|
+
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
|
|
61
|
+
:body => URI.encode_www_form(data)
|
|
62
|
+
}
|
|
63
|
+
options.merge!(:expects => 200) # raise error if unsuccessful
|
|
64
|
+
Excon.post(uri, options)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require 'uri'
|
|
3
|
+
|
|
4
|
+
module Backup
|
|
5
|
+
module Notifier
|
|
6
|
+
class Pushover < Base
|
|
7
|
+
|
|
8
|
+
##
|
|
9
|
+
# The API User Token
|
|
10
|
+
attr_accessor :user
|
|
11
|
+
|
|
12
|
+
##
|
|
13
|
+
# The API Application Token
|
|
14
|
+
attr_accessor :token
|
|
15
|
+
|
|
16
|
+
##
|
|
17
|
+
# The user's device identifier to sent the message directly to,
|
|
18
|
+
# rather than all of the user's devices
|
|
19
|
+
attr_accessor :device
|
|
20
|
+
|
|
21
|
+
##
|
|
22
|
+
# The message title
|
|
23
|
+
attr_accessor :title
|
|
24
|
+
|
|
25
|
+
##
|
|
26
|
+
# The priority of the notification
|
|
27
|
+
attr_accessor :priority
|
|
28
|
+
|
|
29
|
+
def initialize(model, &block)
|
|
30
|
+
super
|
|
31
|
+
instance_eval(&block) if block_given?
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
##
|
|
37
|
+
# Notify the user of the backup operation results.
|
|
38
|
+
#
|
|
39
|
+
# `status` indicates one of the following:
|
|
40
|
+
#
|
|
41
|
+
# `:success`
|
|
42
|
+
# : The backup completed successfully.
|
|
43
|
+
# : Notification will be sent if `on_success` is `true`.
|
|
44
|
+
#
|
|
45
|
+
# `:warning`
|
|
46
|
+
# : The backup completed successfully, but warnings were logged.
|
|
47
|
+
# : Notification will be sent if `on_warning` or `on_success` is `true`.
|
|
48
|
+
#
|
|
49
|
+
# `:failure`
|
|
50
|
+
# : The backup operation failed.
|
|
51
|
+
# : Notification will be sent if `on_warning` or `on_success` is `true`.
|
|
52
|
+
#
|
|
53
|
+
def notify!(status)
|
|
54
|
+
tag = case status
|
|
55
|
+
when :success then '[Backup::Success]'
|
|
56
|
+
when :failure then '[Backup::Failure]'
|
|
57
|
+
when :warning then '[Backup::Warning]'
|
|
58
|
+
end
|
|
59
|
+
message = "#{ tag } #{ model.label } (#{ model.trigger })"
|
|
60
|
+
send_message(message)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def send_message(message)
|
|
64
|
+
uri = 'https://api.pushover.net/1/messages.json'
|
|
65
|
+
data = { :user => user, :token => token, :message => message }
|
|
66
|
+
[:device, :title, :priority].each do |param|
|
|
67
|
+
val = send(param)
|
|
68
|
+
data.merge!(param => val) if val
|
|
69
|
+
end
|
|
70
|
+
options = {
|
|
71
|
+
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
|
|
72
|
+
:body => URI.encode_www_form(data)
|
|
73
|
+
}
|
|
74
|
+
options.merge!(:expects => 200) # raise error if unsuccessful
|
|
75
|
+
Excon.post(uri, options)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|