From d28bcb7e09769e6eaf9e4e9f0206fdc9e76a575a Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Wed, 24 Jan 2018 01:10:15 +0100 Subject: Add --list feature to search for files contained in backups by path Signed-off-by: Florian Pritz --- lib/App/BorgRestore.pm | 15 +++++++++++++++ lib/App/BorgRestore/DB.pm | 16 ++++++++++++++++ script/borg-restore.pl | 27 +++++++++++++++++++++++++-- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/lib/App/BorgRestore.pm b/lib/App/BorgRestore.pm index 828838c..c3b37fd 100644 --- a/lib/App/BorgRestore.pm +++ b/lib/App/BorgRestore.pm @@ -402,6 +402,21 @@ method _add_path_to_hash($hash, $path, $time) { } } +=head3 search_path + + my $paths = $app->search_path($pattern) + +Returns a arrayref of paths that match the pattern. The pattern is matched as +an sqlite LIKE pattern. If no % occurs in the pattern, the patterns is +automatically wrapped between two % so it may match anywhere in the path. + +=cut + +method search_path($pattern) { + $pattern = '%'.$pattern.'%' if $pattern !~ m/%/; + return $self->{db}->search_path($pattern); +} + =head3 get_missing_items my $items = $app->get_missing_items($have, $want); diff --git a/lib/App/BorgRestore/DB.pm b/lib/App/BorgRestore/DB.pm index 3fddbf1..1f1427e 100644 --- a/lib/App/BorgRestore/DB.pm +++ b/lib/App/BorgRestore/DB.pm @@ -177,6 +177,22 @@ method verify_cache_fill_rate_ok() { } } +method search_path($pattern) { + $log->debugf("Preparing path search for pattern '%s'", $pattern); + my $st = $self->{dbh}->prepare('select path from files where path like ?'); + $log->debug("Executing search"); + $st->execute($pattern); + $log->debug("Fetching search result"); + + my @ret; + while (my $row = $st->fetchrow_hashref()) { + push @ret, $row->{path}; + } + + $log->debugf("Found %d matching paths", 0+@ret); + return \@ret; +} + method vacuum() { $self->{dbh}->do("vacuum"); } diff --git a/script/borg-restore.pl b/script/borg-restore.pl index f58ba33..76216dc 100755 --- a/script/borg-restore.pl +++ b/script/borg-restore.pl @@ -14,6 +14,8 @@ borg-restore.pl [options] --help, -h short help message --debug show debug messages --update-cache, -u update cache files + --list [pattern] List paths contained in the backups, optionally + matching an SQLite LIKE pattern --destination, -d Restore backup to directory --time, -t Automatically find newest backup that is at least