Add option to prune instead of backing up

This commit is contained in:
Martin Puppe 2020-03-25 20:42:27 +01:00
parent 9958709a32
commit 8831c2b403

View file

@ -8,7 +8,16 @@ DEFAULT_CONFIG_FILE = Pathname.new('/etc/mpbackup/config.yaml')
NAME = 'mpbackup'
VERSION = '0.1.0'
def set_restic_vars(repo, password)
def set_restic_vars(config)
repo = config['repository']
puts "Repository: #{repo}"
if config['password-file']
puts "Reading password from file #{config['password-file']} …"
password = File.open(config['password-file'], &:gets).chomp
else
password = STDIN.getpass('Please put in your restic password: ')
end
ENV['RESTIC_REPOSITORY'] = repo
ENV['RESTIC_PASSWORD'] = password
end
@ -27,6 +36,7 @@ unset_restic_vars
options = {}
options[:config_names] = []
options[:prune] = false
OptionParser.new do |parser|
parser.on("-c NAME", "--configuration-name NAME", "Name of the configuration to use") do |v|
@ -42,6 +52,10 @@ OptionParser.new do |parser|
exit
end
parser.on('-p', '--prune', 'Prunes the repository. Does NOT do anything else.') do |v|
options[:prune] = v
end
parser.on('--version', "Print version number") do
puts "#{NAME}, #{VERSION}"
exit
@ -58,17 +72,19 @@ if !Pathname.new(config_file).exist? then
error(1, "Config file #{config_file} has not been found!")
end
def do_backup(config)
puts "Applying configuration #{config['name']} …"
repo = config['repository']
puts "Repository: #{repo}"
if config['password-file']
puts "Reading password from file #{config['password-file']} …"
password = File.open(config['password-file'], &:gets).chomp
else
password = STDIN.getpass('Please put in your restic password: ')
def check
puts 'Checking restic repo …'
check_command = ['restic', 'check']
puts("Command: #{check_command.join(' ')}")
system(*check_command)
if $?.exitstatus > 0
error(1, "Checking restic repository #{ENV['RESTIC_REPOSITORY']} failed.")
end
set_restic_vars(repo, password)
end
def backup(config)
puts "Applying configuration #{config['name']} …"
set_restic_vars(config)
puts "Backing up with restic …"
# https://restic.readthedocs.io/en/latest/040_backup.html#including-and-excluding-files
@ -81,13 +97,7 @@ def do_backup(config)
error(1, 'Failed to do backup.')
end
puts 'Checking restic repo …'
check_command = ['restic', 'check']
puts("Command: #{check_command.join(' ')}")
system(*check_command)
if $?.exitstatus > 0
error(1, "Checking restic repository #{ENV['RESTIC_REPOSITORY']} failed.")
end
check
if config.dig('forget', 'enable')
puts 'Forgetting unnecessary snapshots …'
@ -106,13 +116,28 @@ def do_backup(config)
unset_restic_vars
end
def prune(config)
puts "Applying configuration #{config['name']} …"
set_restic_vars(config)
puts 'Pruning restic repo …'
prune_command = ['restic', 'prune']
puts("Command: #{prune_command.join(' ')}")
system(*prune_command)
if $?.exitstatus > 0
error(1, 'Failed to prune.')
end
check
end
puts "Using config file #{config_file} …"
configs = YAML.load_stream(File.open(config_file))
config_names = options[:config_names]
if config_names.empty?
puts "No configuration name has been given. Will use the first "\
"configuration from the file (#{configs.dig(0, 'name')})."
do_backup(configs[0])
backup configs[0]
else
config_hash = configs.map {|c| [c['name'], c]}.to_h
config_names.each do |name|
@ -121,6 +146,11 @@ else
end
end
config_names.each do |config_name|
do_backup(config_hash[config_name])
config = config_hash[config_name]
if options[:prune]
prune config
else
backup config
end
end
end