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

 App::BorgRestore->new(\%deps);

Returns a new instance. The following dependencies can be injected:

=over

=item * borg (optional)

This object is used to interact with the borg repository of the system.
Defaults to L<App::BorgRestore::Borg>

=item * db (optional)

This object is used to store the extracted data (the cache). Defaults to
L<App::BorgRestore::DB>

=back

=cut

method new($class: $deps = {}) {
	$deps->{settings} //= App::BorgRestore::Settings->new();

	my $config = $deps->{settings}->get_config();

	$deps->{borg} //= App::BorgRestore::Borg->new(@{$config->{borg}}{qw(repo backup_prefix)});
	$deps->{db} //= App::BorgRestore::DB->new($config->{cache}->{database_path}, $config->{cache}->{sqlite_memory_cache_size});

	return $class->new_no_defaults($deps, $config);
}

=head3 new_no_defaults

Same as C<new> except that this does not initialize unset dependencies with
their default values. This is probably only useful for tests.

=cut

method new_no_defaults($class: $deps, $config = {}) {
	my $self = {};
	bless $self, $class;

	$self->{deps} = $deps;
	$self->{config} = $config;

	return $self;
}



( run in 0.920 second using v1.01-cache-2.11-cpan-39bf76dae61 )