Move method definitions to the top

This commit is contained in:
Martin Puppe 2020-03-25 23:42:57 +01:00
parent 9a7ac16d9f
commit 9e3a9f27ff

142
mpbackup
View file

@ -32,6 +32,76 @@ def error(exit_status, message)
exit exit_status exit exit_status
end 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 unset_restic_vars
options = {} options = {}
@ -80,78 +150,6 @@ if !Pathname.new(config_file).exist? then
error(1, "Config file #{config_file} has not been found!") error(1, "Config file #{config_file} has not been found!")
end 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} …" 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]