sliday_backup 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +29 -0
  3. data/bin/sliday_backup +5 -0
  4. data/lib/sliday_backup.rb +147 -0
  5. data/lib/sliday_backup/archive.rb +170 -0
  6. data/lib/sliday_backup/binder.rb +22 -0
  7. data/lib/sliday_backup/cleaner.rb +116 -0
  8. data/lib/sliday_backup/cli.rb +374 -0
  9. data/lib/sliday_backup/cloud_io/base.rb +41 -0
  10. data/lib/sliday_backup/cloud_io/cloud_files.rb +298 -0
  11. data/lib/sliday_backup/cloud_io/s3.rb +260 -0
  12. data/lib/sliday_backup/compressor/base.rb +35 -0
  13. data/lib/sliday_backup/compressor/bzip2.rb +39 -0
  14. data/lib/sliday_backup/compressor/custom.rb +53 -0
  15. data/lib/sliday_backup/compressor/gzip.rb +74 -0
  16. data/lib/sliday_backup/config.rb +119 -0
  17. data/lib/sliday_backup/config/dsl.rb +103 -0
  18. data/lib/sliday_backup/config/helpers.rb +143 -0
  19. data/lib/sliday_backup/database/base.rb +86 -0
  20. data/lib/sliday_backup/database/mongodb.rb +187 -0
  21. data/lib/sliday_backup/database/mysql.rb +192 -0
  22. data/lib/sliday_backup/database/openldap.rb +95 -0
  23. data/lib/sliday_backup/database/postgresql.rb +133 -0
  24. data/lib/sliday_backup/database/redis.rb +179 -0
  25. data/lib/sliday_backup/database/riak.rb +82 -0
  26. data/lib/sliday_backup/database/sqlite.rb +57 -0
  27. data/lib/sliday_backup/encryptor/base.rb +29 -0
  28. data/lib/sliday_backup/encryptor/gpg.rb +747 -0
  29. data/lib/sliday_backup/encryptor/open_ssl.rb +77 -0
  30. data/lib/sliday_backup/errors.rb +58 -0
  31. data/lib/sliday_backup/logger.rb +199 -0
  32. data/lib/sliday_backup/logger/console.rb +51 -0
  33. data/lib/sliday_backup/logger/fog_adapter.rb +29 -0
  34. data/lib/sliday_backup/logger/logfile.rb +133 -0
  35. data/lib/sliday_backup/logger/syslog.rb +116 -0
  36. data/lib/sliday_backup/model.rb +479 -0
  37. data/lib/sliday_backup/notifier/base.rb +128 -0
  38. data/lib/sliday_backup/notifier/campfire.rb +63 -0
  39. data/lib/sliday_backup/notifier/command.rb +99 -0
  40. data/lib/sliday_backup/notifier/datadog.rb +107 -0
  41. data/lib/sliday_backup/notifier/flowdock.rb +103 -0
  42. data/lib/sliday_backup/notifier/hipchat.rb +112 -0
  43. data/lib/sliday_backup/notifier/http_post.rb +117 -0
  44. data/lib/sliday_backup/notifier/mail.rb +244 -0
  45. data/lib/sliday_backup/notifier/nagios.rb +69 -0
  46. data/lib/sliday_backup/notifier/pagerduty.rb +81 -0
  47. data/lib/sliday_backup/notifier/prowl.rb +68 -0
  48. data/lib/sliday_backup/notifier/pushover.rb +74 -0
  49. data/lib/sliday_backup/notifier/ses.rb +88 -0
  50. data/lib/sliday_backup/notifier/slack.rb +148 -0
  51. data/lib/sliday_backup/notifier/twitter.rb +58 -0
  52. data/lib/sliday_backup/notifier/zabbix.rb +63 -0
  53. data/lib/sliday_backup/package.rb +55 -0
  54. data/lib/sliday_backup/packager.rb +107 -0
  55. data/lib/sliday_backup/pipeline.rb +124 -0
  56. data/lib/sliday_backup/splitter.rb +76 -0
  57. data/lib/sliday_backup/storage/base.rb +69 -0
  58. data/lib/sliday_backup/storage/cloud_files.rb +158 -0
  59. data/lib/sliday_backup/storage/cycler.rb +75 -0
  60. data/lib/sliday_backup/storage/dropbox.rb +212 -0
  61. data/lib/sliday_backup/storage/ftp.rb +112 -0
  62. data/lib/sliday_backup/storage/local.rb +64 -0
  63. data/lib/sliday_backup/storage/qiniu.rb +65 -0
  64. data/lib/sliday_backup/storage/rsync.rb +248 -0
  65. data/lib/sliday_backup/storage/s3.rb +156 -0
  66. data/lib/sliday_backup/storage/scp.rb +67 -0
  67. data/lib/sliday_backup/storage/sftp.rb +82 -0
  68. data/lib/sliday_backup/storage/sliday_storage.rb +79 -0
  69. data/lib/sliday_backup/syncer/base.rb +70 -0
  70. data/lib/sliday_backup/syncer/cloud/base.rb +179 -0
  71. data/lib/sliday_backup/syncer/cloud/cloud_files.rb +83 -0
  72. data/lib/sliday_backup/syncer/cloud/local_file.rb +100 -0
  73. data/lib/sliday_backup/syncer/cloud/s3.rb +110 -0
  74. data/lib/sliday_backup/syncer/rsync/base.rb +54 -0
  75. data/lib/sliday_backup/syncer/rsync/local.rb +31 -0
  76. data/lib/sliday_backup/syncer/rsync/pull.rb +51 -0
  77. data/lib/sliday_backup/syncer/rsync/push.rb +205 -0
  78. data/lib/sliday_backup/template.rb +46 -0
  79. data/lib/sliday_backup/utilities.rb +224 -0
  80. data/lib/sliday_backup/version.rb +5 -0
  81. data/templates/cli/archive +28 -0
  82. data/templates/cli/compressor/bzip2 +4 -0
  83. data/templates/cli/compressor/custom +7 -0
  84. data/templates/cli/compressor/gzip +4 -0
  85. data/templates/cli/config +123 -0
  86. data/templates/cli/databases/mongodb +15 -0
  87. data/templates/cli/databases/mysql +18 -0
  88. data/templates/cli/databases/openldap +24 -0
  89. data/templates/cli/databases/postgresql +16 -0
  90. data/templates/cli/databases/redis +16 -0
  91. data/templates/cli/databases/riak +17 -0
  92. data/templates/cli/databases/sqlite +11 -0
  93. data/templates/cli/encryptor/gpg +27 -0
  94. data/templates/cli/encryptor/openssl +9 -0
  95. data/templates/cli/model +26 -0
  96. data/templates/cli/notifier/zabbix +15 -0
  97. data/templates/cli/notifiers/campfire +12 -0
  98. data/templates/cli/notifiers/command +32 -0
  99. data/templates/cli/notifiers/datadog +57 -0
  100. data/templates/cli/notifiers/flowdock +16 -0
  101. data/templates/cli/notifiers/hipchat +16 -0
  102. data/templates/cli/notifiers/http_post +32 -0
  103. data/templates/cli/notifiers/mail +24 -0
  104. data/templates/cli/notifiers/nagios +13 -0
  105. data/templates/cli/notifiers/pagerduty +12 -0
  106. data/templates/cli/notifiers/prowl +11 -0
  107. data/templates/cli/notifiers/pushover +11 -0
  108. data/templates/cli/notifiers/ses +15 -0
  109. data/templates/cli/notifiers/slack +22 -0
  110. data/templates/cli/notifiers/twitter +13 -0
  111. data/templates/cli/splitter +7 -0
  112. data/templates/cli/storages/cloud_files +11 -0
  113. data/templates/cli/storages/dropbox +20 -0
  114. data/templates/cli/storages/ftp +13 -0
  115. data/templates/cli/storages/local +8 -0
  116. data/templates/cli/storages/qiniu +12 -0
  117. data/templates/cli/storages/rsync +17 -0
  118. data/templates/cli/storages/s3 +16 -0
  119. data/templates/cli/storages/scp +15 -0
  120. data/templates/cli/storages/sftp +15 -0
  121. data/templates/cli/storages/sliday_storage +6 -0
  122. data/templates/cli/syncers/cloud_files +22 -0
  123. data/templates/cli/syncers/rsync_local +20 -0
  124. data/templates/cli/syncers/rsync_pull +28 -0
  125. data/templates/cli/syncers/rsync_push +28 -0
  126. data/templates/cli/syncers/s3 +27 -0
  127. data/templates/general/links +3 -0
  128. data/templates/general/version.erb +2 -0
  129. data/templates/notifier/mail/failure.erb +16 -0
  130. data/templates/notifier/mail/success.erb +16 -0
  131. data/templates/notifier/mail/warning.erb +16 -0
  132. data/templates/storage/dropbox/authorization_url.erb +6 -0
  133. data/templates/storage/dropbox/authorized.erb +4 -0
  134. data/templates/storage/dropbox/cache_file_written.erb +10 -0
  135. metadata +1079 -0
@@ -0,0 +1,128 @@
1
+ # encoding: utf-8
2
+
3
+ module SlidayBackup
4
+ module Notifier
5
+ class Error < SlidayBackup::Error; end
6
+
7
+ class Base
8
+ include Utilities::Helpers
9
+ include Config::Helpers
10
+
11
+ ##
12
+ # When set to true, the user will be notified by email
13
+ # when a backup process ends without raising any exceptions
14
+ attr_accessor :on_success
15
+ alias :notify_on_success? :on_success
16
+
17
+ ##
18
+ # When set to true, the user will be notified by email
19
+ # when a backup process is successful, but has warnings
20
+ attr_accessor :on_warning
21
+ alias :notify_on_warning? :on_warning
22
+
23
+ ##
24
+ # When set to true, the user will be notified by email
25
+ # when a backup process raises an exception before finishing
26
+ attr_accessor :on_failure
27
+ alias :notify_on_failure? :on_failure
28
+
29
+ ##
30
+ # Number of times to retry failed attempts to send notification.
31
+ # Default: 10
32
+ attr_accessor :max_retries
33
+
34
+ ##
35
+ # Time in seconds to pause before each retry.
36
+ # Default: 30
37
+ attr_accessor :retry_waitsec
38
+
39
+ ##
40
+ # Message to send. Depends on notifier implementation if this is used.
41
+ # Default: lambda returning:
42
+ # "#{ message } #{ model.label } (#{ model.trigger })"
43
+ #
44
+ # @yieldparam [model] SlidayBackup::Model
45
+ # @yieldparam [data] Hash containing `message` and `key` values.
46
+ attr_accessor :message
47
+
48
+ attr_reader :model
49
+
50
+ def initialize(model)
51
+ @model = model
52
+ load_defaults!
53
+
54
+ @on_success = true if on_success.nil?
55
+ @on_warning = true if on_warning.nil?
56
+ @on_failure = true if on_failure.nil?
57
+ @max_retries ||= 10
58
+ @retry_waitsec ||= 30
59
+ @message ||= lambda do |model, data|
60
+ "[#{ data[:status][:message] }] #{ model.label } (#{ model.trigger })"
61
+ end
62
+ end
63
+
64
+ # This method is called from an ensure block in Model#perform! and must
65
+ # not raise any exceptions. However, each Notifier's #notify! method
66
+ # should raise an exception if the request fails so it may be retried.
67
+ def perform!
68
+ status = case model.exit_status
69
+ when 0
70
+ :success if notify_on_success?
71
+ when 1
72
+ :warning if notify_on_success? || notify_on_warning?
73
+ else
74
+ :failure if notify_on_failure?
75
+ end
76
+
77
+ if status
78
+ Logger.info "Sending notification using #{ notifier_name }..."
79
+ with_retries { notify!(status) }
80
+ end
81
+
82
+ rescue Exception => err
83
+ Logger.error Error.wrap(err, "#{ notifier_name } Failed!")
84
+ end
85
+
86
+ private
87
+
88
+ def with_retries
89
+ retries = 0
90
+ begin
91
+ yield
92
+ rescue StandardError, Timeout::Error => err
93
+ retries += 1
94
+ raise if retries > max_retries
95
+
96
+ Logger.info Error.wrap(err, "Retry ##{ retries } of #{ max_retries }.")
97
+ sleep(retry_waitsec)
98
+ retry
99
+ end
100
+ end
101
+
102
+ ##
103
+ # Return the notifier name, with SlidayBackup namespace removed
104
+ def notifier_name
105
+ self.class.to_s.sub('SlidayBackup::', '')
106
+ end
107
+
108
+ ##
109
+ # Return status data for message creation
110
+ def status_data_for(status)
111
+ {
112
+ :success => {
113
+ :message => 'SlidayBackup::Success',
114
+ :key => :success
115
+ },
116
+ :warning => {
117
+ :message => 'SlidayBackup::Warning',
118
+ :key => :warning
119
+ },
120
+ :failure => {
121
+ :message => 'SlidayBackup::Failure',
122
+ :key => :failure
123
+ }
124
+ }[status]
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,63 @@
1
+ # encoding: utf-8
2
+ require 'json'
3
+
4
+ module SlidayBackup
5
+ module Notifier
6
+ class Campfire < Base
7
+
8
+ ##
9
+ # Campfire api authentication token
10
+ attr_accessor :api_token
11
+
12
+ ##
13
+ # Campfire account's subdomain
14
+ attr_accessor :subdomain
15
+
16
+ ##
17
+ # Campfire account's room id
18
+ attr_accessor :room_id
19
+
20
+ def initialize(model, &block)
21
+ super
22
+ instance_eval(&block) if block_given?
23
+ end
24
+
25
+ private
26
+
27
+ ##
28
+ # Notify the user of the backup operation results.
29
+ #
30
+ # `status` indicates one of the following:
31
+ #
32
+ # `:success`
33
+ # : The backup completed successfully.
34
+ # : Notification will be sent if `on_success` is `true`.
35
+ #
36
+ # `:warning`
37
+ # : The backup completed successfully, but warnings were logged.
38
+ # : Notification will be sent if `on_warning` or `on_success` is `true`.
39
+ #
40
+ # `:failure`
41
+ # : The backup operation failed.
42
+ # : Notification will be sent if `on_warning` or `on_success` is `true`.
43
+ #
44
+ def notify!(status)
45
+ send_message(message.call(model, :status => status_data_for(status)))
46
+ end
47
+
48
+ def send_message(message)
49
+ uri = "https://#{ subdomain }.campfirenow.com/room/#{ room_id }/speak.json"
50
+ options = {
51
+ :headers => { 'Content-Type' => 'application/json' },
52
+ :body => JSON.dump(
53
+ { :message => { :body => message, :type => 'Textmessage' } }
54
+ )
55
+ }
56
+ options.merge!(:user => api_token, :password => 'x') # Basic Auth
57
+ options.merge!(:expects => 201) # raise error if unsuccessful
58
+ Excon.post(uri, options)
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,99 @@
1
+ # encoding: utf-8
2
+
3
+ module SlidayBackup
4
+ module Notifier
5
+ class Command < Base
6
+
7
+ ##
8
+ # Command to execute.
9
+ #
10
+ # Make sure it is accessible from your $PATH, or provide
11
+ # the absolute path to the command.
12
+ attr_accessor :command
13
+
14
+ ##
15
+ # Arguments to pass to the command.
16
+ #
17
+ # Must be an array of strings or callable objects.
18
+ #
19
+ # Callables will be invoked with #call(model, status),
20
+ # and the return value used as the argument.
21
+ #
22
+ # In strings you can use the following placeholders:
23
+ #
24
+ # %l - Model label
25
+ # %t - Model trigger
26
+ # %s - Status (success/failure/warning)
27
+ # %v - Status verb (succeeded/failed/succeeded with warnings)
28
+ #
29
+ # All placeholders can be used with uppercase letters to capitalize
30
+ # the value.
31
+ #
32
+ # Defaults to ["%L %v"]
33
+ attr_accessor :args
34
+
35
+ def initialize(model, &block)
36
+ super
37
+ instance_eval(&block) if block_given?
38
+
39
+ @args ||= ["%L %v"]
40
+ end
41
+
42
+ private
43
+
44
+ ##
45
+ # Notify the user of the backup operation results.
46
+ #
47
+ # `status` indicates one of the following:
48
+ #
49
+ # `:success`
50
+ # : The backup completed successfully.
51
+ # : Notification will be sent if `on_success` is `true`.
52
+ #
53
+ # `:warning`
54
+ # : The backup completed successfully, but warnings were logged.
55
+ # : Notification will be sent if `on_warning` or `on_success` is `true`.
56
+ #
57
+ # `:failure`
58
+ # : The backup operation failed.
59
+ # : Notification will be sent if `on_warning` or `on_success` is `true`.
60
+ #
61
+ def notify!(status)
62
+ IO.popen([@command] + args.map { |arg| format_arg(arg, status) })
63
+ end
64
+
65
+ def format_arg(arg, status)
66
+ if arg.respond_to?(:call)
67
+ arg.call(model, status)
68
+ else
69
+ arg.gsub(/%(\w)/) do |match|
70
+ ph = match[1]
71
+ val = case ph.downcase
72
+ when "l"
73
+ model.label
74
+ when "t"
75
+ model.trigger.to_s
76
+ when "v"
77
+ status_verb(status)
78
+ when "s"
79
+ status.to_s
80
+ end
81
+ val.capitalize! if ph == ph.upcase
82
+ val
83
+ end
84
+ end
85
+ end
86
+
87
+ def status_verb(status)
88
+ case status
89
+ when :success
90
+ "succeeded"
91
+ when :failure
92
+ "failed"
93
+ when :warning
94
+ "succeeded with warnings"
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,107 @@
1
+ # encoding: utf-8
2
+ require 'dogapi'
3
+
4
+ module SlidayBackup
5
+ module Notifier
6
+ class DataDog < Base
7
+
8
+ ##
9
+ # The DataDog API key
10
+ attr_accessor :api_key
11
+
12
+ ##
13
+ # The title of the event
14
+ attr_accessor :title
15
+
16
+ attr_deprecate :text,
17
+ :version => '4.2',
18
+ :message => 'Please use the `message` attribute. For more information '\
19
+ 'see https://github.com/backup/backup/pull/698'
20
+
21
+ ##
22
+ # The timestamp for the event
23
+ attr_accessor :date_happened
24
+
25
+ ##
26
+ # The priority of the event (low/normal)
27
+ attr_accessor :priority
28
+
29
+ ##
30
+ # The host that generated the event
31
+ attr_accessor :host
32
+
33
+ ##
34
+ # The tags for this host (should be an array)
35
+ attr_accessor :tags
36
+
37
+ ##
38
+ # The alert_type of the event (error/warning/info/success)
39
+ attr_accessor :alert_type
40
+
41
+ ##
42
+ # The aggregation_key for the event
43
+ attr_accessor :aggregation_key
44
+
45
+ ##
46
+ # The source_type for the event (nagios, hudson, jenkins, user, my apps, feed, chef, puppet, git, bitbucket, fabric, capistrano)
47
+ attr_accessor :source_type_name
48
+
49
+ def initialize(model, &block)
50
+ super
51
+ instance_eval(&block) if block_given?
52
+ @title ||= "SlidayBackup #{ model.label }"
53
+ end
54
+
55
+ private
56
+
57
+ ##
58
+ # Notify the user of the backup operation results.
59
+ #
60
+ # `status` indicates one of the following:
61
+ #
62
+ # `:success`
63
+ # : The backup completed successfully.
64
+ # : Notification will be sent if `on_success` is `true`.
65
+ #
66
+ # `:warning`
67
+ # : The backup completed successfully, but warnings were logged.
68
+ # : Notification will be sent if `on_warning` or `on_success` is `true`.
69
+ #
70
+ # `:failure`
71
+ # : The backup operation failed.
72
+ # : Notification will be sent if `on_warning` or `on_success` is `true`.
73
+ #
74
+ def notify!(status)
75
+ msg = message.call(model, :status => status_data_for(status))
76
+
77
+ hash = { alert_type: default_alert_type(status) }
78
+ hash.store(:msg_title, @title)
79
+ hash.store(:date_happened, @date_happened) if @date_happened
80
+ hash.store(:priority, @priority) if @priority
81
+ hash.store(:host, @host) if @host
82
+ hash.store(:tags, @tags) if @tags
83
+ hash.store(:aggregation_key, @aggregation_key) if @aggregation_key
84
+ hash.store(:source_type_name, @source_type_name) if @source_type_name
85
+ hash.store(:alert_type, @alert_type) if @alert_type
86
+ send_event(msg, hash)
87
+ end
88
+
89
+ # Dogapi::Client will raise an error if unsuccessful.
90
+ def send_event(msg, hash)
91
+ client = Dogapi::Client.new(@api_key)
92
+ event = Dogapi::Event.new(msg, hash)
93
+ client.emit_event(event)
94
+ end
95
+
96
+ # set alert type
97
+ def default_alert_type(status)
98
+ case status
99
+ when :success then 'success'
100
+ when :warning then 'warning'
101
+ when :failure then 'error'
102
+ end
103
+ end
104
+
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,103 @@
1
+ # encoding: utf-8
2
+ require 'flowdock'
3
+
4
+ module SlidayBackup
5
+ module Notifier
6
+ class FlowDock < Base
7
+
8
+ ##
9
+ # The Flowdock API token
10
+ attr_accessor :token
11
+
12
+ ##
13
+ # Who the notification should appear from
14
+ attr_accessor :from_name
15
+
16
+ # Which email the notification should appear from
17
+ attr_accessor :from_email
18
+
19
+ ##
20
+ # source for message
21
+ attr_accessor :source
22
+
23
+ ##
24
+ # Subject for message
25
+ attr_accessor :subject
26
+
27
+ ##
28
+ # tag message in inbox
29
+ attr_accessor :tags
30
+
31
+ ##
32
+ # link for message
33
+ attr_accessor :link
34
+
35
+ def initialize(model, &block)
36
+ super
37
+ instance_eval(&block) if block_given?
38
+
39
+ @subject ||= default_subject
40
+ @source ||= default_source
41
+ @tags ||= []
42
+ end
43
+
44
+ private
45
+
46
+ ##
47
+ # Notify the user of the backup operation results.
48
+ #
49
+ # `status` indicates one of the following:
50
+ #
51
+ # `:success`
52
+ # : The backup completed successfully.
53
+ # : Notification will be sent if `on_success` is `true`.
54
+ #
55
+ # `:warning`
56
+ # : The backup completed successfully, but warnings were logged.
57
+ # : Notification will be sent if `on_warning` or `on_success` is `true`.
58
+ #
59
+ # `:failure`
60
+ # : The backup operation failed.
61
+ # : Notification will be sent if `on_warning` or `on_success` is `true`.
62
+ #
63
+ def notify!(status)
64
+ @tags += default_tags(status)
65
+ send_message(message.call(model, status: status_data_for(status)))
66
+ end
67
+
68
+ # Flowdock::Client will raise an error if unsuccessful.
69
+ def send_message(msg)
70
+ client = Flowdock::Flow.new(
71
+ :api_token => token, :source => source,
72
+ :from => {:name => from_name, :address => from_email }
73
+ )
74
+
75
+ client.push_to_team_inbox(:subject => subject,
76
+ :content => msg,
77
+ :tags => tags,
78
+ :link => link )
79
+ end
80
+
81
+ # set related tags
82
+ def default_tags(status)
83
+ case status
84
+ when :success then ['#SlidayBackupSuccess']
85
+ when :warning then ['#SlidayBackupWarning']
86
+ when :failure then ['#SlidayBackupFailure']
87
+ end
88
+ end
89
+
90
+
91
+ #set default source
92
+ def default_source
93
+ "SlidayBackup #{ model.label }"
94
+ end
95
+
96
+ # set default subject
97
+ def default_subject
98
+ 'SlidayBackup Notification'
99
+ end
100
+
101
+ end
102
+ end
103
+ end