backup 3.0.27 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.md +1 -1
- data/README.md +139 -386
- data/bin/backup +1 -7
- data/lib/backup.rb +3 -9
- data/lib/backup/archive.rb +26 -20
- data/lib/backup/cleaner.rb +2 -2
- data/lib/backup/cli.rb +366 -0
- data/lib/backup/compressor/base.rb +2 -2
- data/lib/backup/compressor/gzip.rb +35 -1
- data/lib/backup/config.rb +1 -2
- data/lib/backup/database/base.rb +2 -2
- data/lib/backup/database/mongodb.rb +3 -3
- data/lib/backup/database/mysql.rb +3 -2
- data/lib/backup/database/postgresql.rb +3 -2
- data/lib/backup/database/riak.rb +18 -5
- data/lib/backup/dependency.rb +144 -93
- data/lib/backup/encryptor/base.rb +2 -2
- data/lib/backup/logger.rb +108 -110
- data/lib/backup/logger/console.rb +51 -0
- data/lib/backup/logger/logfile.rb +113 -0
- data/lib/backup/logger/syslog.rb +116 -0
- data/lib/backup/model.rb +67 -65
- data/lib/backup/notifier/base.rb +1 -1
- data/lib/backup/notifier/hipchat.rb +1 -1
- data/lib/backup/notifier/mail.rb +1 -1
- data/lib/backup/notifier/pushover.rb +6 -3
- data/lib/backup/packager.rb +4 -4
- data/lib/backup/pipeline.rb +17 -3
- data/lib/backup/splitter.rb +2 -2
- data/lib/backup/storage/base.rb +2 -2
- data/lib/backup/storage/cloudfiles.rb +2 -2
- data/lib/backup/storage/dropbox.rb +4 -4
- data/lib/backup/storage/ftp.rb +2 -2
- data/lib/backup/storage/local.rb +2 -2
- data/lib/backup/storage/ninefold.rb +2 -2
- data/lib/backup/storage/rsync.rb +3 -3
- data/lib/backup/storage/s3.rb +2 -2
- data/lib/backup/storage/scp.rb +2 -6
- data/lib/backup/storage/sftp.rb +2 -5
- data/lib/backup/syncer/base.rb +1 -1
- data/lib/backup/syncer/cloud/base.rb +15 -8
- data/lib/backup/syncer/rsync/local.rb +1 -1
- data/lib/backup/syncer/rsync/pull.rb +1 -1
- data/lib/backup/syncer/rsync/push.rb +1 -1
- data/lib/backup/utilities.rb +211 -0
- data/lib/backup/version.rb +1 -1
- data/templates/cli/{utility/archive → archive} +4 -8
- data/templates/cli/{utility/compressor → compressor}/bzip2 +0 -0
- data/templates/cli/{utility/compressor → compressor}/custom +0 -0
- data/templates/cli/{utility/compressor → compressor}/gzip +0 -0
- data/templates/cli/{utility/compressor → compressor}/lzma +0 -0
- data/templates/cli/{utility/compressor → compressor}/pbzip2 +0 -0
- data/templates/cli/config +68 -0
- data/templates/cli/{utility/database → database}/mongodb +1 -1
- data/templates/cli/{utility/database → database}/mysql +1 -1
- data/templates/cli/{utility/database → database}/postgresql +1 -1
- data/templates/cli/{utility/database → database}/redis +0 -0
- data/templates/cli/database/riak +20 -0
- data/templates/cli/{utility/encryptor → encryptor}/gpg +0 -0
- data/templates/cli/{utility/encryptor → encryptor}/openssl +0 -0
- data/templates/cli/{utility/model.erb → model.erb} +4 -4
- data/templates/cli/{utility/notifier → notifier}/campfire +0 -0
- data/templates/cli/{utility/notifier → notifier}/hipchat +0 -0
- data/templates/cli/{utility/notifier → notifier}/mail +0 -0
- data/templates/cli/{utility/notifier → notifier}/prowl +0 -0
- data/templates/cli/{utility/notifier → notifier}/pushover +0 -0
- data/templates/cli/{utility/notifier → notifier}/twitter +0 -0
- data/templates/cli/{utility/splitter → splitter} +0 -0
- data/templates/cli/{utility/storage → storage}/cloud_files +0 -0
- data/templates/cli/{utility/storage → storage}/dropbox +0 -0
- data/templates/cli/{utility/storage → storage}/ftp +0 -0
- data/templates/cli/{utility/storage → storage}/local +0 -0
- data/templates/cli/{utility/storage → storage}/ninefold +0 -0
- data/templates/cli/{utility/storage → storage}/rsync +0 -0
- data/templates/cli/{utility/storage → storage}/s3 +0 -0
- data/templates/cli/{utility/storage → storage}/scp +0 -0
- data/templates/cli/{utility/storage → storage}/sftp +0 -0
- data/templates/cli/{utility/syncer → syncer}/cloud_files +0 -0
- data/templates/cli/{utility/syncer → syncer}/rsync_local +0 -0
- data/templates/cli/{utility/syncer → syncer}/rsync_pull +0 -0
- data/templates/cli/{utility/syncer → syncer}/rsync_push +0 -0
- data/templates/cli/{utility/syncer → syncer}/s3 +0 -0
- metadata +55 -131
- data/.gitignore +0 -8
- data/.travis.yml +0 -10
- data/Gemfile +0 -28
- data/Guardfile +0 -23
- data/backup.gemspec +0 -32
- data/lib/backup/cli/helpers.rb +0 -93
- data/lib/backup/cli/utility.rb +0 -255
- data/spec-live/.gitignore +0 -6
- data/spec-live/README +0 -7
- data/spec-live/backups/config.rb +0 -83
- data/spec-live/backups/config.yml.template +0 -46
- data/spec-live/backups/models.rb +0 -184
- data/spec-live/compressor/custom_spec.rb +0 -30
- data/spec-live/compressor/gzip_spec.rb +0 -30
- data/spec-live/encryptor/gpg_keys.rb +0 -239
- data/spec-live/encryptor/gpg_spec.rb +0 -287
- data/spec-live/notifier/mail_spec.rb +0 -121
- data/spec-live/spec_helper.rb +0 -151
- data/spec-live/storage/dropbox_spec.rb +0 -151
- data/spec-live/storage/local_spec.rb +0 -83
- data/spec-live/storage/scp_spec.rb +0 -193
- data/spec-live/syncer/cloud/s3_spec.rb +0 -124
- data/spec/archive_spec.rb +0 -335
- data/spec/cleaner_spec.rb +0 -312
- data/spec/cli/helpers_spec.rb +0 -301
- data/spec/cli/utility_spec.rb +0 -411
- data/spec/compressor/base_spec.rb +0 -52
- data/spec/compressor/bzip2_spec.rb +0 -217
- data/spec/compressor/custom_spec.rb +0 -106
- data/spec/compressor/gzip_spec.rb +0 -217
- data/spec/compressor/lzma_spec.rb +0 -123
- data/spec/compressor/pbzip2_spec.rb +0 -165
- data/spec/config_spec.rb +0 -321
- data/spec/configuration/helpers_spec.rb +0 -247
- data/spec/configuration/store_spec.rb +0 -39
- data/spec/configuration_spec.rb +0 -62
- data/spec/database/base_spec.rb +0 -63
- data/spec/database/mongodb_spec.rb +0 -510
- data/spec/database/mysql_spec.rb +0 -411
- data/spec/database/postgresql_spec.rb +0 -353
- data/spec/database/redis_spec.rb +0 -334
- data/spec/database/riak_spec.rb +0 -176
- data/spec/dependency_spec.rb +0 -51
- data/spec/encryptor/base_spec.rb +0 -40
- data/spec/encryptor/gpg_spec.rb +0 -909
- data/spec/encryptor/open_ssl_spec.rb +0 -148
- data/spec/errors_spec.rb +0 -306
- data/spec/logger_spec.rb +0 -367
- data/spec/model_spec.rb +0 -666
- data/spec/notifier/base_spec.rb +0 -104
- data/spec/notifier/campfire_spec.rb +0 -217
- data/spec/notifier/hipchat_spec.rb +0 -211
- data/spec/notifier/mail_spec.rb +0 -316
- data/spec/notifier/prowl_spec.rb +0 -138
- data/spec/notifier/pushover_spec.rb +0 -123
- data/spec/notifier/twitter_spec.rb +0 -153
- data/spec/package_spec.rb +0 -61
- data/spec/packager_spec.rb +0 -213
- data/spec/pipeline_spec.rb +0 -259
- data/spec/spec_helper.rb +0 -60
- data/spec/splitter_spec.rb +0 -120
- data/spec/storage/base_spec.rb +0 -166
- data/spec/storage/cloudfiles_spec.rb +0 -254
- data/spec/storage/cycler_spec.rb +0 -247
- data/spec/storage/dropbox_spec.rb +0 -480
- data/spec/storage/ftp_spec.rb +0 -271
- data/spec/storage/local_spec.rb +0 -259
- data/spec/storage/ninefold_spec.rb +0 -343
- data/spec/storage/rsync_spec.rb +0 -362
- data/spec/storage/s3_spec.rb +0 -245
- data/spec/storage/scp_spec.rb +0 -233
- data/spec/storage/sftp_spec.rb +0 -244
- data/spec/syncer/base_spec.rb +0 -109
- data/spec/syncer/cloud/base_spec.rb +0 -515
- data/spec/syncer/cloud/cloud_files_spec.rb +0 -181
- data/spec/syncer/cloud/s3_spec.rb +0 -174
- data/spec/syncer/rsync/base_spec.rb +0 -98
- data/spec/syncer/rsync/local_spec.rb +0 -149
- data/spec/syncer/rsync/pull_spec.rb +0 -98
- data/spec/syncer/rsync/push_spec.rb +0 -333
- data/spec/version_spec.rb +0 -21
- data/templates/cli/utility/config +0 -32
- 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::
|
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.
|
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
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
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
|
-
#
|
42
|
-
|
43
|
-
|
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
|
48
|
-
|
49
|
-
|
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
|
-
#
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
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
|
-
#
|
91
|
-
#
|
92
|
-
|
93
|
-
|
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
|
-
#
|
108
|
-
def
|
109
|
-
|
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
|
-
#
|
115
|
-
|
116
|
-
|
117
|
-
|
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
|
-
#
|
124
|
-
|
125
|
-
|
126
|
-
|
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
|
-
#
|
131
|
-
#
|
132
|
-
|
133
|
-
|
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
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
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
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
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
|