diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/byteback/backup_directory.rb | 14 | ||||
-rw-r--r-- | lib/byteback/disk_free_history.rb | 4 | ||||
-rw-r--r-- | lib/byteback/log.rb | 17 | ||||
-rw-r--r-- | lib/byteback/restore.rb | 45 | ||||
-rw-r--r-- | lib/byteback/restore_file.rb | 70 | ||||
-rw-r--r-- | lib/byteback/util.rb | 9 |
6 files changed, 70 insertions, 89 deletions
diff --git a/lib/byteback/backup_directory.rb b/lib/byteback/backup_directory.rb index a39cf84..8c0521b 100644 --- a/lib/byteback/backup_directory.rb +++ b/lib/byteback/backup_directory.rb @@ -3,7 +3,6 @@ require 'byteback/utils' module Byteback # Represents a particular timestamped backup directory class Snapshot - include Byteback::Util class << self @@ -12,10 +11,10 @@ module Byteback # Order backups by their closeness to defined backup times, which are # listed in a set order (i.e. today's backup is more important than yesterday's). # - BACKUP_IMPORTANCE = [1, 2, 7, 14, 21, 28, 56, 112] + BACKUP_IMPORTANCE = [1, 2, 7, 14, 21, 28, 56, 112].freeze def sort_by_importance(snapshots_unsorted, now = Time.now) - return snapshots_unsorted if snapshots_unsorted.size < 1 + return snapshots_unsorted if snapshots_unsorted.size < 1 # # Keep the last 7 days backups @@ -78,11 +77,10 @@ module Byteback # host was more recent, i.e. we've reached the oldest, and are # bouncing back again. # - if last_nearest[host].nil? || last_nearest[host].time > nearest.time - last_nearest[host] = nearest - snapshots_by_host[host] -= [nearest] - snapshots_sorted << nearest - end + next unless last_nearest[host].nil? || last_nearest[host].time > nearest.time + last_nearest[host] = nearest + snapshots_by_host[host] -= [nearest] + snapshots_sorted << nearest end end diff --git a/lib/byteback/disk_free_history.rb b/lib/byteback/disk_free_history.rb index 5f45f14..db7f45a 100644 --- a/lib/byteback/disk_free_history.rb +++ b/lib/byteback/disk_free_history.rb @@ -67,8 +67,8 @@ module Byteback list.reverse.each do |reading| if later_reading difference = - value_from_reading.call(reading) - - value_from_reading.call(later_reading) + yield reading - + yield later_reading total += difference end readings += 1 diff --git a/lib/byteback/log.rb b/lib/byteback/log.rb index 6753ca8..83679e6 100644 --- a/lib/byteback/log.rb +++ b/lib/byteback/log.rb @@ -2,7 +2,6 @@ require 'logger' require 'syslog' module Byteback - # # Translates Ruby's Logger calls to similar calls to Syslog # (implemented in Ruby 2.0 as Syslog::Logger). @@ -18,15 +17,15 @@ module Byteback def info(m) log_nopc(Syslog::LOG_INFO, m) end - + def warn(m) log_nopc(Syslog::LOG_WARNING, m) end - + def error(m) log_nopc(Syslog::LOG_ERR, m) end - + # # syslog(3) says: # @@ -52,7 +51,6 @@ module Byteback module Log @@me = File.expand_path($PROGRAM_NAME).split('/').last - # # If we're running interactively then we have simple logging. # @@ -80,24 +78,23 @@ module Byteback @@logger = SyslogProxy end - def debug(*a) @@logger.__send__(:debug, *a) end - + def info(*a) @@logger.__send__(:info, *a) end - + def warn(*a) @@logger.__send__(:warn, *a) end - + def fatal(*a) @@logger.__send__(:fatal, *a) exit 1 end - + def error(*a) @@logger.__send__(:error, *a) end diff --git a/lib/byteback/restore.rb b/lib/byteback/restore.rb index 7055be3..259e108 100644 --- a/lib/byteback/restore.rb +++ b/lib/byteback/restore.rb @@ -2,14 +2,12 @@ require 'byteback/restore_file' module Byteback - class Restore - def self.find(byteback_root, snapshot, paths) x = Byteback::Restore.new(byteback_root) x.snapshot = snapshot x.find(paths) - return x + x end # @@ -19,7 +17,7 @@ module Byteback # Returns an array of encoded strings. # def self.encode_args(args) - [args].flatten.collect{|s| [s].pack("M").gsub(" ","=20").gsub("=\n","")} + [args].flatten.collect { |s| [s].pack('M').gsub(' ', '=20').gsub("=\n", '') } end # @@ -29,17 +27,17 @@ module Byteback # Returns an array of decoded strings. # def self.decode_args(args) - [args].flatten.collect{|s| (s + "=\n").unpack("M")}.flatten + [args].flatten.collect { |s| (s + "=\n").unpack('M') }.flatten end def initialize(byteback_root) - # + # # We use expand_path here to make sure we have a full path, with no # trailing slash. # @byteback_root = File.expand_path(byteback_root) - @now = Time.now - @snapshot = "*" + @now = Time.now + @snapshot = '*' @results = [] end @@ -51,16 +49,14 @@ module Byteback end end - def results - @results - end + attr_reader :results def find(paths, opts = {}) results = [] # # Make sure we've an array, and that we get rid of any ".." nonsense. # - paths = [paths].flatten.collect{|p| File.expand_path(p, "/")} + paths = [paths].flatten.collect { |p| File.expand_path(p, '/') } seen = [] @results = paths.collect do |path| @@ -72,19 +68,19 @@ module Byteback # # If we want an unpruned list, return it now. # - if opts == true or (opts.is_a?(Hash) and opts[:verbose]) - @results = @results.sort{|a,b| [a.path, a.snapshot_time] <=> [b.path, b.snapshot_time]} + if opts == true || (opts.is_a?(Hash) && opts[:verbose]) + @results = @results.sort { |a, b| [a.path, a.snapshot_time] <=> [b.path, b.snapshot_time] } return @results end - @results = @results.sort{|a,b| [a.path, b.snapshot_time] <=> [b.path, a.snapshot_time]} + @results = @results.sort { |a, b| [a.path, b.snapshot_time] <=> [b.path, a.snapshot_time] } pruned_results = [] @results.each do |r| - if (opts.is_a?(Hash) and opts[:all]) + if opts.is_a?(Hash) && opts[:all] pruned_results << r unless pruned_results.include?(r) else - pruned_results << r unless pruned_results.any?{|pr| pr.path == r.path} + pruned_results << r unless pruned_results.any? { |pr| pr.path == r.path } end end @@ -95,14 +91,12 @@ module Byteback heading = %w(snapshot modestring size uid gid mtime path) listings = [heading] @results.sort.each do |r| - listing = heading.collect{|m| r.__send__(m.to_sym).to_s } - if r.symlink? - listing[-1] << " -> "+r.readlink - end + listing = heading.collect { |m| r.__send__(m.to_sym).to_s } + listing[-1] << ' -> ' + r.readlink if r.symlink? listings << listing end - field_sizes = [0]*heading.length + field_sizes = [0] * heading.length listings.each do |fields| fields.each_with_index do |field, i| @@ -110,9 +104,9 @@ module Byteback end end - fmt = field_sizes.collect{|i| "%-#{i}.#{i}s"}.join(" ") + fmt = field_sizes.collect { |i| "%-#{i}.#{i}s" }.join(' ') - bar = "-"*field_sizes.inject(field_sizes.length){|m,s| m+=s} + bar = '-' * field_sizes.inject(field_sizes.length) { |m, s| m += s } output = [] listings.each do |fields| @@ -123,8 +117,7 @@ module Byteback end end - return output.join("\n") + output.join("\n") end - end end diff --git a/lib/byteback/restore_file.rb b/lib/byteback/restore_file.rb index daceb83..2015eb0 100644 --- a/lib/byteback/restore_file.rb +++ b/lib/byteback/restore_file.rb @@ -35,7 +35,7 @@ module Byteback include Comparable - def initialize(full_path, byteback_root=".", now = Time.now) + def initialize(full_path, byteback_root = '.', now = Time.now) @full_path = full_path @byteback_root = byteback_root @now = now @@ -43,7 +43,7 @@ module Byteback # # The snapshot is the first directory after the byteback_root # - @snapshot = full_path.sub(%r(^#{Regexp.escape @byteback_root}),'').split("/")[1] + @snapshot = full_path.sub(/^#{Regexp.escape @byteback_root}/, '').split('/')[1] # # If we can parse the time, use it, otherwise assume "now". @@ -57,13 +57,13 @@ module Byteback # # Restore path # - @path = full_path.sub(%r(^#{Regexp.escape @byteback_root}/#{Regexp.escape @snapshot}),'') + @path = full_path.sub(%r{^#{Regexp.escape @byteback_root}/#{Regexp.escape @snapshot}}, '') @stat = @mode = @dev_major = @dev_minor = @uid = @gid = nil end def <=>(other) - [self.path, self.mtime.to_i, self.size] <=> [other.path, other.mtime.to_i, other.size] + [path, mtime.to_i, size] <=> [other.path, other.mtime.to_i, other.size] end def stat @@ -71,36 +71,30 @@ module Byteback @stat end - def snapshot - @snapshot - end + attr_reader :snapshot - def snapshot_time - @snapshot_time - end + attr_reader :snapshot_time - def path - @path - end + attr_reader :path def to_s - sprintf("%10s %i %4i %4i %s %s %s", self.modestring, self.size, self.uid, self.gid, self.mtime.strftime("%b %2d %H:%M"), @snapshot, @path) + sprintf('%10s %i %4i %4i %s %s %s', modestring, size, uid, gid, mtime.strftime('%b %2d %H:%M'), @snapshot, @path) end def read_rsync_xattrs - xattr = Xattr.new(@full_path, :no_follow => false) - rsync_xattrs = xattr["user.rsync.%stat"] + xattr = Xattr.new(@full_path, no_follow: false) + rsync_xattrs = xattr['user.rsync.%stat'] if rsync_xattrs - @mode, @dev_major, @dev_minor, @uid, @gid = rsync_xattrs.scanf("%o %d,%d %d:%d") - raise ArgumentError, "Corrupt rsync stat xattr found for #{@full_path} (#{rsync_xattrs})" unless [@mode, @dev_major, @dev_minor, @uid, @gid].all?{|i| i.is_a?(Integer)} + @mode, @dev_major, @dev_minor, @uid, @gid = rsync_xattrs.scanf('%o %d,%d %d:%d') + fail ArgumentError, "Corrupt rsync stat xattr found for #{@full_path} (#{rsync_xattrs})" unless [@mode, @dev_major, @dev_minor, @uid, @gid].all? { |i| i.is_a?(Integer) } else - warn "No rsync stat xattr found for #{@full_path}" - @mode, @dev_major, @dev_minor, @uid, @gid = %w(mode dev_major dev_minor uid gid).collect{|m| self.stat.__send__(m.to_sym)} + warn "No rsync stat xattr found for #{@full_path}" + @mode, @dev_major, @dev_minor, @uid, @gid = %w(mode dev_major dev_minor uid gid).collect { |m| stat.__send__(m.to_sym) } end end def mode - return self.stat.mode if self.stat.symlink? + return stat.mode if stat.symlink? read_rsync_xattrs unless @mode @mode end @@ -130,22 +124,22 @@ module Byteback # def ftypelet if file? - "-" + '-' elsif directory? - "d" + 'd' elsif blockdev? - "b" + 'b' elsif chardev? - "c" + 'c' elsif symlink? - "l" + 'l' elsif fifo? - "p" + 'p' elsif socket? - "s" + 's' else - "?" - end + '?' + end end # @@ -153,7 +147,7 @@ module Byteback # This has mostly been copied from strmode from filemode.h in coreutils. # def modestring - str = "" + str = '' str << ftypelet str << ((mode & S_IRUSR == S_IRUSR) ? 'r' : '-') str << ((mode & S_IWUSR == S_IWUSR) ? 'w' : '-') @@ -170,7 +164,7 @@ module Byteback str << ((mode & S_ISVTX == S_ISVTX) ? ((mode & S_IXOTH == S_IXOTH) ? 't' : 'T') : ((mode & S_IXOTH == S_IXOTH) ? 'x' : '-')) - return str + str end def socket? @@ -178,7 +172,7 @@ module Byteback end def symlink? - self.stat.symlink? || (mode & S_IFMT) == S_IFLNK + stat.symlink? || (mode & S_IFMT) == S_IFLNK end def file? @@ -202,17 +196,17 @@ module Byteback end def readlink - if self.stat.symlink? + if stat.symlink? File.readlink(@full_path) else File.read(@full_path).chomp end end - def method_missing(m, *args, &blk) - return self.stat.__send__(m) if self.stat.respond_to?(m) + def method_missing(m, *_args, &_blk) + return stat.__send__(m) if stat.respond_to?(m) - raise NoMethodError, m - end + fail NoMethodError, m + end end end diff --git a/lib/byteback/util.rb b/lib/byteback/util.rb index ebf5ca5..7678d81 100644 --- a/lib/byteback/util.rb +++ b/lib/byteback/util.rb @@ -15,7 +15,7 @@ module Byteback if File.exist? @@lockfile # check the lockfile is sane exist_pid = File.read(@@lockfile).to_i - if exist_pid > 1 && exist_pid < (File.read('/proc/sys/kernel/pid_max').to_i) + if exist_pid > 1 && exist_pid < File.read('/proc/sys/kernel/pid_max').to_i begin Process.getpgid(exist_pid) # if no exception, process is running, abort @@ -40,7 +40,7 @@ module Byteback lockfile.puts Process.pid end rescue => _ex - fatal("Failed to open lockfile - are you running as root?") + fatal('Failed to open lockfile - are you running as root?') end end @@ -51,7 +51,7 @@ module Byteback end def log_system(*args) - debug('system: ' + args.map { |a| / /.match(a) ? "\"#{a}\"" : a }.join(' ')) + debug('system: ' + args.map { |a| / / =~ a ? "\"#{a}\"" : a }.join(' ')) rd, wr = IO.pipe pid = fork if pid.nil? # child @@ -71,8 +71,7 @@ module Byteback %w(/bin/btrfs /sbin/btrfs).each do |path| return path if File.exist?(path) end - raise Errno::ENOENT, 'btrfs' + fail Errno::ENOENT, 'btrfs' end - end end |