From c400af5ed4304744ac96519674ad6debbceb966f Mon Sep 17 00:00:00 2001 From: Martin Puppe Date: Wed, 25 Mar 2020 23:27:00 +0100 Subject: [PATCH] Add option -r for executing restic The new option -r allows executing any restic command with the repository and password settings specified in the configuration file. --- mpbackup | 52 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/mpbackup b/mpbackup index 0060439..191864e 100644 --- a/mpbackup +++ b/mpbackup @@ -37,17 +37,18 @@ unset_restic_vars options = {} options[:config_names] = [] options[:prune] = false +options[:run_restic] = false OptionParser.new do |parser| parser.on("-c NAME", "--configuration-name NAME", "Name of the configuration to use") do |v| options[:config_names] << v end - parser.on("-f CONFIG_FILE", "--config-file CONFIG_FILE", "Path to the configuration file") do |v| + parser.on("-f CONFIG_FILE", "--config-file CONFIG_FILE", "Path to the configuration file.") do |v| options[:config_file] = v end - parser.on("-h", "--help", "Print this help") do + parser.on("-h", "--help", "Print this help.") do puts parser exit end @@ -56,7 +57,14 @@ OptionParser.new do |parser| options[:prune] = v end - parser.on('--version', "Print version number") do + parser.on('-r', '--run-restic', 'Set environment variables '\ + 'RESTIC_REPOSITORY and RESTIC_PASSWORD, execute restic (while passing '\ + 'all following command line arguments to restic).') do |v| + options[:run_restic] = v + parser.terminate + end + + parser.on('--version', "Print version number.") do puts "#{NAME}, #{VERSION}" exit end @@ -83,9 +91,6 @@ def check 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 exclude = config.dig('backup', 'exclude')&.flat_map{|e| ['--exclude', e]} || [] @@ -117,9 +122,6 @@ def backup(config) 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(' ')}") @@ -131,17 +133,32 @@ def prune(config) 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] if config_names.empty? puts "No configuration name has been given. Will use the first "\ "configuration from the file (‘#{configs.dig(0, 'name')}’)." - if options[:prune] - prune configs[0] - else - backup configs[0] - end + act(configs[0], options) else config_hash = configs.map {|c| [c['name'], c]}.to_h config_names.each do |name| @@ -150,11 +167,6 @@ else end end config_names.each do |config_name| - config = config_hash[config_name] - if options[:prune] - prune config - else - backup config - end + act(config_hash[config_name], options) end end