summaryrefslogtreecommitdiffstats
path: root/lib/App/BorgRestore/Borg.pm
blob: bd9ac3cbcca3368425567016976f32d6d56c74a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package App::BorgRestore::Borg;
use v5.14;
use warnings;
use strict;

use App::BorgRestore::Helper;

use autodie;
use Date::Parse;
use Function::Parameters;
use IPC::Run qw(run start new_chunker);
use JSON;
use Log::Any qw($log);
use Version::Compare;

=encoding utf-8

=head1 NAME

App::BorgRestore::Borg - Borg abstraction

=head1 DESCRIPTION

App::BorgRestore::Borg abstracts borg commands used by L<App::BorgRestore>.

=cut

method new($class: $borg_repo) {
	my $self = {};
	bless $self, $class;

	$self->{borg_repo} = $borg_repo;

	$self->{borg_version} = $self->borg_version();

	return $self;
}

=head3 borg_version

Return the version of borg.

=cut

method borg_version() {
	run [qw(borg --version)], ">", \my $output or die $log->error("Failed to determined borg version")."\n";
	if ($output =~ m/^.* ([0-9.a-z]+)$/) {
		return $1;
	}
	die $log->error("Unable to extract borg version from borg --version output")."\n";
}


method borg_list() {
	my @archives;

	$log->debug("Getting archive list");
	run [qw(borg list), $self->{borg_repo}], '>', \my $output or die $log->error("borg list returned $?")."\n";

	for (split/^/, $output) {
		if (m/^([^\s]+)\s/) {
			push @archives, $1;
		}
	}

	return \@archives;
}

method borg_list_time() {
	my @archives;

	if (Version::Compare::version_compare($self->{borg_version}, "1.1") >= 0) {
		$log->debug("Getting archive list via json");
		run [qw(borg list --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->{archive},
				"modification_time" => str2time($archive->{time}),
			};
		}
	} else {
		$log->debug("Getting archive list");
		run [qw(borg list), $self->{borg_repo}], '>', \my $output or die $log->error("borg list returned $?")."\n";

		for (split/^/, $output) {
			# example timestamp: "Wed, 2016-01-27 10:31:59" = 24 chars
			if (m/^([^\s]+)\s+(.{24})/) {
				my $time = App::BorgRestore::Helper::parse_borg_time($2);
				if ($time) {
					push @archives, {
						"archive" => $1,
						"modification_time" => $time,
					};
				}
			}
		}
	}

	return \@archives;
}

method restore($components_to_strip, $archive_name, $path) {
	$log->debugf("Restoring '%s' from archive %s, stripping %d components of the path", $path, $archive_name, $components_to_strip);
	system(qw(borg extract -v --strip-components), $components_to_strip, $self->{borg_repo}."::".$archive_name, $path);
}

method list_archive($archive, $cb) {
	$log->debugf("Fetching file list for archive %s", $archive);
	open (my $fh, '-|', 'borg', qw/list --list-format/, '{isomtime} {path}{NEWLINE}', $self->{borg_repo}."::".$archive);
	while (<$fh>) {
		$cb->($_);
	}

	# this is slow
	#return start [qw(borg list --list-format), '{isomtime} {path}{NEWLINE}', "::".$archive], ">", new_chunker, $cb;
	#$proc->finish() or die "borg list returned $?";
}

1;

__END__