Exit if configuration with given name is not found

This commit is contained in:
Martin Puppe 2020-03-25 20:20:37 +01:00
parent 689b1a904e
commit 9958709a32

View file

@ -18,6 +18,11 @@ def unset_restic_vars
ENV.delete('RESTIC_PASSWORD') ENV.delete('RESTIC_PASSWORD')
end end
def error(exit_status, message)
STDERR.puts("Error: #{message}")
exit exit_status
end
unset_restic_vars unset_restic_vars
options = {} options = {}
@ -50,8 +55,7 @@ else
end end
if !Pathname.new(config_file).exist? then if !Pathname.new(config_file).exist? then
STDERR.puts "Config file #{config_file} has not been found!" error(1, "Config file #{config_file} has not been found!")
exit 1
end end
def do_backup(config) def do_backup(config)
@ -74,8 +78,7 @@ def do_backup(config)
puts("Command: #{backup_command.join(' ')}") puts("Command: #{backup_command.join(' ')}")
system(*backup_command) system(*backup_command)
if $?.exitstatus > 0 if $?.exitstatus > 0
STDERR.puts 'Failed to do backup.' error(1, 'Failed to do backup.')
exit 1
end end
puts 'Checking restic repo …' puts 'Checking restic repo …'
@ -83,8 +86,7 @@ def do_backup(config)
puts("Command: #{check_command.join(' ')}") puts("Command: #{check_command.join(' ')}")
system(*check_command) system(*check_command)
if $?.exitstatus > 0 if $?.exitstatus > 0
STDERR.puts "Checking restic repository #{ENV['RESTIC_REPOSITORY']} failed." error(1, "Checking restic repository #{ENV['RESTIC_REPOSITORY']} failed.")
exit 1
end end
if config.dig('forget', 'enable') if config.dig('forget', 'enable')
@ -97,8 +99,7 @@ def do_backup(config)
# Data will only be deleted when `restic prune` is executed or when # Data will only be deleted when `restic prune` is executed or when
# `restic forget` is called with `--prune`. # `restic forget` is called with `--prune`.
if $?.exitstatus > 0 if $?.exitstatus > 0
STDERR.puts "Forgetting snapshots failed." error(1, "Forgetting snapshots failed.")
exit 1
end end
end end
@ -113,11 +114,13 @@ if config_names.empty?
"configuration from the file (#{configs.dig(0, 'name')})." "configuration from the file (#{configs.dig(0, 'name')})."
do_backup(configs[0]) do_backup(configs[0])
else else
config_names.each do |config_name| config_hash = configs.map {|c| [c['name'], c]}.to_h
configs.each do |config| config_names.each do |name|
if config_name == config['name'] if !config_hash.key? name
do_backup(config) error(1, "Cannot find configuration #{name}.")
end
end end
end end
config_names.each do |config_name|
do_backup(config_hash[config_name])
end
end end