backup 3.0.27 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (166) hide show
  1. data/LICENSE.md +1 -1
  2. data/README.md +139 -386
  3. data/bin/backup +1 -7
  4. data/lib/backup.rb +3 -9
  5. data/lib/backup/archive.rb +26 -20
  6. data/lib/backup/cleaner.rb +2 -2
  7. data/lib/backup/cli.rb +366 -0
  8. data/lib/backup/compressor/base.rb +2 -2
  9. data/lib/backup/compressor/gzip.rb +35 -1
  10. data/lib/backup/config.rb +1 -2
  11. data/lib/backup/database/base.rb +2 -2
  12. data/lib/backup/database/mongodb.rb +3 -3
  13. data/lib/backup/database/mysql.rb +3 -2
  14. data/lib/backup/database/postgresql.rb +3 -2
  15. data/lib/backup/database/riak.rb +18 -5
  16. data/lib/backup/dependency.rb +144 -93
  17. data/lib/backup/encryptor/base.rb +2 -2
  18. data/lib/backup/logger.rb +108 -110
  19. data/lib/backup/logger/console.rb +51 -0
  20. data/lib/backup/logger/logfile.rb +113 -0
  21. data/lib/backup/logger/syslog.rb +116 -0
  22. data/lib/backup/model.rb +67 -65
  23. data/lib/backup/notifier/base.rb +1 -1
  24. data/lib/backup/notifier/hipchat.rb +1 -1
  25. data/lib/backup/notifier/mail.rb +1 -1
  26. data/lib/backup/notifier/pushover.rb +6 -3
  27. data/lib/backup/packager.rb +4 -4
  28. data/lib/backup/pipeline.rb +17 -3
  29. data/lib/backup/splitter.rb +2 -2
  30. data/lib/backup/storage/base.rb +2 -2
  31. data/lib/backup/storage/cloudfiles.rb +2 -2
  32. data/lib/backup/storage/dropbox.rb +4 -4
  33. data/lib/backup/storage/ftp.rb +2 -2
  34. data/lib/backup/storage/local.rb +2 -2
  35. data/lib/backup/storage/ninefold.rb +2 -2
  36. data/lib/backup/storage/rsync.rb +3 -3
  37. data/lib/backup/storage/s3.rb +2 -2
  38. data/lib/backup/storage/scp.rb +2 -6
  39. data/lib/backup/storage/sftp.rb +2 -5
  40. data/lib/backup/syncer/base.rb +1 -1
  41. data/lib/backup/syncer/cloud/base.rb +15 -8
  42. data/lib/backup/syncer/rsync/local.rb +1 -1
  43. data/lib/backup/syncer/rsync/pull.rb +1 -1
  44. data/lib/backup/syncer/rsync/push.rb +1 -1
  45. data/lib/backup/utilities.rb +211 -0
  46. data/lib/backup/version.rb +1 -1
  47. data/templates/cli/{utility/archive → archive} +4 -8
  48. data/templates/cli/{utility/compressor → compressor}/bzip2 +0 -0
  49. data/templates/cli/{utility/compressor → compressor}/custom +0 -0
  50. data/templates/cli/{utility/compressor → compressor}/gzip +0 -0
  51. data/templates/cli/{utility/compressor → compressor}/lzma +0 -0
  52. data/templates/cli/{utility/compressor → compressor}/pbzip2 +0 -0
  53. data/templates/cli/config +68 -0
  54. data/templates/cli/{utility/database → database}/mongodb +1 -1
  55. data/templates/cli/{utility/database → database}/mysql +1 -1
  56. data/templates/cli/{utility/database → database}/postgresql +1 -1
  57. data/templates/cli/{utility/database → database}/redis +0 -0
  58. data/templates/cli/database/riak +20 -0
  59. data/templates/cli/{utility/encryptor → encryptor}/gpg +0 -0
  60. data/templates/cli/{utility/encryptor → encryptor}/openssl +0 -0
  61. data/templates/cli/{utility/model.erb → model.erb} +4 -4
  62. data/templates/cli/{utility/notifier → notifier}/campfire +0 -0
  63. data/templates/cli/{utility/notifier → notifier}/hipchat +0 -0
  64. data/templates/cli/{utility/notifier → notifier}/mail +0 -0
  65. data/templates/cli/{utility/notifier → notifier}/prowl +0 -0
  66. data/templates/cli/{utility/notifier → notifier}/pushover +0 -0
  67. data/templates/cli/{utility/notifier → notifier}/twitter +0 -0
  68. data/templates/cli/{utility/splitter → splitter} +0 -0
  69. data/templates/cli/{utility/storage → storage}/cloud_files +0 -0
  70. data/templates/cli/{utility/storage → storage}/dropbox +0 -0
  71. data/templates/cli/{utility/storage → storage}/ftp +0 -0
  72. data/templates/cli/{utility/storage → storage}/local +0 -0
  73. data/templates/cli/{utility/storage → storage}/ninefold +0 -0
  74. data/templates/cli/{utility/storage → storage}/rsync +0 -0
  75. data/templates/cli/{utility/storage → storage}/s3 +0 -0
  76. data/templates/cli/{utility/storage → storage}/scp +0 -0
  77. data/templates/cli/{utility/storage → storage}/sftp +0 -0
  78. data/templates/cli/{utility/syncer → syncer}/cloud_files +0 -0
  79. data/templates/cli/{utility/syncer → syncer}/rsync_local +0 -0
  80. data/templates/cli/{utility/syncer → syncer}/rsync_pull +0 -0
  81. data/templates/cli/{utility/syncer → syncer}/rsync_push +0 -0
  82. data/templates/cli/{utility/syncer → syncer}/s3 +0 -0
  83. metadata +55 -131
  84. data/.gitignore +0 -8
  85. data/.travis.yml +0 -10
  86. data/Gemfile +0 -28
  87. data/Guardfile +0 -23
  88. data/backup.gemspec +0 -32
  89. data/lib/backup/cli/helpers.rb +0 -93
  90. data/lib/backup/cli/utility.rb +0 -255
  91. data/spec-live/.gitignore +0 -6
  92. data/spec-live/README +0 -7
  93. data/spec-live/backups/config.rb +0 -83
  94. data/spec-live/backups/config.yml.template +0 -46
  95. data/spec-live/backups/models.rb +0 -184
  96. data/spec-live/compressor/custom_spec.rb +0 -30
  97. data/spec-live/compressor/gzip_spec.rb +0 -30
  98. data/spec-live/encryptor/gpg_keys.rb +0 -239
  99. data/spec-live/encryptor/gpg_spec.rb +0 -287
  100. data/spec-live/notifier/mail_spec.rb +0 -121
  101. data/spec-live/spec_helper.rb +0 -151
  102. data/spec-live/storage/dropbox_spec.rb +0 -151
  103. data/spec-live/storage/local_spec.rb +0 -83
  104. data/spec-live/storage/scp_spec.rb +0 -193
  105. data/spec-live/syncer/cloud/s3_spec.rb +0 -124
  106. data/spec/archive_spec.rb +0 -335
  107. data/spec/cleaner_spec.rb +0 -312
  108. data/spec/cli/helpers_spec.rb +0 -301
  109. data/spec/cli/utility_spec.rb +0 -411
  110. data/spec/compressor/base_spec.rb +0 -52
  111. data/spec/compressor/bzip2_spec.rb +0 -217
  112. data/spec/compressor/custom_spec.rb +0 -106
  113. data/spec/compressor/gzip_spec.rb +0 -217
  114. data/spec/compressor/lzma_spec.rb +0 -123
  115. data/spec/compressor/pbzip2_spec.rb +0 -165
  116. data/spec/config_spec.rb +0 -321
  117. data/spec/configuration/helpers_spec.rb +0 -247
  118. data/spec/configuration/store_spec.rb +0 -39
  119. data/spec/configuration_spec.rb +0 -62
  120. data/spec/database/base_spec.rb +0 -63
  121. data/spec/database/mongodb_spec.rb +0 -510
  122. data/spec/database/mysql_spec.rb +0 -411
  123. data/spec/database/postgresql_spec.rb +0 -353
  124. data/spec/database/redis_spec.rb +0 -334
  125. data/spec/database/riak_spec.rb +0 -176
  126. data/spec/dependency_spec.rb +0 -51
  127. data/spec/encryptor/base_spec.rb +0 -40
  128. data/spec/encryptor/gpg_spec.rb +0 -909
  129. data/spec/encryptor/open_ssl_spec.rb +0 -148
  130. data/spec/errors_spec.rb +0 -306
  131. data/spec/logger_spec.rb +0 -367
  132. data/spec/model_spec.rb +0 -666
  133. data/spec/notifier/base_spec.rb +0 -104
  134. data/spec/notifier/campfire_spec.rb +0 -217
  135. data/spec/notifier/hipchat_spec.rb +0 -211
  136. data/spec/notifier/mail_spec.rb +0 -316
  137. data/spec/notifier/prowl_spec.rb +0 -138
  138. data/spec/notifier/pushover_spec.rb +0 -123
  139. data/spec/notifier/twitter_spec.rb +0 -153
  140. data/spec/package_spec.rb +0 -61
  141. data/spec/packager_spec.rb +0 -213
  142. data/spec/pipeline_spec.rb +0 -259
  143. data/spec/spec_helper.rb +0 -60
  144. data/spec/splitter_spec.rb +0 -120
  145. data/spec/storage/base_spec.rb +0 -166
  146. data/spec/storage/cloudfiles_spec.rb +0 -254
  147. data/spec/storage/cycler_spec.rb +0 -247
  148. data/spec/storage/dropbox_spec.rb +0 -480
  149. data/spec/storage/ftp_spec.rb +0 -271
  150. data/spec/storage/local_spec.rb +0 -259
  151. data/spec/storage/ninefold_spec.rb +0 -343
  152. data/spec/storage/rsync_spec.rb +0 -362
  153. data/spec/storage/s3_spec.rb +0 -245
  154. data/spec/storage/scp_spec.rb +0 -233
  155. data/spec/storage/sftp_spec.rb +0 -244
  156. data/spec/syncer/base_spec.rb +0 -109
  157. data/spec/syncer/cloud/base_spec.rb +0 -515
  158. data/spec/syncer/cloud/cloud_files_spec.rb +0 -181
  159. data/spec/syncer/cloud/s3_spec.rb +0 -174
  160. data/spec/syncer/rsync/base_spec.rb +0 -98
  161. data/spec/syncer/rsync/local_spec.rb +0 -149
  162. data/spec/syncer/rsync/pull_spec.rb +0 -98
  163. data/spec/syncer/rsync/push_spec.rb +0 -333
  164. data/spec/version_spec.rb +0 -21
  165. data/templates/cli/utility/config +0 -32
  166. data/templates/cli/utility/database/riak +0 -11
@@ -3,7 +3,7 @@
3
3
  module Backup
4
4
  module Encryptor
5
5
  class Base
6
- include Backup::CLI::Helpers
6
+ include Backup::Utilities::Helpers
7
7
  include Backup::Configuration::Helpers
8
8
 
9
9
  def initialize
@@ -22,7 +22,7 @@ module Backup
22
22
  # Logs a message to the console and log file to inform
23
23
  # the client that Backup is encrypting the archive
24
24
  def log!
25
- Logger.message "Using #{ encryptor_name } to encrypt the archive."
25
+ Logger.info "Using #{ encryptor_name } to encrypt the archive."
26
26
  end
27
27
  end
28
28
  end
data/lib/backup/logger.rb CHANGED
@@ -1,152 +1,150 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'backup/logger/console'
4
+ require 'backup/logger/logfile'
5
+ require 'backup/logger/syslog'
6
+
3
7
  module Backup
4
8
  module Logger
5
- class << self
6
-
7
- attr_accessor :quiet
8
9
 
9
- ##
10
- # Outputs a messages to the console and writes it to the backup.log
11
- def message(string)
12
- to_console loggify(string, :message, :green)
13
- to_file loggify(string, :message)
10
+ class Config
11
+ class Logger < Struct.new(:class, :options)
12
+ def enabled?
13
+ options.enabled?
14
+ end
14
15
  end
15
16
 
16
- ##
17
- # Outputs an error to the console and writes it to the backup.log
18
- # Called when an Exception has caused the backup process to abort.
19
- def error(string)
20
- to_console loggify(string, :error, :red), true
21
- to_file loggify(string, :error)
22
- end
17
+ DSL = Struct.new(:console, :logfile, :syslog)
23
18
 
24
- ##
25
- # Outputs a notice to the console and writes it to the backup.log
26
- # Sets #has_warnings? true so :on_warning notifications will be sent
27
- def warn(string)
28
- @has_warnings = true
29
- to_console loggify(string, :warning, :yellow), true
30
- to_file loggify(string, :warning)
31
- end
19
+ attr_reader :loggers, :dsl
32
20
 
33
- # Outputs the data as if it were a regular 'puts' command,
34
- # but also logs it to the backup.log
35
- def normal(string)
36
- to_console loggify(string)
37
- to_file loggify(string)
21
+ def initialize
22
+ @loggers = [
23
+ Logger.new(Console, Console::Options.new),
24
+ Logger.new(Logfile, Logfile::Options.new),
25
+ Logger.new(Syslog, Syslog::Options.new)
26
+ ]
27
+ @dsl = DSL.new(*@loggers.map(&:options))
38
28
  end
29
+ end
39
30
 
31
+ ##
32
+ # All messages sent to the Logger are stored in Logger.messages
33
+ # and sent to all enabled logger's #log method as Message objects.
34
+ class Message < Struct.new(:time, :level, :lines)
40
35
  ##
41
- # Silently logs data to the log file
42
- def silent(string)
43
- to_file loggify(string, :silent)
36
+ # Returns an Array of the message lines in the following format:
37
+ #
38
+ # [YYYY/MM/DD HH:MM:SS][level] message line text
39
+ def formatted_lines
40
+ timestamp = time.strftime("%Y/%m/%d %H:%M:%S")
41
+ lines.map {|line| "[#{ timestamp }][#{ level }] #{ line }" }
44
42
  end
43
+ end
45
44
 
45
+ class << self
46
46
  ##
47
- # Returns an Array of all messages written to the log file for this session
48
- def messages
49
- @messages ||= []
50
- end
47
+ # Returns an Array of Message objects for all logged messages received.
48
+ # These are used to attach log files to Mail notifications.
49
+ attr_reader :messages
51
50
 
52
51
  ##
53
- # Returns true if any warnings have been issued
54
- def has_warnings?
55
- @has_warnings ||= false
56
- end
57
-
58
- def clear!
59
- messages.clear
60
- @has_warnings = false
61
- end
62
-
63
- def truncate!(max_bytes = 500_000)
64
- log_file = File.join(Config.log_path, 'backup.log')
65
- return unless File.exist?(log_file)
66
-
67
- if File.stat(log_file).size > max_bytes
68
- FileUtils.mv(log_file, log_file + '~')
69
- File.open(log_file + '~', 'r') do |io_in|
70
- File.open(log_file, 'w') do |io_out|
71
- io_in.seek(-max_bytes, IO::SEEK_END) && io_in.gets
72
- while line = io_in.gets
73
- io_out.puts line
74
- end
75
- end
76
- end
77
- FileUtils.rm_f(log_file + '~')
78
- end
79
- end
80
-
81
- private
82
-
83
- ##
84
- # Returns the time in [YYYY/MM/DD HH:MM:SS] format
85
- def time
86
- Time.now.strftime("%Y/%m/%d %H:%M:%S")
52
+ # Allows the Logger to be configured.
53
+ #
54
+ # # shown with their default values
55
+ # Backup::Logger.configure do
56
+ # # Console options:
57
+ # console.quiet = false
58
+ #
59
+ # # Logfile options:
60
+ # logfile.enabled = true
61
+ # logfile.log_path = 'log'
62
+ # logfile.max_bytes = 500_000
63
+ #
64
+ # # Syslog options:
65
+ # syslog.enabled = false
66
+ # syslog.ident = 'backup'
67
+ # syslog.options = Syslog::LOG_PID
68
+ # syslog.facility = Syslog::LOG_LOCAL0
69
+ # syslog.info = Syslog::LOG_INFO
70
+ # syslog.warn = Syslog::LOG_WARNING
71
+ # syslog.error = Syslog::LOG_ERR
72
+ # end
73
+ #
74
+ # See each Logger's Option class for details.
75
+ # @see Console::Options
76
+ # @see Logfile::Options
77
+ # @see Syslog::Options
78
+ def configure(&block)
79
+ @config.dsl.instance_eval(&block)
87
80
  end
88
81
 
89
82
  ##
90
- # Receives a String, or an Object that responds to #to_s (e.g. an
91
- # Exception), from one of the messaging methods and converts it into an
92
- # Array of Strings, split on newline separators. Each line is then
93
- # formatted into a log format based on the given options, and the Array
94
- # returned to be passed to to_console() and/or to_file().
95
- def loggify(string, type = false, color = false)
96
- lines = string.to_s.split("\n")
97
- if type
98
- type = send(color, type) if color
99
- time_now = time
100
- lines.map {|line| "[#{time_now}][#{type}] #{line}" }
101
- else
102
- lines
103
- end
83
+ # Sends a message to the Logger using the specified log level.
84
+ # +obj+ may be any Object that responds to #to_s (i.e. an Exception)
85
+ [:info, :warn, :error].each do |level|
86
+ define_method level, lambda {|obj| log(obj, level) }
104
87
  end
105
88
 
106
89
  ##
107
- # Receives an Array of Strings to be written to the console.
108
- def to_console(lines, stderr = false)
109
- return if quiet
110
- lines.each {|line| stderr ? Kernel.warn(line) : puts(line) }
90
+ # Returns true if any +:warn+ level messages have been received.
91
+ def has_warnings?
92
+ @has_warnings
111
93
  end
112
94
 
113
95
  ##
114
- # Receives an Array of Strings to be written to the log file.
115
- def to_file(lines)
116
- File.open(File.join(Config.log_path, 'backup.log'), 'a') do |file|
117
- lines.each {|line| file.puts line }
96
+ # The Logger is available as soon as Backup is loaded, and stores all
97
+ # messages it receives. Since the Logger may be configured via the
98
+ # command line and/or the user's +config.rb+, no messages are sent
99
+ # until configuration can be completed. (see CLI#perform)
100
+ #
101
+ # Once configuration is completed, this method is called to activate
102
+ # all enabled loggers and send them any messages that have been received
103
+ # up to this point. From this point onward, these loggers will be sent
104
+ # all messages as soon as they're received.
105
+ def start!
106
+ @config.loggers.each do |logger|
107
+ @loggers << logger.class.new(logger.options) if logger.enabled?
108
+ end
109
+ @messages.each do |message|
110
+ @loggers.each {|logger| logger.log(message) }
118
111
  end
119
- messages.push(*lines)
120
112
  end
121
113
 
122
114
  ##
123
- # Invokes the #colorize method with the provided string
124
- # and the color code "32" (for green)
125
- def green(string)
126
- colorize(string, 32)
115
+ # Called after each backup model/trigger has been performed.
116
+ def clear!
117
+ messages.clear
118
+ @has_warnings = false
127
119
  end
128
120
 
129
121
  ##
130
- # Invokes the #colorize method with the provided string
131
- # and the color code "33" (for yellow)
132
- def yellow(string)
133
- colorize(string, 33)
122
+ # If errors are encountered by Backup::CLI while preparing to perform
123
+ # the backup jobs, this method is called to dump all messages to the
124
+ # console before Backup exits.
125
+ def abort!
126
+ console = Console.new
127
+ console.log(@messages.shift) until @messages.empty?
134
128
  end
135
129
 
136
- ##
137
- # Invokes the #colorize method the with provided string
138
- # and the color code "31" (for red)
139
- def red(string)
140
- colorize(string, 31)
130
+ private
131
+
132
+ def initialize!
133
+ @messages = []
134
+ @loggers = []
135
+ @config = Config.new
136
+ @has_warnings = false
141
137
  end
142
138
 
143
- ##
144
- # Wraps the provided string in colorizing tags to provide
145
- # easier to view output to the client
146
- def colorize(string, code)
147
- "\e[#{code}m#{string}\e[0m"
139
+ def log(obj, level)
140
+ @has_warnings ||= level == :warn
141
+ message = Message.new(Time.now, level, obj.to_s.split("\n"))
142
+ @messages << message
143
+ @loggers.each {|logger| logger.log(message) }
148
144
  end
149
145
 
150
146
  end
147
+
148
+ initialize!
151
149
  end
152
150
  end
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+
3
+ module Backup
4
+ module Logger
5
+ class Console
6
+ class Options
7
+ ##
8
+ # Disables all console output.
9
+ #
10
+ # This may also be set on the command line using +--quiet+.
11
+ #
12
+ # If +--no-quiet+ is used on the command line, console output
13
+ # will be enabled and any setting here will be ignored.
14
+ #
15
+ # @param [Boolean, nil]
16
+ # @return [Boolean, nil] Default: +false+
17
+ attr_reader :quiet
18
+
19
+ def initialize
20
+ @quiet = false
21
+ end
22
+
23
+ def enabled?
24
+ !quiet
25
+ end
26
+
27
+ def quiet=(val)
28
+ @quiet = val unless quiet.nil?
29
+ end
30
+ end
31
+
32
+ COLORS = {
33
+ :info => "\e[32m%s\e[0m", # green
34
+ :warn => "\e[33m%s\e[0m", # yellow
35
+ :error => "\e[31m%s\e[0m" # red
36
+ }
37
+
38
+ def initialize(options = nil)
39
+ $stdout.sync = $stderr.sync = true
40
+ end
41
+
42
+ def log(message)
43
+ io = message.level == :info ? $stdout : $stderr
44
+ lines = message.formatted_lines
45
+ lines.map! {|line| COLORS[message.level] % line } if io.tty?
46
+ io.puts lines
47
+ end
48
+
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,113 @@
1
+ # encoding: utf-8
2
+
3
+ module Backup
4
+ module Logger
5
+ class Logfile
6
+ class Options
7
+ ##
8
+ # Enable the use of Backup's log file.
9
+ #
10
+ # While not necessary, as this is +true+ by default,
11
+ # this may also be set on the command line using +--logfile+.
12
+ #
13
+ # The use of Backup's log file may be disabled using the
14
+ # command line option +--no-logfile+.
15
+ #
16
+ # If +--no--logfile+ is used on the command line, then the
17
+ # log file will be disabled and any setting here will be ignored.
18
+ #
19
+ # @param [Boolean, nil]
20
+ # @return [Boolean, nil] Default: +true+
21
+ attr_reader :enabled
22
+
23
+ ##
24
+ # Path to directory where Backup's logfile will be written.
25
+ #
26
+ # This may be given as an absolute path, or a path relative
27
+ # to Backup's +--root-path+ (which defaults to +~/Backup+).
28
+ #
29
+ # This may also be set on the command line using +--log-path+.
30
+ # If set on the command line, any setting here will be ignored.
31
+ #
32
+ # @param [String]
33
+ # @return [String] Default: 'log'
34
+ attr_reader :log_path
35
+
36
+ ##
37
+ # Size in bytes to truncate logfile to before backup jobs are run.
38
+ #
39
+ # This is done once before all +triggers+, so the maximum logfile size
40
+ # would be this value plus whatever the jobs produce.
41
+ #
42
+ # @param [Integer]
43
+ # @return [Integer] Default: +500_000+
44
+ attr_accessor :max_bytes
45
+
46
+ def initialize
47
+ @enabled = true
48
+ @log_path = ''
49
+ @max_bytes = 500_000
50
+ end
51
+
52
+ def enabled?
53
+ !!enabled
54
+ end
55
+
56
+ def enabled=(val)
57
+ @enabled = val unless enabled.nil?
58
+ end
59
+
60
+ def log_path=(val)
61
+ @log_path = val.to_s.strip if log_path.empty?
62
+ end
63
+ end
64
+
65
+ def initialize(options)
66
+ @options = options
67
+ @logfile = setup_logfile
68
+ truncate!
69
+ end
70
+
71
+ def log(message)
72
+ File.open(@logfile, 'a') {|f| f.puts message.formatted_lines }
73
+ end
74
+
75
+ private
76
+
77
+ ##
78
+ # Returns the full path to the log file, based on the configured
79
+ # @options.log_path, and ensures the path to the log file exists.
80
+ def setup_logfile
81
+ # strip any trailing '/' in case the user supplied this as part of
82
+ # an absolute path, so we can match it against File.expand_path()
83
+ path = @options.log_path.chomp('/')
84
+ if path.empty?
85
+ path = File.join(Backup::Config.root_path, 'log')
86
+ elsif path != File.expand_path(path)
87
+ path = File.join(Backup::Config.root_path, path)
88
+ end
89
+ FileUtils.mkdir_p(path)
90
+ File.join(path, 'backup.log')
91
+ end
92
+
93
+ ##
94
+ # Truncates the logfile to @options.max_bytes
95
+ def truncate!
96
+ return unless File.exist?(@logfile)
97
+
98
+ if File.stat(@logfile).size > @options.max_bytes
99
+ FileUtils.mv(@logfile, @logfile + '~')
100
+ File.open(@logfile + '~', 'r') do |io_in|
101
+ File.open(@logfile, 'w') do |io_out|
102
+ io_in.seek(-@options.max_bytes, IO::SEEK_END) && io_in.gets
103
+ while line = io_in.gets
104
+ io_out.puts line
105
+ end
106
+ end
107
+ end
108
+ FileUtils.rm_f(@logfile + '~')
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,116 @@
1
+ # encoding: utf-8
2
+
3
+ module Backup
4
+ module Logger
5
+ class Syslog
6
+ class Options
7
+ ##
8
+ # Enables logging to the system's Syslog compatible logger.
9
+ #
10
+ # This may also be enabled using +--syslog+ on the command line.
11
+ #
12
+ # If +--no-syslog+ is used on the command line, this will be
13
+ # disabled and any settings here will be ignored.
14
+ #
15
+ # @param [Boolean, nil]
16
+ # @return [Boolean, nil] Default: +false+
17
+ attr_reader :enabled
18
+
19
+ ##
20
+ # Specify the identification string to be used with Syslog.
21
+ #
22
+ # @param [String]
23
+ # @return [String] Default: 'backup'
24
+ attr_accessor :ident
25
+
26
+ ##
27
+ # Specify the options to be used with Syslog.
28
+ #
29
+ # See the Ruby Standard Library documentation for +Syslog+ for more info.
30
+ # http://rdoc.info/stdlib/syslog/Syslog.open
31
+ #
32
+ # Note that setting this to +nil+ will cause this to default
33
+ # to a setting of +Syslog::LOG_PID | Syslog::LOG_CONS+
34
+ #
35
+ # @param [Integer]
36
+ # @return [Integer] Default: +Syslog::LOG_PID+
37
+ attr_accessor :options
38
+
39
+ ##
40
+ # Specify the facility to be used with Syslog.
41
+ #
42
+ # See the Ruby Standard Library documentation for +Syslog+ for more info.
43
+ # http://rdoc.info/stdlib/syslog/Syslog.open
44
+ #
45
+ # Note that setting this to +nil+ will cause this to default
46
+ # to a setting of +Syslog::LOG_USER+
47
+ #
48
+ # @param [Integer]
49
+ # @return [Integer] Default: +Syslog::LOG_LOCAL0+
50
+ attr_accessor :facility
51
+
52
+ ##
53
+ # Specify the priority level to be used for +:info+ messages.
54
+ #
55
+ # See the Ruby Standard Library documentation for +Syslog+ for more info.
56
+ # http://rdoc.info/stdlib/syslog/Syslog.log
57
+ #
58
+ # @param [Integer]
59
+ # @return [Integer] Default: +Syslog::LOG_INFO+
60
+ attr_accessor :info
61
+
62
+ ##
63
+ # Specify the priority level to be used for +:warn+ messages.
64
+ #
65
+ # See the Ruby Standard Library documentation for +Syslog+ for more info.
66
+ # http://rdoc.info/stdlib/syslog/Syslog.log
67
+ #
68
+ # @param [Integer]
69
+ # @return [Integer] Default: +Syslog::LOG_WARNING+
70
+ attr_accessor :warn
71
+
72
+ ##
73
+ # Specify the priority level to be used for +:error+ messages.
74
+ #
75
+ # See the Ruby Standard Library documentation for +Syslog+ for more info.
76
+ # http://rdoc.info/stdlib/syslog/Syslog.log
77
+ #
78
+ # @param [Integer]
79
+ # @return [Integer] Default: +Syslog::LOG_ERR+
80
+ attr_accessor :error
81
+
82
+ def initialize
83
+ @enabled = false
84
+ @ident = 'backup'
85
+ @options = ::Syslog::LOG_PID
86
+ @facility = ::Syslog::LOG_LOCAL0
87
+ @info = ::Syslog::LOG_INFO
88
+ @warn = ::Syslog::LOG_WARNING
89
+ @error = ::Syslog::LOG_ERR
90
+ end
91
+
92
+ def enabled?
93
+ !!enabled
94
+ end
95
+
96
+ def enabled=(val)
97
+ @enabled = val unless enabled.nil?
98
+ end
99
+ end
100
+
101
+ def initialize(options)
102
+ @options = options
103
+ end
104
+
105
+ ##
106
+ # Message lines are sent without formatting (timestamp, level),
107
+ # since Syslog will provide it's own timestamp and priority.
108
+ def log(message)
109
+ level = @options.send(message.level)
110
+ ::Syslog.open(@options.ident, @options.options, @options.facility) do |s|
111
+ message.lines.each {|line| s.log(level, line) }
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end