mirror of
https://codeberg.org/puppe/mpbackup.git
synced 2025-12-19 21:42:17 +01:00
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.
This commit is contained in:
parent
fa451cb664
commit
c400af5ed4
1 changed files with 32 additions and 20 deletions
52
mpbackup
52
mpbackup
|
|
@ -37,17 +37,18 @@ unset_restic_vars
|
||||||
options = {}
|
options = {}
|
||||||
options[:config_names] = []
|
options[:config_names] = []
|
||||||
options[:prune] = false
|
options[:prune] = false
|
||||||
|
options[:run_restic] = false
|
||||||
|
|
||||||
OptionParser.new do |parser|
|
OptionParser.new do |parser|
|
||||||
parser.on("-c NAME", "--configuration-name NAME", "Name of the configuration to use") do |v|
|
parser.on("-c NAME", "--configuration-name NAME", "Name of the configuration to use") do |v|
|
||||||
options[:config_names] << v
|
options[:config_names] << v
|
||||||
end
|
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
|
options[:config_file] = v
|
||||||
end
|
end
|
||||||
|
|
||||||
parser.on("-h", "--help", "Print this help") do
|
parser.on("-h", "--help", "Print this help.") do
|
||||||
puts parser
|
puts parser
|
||||||
exit
|
exit
|
||||||
end
|
end
|
||||||
|
|
@ -56,7 +57,14 @@ OptionParser.new do |parser|
|
||||||
options[:prune] = v
|
options[:prune] = v
|
||||||
end
|
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}"
|
puts "#{NAME}, #{VERSION}"
|
||||||
exit
|
exit
|
||||||
end
|
end
|
||||||
|
|
@ -83,9 +91,6 @@ def check
|
||||||
end
|
end
|
||||||
|
|
||||||
def backup(config)
|
def backup(config)
|
||||||
puts "Applying configuration ‘#{config['name']}’ …"
|
|
||||||
set_restic_vars(config)
|
|
||||||
|
|
||||||
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
|
||||||
exclude = config.dig('backup', 'exclude')&.flat_map{|e| ['--exclude', e]} || []
|
exclude = config.dig('backup', 'exclude')&.flat_map{|e| ['--exclude', e]} || []
|
||||||
|
|
@ -117,9 +122,6 @@ def backup(config)
|
||||||
end
|
end
|
||||||
|
|
||||||
def prune(config)
|
def prune(config)
|
||||||
puts "Applying configuration ‘#{config['name']}’ …"
|
|
||||||
set_restic_vars(config)
|
|
||||||
|
|
||||||
puts 'Pruning restic repo …'
|
puts 'Pruning restic repo …'
|
||||||
prune_command = ['restic', 'prune']
|
prune_command = ['restic', 'prune']
|
||||||
puts("Command: #{prune_command.join(' ')}")
|
puts("Command: #{prune_command.join(' ')}")
|
||||||
|
|
@ -131,17 +133,32 @@ def prune(config)
|
||||||
check
|
check
|
||||||
end
|
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} …"
|
puts "Using config file #{config_file} …"
|
||||||
configs = YAML.load_stream(File.open(config_file))
|
configs = YAML.load_stream(File.open(config_file))
|
||||||
config_names = options[:config_names]
|
config_names = options[:config_names]
|
||||||
if config_names.empty?
|
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')}’)."
|
||||||
if options[:prune]
|
act(configs[0], options)
|
||||||
prune configs[0]
|
|
||||||
else
|
|
||||||
backup configs[0]
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
config_hash = configs.map {|c| [c['name'], c]}.to_h
|
config_hash = configs.map {|c| [c['name'], c]}.to_h
|
||||||
config_names.each do |name|
|
config_names.each do |name|
|
||||||
|
|
@ -150,11 +167,6 @@ else
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
config_names.each do |config_name|
|
config_names.each do |config_name|
|
||||||
config = config_hash[config_name]
|
act(config_hash[config_name], options)
|
||||||
if options[:prune]
|
|
||||||
prune config
|
|
||||||
else
|
|
||||||
backup config
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue