summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rw-r--r--bin/byteback-backup108
-rwxr-xr-xbin/byteback-prune20
-rwxr-xr-xbin/byteback-receive62
-rwxr-xr-xbin/byteback-restore61
-rwxr-xr-xbin/byteback-setup-client6
-rwxr-xr-xbin/byteback-setup-client-receive2
-rwxr-xr-xbin/byteback-snapshot4
7 files changed, 123 insertions, 140 deletions
diff --git a/bin/byteback-backup b/bin/byteback-backup
index 8268353..713c127 100644
--- a/bin/byteback-backup
+++ b/bin/byteback-backup
@@ -16,8 +16,6 @@ require 'byteback/log'
include Byteback::Util
include Byteback::Log
-
-
#
# Run an ssh-command.
#
@@ -40,8 +38,6 @@ def ssh(*ssh_args)
log_system(*args)
end
-
-
#
# Call rsync to copy certain sources, returns exit status (see man rsync)
#
@@ -60,8 +56,7 @@ def rsync(*sources)
#
# Add on the I/O-timeout
#
- args += ['--timeout', @io_timeout.to_s ] unless ( @io_timeout.nil? )
-
+ args += ['--timeout', @io_timeout.to_s] unless @io_timeout.nil?
args += ['--rsh', "ssh -o BatchMode=yes -x -a -i #{@ssh_key} -l #{@destination_user}"]
args << '--verbose' if @verbose
@@ -96,18 +91,16 @@ def rsync(*sources)
log_system(*args)
end
-
#
# Run all the executable-scripts in the specified directory.
#
def run_parts(dir)
if File.directory? dir
- args = ['run-parts', dir ]
- log_system(*args)
+ args = ['run-parts', dir]
+ log_system(*args)
end
end
-
##
## Entry-point to our code.
##
@@ -119,42 +112,41 @@ if __FILE__ == $PROGRAM_NAME
banner "#{ME}: Back up this system to a byteback-enabled server\n "
opt :destination, 'Backup destination (i.e. user@host:/path)',
- :type => :string
+ type: :string
opt :source, 'Source paths',
- :type => :strings,
- :default => ['/']
+ type: :strings,
+ default: ['/']
opt :exclude, 'Paths to exclude',
- :type => :strings,
- :short => 'x'
+ type: :strings,
+ short: 'x'
opt :verbose, 'Show debugging messages'
opt :retry_number, 'Number of retries on error',
- :type => :integer,
- :default => 3
+ type: :integer,
+ default: 3
opt :io_timeout, 'Number of seconds to allow I/O timeout for',
- :type => :integer,
- :default => 10800
+ type: :integer,
+ default: 10_800
opt :retry_delay, 'Number of seconds between retries after an error',
- :type => :integer,
- :default => 300
+ type: :integer,
+ default: 300
opt :ssh_key, 'SSH key filename',
- :type => :string,
- :default => '/etc/byteback/key',
- :short => 'k'
+ type: :string,
+ default: '/etc/byteback/key',
+ short: 'k'
opt :help, 'Show this message',
- :short => 'h'
+ short: 'h'
banner "\nAdditional excludes can be specified using /etc/byteback/rsync_filter, which is an rsync filter file. See the rsync man page for information on how this works.\n"
end
-
#
# Abort if we're already running.
#
@@ -169,28 +161,26 @@ if __FILE__ == $PROGRAM_NAME
@retry_delay = opts[:retry_delay]
@io_timeout = opts[:io_timeout] if opts[:io_timeout]
-
# Read the default destination
if File.exist?('/etc/byteback/destination')
@destination = File.read('/etc/byteback/destination').chomp
end
# Set the default SSH key
- if File.exist?('/etc/byteback/key')
- @ssh_key = '/etc/byteback/key'
- end
+ @ssh_key = '/etc/byteback/key' if File.exist?('/etc/byteback/key')
# If we have a local timeout-file then use that
if File.exist?('/etc/byteback/io_timeout')
@io_timeout = File.foreach('/etc/byteback/io_timeout').first.to_i
end
-
#
# Check our destination
#
if @destination =~ /^(?:(.+)@)?([^@:]+):(.+)?$/
- @destination_user, @destination_host, @destination_path = [Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3)]
+ @destination_user = Regexp.last_match(1)
+ @destination_host = Regexp.last_match(2)
+ @destination_path = Regexp.last_match(3)
else
fatal('Destination must be a remote path, e.g. ssh@host.com:/store/backups')
end
@@ -215,28 +205,28 @@ if __FILE__ == $PROGRAM_NAME
if @excludes.nil?
PROBABLY_LOCAL = %w(
- btrfs
- ext2
- ext3
- ext4
- reiserfs
- xfs
- nilfs
- jfs
- reiser4
- zfs
- rootfs
- )
+ btrfs
+ ext2
+ ext3
+ ext4
+ reiserfs
+ xfs
+ nilfs
+ jfs
+ reiser4
+ zfs
+ rootfs
+ ).freeze
COMMON_JUNK = %w(
- /swap.file
- /tmp/
- /var/backups/localhost/
- /var/cache/apt/archives/
- /var/lib/php5/
- /var/tmp/
- /var/lib/mysql/
- )
+ /swap.file
+ /tmp/
+ /var/backups/localhost/
+ /var/cache/apt/archives/
+ /var/lib/php5/
+ /var/tmp/
+ /var/lib/mysql/
+ ).freeze
MOUNT_HEADINGS = %w( spec file vfstype mntops freq passno ).map(&:to_sym)
@@ -261,27 +251,24 @@ if __FILE__ == $PROGRAM_NAME
#
fatal("Could not read ssh key #{@ssh_key}") unless File.readable?(@ssh_key)
-
#
# Run our pre-backup actions, if any
#
- run_parts( "/etc/byteback/pre-backup.d/" )
+ run_parts('/etc/byteback/pre-backup.d/')
-
#
# Test ssh connection is good before we start
- #
+ #
fatal("Could not connect to #{@destination}") unless
ssh('byteback-receive', '--ping', @verbose) == 0
-
#
# We treat exit statuses 0 and 24 as success; 0 is "Success"; 24 is "Partial
# transfer due to vanished source files", which we treat as success otherwise
# on some hosts the backup process never finishes.
#
- RSYNC_EXIT_STATUSES_TO_ACCEPT = [0, 24]
- RSYNC_EXIT_STATUSES_TO_RETRY_ON = [10, 11, 20, 21, 22, 23, 30]
+ RSYNC_EXIT_STATUSES_TO_ACCEPT = [0, 24].freeze
+ RSYNC_EXIT_STATUSES_TO_RETRY_ON = [10, 11, 20, 21, 22, 23, 30].freeze
# Run the file copy, retrying if necessary
#
@@ -314,11 +301,10 @@ if __FILE__ == $PROGRAM_NAME
fatal('Backup could not be marked complete') unless
ssh('byteback-receive', '--complete', @verbose) == 0
-
#
# Run our completion-actions, if any.
#
- run_parts( "/etc/byteback/post-backup.d/" )
+ run_parts('/etc/byteback/post-backup.d/')
info('Finished')
end
diff --git a/bin/byteback-prune b/bin/byteback-prune
index a0cd0fb..3fef2c4 100755
--- a/bin/byteback-prune
+++ b/bin/byteback-prune
@@ -1,4 +1,5 @@
#!/usr/bin/ruby
+# frozen_string_literal: true
#
# Program to free up space on the backup-storage volume, by removing
# backups (whether by age, or importance).
@@ -17,12 +18,12 @@ opts = Trollop.options do
banner "Prune old backup directories to ensure there's enough space"
opt :minpercent, 'Start prune when disk has less than this %age free',
- :type => :integer,
- :default => 5
+ type: :integer,
+ default: 5
opt :maxpercent, 'Stop prune when disk has more than this %age free',
- :type => :integer,
- :default => 10
+ type: :integer,
+ default: 10
opt :list, 'List backups in pruning order, no other action'
@@ -31,8 +32,8 @@ opts = Trollop.options do
opt :prune_force, 'Prune the next backup regardless'
opt :order, "Order backups by 'age' or 'importance'",
- :type => :string,
- :default => 'importance'
+ type: :string,
+ default: 'importance'
opt :verbose, 'Show debugging messages'
end
@@ -80,7 +81,7 @@ gradient_30m = @df_history.gradient(1800)
# Check whether we should still be pruning
#
@free = @df_history.list.last.percent_free
-PRUNING_FLAG = "#{ENV['HOME']}/.byteback.pruning"
+PRUNING_FLAG = "#{ENV['HOME']}/.byteback.pruning".freeze
if @do_prune_force
info('Forcing prune')
@@ -127,14 +128,13 @@ unless @do_prune_force
#
# Do not prune unless at least one snapshot is a week old
#
- last_week = Time.now - (7*86400)
- unless snapshots.any?{|snapshot| last_week > snapshot.time}
+ last_week = Time.now - (7 * 86_400)
+ unless snapshots.any? { |snapshot| last_week > snapshot.time }
warn('There are no snapshots older than a week. Not pruning.')
exit 0
end
end
-
exit 0 unless
(@do_prune && File.exist?(PRUNING_FLAG)) ||
@do_prune_force
diff --git a/bin/byteback-receive b/bin/byteback-receive
index 1326bba..8cdb6ee 100755
--- a/bin/byteback-receive
+++ b/bin/byteback-receive
@@ -25,13 +25,13 @@ fatal("#{byteback_root} does not exist") unless File.directory?(byteback_root)
#
# Calling byteback-restore really needs rsync to restore the files.
#
-if (ARGV[0] == 'byteback-restore')
+if ARGV[0] == 'byteback-restore'
#
# Ignore the first arg
#
ARGV.shift
- args = ["rsync"]
+ args = ['rsync']
snapshot = nil
verbose = false
all = false
@@ -39,25 +39,25 @@ if (ARGV[0] == 'byteback-restore')
#
# Mangle the arguments, and pull out any that are not-rsync compatible.
#
- while(arg = ARGV.shift)
+ while (arg = ARGV.shift)
case arg
- when "."
- break
- when /^-([^-]*v[^-]*|-verbose)$/
- verbose = true
- args << arg
- when "--snapshot"
- snapshot = ARGV.shift
- when "--all"
- all = true
- else
- args << arg
+ when '.'
+ break
+ when /^-([^-]*v[^-]*|-verbose)$/
+ verbose = true
+ args << arg
+ when '--snapshot'
+ snapshot = ARGV.shift
+ when '--all'
+ all = true
+ else
+ args << arg
end
end
# Always exclude the current directory, because it can change the
# ownership at the restorers end.
- args << "--exclude=."
+ args << '--exclude=.'
#
# Search for certain files
@@ -67,25 +67,25 @@ if (ARGV[0] == 'byteback-restore')
restore = Byteback::Restore.new(byteback_root)
restore.snapshot = snapshot if snapshot
- restore.find(paths, :all => all)
+ restore.find(paths, all: all)
Dir.chdir(byteback_host)
- args << "."
+ args << '.'
restore.results.each do |r|
- args << File.join(".", r.snapshot, r.path)
+ args << File.join('.', r.snapshot, r.path)
end
if restore.results.empty?
- STDERR.puts "** Sorry. There were no files matching:"
- STDERR.puts "--> "+paths.join("\n--> ")
+ STDERR.puts '** Sorry. There were no files matching:'
+ STDERR.puts '--> ' + paths.join("\n--> ")
exit 1
end
- STDERR.puts "Restoring:"
+ STDERR.puts 'Restoring:'
STDERR.puts restore.list
- STDERR.puts(args.join(" ")) if verbose
+ STDERR.puts(args.join(' ')) if verbose
exec(*args)
@@ -94,11 +94,11 @@ elsif ARGV[0] == 'rsync'
exec(*ARGV)
elsif ARGV[0] == 'byteback-snapshot'
- ARGV.concat(['--root', "#{byteback_root}"])
+ ARGV.concat(['--root', byteback_root.to_s])
exec(*ARGV)
-elsif ARGV[0] == "restore"
- puts "** Your byteback package needs to be updated. Please update and try again."
+elsif ARGV[0] == 'restore'
+ puts '** Your byteback package needs to be updated. Please update and try again.'
exit(1)
end
@@ -108,15 +108,15 @@ opts = Trollop.options do
opt :complete, 'Mark current backup as complete'
opt :list, 'Show backed up files matching the given pattern'
opt :all, 'Show all stored versions of a file'
- opt :snapshot, 'Show backed up files in a certain snapshot.', :default => '*'
+ opt :snapshot, 'Show backed up files in a certain snapshot.', default: '*'
opt :verbose, 'Print diagnostics'
end
#
# Make sure we don't get crazy option combinations.
#
-n_modes = opts.keys.inject(0) do |s,m|
- [:ping, :complete, :list].include?(m) ? s += 1 : s
+n_modes = opts.keys.inject(0) do |s, m|
+ [:ping, :complete, :list].include?(m) ? s += 1 : s
end
error('Please only choose one mode') unless n_modes == 1
@@ -129,11 +129,11 @@ elsif opts[:list]
restore = Byteback::Restore.new(byteback_root)
restore.snapshot = opts[:snapshot]
- restore.find(args, :all => opts[:all], :verbose => opts[:verbose])
+ restore.find(args, all: opts[:all], verbose: opts[:verbose])
if restore.results.empty?
- puts "** Sorry. There were no files matching:"
- puts "--> "+args.join("\n--> ")
+ puts '** Sorry. There were no files matching:'
+ puts '--> ' + args.join("\n--> ")
else
puts restore.list
end
diff --git a/bin/byteback-restore b/bin/byteback-restore
index 2cd54c5..ef9e40b 100755
--- a/bin/byteback-restore
+++ b/bin/byteback-restore
@@ -31,13 +31,13 @@ def ssh(*ssh_args)
] +
ssh_args.map { |a| a ? a : '' }
- puts args.join(" " ) if @verbose
+ puts args.join(' ') if @verbose
system(*args)
end
def list_files(pattern, snapshot, all)
args = ['byteback-receive', '--snapshot', snapshot, '--list']
- args << "--all" if all
+ args << '--all' if all
args << @verbose if @verbose
args += Byteback::Restore.encode_args(pattern)
ssh(*args)
@@ -60,28 +60,28 @@ def restore_files(paths, snapshot, all)
#
# Add on the I/O-timeout
#
- args += ['--timeout', @io_timeout.to_s ] unless ( @io_timeout.nil? )
+ args += ['--timeout', @io_timeout.to_s] unless @io_timeout.nil?
args += ['--rsh', "ssh -o BatchMode=yes -x -a -i #{@ssh_key} -l #{@destination_user}"]
args << '--verbose' if @verbose
- args += ['--rsync-path', "byteback-restore --fake-super --snapshot #{snapshot}" + (all ? " --all" : "")]
+ args += ['--rsync-path', "byteback-restore --fake-super --snapshot #{snapshot}" + (all ? ' --all' : '')]
#
# To add extra rsync flags, a file can be used. This can have flags all on one line, or one per line.
#
- if File.exists?("/etc/byteback/rsync_flags")
- args += File.readlines("/etc/byteback/rsync_flags").map(&:chomp)
+ if File.exist?('/etc/byteback/rsync_flags')
+ args += File.readlines('/etc/byteback/rsync_flags').map(&:chomp)
end
dst = "#{@destination_user}@#{@destination_host}:"
paths.each do |path|
path = Byteback::Restore.encode_args(path).first
- args << File.join(dst,path)
- dst = ":"
+ args << File.join(dst, path)
+ dst = ':'
end
- args << "."
- puts args.join(" ") if @verbose
+ args << '.'
+ puts args.join(' ') if @verbose
system(*args)
end
@@ -98,36 +98,35 @@ if __FILE__ == $PROGRAM_NAME
opts = Trollop.options do
banner "#{ME}: Restore a file to this system from a byteback-enabled server\n "
- opt :list, "List or find files on the backup server. This is the default mode."
+ opt :list, 'List or find files on the backup server. This is the default mode.'
- opt :restore, "Restore files from the backup server."
+ opt :restore, 'Restore files from the backup server.'
- opt :snapshot, "The specific snapshot to use",
- :type => :string, :default => "*"
+ opt :snapshot, 'The specific snapshot to use',
+ type: :string, default: '*'
opt :destination, 'Backup destination (i.e. user@host:/path)',
- :type => :string
+ type: :string
opt :io_timeout, 'Number of seconds to allow I/O to timeout',
- :type => :integer,
- :default => 300
+ type: :integer,
+ default: 300
opt :ssh_key, 'SSH key filename',
- :type => :string,
- :default => '/etc/byteback/key',
- :short => 'k'
+ type: :string,
+ default: '/etc/byteback/key',
+ short: 'k'
opt :all, 'List or restore all versrions of each file',
- :short => 'a'
+ short: 'a'
opt :verbose, 'Show more output'
-
end
#
# Make sure we know what we're doing.
#
- fatal("Please choose either --list or --restore") if opts[:list] and opts[:restore]
+ fatal('Please choose either --list or --restore') if opts[:list] && opts[:restore]
@verbose = opts[:verbose] ? '--verbose' : nil
@io_timeout = opts[:io_timeout] if opts[:io_timeout]
@@ -138,9 +137,7 @@ if __FILE__ == $PROGRAM_NAME
end
# Set the default SSH key
- if File.exist?('/etc/byteback/key')
- @ssh_key = '/etc/byteback/key'
- end
+ @ssh_key = '/etc/byteback/key' if File.exist?('/etc/byteback/key')
#
# Allow the command-line to override them.
@@ -157,7 +154,9 @@ if __FILE__ == $PROGRAM_NAME
# Check our destination is well-formed
#
if @destination =~ /^(?:(.+)@)?([^@:]+):(.+)?$/
- @destination_user, @destination_host, @destination_path = [Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3)]
+ @destination_user = Regexp.last_match(1)
+ @destination_host = Regexp.last_match(2)
+ @destination_path = Regexp.last_match(3)
else
fatal('Destination must be a remote path, e.g. ssh@host.com:/store/backups')
end
@@ -171,18 +170,16 @@ if __FILE__ == $PROGRAM_NAME
# If the user didn't specify a file then we're not restoring anything,
# and we should abort.
#
- if ARGV.empty?
- fatal('You must specify a file to search/restore')
- end
+ fatal('You must specify a file to search/restore') if ARGV.empty?
if opts[:restore]
#
# Restore a file
#
- restore_files(ARGV.collect{|a| File.expand_path(a)}, opts[:snapshot], opts[:all])
+ restore_files(ARGV.collect { |a| File.expand_path(a) }, opts[:snapshot], opts[:all])
exit(0)
end
- list_files(ARGV.collect{|a| File.expand_path(a)}, opts[:snapshot], opts[:all])
+ list_files(ARGV.collect { |a| File.expand_path(a) }, opts[:snapshot], opts[:all])
exit(0)
end
diff --git a/bin/byteback-setup-client b/bin/byteback-setup-client
index 988d4b8..c81ca1f 100755
--- a/bin/byteback-setup-client
+++ b/bin/byteback-setup-client
@@ -23,17 +23,17 @@ end
opts = Trollop.options do
opt :hostname, 'Set host name for backups',
- :type => :string
+ type: :string
opt :destination, 'Backup destination (i.e. user@host:/path)',
- :type => :string
+ type: :string
end
@destination = opts[:destination]
@hostname = opts[:hostname]
_dummy, @destination_user, @destination_host, colon, @destination_path =
- /^(.*)?(?:@)([^:]+)(:)(.*)?$/.match(@destination).to_a
+ /^(.*)?(?:@)([^:]+)(:)(.*)?$/.match(@destination).to_a
@destination_user ||= 'byteback'
@destination_path ||= ''
diff --git a/bin/byteback-setup-client-receive b/bin/byteback-setup-client-receive
index 528ccc9..0afc595 100755
--- a/bin/byteback-setup-client-receive
+++ b/bin/byteback-setup-client-receive
@@ -44,7 +44,7 @@ if File.exist?('.ssh/authorized_keys') &&
else
File.open('.ssh/authorized_keys', 'a+') do |fh|
- fh.print <<-LINE.gsub(/\n/, '') + "\n"
+ fh.print <<-LINE.delete("\n") + "\n"
command="byteback-receive",
from="#{@client_ip}",
environment="BYTEBACK_HOST=#{@hostname}"
diff --git a/bin/byteback-snapshot b/bin/byteback-snapshot
index 18a7a67..c7cb920 100755
--- a/bin/byteback-snapshot
+++ b/bin/byteback-snapshot
@@ -13,7 +13,7 @@ include Byteback::Log
opts = Trollop.options do
opt :root, 'Backups directory (must be a btrfs subvolume)',
- :type => :string
+ type: :string
opt :snapshot, '(ignored for compatibility)'
@@ -23,7 +23,7 @@ end
@root = opts[:root]
@verbose = opts[:verbose]
-fatal('--root not readable') unless File.directory?("#{@root}")
+fatal('--root not readable') unless File.directory?(@root.to_s)
@backups = BackupDirectory.new(@root)
snapshots = @backups.snapshots