summaryrefslogtreecommitdiffstats
path: root/script/borg-restore.pl
blob: 923732be4a48571bbc4787fa36cec4971ba8d449 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/perl -T
use warnings;
use strict;

=head1 NAME

borg-restore.pl - Restore paths from borg backups

=head1 SYNOPSIS

borg-restore.pl [options] <path>

 Options:
  --help, -h                 short help message
  --debug                    show debug messages
  --update-cache, -u         update cache files
  --destination, -d <path>   Restore backup to directory <path>
  --time, -t <timespec>      Automatically find newest backup that is at least
                             <time spec> old
  --adhoc                    Do not use the cache, instead provide an
                             unfiltered list of archive to choose from

 Time spec:
  Select the newest backup that is at least <time spec> old.
  Format: <number><unit>
  Units: s (seconds), min (minutes), h (hours), d (days), m (months = 31 days), y (year)

=head1 DESCRIPTION

borg-restore.pl helps to restore files from borg backups.

It takes one path, looks for its backups, shows a list of distinct versions and
allows to select one to be restored. Versions are based on the modification
time of the file.

It is also possible to specify a time for automatic selection of the backup
that has to be restored. If a time is specified, the script will automatically
select the newest backup that is at least as old as the time value that is
passed and restore it without further user interaction.

B<borg-restore.pl --update-cache> has to be executed regularly, ideally after
creating or removing backups.

=cut

=head1 OPTIONS

=over 4

=item B<--help>, B<-h>

Show help message.

=item B<--debug>

Enable debug messages.

=item B<--update-cache>, B<-u>

Update the lookup database. You should run this after creating or removing a backup.

=item B<--destination=>I<path>, B<-d >I<path>

Restore the backup to 'path' instead of its original location. The destination
either has to be a directory or missing in which case it will be created. The
backup will then be restored into the directory with its original file or
directory name.

=item B<--time=>I<timespec>, B<-t >I<timespec>

Automatically find the newest backup that is at least as old as I<timespec>
specifies. I<timespec> is a string of the form "<I<number>><I<unit>>" with I<unit> being one of the following:
s (seconds), min (minutes), h (hours), d (days), m (months = 31 days), y (year). Example: 5.5d

=item B<--adhoc>

Disable usage of the database. In this mode, the list of archives is fetched
directly from borg at run time.  Use this when the cache has not been created
yet and you want to restore a file without having to manually call borg
extract. Using this option will show all archives that borg knows about, even
if they do not contain the file that shall be restored.

=back

=head1 CONFIGURATION

For configuration options please see L<App::BorgRestore::Settings>.

=head1 LICENSE

Copyright (C) 2016-2017  Florian Pritz <bluewind@xinu.at>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

See LICENSE for the full license text.

=cut

use v5.10;

use App::BorgRestore;
use App::BorgRestore::Borg;
use App::BorgRestore::DB;
use App::BorgRestore::Helper;
use App::BorgRestore::Settings;

use autodie;
use Cwd qw(abs_path);
use File::Basename;
use Function::Parameters;
use Getopt::Long;
use Log::Any qw($log);
use Log::Any::Adapter;
use Log::Log4perl;
use Log::Log4perl::Appender::Screen;
use Log::Log4perl::Appender::ScreenColoredLevels;
use Log::Log4perl::Layout::PatternLayout;
use Log::Log4perl::Level;
use Pod::Usage;

my $app;

fun user_select_archive ($archives) {
	my $selected_archive;

	if (!@$archives) {
		return;
	}

	my $counter = 0;
	for my $archive (@$archives) {
		printf "\e[0;33m%3d: \e[1;33m%s\e[0m %s\n", $counter++, App::BorgRestore::Helper::format_timestamp($archive->{modification_time}), $archive->{archive};
	}

	printf "\e[0;34m%s: \e[0m", "Enter ID to restore (Enter to skip)";
	my $selection = <STDIN>;
	return if !defined($selection);
	chomp $selection;

	return unless ($selection =~ /^\d+$/ && defined(${$archives}[$selection]));
	return ${$archives}[$selection];
}

sub logger_setup {
	my $appender = "Screen";
	$appender = "ScreenColoredLevels" if -t STDERR; ## no critic (InputOutput::ProhibitInteractiveTest)

	my $conf = "
	log4perl.rootLogger = INFO, screenlog

	log4perl.appender.screenlog          = Log::Log4perl::Appender::$appender
	log4perl.appender.screenlog.stderr   = 1
	log4perl.appender.screenlog.layout   = Log::Log4perl::Layout::PatternLayout
	log4perl.appender.screenlog.layout.ConversionPattern = %p %m%n

	log4perl.PatternLayout.cspec.U = sub {my \@c = caller(\$_[4]); \$c[0] =~ s/::/./g; return sprintf('%s:%s', \$c[0], \$c[2]);}
	";
	Log::Log4perl::init( \$conf );
	Log::Any::Adapter->set('Log4perl');

	$SIG{__WARN__} = sub {
		local $Log::Log4perl::caller_depth =
			$Log::Log4perl::caller_depth + 1;
		 Log::Log4perl->get_logger()->warn(@_);
	};

	$SIG{__DIE__} = sub {
		# ignore eval blocks
		return if($^S);
		local $Log::Log4perl::caller_depth =
			$Log::Log4perl::caller_depth + 1;
		 Log::Log4perl->get_logger()->fatal(@_);
		 exit(2);
	};
}

sub main {
	logger_setup();

	my %opts;
	# untaint PATH because we do not expect this to be run across user boundaries
	$ENV{PATH} = App::BorgRestore::Helper::untaint($ENV{PATH}, qr(.*));

	Getopt::Long::Configure ("bundling");
	GetOptions(\%opts, "help|h", "debug", "update-cache|u", "destination|d=s", "time|t=s", "adhoc") or pod2usage(2);
	pod2usage(0) if $opts{help};

	if ($opts{debug}) {
		my $logger = Log::Log4perl->get_logger('');
		$logger->level($DEBUG);
		Log::Log4perl->appenders()->{"screenlog"}->layout(
			Log::Log4perl::Layout::PatternLayout->new("%d %8r [%-30U] %p %m%n"));
	}

	$app = App::BorgRestore->new();

	if ($opts{"update-cache"}) {
		$app->update_cache();
		return 0;
	}

	pod2usage(-verbose => 0) if (@ARGV== 0);

	my @paths = @ARGV;

	my $path;
	my $timespec;
	my $destination;
	my $archives;

	$path = $ARGV[0];

	if (defined($opts{destination})) {
		$destination = $opts{destination};
	}

	if (defined($opts{time})) {
		$timespec = $opts{time};
	}

	if (@ARGV > 1) {
		die "Too many arguments";
	}

	my $abs_path = $app->resolve_relative_path($path);

	$destination = dirname($abs_path) unless defined($destination);
	my $backup_path = $app->map_path_to_backup_path($abs_path);

	$log->debug("Asked to restore $backup_path to $destination");

	if ($opts{adhoc}) {
		$archives = $app->get_all_archives();
	} else {
		$archives = $app->find_archives($backup_path);
	}

	my $selected_archive;
	if (defined($timespec)) {
		$selected_archive = $app->select_archive_timespec($archives, $timespec);
	} else {
		$selected_archive = user_select_archive($archives);
	}

	if (!defined($selected_archive)) {
		die "No archive selected or selection invalid";
	}

	$app->restore($backup_path, $selected_archive, $destination);

	return 0;
}

exit main();