-
Notifications
You must be signed in to change notification settings - Fork 1
/
brackup-restore
executable file
·223 lines (165 loc) · 7.22 KB
/
brackup-restore
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
#!/usr/bin/perl
# LICENCE INFORMATION
#
# This file is part of brackup-nn, a backup tool based on Brackup.
#
# Brackup is authored by Brad Fitzpatrick <[email protected]> (and others)
# and is copyright (c) Six Apart, Ltd, with portions copyright (c) Gavin Carr
# <[email protected]> (see code for details). Brackup is licensed for
# use, modification and/or distribution under the same terms as Perl itself.
#
# This file was forked from Brackup on 18 March 2013 and changed on and since
# this date by NewsNow Publishing Limited to effect bug fixes, reliability
# stability and/or performance improvements, and/or feature enhancements;
# and such changes are copyright (c) 2013 NewsNow Publishing Limited. You may
# use, modify, and/or redistribute brackup-nn under the same terms as Perl itself.
#
# TODO: onerror=prompt?
=head1 NAME
brackup-restore - The brackup restore tool.
=head1 SYNOPSIS
$ brackup-restore [-v] --from=foo.brackup --to=<base_directory> --all
$ brackup-restore [-v] --from=foo.brackup --to=<base_directory> --just=<file>
$ brackup-restore [-v] --from=foo.brackup --to=<base_directory> --just=<dir>
=head2 OPTIONS
=over 4
=item --from=NAME
Required. The backup metafile, describing the tree you want to restore.
Probably named like "source-target-YYYYMMDD.brackup". If you lost it,
it's also stored on your backup target, and you can fetch it with
L<brackup-target>.
=item --to=NAME
Required. The destination root directory for your restored files.
Will be created if it doesn't exist.
=item --all
Restore all files.
=item --just="DIRECTORY"
Restore just the directory named (and all its contents).
=item --just="FILE"
Restore just the file named.
=item --onerror=abort|continue
How to handle restore errors. 'abort' reports and stops as soon as an
error is detected. 'continue' continues with the restore, collecting all
errors and reporting them at the end of the restore.
Default: abort.
=item --conflict=abort|skip|overwrite|update|correct|correct-stat-only
How to handle files that already exist (with size > zero bytes).
'skip' means don't restore, keeping the existing file. 'overwrite'
means always restore, replacing the existing file. 'update' means
overwrite iff the file we are restoring is newer than the existing one;
'correct' means overwrite iff the file we are restoring has different
modification time or size; 'correct-stat-only' means overwrite if the
file we are restoring has different file size, but other just correct
file status (access/modification time), ownership & permissions.
Default: abort.
=item --config=NAME
Brackup config file to use instead of default.
=item --verbose|-v
Show more info during restore.
=item --threads=NUMBER
Number of processes to use to restore files in parallel.
=item --numeric-ids
Use original UID and GID numbers, without attempting to map
them to local users and groups.
=item --no-lchown
If the Lchown package is unavailable, an error will be displayed since
it will not be possible to restore the ownership of symlinks. This option
inhibits this error, turning it into a warning.
=item --relative|-R
Rather like rsync's --relative option, this causes files selected using
--just to be restored with their full path relative to the destination root.
=back
=head1 WARRANTY
Brackup is distributed as-is and comes without warranty of any kind,
expressed or implied. We aren't responsible for your data loss.
=head1 AUTHOR
Brad Fitzpatrick E<lt>[email protected]<gt>
Copyright (c) 2006-2007 Six Apart, Ltd. All rights reserved.
This module is free software. You may use, modify, and/or redistribute this
software under the terms of same terms as perl itself.
=cut
use strict;
use warnings;
use Getopt::Long;
use File::Path;
use Try::Tiny;
use FindBin qw($Bin);
use lib "$Bin/lib";
use Brackup;
use Brackup::Util qw(tempfile);
my ($opt_verbose, $meta_file, $opt_help, $restore_dir, $opt_all, $prefix, $config_file, $opt_daemons, $opt_numeric_ids, $opt_no_lchown, $opt_relative);
my $onerror = 'abort';
my $conflict = 'abort';
usage() unless
GetOptions(
'from=s' => \$meta_file,
'to=s' => \$restore_dir,
'verbose' => \$opt_verbose,
'help' => \$opt_help,
'all' => \$opt_all,
'just=s' => \$prefix,
'onerror|on-error=s' => \$onerror,
'conflict=s' => \$conflict,
'config=s' => \$config_file,
'threads=s' => \$opt_daemons,
'numeric_ids|numeric-ids' => \$opt_numeric_ids,
'no-lchown' => \$opt_no_lchown,
'relative|R' => \$opt_relative
);
if ($opt_help) {
eval "use Pod::Usage;";
Pod::Usage::pod2usage( -verbose => 1, -exitval => 0 );
exit 0;
}
usage() unless $meta_file && $restore_dir && ($prefix || $opt_all);
usage("Backup metafile '$meta_file' doesn't exist") unless -e $meta_file;
usage("Backup metafile '$meta_file' isn't a file") unless -f $meta_file;
usage("Restore directory '$restore_dir' isn't a directory") if -e $restore_dir && ! -d $restore_dir;
usage("Config file '$config_file' doesn't exist") if $config_file && ! -f $config_file;
usage("Invalid --onerror option '$onerror' (not abort|continue)")
if $onerror !~ m/^(abort|continue)$/;
usage("Invalid --conflict option '$conflict' (not abort|skip|overwrite|update|correct-stat-only|correct)")
if $conflict !~ m/^(abort|skip|overwrite|update|correct-stat-only|correct)$/;
$prefix ||= ""; # with -all, "", which means everything
if (! -e $restore_dir) {
mkpath($restore_dir, 0, 0700) or die "Cannot create restore directory: $!";
}
$config_file ||= Brackup::Config->default_config_file_name;
my $config = Brackup::Config->load($config_file) if -f $config_file;
my $restore = Brackup::Restore->new(
to => $restore_dir,
prefix => $prefix,
file => $meta_file,
config => $config,
onerror => $onerror,
conflict => $conflict,
verbose => $opt_verbose,
daemons => $opt_daemons,
numeric_ids => $opt_numeric_ids,
no_lchown => $opt_no_lchown,
relative => $opt_relative
);
try {
$restore->restore;
Brackup::ProcManager->assert_all_reaped();
warn "Restore complete.\n" if $opt_verbose;
exit 0;
} catch {
if (ref $_ and ref $_ eq 'ARRAY') {
warn "Restore complete.\n" if $opt_verbose;
warn "\n*** Errors encountered doing restore ***\n" . join('', @$_) . "\n";
}
else {
chomp $_;
warn "Error doing restore: $_\n";
}
exit 1;
};
sub usage {
my $why = shift || "";
if ($why) {
$why =~ s/\s+$//;
$why = "Error: $why\n\n";
}
die "${why}brackup-restore [-v] --from=[metafile.brackup] --to=[restore_dir] <--all|--just=[what]>\nbrackup-restore --help\n";
}