diff --git a/.envrc b/.envrc deleted file mode 100644 index 1d953f4..0000000 --- a/.envrc +++ /dev/null @@ -1 +0,0 @@ -use nix diff --git a/mpbackup b/mpbackup index 0613d7d..594526b 100644 --- a/mpbackup +++ b/mpbackup @@ -6,7 +6,7 @@ require 'yaml' DEFAULT_CONFIG_FILE = Pathname.new('/etc/mpbackup/config.yaml') NAME = 'mpbackup' -VERSION = '0.4.0' +VERSION = '0.6.0' def set_restic_vars(config) if config['repository-file'] @@ -23,6 +23,10 @@ def set_restic_vars(config) else password = STDIN.getpass('Please put in your restic password: ') end + if config['cache-dir'] + puts "Setting RESTIC_CACHE_DIR to #{config['cache-dir']} …" + ENV['RESTIC_CACHE_DIR'] = config['cache-dir'] + end ENV['RESTIC_REPOSITORY'] = repo ENV['RESTIC_PASSWORD'] = password @@ -31,6 +35,13 @@ end def unset_restic_vars ENV.delete('RESTIC_REPOSITORY') ENV.delete('RESTIC_PASSWORD') + ENV.delete('RESTIC_CACHE_DIR') +end + +def set_restic_path(config) + if !config.key? 'restic-path' + config['restic-path'] = 'restic' + end end def error(exit_status, message) @@ -38,35 +49,39 @@ def error(exit_status, message) exit exit_status end -def check +def check(config) puts 'Checking restic repo …' - check_command = ['restic', 'check'] + check_command = [config['restic-path'], 'check'] puts("Command: #{check_command.join(' ')}") system(*check_command) if $?.exitstatus > 0 - error(1, "Checking restic repository #{ENV['RESTIC_REPOSITORY']} failed.") + error(1, "Checking 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 + flags = config['backup'].select{|k,v| + k != 'exclude' && k != 'paths' && k != 'tags'} + flags = flags.map{|k,v| "--#{k}=#{v.to_s}"} exclude = config.dig('backup', 'exclude')&.flat_map{|e| ['--exclude', e]} || [] + tags = config.dig('backup', 'tags')&.flat_map{|t| ['--tag', t]} || [] paths = config.dig('backup', 'paths') || [] - backup_command = ['restic', 'backup', *exclude, *paths] + backup_command = [config['restic-path'], 'backup', *exclude, *paths, *flags] puts("Command: #{backup_command.join(' ')}") system(*backup_command) if $?.exitstatus > 0 error(1, 'Failed to do backup.') end - check + check(config) 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 + forget_command = [config['restic-path'], 'forget'] + flags puts("Command: #{forget_command.join(' ')}") system(*forget_command) # Data will only be deleted when `restic prune` is executed or when @@ -79,7 +94,7 @@ end def prune(config) puts 'Pruning restic repo …' - prune_command = ['restic', 'prune'] + prune_command = [config['restic-path'], 'prune'] puts("Command: #{prune_command.join(' ')}") system(*prune_command) if $?.exitstatus > 0 @@ -92,12 +107,13 @@ end def run_restic(config) puts 'Executing restic with the following arguments …' puts "ARGV: #{ARGV}" - exec('restic', *ARGV) + exec(config['restic-path'], *ARGV) end def act(config, options) puts "Applying configuration ‘#{config['name']}’ …" set_restic_vars(config) + set_restic_path(config) if options[:prune] prune config elsif options[:run_restic]