Select configuration via flag -c

The configuration names used to be positional arguments at the end of
the invocation of mpbackup. Now, they are specified with the flag c and
can occur at any position.

This commit also adds the --version flag that prints the version number.
This commit is contained in:
Martin Puppe 2020-03-25 19:33:37 +01:00
parent 882c3140fb
commit 689b1a904e

View file

@ -4,22 +4,43 @@ require 'optparse'
require 'pathname' require 'pathname'
require 'yaml' require 'yaml'
ENV.delete('RESTIC_REPOSITORY')
ENV.delete('RESTIC_PASSWORD')
DEFAULT_CONFIG_FILE = Pathname.new('/etc/mpbackup/config.yaml') 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 = {}
options[:config_names] = []
OptionParser.new do |parser| 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 options[:config_file] = v
end end
parser.on("-h", "--help", "Prints this help") do parser.on("-h", "--help", "Print this help") do
puts parser puts parser
exit exit
end end
parser.on('--version', "Print version number") do
puts "#{NAME}, #{VERSION}"
exit
end
end.parse! end.parse!
if options[:config_file] if options[:config_file]
@ -35,14 +56,15 @@ end
def do_backup(config) def do_backup(config)
puts "Applying configuration #{config['name']} …" puts "Applying configuration #{config['name']} …"
ENV['RESTIC_REPOSITORY'] = config['repository'] repo = config['repository']
puts "Repository: #{ENV['RESTIC_REPOSITORY']}" puts "Repository: #{repo}"
if config['password-file'] if config['password-file']
puts "Reading password from file #{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 else
ENV['RESTIC_PASSWORD'] = STDIN.getpass('Please put in your restic password: ') password = STDIN.getpass('Please put in your restic password: ')
end end
set_restic_vars(repo, password)
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
@ -80,18 +102,18 @@ def do_backup(config)
end end
end end
ENV.delete('RESTIC_REPOSITORY') unset_restic_vars
ENV.delete('RESTIC_PASSWORD')
end 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))
if ARGV.empty? config_names = options[:config_names]
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]) do_backup(configs[0])
else else
ARGV.each do |config_name| config_names.each do |config_name|
configs.each do |config| configs.each do |config|
if config_name == config['name'] if config_name == config['name']
do_backup(config) do_backup(config)