Compare commits

..

No commits in common. "master" and "v0.5.0" have entirely different histories.

View file

@ -6,7 +6,9 @@ require 'yaml'
DEFAULT_CONFIG_FILE = Pathname.new('/etc/mpbackup/config.yaml') DEFAULT_CONFIG_FILE = Pathname.new('/etc/mpbackup/config.yaml')
NAME = 'mpbackup' NAME = 'mpbackup'
VERSION = '0.6.0' VERSION = '0.5.0'
$original_cache_home = ENV['XDG_CACHE_HOME']
def set_restic_vars(config) def set_restic_vars(config)
if config['repository-file'] if config['repository-file']
@ -23,9 +25,9 @@ def set_restic_vars(config)
else else
password = STDIN.getpass('Please put in your restic password: ') password = STDIN.getpass('Please put in your restic password: ')
end end
if config['cache-dir'] if config['cache-home']
puts "Setting RESTIC_CACHE_DIR to #{config['cache-dir']} …" puts "Setting XDG_CACHE_HOME to #{config['cache-home']} …"
ENV['RESTIC_CACHE_DIR'] = config['cache-dir'] ENV['XDG_CACHE_HOME'] = config['cache-home']
end end
ENV['RESTIC_REPOSITORY'] = repo ENV['RESTIC_REPOSITORY'] = repo
@ -35,12 +37,10 @@ end
def unset_restic_vars def unset_restic_vars
ENV.delete('RESTIC_REPOSITORY') ENV.delete('RESTIC_REPOSITORY')
ENV.delete('RESTIC_PASSWORD') ENV.delete('RESTIC_PASSWORD')
ENV.delete('RESTIC_CACHE_DIR') if $original_cache_home
end ENV['XDG_CACHE_HOME'] = $original_cache_home
else
def set_restic_path(config) ENV.delete('XDG_CACHE_HOME')
if !config.key? 'restic-path'
config['restic-path'] = 'restic'
end end
end end
@ -49,9 +49,9 @@ def error(exit_status, message)
exit exit_status exit exit_status
end end
def check(config) def check
puts 'Checking restic repo …' puts 'Checking restic repo …'
check_command = [config['restic-path'], 'check'] check_command = ['restic', 'check']
puts("Command: #{check_command.join(' ')}") puts("Command: #{check_command.join(' ')}")
system(*check_command) system(*check_command)
if $?.exitstatus > 0 if $?.exitstatus > 0
@ -62,26 +62,24 @@ end
def backup(config) def backup(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
flags = config['backup'].select{|k,v| flags = config['backup'].select{|k,v| k != 'exclude' && k != 'paths'}
k != 'exclude' && k != 'paths' && k != 'tags'}
flags = flags.map{|k,v| "--#{k}=#{v.to_s}"} flags = flags.map{|k,v| "--#{k}=#{v.to_s}"}
exclude = config.dig('backup', 'exclude')&.flat_map{|e| ['--exclude', e]} || [] 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') || [] paths = config.dig('backup', 'paths') || []
backup_command = [config['restic-path'], 'backup', *exclude, *paths, *flags] backup_command = ['restic', 'backup', *exclude, *paths, *flags]
puts("Command: #{backup_command.join(' ')}") puts("Command: #{backup_command.join(' ')}")
system(*backup_command) system(*backup_command)
if $?.exitstatus > 0 if $?.exitstatus > 0
error(1, 'Failed to do backup.') error(1, 'Failed to do backup.')
end end
check(config) check
if config.dig('forget', 'enable') if config.dig('forget', 'enable')
puts 'Forgetting unnecessary snapshots …' puts 'Forgetting unnecessary snapshots …'
flags = config['forget'].select{|k,v| k != 'enable'} flags = config['forget'].select{|k,v| k != 'enable'}
flags = flags.flat_map{|k,v| ['--' + k, v.to_s]} flags = flags.flat_map{|k,v| ['--' + k, v.to_s]}
forget_command = [config['restic-path'], 'forget'] + flags forget_command = ['restic', 'forget'] + flags
puts("Command: #{forget_command.join(' ')}") puts("Command: #{forget_command.join(' ')}")
system(*forget_command) system(*forget_command)
# Data will only be deleted when `restic prune` is executed or when # Data will only be deleted when `restic prune` is executed or when
@ -94,7 +92,7 @@ end
def prune(config) def prune(config)
puts 'Pruning restic repo …' puts 'Pruning restic repo …'
prune_command = [config['restic-path'], 'prune'] prune_command = ['restic', 'prune']
puts("Command: #{prune_command.join(' ')}") puts("Command: #{prune_command.join(' ')}")
system(*prune_command) system(*prune_command)
if $?.exitstatus > 0 if $?.exitstatus > 0
@ -107,13 +105,12 @@ end
def run_restic(config) def run_restic(config)
puts 'Executing restic with the following arguments …' puts 'Executing restic with the following arguments …'
puts "ARGV: #{ARGV}" puts "ARGV: #{ARGV}"
exec(config['restic-path'], *ARGV) exec('restic', *ARGV)
end end
def act(config, options) def act(config, options)
puts "Applying configuration #{config['name']} …" puts "Applying configuration #{config['name']} …"
set_restic_vars(config) set_restic_vars(config)
set_restic_path(config)
if options[:prune] if options[:prune]
prune config prune config
elsif options[:run_restic] elsif options[:run_restic]