From 1b3edb9db50fc047f10f2b082becbc3de6863727 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 19 Feb 2017 19:47:17 +0100 Subject: borg-restore.pl: Add config file Signed-off-by: Florian Pritz --- borg-restore.pl | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 80 insertions(+), 8 deletions(-) diff --git a/borg-restore.pl b/borg-restore.pl index 5a0a139..2735a77 100755 --- a/borg-restore.pl +++ b/borg-restore.pl @@ -55,6 +55,60 @@ s (seconds), min (minutes), h (hours), d (days), m (months = 31 days), y (year). =back +=head1 CONFIGURATION + +borg-restore.pl searches for configuration files in the following locations in +order. The first file found will be used, any later ones are ignored. If no +files are found, defaults are used. + +=over + +=item * $XDG_CONFIG_HOME/borg-restore.cfg + +=item * /etc/borg-restore.cfg + +=back + +=head2 Configuration Options + +You can set the following options in the config file. + +Note that the configuration file is parsed as a perl script. Thus you can also +use any features available in perl itself. + +=over + +=item C<$cache_path_base> + +This defaults to "C<$XDG_CACHE_HOME>/borg-restore.pl". It contains the lookup database. + +=item C<@backup_prefixes> + +This is an array of prefixes that need to be added when looking up a file in the +backup archives. If you use filesystem snapshots and the snapshot for /home is +located at /mnt/snapshots/home, you have to add the following: + +# In the backup archives, /home has the path /mnt/snapshots/home +{regex => "^/home/", replacement => "mnt/snapshots/home/"}, + +The regex must always include the leading slash and it is suggested to include +a tailing slash as well to prevent clashes with directories that start with the +same string. The first regex that matches for a given file is used. This +setting only affects lookups, it does not affect the creation of the database +with --update-database. + +=back + +=head2 Example Configuration + + $cache_path_base = "/mnt/somewhere/borg-restore.pl-cache"; + @backup_prefixes = ( + {regex => "^/home/", replacement => "mnt/snapshots/home/"}, + # /boot is not snapshotted + {regex => "^/boot", replacement => ""}, + {regex => "^/", replacement => "mnt/snapshots/root/"}, + ); + =head1 SYNOPSIS borg-restore.pl [options] @@ -76,6 +130,30 @@ borg-restore.pl [options] use v5.10; +{ +package Settings; + our $cache_path_base = sprintf("%s/borg-restore.pl", $ENV{XDG_CACHE_HOME} // $ENV{HOME}."/.cache"); + our @backup_prefixes = ( + {regex => "^/", replacement => ""}, + ); + + my @configfiles = ( + sprintf("%s/borg-restore.cfg", $ENV{XDG_CONFIG_HOME} // $ENV{HOME}."/.config"), + "/etc/borg-restore.cfg", + ); + + for my $configfile (@configfiles) { + if (-e $configfile) { + unless (my $return = do $configfile) { + die "couldn't parse $configfile: $@" if $@; + die "couldn't do $configfile: $!" unless defined $return; + die "couldn't run $configfile" unless $return; + } + } + } + $cache_path_base = Helper::untaint($cache_path_base, qr/.*/); +} + package main; use autodie; @@ -95,12 +173,6 @@ use Time::HiRes; my %opts; my %db; -my $cache_path_base = "/home/flo/.cache/borg-restore.pl"; -my @backup_prefixes = ( - {regex => "^/home/", replacement => "mnt/snapshots/vg0-homeborg/"}, - {regex => "^/boot/", replacement => ""}, - {regex => "^/", replacement => "mnt/snapshots/vg0-rootborg/"}, -); sub debug { say STDERR @_ if $opts{debug}; @@ -268,7 +340,7 @@ sub restore { } sub get_cache_dir { - return "$cache_path_base/v2"; + return "$Settings::cache_path_base/v2"; } sub get_cache_path { @@ -534,7 +606,7 @@ sub main { $destination = dirname($abs_path); } my $backup_path = $abs_path; - for my $backup_prefix (@backup_prefixes) { + for my $backup_prefix (@Settings::backup_prefixes) { if ($backup_path =~ m/$backup_prefix->{regex}/) { $backup_path =~ s/$backup_prefix->{regex}/$backup_prefix->{replacement}/; last; -- cgit v1.2.3-24-g4f1b