diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/App/BorgRestore.pm | 31 | ||||
-rw-r--r-- | lib/App/BorgRestore/DB.pm | 10 | ||||
-rw-r--r-- | lib/App/BorgRestore/Settings.pm | 15 |
3 files changed, 33 insertions, 23 deletions
diff --git a/lib/App/BorgRestore.pm b/lib/App/BorgRestore.pm index 8599f6b..bb382e9 100644 --- a/lib/App/BorgRestore.pm +++ b/lib/App/BorgRestore.pm @@ -5,10 +5,14 @@ use warnings; our $VERSION = "2.0.0"; +use App::BorgRestore::Borg; +use App::BorgRestore::DB; +use App::BorgRestore::Helper; +use App::BorgRestore::Settings; + use autodie; use Cwd qw(abs_path getcwd); use File::Basename; -use File::Path qw(mkpath); use File::Slurp; use File::Spec; use File::Temp; @@ -65,9 +69,12 @@ sub new { my $self = {}; bless $self, $class; + my $db_path = App::BorgRestore::Settings::get_cache_path('archives.db'); + # TODO: make db_path configurable, probably settings too + $self->{opts} = $opts; $self->{borg} = $deps->{borg} // App::BorgRestore::Borg->new(); - $self->{db} = $deps->{db} // App::BorgRestore::DB->new(); + $self->{db} = $deps->{db} // App::BorgRestore::DB->new($db_path); return $self; } @@ -214,17 +221,6 @@ sub restore { $self->{borg}->restore($components_to_strip, $archive_name, $path); } -sub get_cache_dir { - my $self = shift; - return "$App::BorgRestore::Settings::cache_path_base/v2"; -} - -sub get_cache_path { - my $self = shift; - my $item = shift; - return $self->get_cache_dir()."/$item"; -} - sub get_temp_path { my $self = shift; my $item = shift; @@ -353,15 +349,6 @@ sub build_archive_cache { my $borg_archives = $self->{borg}->borg_list(); my $db_path = $self->get_cache_path('archives.db'); - # ensure the cache directory exists - mkpath($self->get_cache_dir(), {mode => oct(700)}); - - if (! -f $db_path) { - $self->debug("Creating initial database"); - my $db = $self->open_db($db_path); - $self->{db}->initialize_db(); - } - my $archives = $self->{db}->get_archive_names(); $self->debug(sprintf("Found %d archives in db", scalar(@$archives))); diff --git a/lib/App/BorgRestore/DB.pm b/lib/App/BorgRestore/DB.pm index feebd4c..a7c5916 100644 --- a/lib/App/BorgRestore/DB.pm +++ b/lib/App/BorgRestore/DB.pm @@ -7,6 +7,7 @@ use App::BorgRestore::Helper; use Data::Dumper; use DBI; +use Log::Any qw($log); sub new { my $class = shift; @@ -15,7 +16,12 @@ sub new { my $self = {}; bless $self, $class; - $self->_open_db($db_path); + if (! -f $db_path) { + my $db = $self->open_db($db_path); + $self->{db}->initialize_db(); + } else { + $self->_open_db($db_path); + } return $self; } @@ -24,6 +30,7 @@ sub _open_db { my $self = shift; my $dbfile = shift; + $log->debugf("Opening database at %s", $dbfile); $self->{dbh} = DBI->connect("dbi:SQLite:dbname=$dbfile","","", {RaiseError => 1, Taint => 1}); $self->{dbh}->do("PRAGMA cache_size=-1024000"); $self->{dbh}->do("PRAGMA strict=ON"); @@ -32,6 +39,7 @@ sub _open_db { sub initialize_db { my $self = shift; + $log->debug("Creating initial database"); $self->{dbh}->do('create table `files` (`path` text, primary key (`path`)) without rowid;'); $self->{dbh}->do('create table `archives` (`archive_name` text unique);'); } diff --git a/lib/App/BorgRestore/Settings.pm b/lib/App/BorgRestore/Settings.pm index 7416c54..93312b8 100644 --- a/lib/App/BorgRestore/Settings.pm +++ b/lib/App/BorgRestore/Settings.pm @@ -5,6 +5,8 @@ use warnings; use App::BorgRestore::Helper; +use File::Path qw(mkpath); + our $borg_repo = ""; our $cache_path_base = sprintf("%s/borg-restore.pl", $ENV{XDG_CACHE_HOME} // $ENV{HOME}."/.cache"); our @backup_prefixes = ( @@ -28,6 +30,19 @@ for my $configfile (@configfiles) { } $cache_path_base = App::BorgRestore::Helper::untaint($cache_path_base, qr/.*/); +# ensure the cache directory exists +mkpath(get_cache_dir(), {mode => oct(700)}); + +sub get_cache_dir { + return "$cache_path_base/v2"; +} + +sub get_cache_path { + my $item = shift; + return get_cache_dir()."/$item"; +} + + 1; __END__ |