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