From df777475af4694cfc5489ccb29a75a7789a29df6 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Mon, 27 Feb 2017 10:33:55 +0100 Subject: Extract borg related methods into dedicated package Signed-off-by: Florian Pritz --- lib/App/BorgRestore/Borg.pm | 39 +++++++++++++++++++++++++++++++++++++++ script/borg-restore.pl | 22 ++++------------------ 2 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 lib/App/BorgRestore/Borg.pm diff --git a/lib/App/BorgRestore/Borg.pm b/lib/App/BorgRestore/Borg.pm new file mode 100644 index 0000000..426f809 --- /dev/null +++ b/lib/App/BorgRestore/Borg.pm @@ -0,0 +1,39 @@ +package App::BorgRestore::Borg; +use v5.10; +use warnings; +use strict; + +use IPC::Run qw(run start); + +sub borg_list { + my @archives; + + run [qw(borg list)], '>', \my $output or die "borg list returned $?"; + + for (split/^/, $output) { + if (m/^([^\s]+)\s/) { + push @archives, $1; + } + } + + return \@archives; +} + +sub restore { + my $components_to_strip = shift; + my $archive_name = shift; + my $path = shift; + + system(qw(borg extract -v --strip-components), $components_to_strip, "::".$archive_name, $path); +} + +sub list_archive { + my $archive = shift; + my $fh = shift; + + return start [qw(borg list --list-format), '{isomtime} {path}{NEWLINE}', "::".$archive], ">pipe", $fh; +} + +1; + +__END__ diff --git a/script/borg-restore.pl b/script/borg-restore.pl index a7432c4..1876d9f 100755 --- a/script/borg-restore.pl +++ b/script/borg-restore.pl @@ -155,6 +155,7 @@ See gpl-3.0.txt for the full license text. use v5.10; +use App::BorgRestore::Borg; use App::BorgRestore::DB; use App::BorgRestore::Helper; use App::BorgRestore::Settings; @@ -169,7 +170,6 @@ use File::Slurp; use File::Spec; use File::Temp; use Getopt::Long; -use IPC::Run qw(run start); use List::Util qw(any all); use Pod::Usage; use Time::HiRes; @@ -181,20 +181,6 @@ sub debug { say STDERR @_ if $opts{debug}; } -sub borg_list { - my @archives; - - run [qw(borg list)], '>', \my $output or die "borg list returned $?"; - - for (split/^/, $output) { - if (m/^([^\s]+)\s/) { - push @archives, $1; - } - } - - return \@archives; -} - sub find_archives { my $path = shift; @@ -339,7 +325,7 @@ sub restore { $final_destination = App::BorgRestore::Helper::untaint($final_destination, qr(.*)); debug("Removing ".$final_destination); File::Path::remove_tree($final_destination); - system(qw(borg extract -v --strip-components), $components_to_strip, "::".$archive_name, $path); + App::BorgRestore::Borg::restore($components_to_strip, $archive_name, $path); } sub get_cache_dir { @@ -461,7 +447,7 @@ sub handle_added_archives { debug(sprintf("Adding archive %s", $archive)); - my $proc = start [qw(borg list --list-format), '{isomtime} {path}{NEWLINE}', "::".$archive], ">pipe", \*OUT; + my $proc = App::BorgRestore::Borg::list_archive($archive, \*OUT); while () { # roll our own parsing of timestamps for speed since we will be parsing # a huge number of lines here @@ -489,7 +475,7 @@ sub handle_added_archives { } sub build_archive_cache { - my $borg_archives = borg_list(); + my $borg_archives = App::BorgRestore::Borg::borg_list(); my $db_path = get_cache_path('archives.db'); # ensure the cache directory exists -- cgit v1.2.3-24-g4f1b