2 # Copyright (c) 2009, 2010 Zmanda, Inc. All Rights Reserved.
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License version 2 as published
6 # by the Free Software Foundation.
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20 use lib '@amperldir@';
26 use Amanda::Device qw( :constants );
27 use Amanda::Debug qw( :logging );
28 use Amanda::Config qw( :init :getconf config_dir_relative );
29 use Amanda::Util qw( :constants );
31 use Amanda::Constants;
33 use Amanda::MainLoop qw( :GIOCondition );
38 use Amanda::Xfer qw( :constants );
42 print STDERR "$msg\n" if $msg;
44 Usage: amrestore [--config config] [-b blocksize] [-r|-c|-C] [-p] [-h]
45 [-f filenum] [-l label] [-o configoption]*
46 {device | [--holding] holdingfile}
47 [hostname [diskname [datestamp [hostname [diskname [datestamp ... ]]]]]]"));
55 Amanda::Util::setup_application("amrestore", "server", $CONTEXT_CMDLINE);
57 my $config_overrides = new_config_overrides($#ARGV+1);
59 my ($opt_config, $opt_blocksize, $opt_raw, $opt_compress, $opt_compress_best,
60 $opt_pipe, $opt_header, $opt_filenum, $opt_label, $opt_holding, $opt_restore_src);
62 debug("Arguments: " . join(' ', @ARGV));
63 Getopt::Long::Configure(qw(bundling));
65 'version' => \&Amanda::Util::version_opt,
66 'help|usage|?' => \&usage,
67 'config=s' => \$opt_config,
68 'holding' => \$opt_holding,
69 'b=i' => \$opt_blocksize,
71 'c' => \$opt_compress,
72 'C' => \$opt_compress_best,
75 'f=i' => \$opt_filenum,
77 'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
80 $opt_compress = 1 if $opt_compress_best;
82 # see if we have a holding file or a device
83 usage("Must specify a device or holding-disk file") unless (@ARGV);
84 $opt_restore_src = shift @ARGV;
87 if (Amanda::Holding::get_header($opt_restore_src));
90 my @opt_dumpspecs = Amanda::Cmdline::parse_dumpspecs([@ARGV],
91 $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP);
93 usage("Cannot check a label on a holding-disk file")
94 if ($opt_holding and $opt_label);
95 usage("Cannot use both -r (raw) and -c/-C (compression) -- use -h instead")
96 if ($opt_raw and $opt_compress);
98 # -r implies -h, plus appending ".RAW" to filenames
99 $opt_header = 1 if $opt_raw;
101 set_config_overrides($config_overrides);
103 config_init($CONFIG_INIT_EXPLICIT_NAME, $opt_config);
105 config_init(0, undef);
107 my ($cfgerr_level, @cfgerr_errors) = config_errors();
108 if ($cfgerr_level >= $CFGERR_WARNINGS) {
109 config_print_errors();
110 if ($cfgerr_level >= $CFGERR_ERRORS) {
111 die("errors processing config file");
115 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
121 my ($msg, $finished_cb) = @_;
122 print STDERR "ERROR: $msg\n";
125 $res->release(finished_cb => sub {
135 my ($finished_cb) = @_;
140 my $filenum = $opt_filenum;
141 $filenum = 1 if (!$filenum);
142 $filenum = 0 + "$filenum"; # convert to integer
146 my $steps = define_steps
147 cb_ref => \$finished_cb,
148 finalize => sub { $chg->quit() if defined $chg };
151 # first, return to the original working directory we were started in
152 if (!chdir Amanda::Util::get_original_cwd()) {
153 return failure("Cannot chdir to original working directory", $finished_cb);
157 $steps->{'read_header'}->();
159 my $tlf = Amanda::Config::config_dir_relative(getconf($CNF_TAPELIST));
160 my $tl = Amanda::Tapelist->new($tlf);
161 $chg = Amanda::Changer->new($opt_restore_src, tapelist => $tl);
162 if ($chg->isa("Amanda::Changer::Error")) {
163 return failure($chg, $finished_cb);
166 $chg->load(relative_slot => "current", mode => "read",
167 res_cb => $steps->{'slot_loaded'});
171 step slot_loaded => sub {
172 (my $err, $res) = @_;
173 return failure($err, $finished_cb) if $err;
175 $dev = $res->{'device'};
176 if ($dev->status != $DEVICE_STATUS_SUCCESS) {
177 return failure($dev->error_or_status, $finished_cb);
180 $steps->{'check_label'}->();
183 step check_label => sub {
184 if ($dev->status != $DEVICE_STATUS_SUCCESS) {
185 return failure($dev->error_or_status, $finished_cb);
189 if ($dev->volume_label ne $opt_label) {
190 my $got = $dev->volume_label;
191 return failure("Found unexpected label '$got'", $finished_cb);
195 my $lbl = $dev->volume_label;
196 print STDERR "Restoring from tape $lbl starting with file $filenum.\n";
198 $steps->{'start_device'}->();
201 step start_device => sub {
202 if (!$dev->start($ACCESS_READ, undef, undef)) {
203 return failure($dev->error_or_status(), $finished_cb);
206 $steps->{'read_header'}->();
209 step read_header => sub {
211 print STDERR "Reading from '$opt_restore_src'\n";
212 $hdr = Amanda::Holding::get_header($opt_restore_src);
214 $hdr = $dev->seek_file($filenum);
216 return failure("while reading next header: " . $dev->error_or_status(),
218 } elsif ($hdr->{'type'} == $Amanda::Header::F_TAPEEND) {
219 return $steps->{'finished'}->();
222 # seek_file may have skipped ahead; plan accordingly
223 $filenum = $dev->file + 1;
226 $steps->{'filter_dumpspecs'}->();
229 step filter_dumpspecs => sub {
230 if (@opt_dumpspecs and not $hdr->matches_dumpspecs([@opt_dumpspecs])) {
232 my $dev_filenum = $dev->file;
233 print STDERR "amrestore: $dev_filenum: skipping ",
234 $hdr->summary(), "\n";
237 # skip to the next file without restoring this one
238 return $steps->{'next_file'}->();
242 my $dev_filenum = $dev->file;
243 print STDERR "amrestore: $dev_filenum: restoring ";
245 print STDERR $hdr->summary(), "\n";
247 $steps->{'xfer_dumpfile'}->();
250 step xfer_dumpfile => sub {
253 # set up the source..
255 $src = Amanda::Xfer::Source::Holding->new($opt_restore_src);
257 $src = Amanda::Xfer::Source::Device->new($dev);
260 # and set up the destination..
265 my $filename = sprintf("%s.%s.%s.%d",
267 Amanda::Util::sanitise_filename("".$hdr->{'disk'}), # workaround SWIG bug
269 $hdr->{'dumplevel'});
270 if ($hdr->{'partnum'} > 0) {
271 $filename .= sprintf(".%07d", $hdr->{'partnum'});
274 # add an appropriate suffix
277 } elsif ($opt_compress) {
278 $filename .= ($hdr->{'compressed'} && $hdr->{'comp_suffix'})?
279 $hdr->{'comp_suffix'} : $Amanda::Constants::COMPRESS_SUFFIX;
282 if (!open($dest_fh, ">", $filename)) {
283 return failure("Could not open '$filename' for writing: $!", $finished_cb);
286 $dest = Amanda::Xfer::Dest::Fd->new($dest_fh);
288 # set up any filters that need to be applied, decryption first
290 if ($hdr->{'encrypted'} and not $opt_raw) {
291 if ($hdr->{'srv_encrypt'}) {
293 Amanda::Xfer::Filter::Process->new(
294 [ $hdr->{'srv_encrypt'}, $hdr->{'srv_decrypt_opt'} ], 0);
295 } elsif ($hdr->{'clnt_encrypt'}) {
297 Amanda::Xfer::Filter::Process->new(
298 [ $hdr->{'clnt_encrypt'}, $hdr->{'clnt_decrypt_opt'} ], 0);
300 return failure("could not decrypt encrypted dump: no program specified",
304 $hdr->{'encrypted'} = 0;
305 $hdr->{'srv_encrypt'} = '';
306 $hdr->{'srv_decrypt_opt'} = '';
307 $hdr->{'clnt_encrypt'} = '';
308 $hdr->{'clnt_decrypt_opt'} = '';
309 $hdr->{'encrypt_suffix'} = 'N';
311 if (!$opt_raw and $hdr->{'compressed'} and not $opt_compress) {
312 # need to uncompress this file
314 if ($hdr->{'srvcompprog'}) {
315 # TODO: this assumes that srvcompprog takes "-d" to decompress
317 Amanda::Xfer::Filter::Process->new(
318 [ $hdr->{'srvcompprog'}, "-d" ], 0);
319 } elsif ($hdr->{'clntcompprog'}) {
320 # TODO: this assumes that clntcompprog takes "-d" to decompress
322 Amanda::Xfer::Filter::Process->new(
323 [ $hdr->{'clntcompprog'}, "-d" ], 0);
326 Amanda::Xfer::Filter::Process->new(
327 [ $Amanda::Constants::UNCOMPRESS_PATH,
328 $Amanda::Constants::UNCOMPRESS_OPT ], 0);
332 $hdr->{'compressed'} = 0;
333 $hdr->{'uncompress_cmd'} = '';
334 } elsif (!$opt_raw and !$hdr->{'compressed'} and $opt_compress) {
335 # need to compress this file
337 my $compress_opt = $opt_compress_best?
338 $Amanda::Constants::COMPRESS_BEST_OPT :
339 $Amanda::Constants::COMPRESS_FAST_OPT;
341 Amanda::Xfer::Filter::Process->new(
342 [ $Amanda::Constants::COMPRESS_PATH,
347 $hdr->{'compressed'} = 1;
348 $hdr->{'uncompress_cmd'} = " $Amanda::Constants::UNCOMPRESS_PATH " .
349 "$Amanda::Constants::UNCOMPRESS_OPT |";
350 $hdr->{'comp_suffix'} = $Amanda::Constants::COMPRESS_SUFFIX;
353 # write the header to the destination if requested
355 $hdr->{'blocksize'} = Amanda::Holding::DISK_BLOCK_BYTES;
356 $dest_fh->syswrite($hdr->to_string(32768, 32768));
359 # start reading all filter stderr
360 foreach my $filter (@filters) {
361 my $fd = $filter->get_stderr_fd();
364 my $src = Amanda::MainLoop::fd_source($fd,
365 $G_IO_IN|$G_IO_HUP|$G_IO_ERR);
367 $all_filter{$src} = 1;
368 $src->set_callback( sub {
370 my $n_read = POSIX::read($fd, $b, 1);
371 if (!defined $n_read) {
373 } elsif ($n_read == 0) {
374 delete $all_filter{$src};
377 if (!%all_filter and $restore_done) {
384 print STDERR "filter stderr: $line";
386 debug("filter stderr: $line");
393 my $xfer = Amanda::Xfer->new([ $src, @filters, $dest ]);
395 $xfer->get_source()->set_callback(sub {
396 my ($src, $msg, $xfer) = @_;
398 if ($msg->{'type'} == $XMSG_INFO) {
399 Amanda::Debug::info($msg->{'message'});
400 } elsif ($msg->{'type'} == $XMSG_ERROR) {
401 $got_err = $msg->{'message'};
402 } elsif ($msg->{'type'} == $XMSG_DONE) {
404 $steps->{'xfer_done'}->($got_err);
410 step xfer_done => sub {
412 return failure($err, $finished_cb) if $err;
414 $steps->{'next_file'}->('extracted');
417 step next_file => sub {
418 my ($extracted) = @_;
419 # amrestore does not loop over multiple files when reading from holding
420 # when outputting to a pipe amrestore extracts only the first file
421 if ($opt_holding or ($opt_pipe and $extracted)) {
422 return $steps->{'finished'}->();
425 # otherwise, try to read the next header from the device
426 $steps->{'read_header'}->();
429 step finished => sub {
431 $res->release(finished_cb => $steps->{'quit'});
433 $steps->{'quit'}->();
441 return failure($err, $finished_cb) if $err;
448 main(\&Amanda::MainLoop::quit);
449 Amanda::MainLoop::run();
450 Amanda::Util::finish_application();