summaryrefslogtreecommitdiffstats
path: root/lib/App/BorgRestore
diff options
context:
space:
mode:
Diffstat (limited to 'lib/App/BorgRestore')
-rw-r--r--lib/App/BorgRestore/Borg.pm9
-rw-r--r--lib/App/BorgRestore/DB.pm30
-rw-r--r--lib/App/BorgRestore/PathTimeTable/DB.pm6
3 files changed, 44 insertions, 1 deletions
diff --git a/lib/App/BorgRestore/Borg.pm b/lib/App/BorgRestore/Borg.pm
index 04ad89c..9884b21 100644
--- a/lib/App/BorgRestore/Borg.pm
+++ b/lib/App/BorgRestore/Borg.pm
@@ -55,7 +55,14 @@ method borg_list() {
my @archives;
my $backup_prefix = $self->{backup_prefix};
- if (Version::Compare::version_compare($self->{borg_version}, "1.1") >= 0) {
+ if (Version::Compare::version_compare($self->{borg_version}, "1.2") >= 0) {
+ $log->debug("Getting archive list via json");
+ run [qw(borg list --glob-archives), "$backup_prefix*", qw(--json), $self->{borg_repo}], '>', \my $output or die $log->error("borg list returned $?")."\n";
+ my $json = decode_json($output);
+ for my $archive (@{$json->{archives}}) {
+ push @archives, $archive->{archive};
+ }
+ } elsif (Version::Compare::version_compare($self->{borg_version}, "1.1") >= 0) {
$log->debug("Getting archive list via json");
run [qw(borg list --prefix), $backup_prefix, qw(--json), $self->{borg_repo}], '>', \my $output or die $log->error("borg list returned $?")."\n";
my $json = decode_json($output);
diff --git a/lib/App/BorgRestore/DB.pm b/lib/App/BorgRestore/DB.pm
index 3d1d6ad..2999eb4 100644
--- a/lib/App/BorgRestore/DB.pm
+++ b/lib/App/BorgRestore/DB.pm
@@ -95,7 +95,32 @@ method _migrate() {
$self->{dbh}->do('delete from `archives`');
$self->{dbh}->do('delete from `files`');
},
+ 5 => sub {
+ # Remove columns left over by migrations 3 and 4 from files tables
+ my @archive_ids;
+ my $st = $self->{dbh}->prepare("select `id` from `archives`;");
+ $st->execute();
+ while (my $result = $st->fetchrow_hashref) {
+ push @archive_ids, $result->{id};
+ }
+
+ $self->{dbh}->do('create table `files_new` (`path` text, primary key (`path`)) without rowid;');
+ for my $archive_id (@archive_ids) {
+ $archive_id = untaint($archive_id, qr(.*));
+ $self->{dbh}->do('alter table `files_new` add column `'.$archive_id.'` integer;');
+ }
+
+ if (@archive_ids > 0) {
+ my @columns_to_copy = map {'`'.$_.'`'} @archive_ids;
+ @columns_to_copy = ('`path`', @columns_to_copy);
+ $self->{dbh}->do('insert into `files_new` select '.join(',', @columns_to_copy).' from files');
+ }
+
+ $self->{dbh}->do('drop table `files`');
+ $self->{dbh}->do('alter table `files_new` rename to `files`');
+ },
};
+ my $ran_migrations = 0;
for my $target_version (sort { $a <=> $b } keys %$schema) {
if ($version < $target_version) {
@@ -105,8 +130,13 @@ method _migrate() {
$self->_set_db_version($target_version);
$self->{dbh}->commit();
$log->debugf("Schema upgrade to version %s complete", $target_version);
+ $ran_migrations = 1;
}
}
+ if ($ran_migrations) {
+ $log->debug("Vacuuming database");
+ $self->{dbh}->do("vacuum");
+ }
}
method _get_db_version() {
diff --git a/lib/App/BorgRestore/PathTimeTable/DB.pm b/lib/App/BorgRestore/PathTimeTable/DB.pm
index 069d991..685c7b9 100644
--- a/lib/App/BorgRestore/PathTimeTable/DB.pm
+++ b/lib/App/BorgRestore/PathTimeTable/DB.pm
@@ -1,6 +1,7 @@
package App::BorgRestore::PathTimeTable::DB;
use strictures 2;
+use Carp::Assert;
use Function::Parameters;
BEGIN {
use Log::Any qw($log);
@@ -130,6 +131,11 @@ method save_nodes() {
for my $key (keys %{$self->{stats}}) {
$log->debugf("Performance counter %s = %s", $key, $self->{stats}->{$key});
}
+
+ # +2 because:
+ # - borg list gives us `.` as the first path and we essentially skip it
+ # - we call `add_path` with `.` at the beginning of this method
+ assert($self->{stats}->{real_calls_to_db_class} + 2 == $self->{stats}->{total_paths}, "All files were actually added to the database");
}
1;