diff --git a/mpbackup b/mpbackup index 0825454..748aab3 100644 --- a/mpbackup +++ b/mpbackup @@ -32,6 +32,76 @@ def error(exit_status, message) exit exit_status end +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 +end + +def backup(config) + puts "Backing up with restic …" + # https://restic.readthedocs.io/en/latest/040_backup.html#including-and-excluding-files + exclude = config.dig('backup', 'exclude')&.flat_map{|e| ['--exclude', e]} || [] + paths = config.dig('backup', 'paths') || [] + backup_command = ['restic', 'backup', *exclude, *paths] + puts("Command: #{backup_command.join(' ')}") + system(*backup_command) + if $?.exitstatus > 0 + error(1, 'Failed to do backup.') + end + + check + + if config.dig('forget', 'enable') + puts 'Forgetting unnecessary snapshots …' + flags = config['forget'].select{|k,v| k != 'enable'} + flags = flags.flat_map{|k,v| ['--' + k, v.to_s]} + forget_command = ['restic', 'forget'] + flags + puts("Command: #{forget_command.join(' ')}") + system(*forget_command) + # Data will only be deleted when `restic prune` is executed or when + # `restic forget` is called with `--prune`. + if $?.exitstatus > 0 + error(1, "Forgetting snapshots failed.") + end + end +end + +def prune(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 + +def run_restic(config) + puts 'Executing restic with the following arguments …' + puts "ARGV: #{ARGV}" + exec('restic', *ARGV) +end + +def act(config, options) + puts "Applying configuration ‘#{config['name']}’ …" + set_restic_vars(config) + if options[:prune] + prune config + elsif options[:run_restic] + run_restic config + else + backup config + end + unset_restic_vars +end + unset_restic_vars options = {} @@ -80,78 +150,6 @@ if !Pathname.new(config_file).exist? then error(1, "Config file #{config_file} has not been found!") end -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 -end - -def backup(config) - puts "Backing up with restic …" - # https://restic.readthedocs.io/en/latest/040_backup.html#including-and-excluding-files - exclude = config.dig('backup', 'exclude')&.flat_map{|e| ['--exclude', e]} || [] - paths = config.dig('backup', 'paths') || [] - backup_command = ['restic', 'backup', *exclude, *paths] - puts("Command: #{backup_command.join(' ')}") - system(*backup_command) - if $?.exitstatus > 0 - error(1, 'Failed to do backup.') - end - - check - - if config.dig('forget', 'enable') - puts 'Forgetting unnecessary snapshots …' - flags = config['forget'].select{|k,v| k != 'enable'} - flags = flags.flat_map{|k,v| ['--' + k, v.to_s]} - forget_command = ['restic', 'forget'] + flags - puts("Command: #{forget_command.join(' ')}") - system(*forget_command) - # Data will only be deleted when `restic prune` is executed or when - # `restic forget` is called with `--prune`. - if $?.exitstatus > 0 - error(1, "Forgetting snapshots failed.") - end - end - - unset_restic_vars -end - -def prune(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 - -def run_restic(config) - puts 'Executing restic with the following arguments …' - puts "ARGV: #{ARGV}" - exec('restic', *ARGV) -end - -def act(config, options) - puts "Applying configuration ‘#{config['name']}’ …" - set_restic_vars(config) - if options[:prune] - prune config - elsif options[:run_restic] - run_restic config - else - backup config - end - unset_restic_vars -end - puts "Using config file #{config_file} …" configs = YAML.load_stream(File.open(config_file)) config_names = options[:config_names]