diff --git a/mpbackup b/mpbackup index c4904bd..b3369f3 100644 --- a/mpbackup +++ b/mpbackup @@ -4,22 +4,43 @@ require 'optparse' require 'pathname' require 'yaml' -ENV.delete('RESTIC_REPOSITORY') -ENV.delete('RESTIC_PASSWORD') - DEFAULT_CONFIG_FILE = Pathname.new('/etc/mpbackup/config.yaml') +NAME = 'mpbackup' +VERSION = '0.1.0' + +def set_restic_vars(repo, password) + ENV['RESTIC_REPOSITORY'] = repo + ENV['RESTIC_PASSWORD'] = password +end + +def unset_restic_vars + ENV.delete('RESTIC_REPOSITORY') + ENV.delete('RESTIC_PASSWORD') +end + +unset_restic_vars options = {} +options[:config_names] = [] OptionParser.new do |parser| - parser.on("-f CONFIG_FILE", "--config-file CONFIG_FILE", "Path to config file") do |v| + 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| options[:config_file] = v end - parser.on("-h", "--help", "Prints this help") do + parser.on("-h", "--help", "Print this help") do puts parser exit end + + parser.on('--version', "Print version number") do + puts "#{NAME}, #{VERSION}" + exit + end end.parse! if options[:config_file] @@ -35,14 +56,15 @@ end def do_backup(config) puts "Applying configuration ‘#{config['name']}’ …" - ENV['RESTIC_REPOSITORY'] = config['repository'] - puts "Repository: #{ENV['RESTIC_REPOSITORY']}" + repo = config['repository'] + puts "Repository: #{repo}" if config['password-file'] puts "Reading password from file #{config['password-file']} …" - ENV['RESTIC_PASSWORD'] = File.open(config['password-file'], &:gets).chomp + password = File.open(config['password-file'], &:gets).chomp else - ENV['RESTIC_PASSWORD'] = STDIN.getpass('Please put in your restic password: ') + password = STDIN.getpass('Please put in your restic password: ') end + set_restic_vars(repo, password) puts "Backing up with restic …" # https://restic.readthedocs.io/en/latest/040_backup.html#including-and-excluding-files @@ -80,18 +102,18 @@ def do_backup(config) end end - ENV.delete('RESTIC_REPOSITORY') - ENV.delete('RESTIC_PASSWORD') + unset_restic_vars end puts "Using config file #{config_file} …" configs = YAML.load_stream(File.open(config_file)) -if ARGV.empty? +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]) else - ARGV.each do |config_name| + config_names.each do |config_name| configs.each do |config| if config_name == config['name'] do_backup(config)