App-BorgRestore
view release on metacpan or search on metacpan
lib/App/BorgRestore.pm view on Meta::CPAN
package App::BorgRestore;
use v5.14;
use strictures 2;
our $VERSION = "3.4.5";
use App::BorgRestore::Borg;
use App::BorgRestore::DB;
use App::BorgRestore::Helper qw(untaint);
use App::BorgRestore::PathTimeTable::DB;
use App::BorgRestore::PathTimeTable::Memory;
use App::BorgRestore::Settings;
use autodie;
use Carp;
use Cwd qw(abs_path getcwd);
use Path::Tiny;
use File::pushd;
use Function::Parameters;
use Getopt::Long;
use List::Util qw(any all);
use Log::Any qw($log);
use Pod::Usage;
use POSIX ();
use Time::HiRes;
=encoding utf-8
=head1 NAME
App::BorgRestore - Restore paths from borg backups
=head1 SYNOPSIS
use App::BorgRestore;
my $app = App::BorgRestore->new();
# Update the cache (call after creating/removing backups)
$app->update_cache();
# Restore a path from a backup that is at least 5 days old. Optionally
# restore it to a different directory than the original.
# Look at the implementation of this method if you want to know how the
# other parts of this module work together.
$app->restore_simple($path, "5days", $optional_destination_directory);
=head1 DESCRIPTION
App::BorgRestore is a restoration helper for borg.
It maintains a cache of borg backup contents (path and latest modification
time) and allows to quickly look up backups that contain a path. It further
supports restoring a path from an archive. The archive to be used can also be
automatically determined based on the age of the path.
The cache has to be updated regularly, ideally after creating or removing
backups.
L<borg-restore.pl> is a wrapper around this class that allows for simple CLI
usage.
This package uses L<Log::Any> for logging.
=head1 METHODS
=head2 Constructors
=head3 new
lib/App/BorgRestore.pm view on Meta::CPAN
for my $archive (reverse @$archives) {
if ($archive->{modification_time} < $target_timestamp) {
$log->debugf("Found archive with timestamp %s", App::BorgRestore::Helper::format_timestamp($archive->{modification_time}));
return $archive;
}
}
die $log->error("Failed to find archive matching time specification")."\n";
}
method _timespec_to_seconds($timespec) {
if ($timespec =~ m/^(?>(?<value>[0-9.]+))(?>(?<unit>[a-z]+))$/) {
my $value = $+{value};
my $unit = $+{unit};
my %factors = (
s => 1,
second => 1,
seconds => 1,
minute => 60,
minutes => 60,
h => 60*60,
hour => 60*60,
hours => 60*60,
d => 60*60*24,
day => 60*60*24,
days => 60*60*24,
m => 60*60*24*31,
month => 60*60*24*31,
months => 60*60*24*31,
y => 60*60*24*365,
year => 60*60*24*365,
years => 60*60*24*365,
);
if (exists($factors{$unit})) {
return $value * $factors{$unit};
}
}
return;
}
=head3 restore
$app->restore($backup_path, $archive, $destination);
Restore a backup path (returned by C<map_path_to_backup_path>) from an archive
(returned by C<find_archives> or C<get_all_archives>) to a destination
directory.
If the destination path (C<$destination/$last_elem_of_backup_path>) exists, it
is removed before beginning extraction from the backup.
Warning: This method temporarily modifies the current working directory of the
process during method execution since this is required by C<`borg extract`>.
=cut
method restore($path, $archive, $destination) {
$destination = untaint($destination, qr(.*));
$path = untaint($path, qr(.*));
$log->infof("Restoring %s to %s from archive %s", $path, $destination, $archive->{archive});
my $basename = path($path)->basename;
my $components_to_strip =()= $path =~ /\//g;
$log->debugf("CWD is %s", getcwd());
{
$log->debugf("Changing CWD to %s", $destination);
mkdir($destination) unless -d $destination;
my $workdir = pushd($destination, {untaint_pattern => qr{^(.*)$}});
my $final_destination = abs_path($basename);
$final_destination = untaint($final_destination, qr(.*));
$log->debugf("Removing %s", $final_destination);
File::Path::remove_tree($final_destination);
$self->{deps}->{borg}->restore($components_to_strip, $archive->{archive}, $path);
}
$log->debugf("CWD is %s", getcwd());
}
=head3 restore_simple
$app->restore_simple($path, $timespec, $destination);
Restores a C<$path> based on a C<$timespec> to an optional C<$destination>. If
C<$destination> is not specified, it is set to the parent directory of C<$path>
so that C<$path> is restored to its original place.
Refer to L</"select_archive_timespec"> for an explanation of the C<$timespec>
variable.
=cut
method restore_simple($path, $timespec, $destination) {
my $abs_path = $self->resolve_relative_path($path);
my $backup_path = $self->map_path_to_backup_path($abs_path);
$destination //= dirname($abs_path);
my $archives = $self->find_archives($backup_path);
my $selected_archive = $self->select_archive_timespec($archives, $timespec);
$self->restore($backup_path, $selected_archive, $destination);
}
=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->{deps}->{db}->search_path($pattern);
}
=head3 get_missing_items
my $items = $app->get_missing_items($have, $want);
Returns an arrayref of items that are part of C<$want>, but not of C<$have>.
=cut
method get_missing_items($have, $want) {
my $ret = [];
for my $item (@$want) {
my $exists = any { $_ eq $item } @$have;
push @$ret, $item if not $exists;
( run in 1.048 second using v1.01-cache-2.11-cpan-7fcb06a456a )