2 # Copyright (c) 2009-2012 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'};
177 if ($opt_blocksize) {
178 if ( !$dev->property_set("BLOCK_SIZE", $opt_blocksize)) {
179 return failure($dev->error_or_status, $finished_cb);
182 # re-read the label with the correct blocksize
186 if ($dev->status != $DEVICE_STATUS_SUCCESS) {
187 return failure($dev->error_or_status, $finished_cb);
190 $steps->{'check_label'}->();
193 step check_label => sub {
194 if ($dev->status != $DEVICE_STATUS_SUCCESS) {
195 return failure($dev->error_or_status, $finished_cb);
199 if ($dev->volume_label ne $opt_label) {
200 my $got = $dev->volume_label;
201 return failure("Found unexpected label '$got'", $finished_cb);
205 my $lbl = $dev->volume_label;
206 print STDERR "Restoring from tape $lbl starting with file $filenum.\n";
208 $steps->{'start_device'}->();
211 step start_device => sub {
212 if (!$dev->start($ACCESS_READ, undef, undef)) {
213 return failure($dev->error_or_status(), $finished_cb);
216 $steps->{'read_header'}->();
219 step read_header => sub {
221 print STDERR "Reading from '$opt_restore_src'\n";
222 $hdr = Amanda::Holding::get_header($opt_restore_src);
224 $hdr = $dev->seek_file($filenum);
226 return failure("while reading next header: " . $dev->error_or_status(),
228 } elsif ($hdr->{'type'} == $Amanda::Header::F_TAPEEND) {
229 return $steps->{'finished'}->();
232 # seek_file may have skipped ahead; plan accordingly
233 $filenum = $dev->file + 1;
236 $steps->{'filter_dumpspecs'}->();
239 step filter_dumpspecs => sub {
240 if (@opt_dumpspecs and not $hdr->matches_dumpspecs([@opt_dumpspecs])) {
242 my $dev_filenum = $dev->file;
243 print STDERR "amrestore: $dev_filenum: skipping ",
244 $hdr->summary(), "\n";
247 # skip to the next file without restoring this one
248 return $steps->{'next_file'}->();
252 my $dev_filenum = $dev->file;
253 print STDERR "amrestore: $dev_filenum: restoring ";
255 print STDERR $hdr->summary(), "\n";
257 $steps->{'xfer_dumpfile'}->();
260 step xfer_dumpfile => sub {
263 # set up the source..
265 $src = Amanda::Xfer::Source::Holding->new($opt_restore_src);
267 $src = Amanda::Xfer::Source::Device->new($dev);
270 # and set up the destination..
275 my $filename = sprintf("%s.%s.%s.%d",
277 Amanda::Util::sanitise_filename("".$hdr->{'disk'}), # workaround SWIG bug
279 $hdr->{'dumplevel'});
280 if ($hdr->{'partnum'} > 0) {
281 $filename .= sprintf(".%07d", $hdr->{'partnum'});
284 # add an appropriate suffix
287 } elsif ($opt_compress) {
288 $filename .= ($hdr->{'compressed'} && $hdr->{'comp_suffix'})?
289 $hdr->{'comp_suffix'} : $Amanda::Constants::COMPRESS_SUFFIX;
292 if (!open($dest_fh, ">", $filename)) {
293 return failure("Could not open '$filename' for writing: $!", $finished_cb);
296 $dest = Amanda::Xfer::Dest::Fd->new($dest_fh);
298 # set up any filters that need to be applied, decryption first
300 if ($hdr->{'encrypted'} and not $opt_raw) {
301 if ($hdr->{'srv_encrypt'}) {
303 Amanda::Xfer::Filter::Process->new(
304 [ $hdr->{'srv_encrypt'}, $hdr->{'srv_decrypt_opt'} ], 0);
305 } elsif ($hdr->{'clnt_encrypt'}) {
307 Amanda::Xfer::Filter::Process->new(
308 [ $hdr->{'clnt_encrypt'}, $hdr->{'clnt_decrypt_opt'} ], 0);
310 return failure("could not decrypt encrypted dump: no program specified",
314 $hdr->{'encrypted'} = 0;
315 $hdr->{'srv_encrypt'} = '';
316 $hdr->{'srv_decrypt_opt'} = '';
317 $hdr->{'clnt_encrypt'} = '';
318 $hdr->{'clnt_decrypt_opt'} = '';
319 $hdr->{'encrypt_suffix'} = 'N';
321 if (!$opt_raw and $hdr->{'compressed'} and not $opt_compress) {
322 # need to uncompress this file
324 if ($hdr->{'srvcompprog'}) {
325 # TODO: this assumes that srvcompprog takes "-d" to decompress
327 Amanda::Xfer::Filter::Process->new(
328 [ $hdr->{'srvcompprog'}, "-d" ], 0);
329 } elsif ($hdr->{'clntcompprog'}) {
330 # TODO: this assumes that clntcompprog takes "-d" to decompress
332 Amanda::Xfer::Filter::Process->new(
333 [ $hdr->{'clntcompprog'}, "-d" ], 0);
336 Amanda::Xfer::Filter::Process->new(
337 [ $Amanda::Constants::UNCOMPRESS_PATH,
338 $Amanda::Constants::UNCOMPRESS_OPT ], 0);
342 $hdr->{'compressed'} = 0;
343 $hdr->{'uncompress_cmd'} = '';
344 } elsif (!$opt_raw and !$hdr->{'compressed'} and $opt_compress) {
345 # need to compress this file
347 my $compress_opt = $opt_compress_best?
348 $Amanda::Constants::COMPRESS_BEST_OPT :
349 $Amanda::Constants::COMPRESS_FAST_OPT;
351 Amanda::Xfer::Filter::Process->new(
352 [ $Amanda::Constants::COMPRESS_PATH,
357 $hdr->{'compressed'} = 1;
358 $hdr->{'uncompress_cmd'} = " $Amanda::Constants::UNCOMPRESS_PATH " .
359 "$Amanda::Constants::UNCOMPRESS_OPT |";
360 $hdr->{'comp_suffix'} = $Amanda::Constants::COMPRESS_SUFFIX;
363 # write the header to the destination if requested
365 $hdr->{'blocksize'} = Amanda::Holding::DISK_BLOCK_BYTES;
366 $dest_fh->syswrite($hdr->to_string(32768, 32768));
369 # start reading all filter stderr
370 foreach my $filter (@filters) {
371 my $fd = $filter->get_stderr_fd();
374 my $src = Amanda::MainLoop::fd_source($fd,
375 $G_IO_IN|$G_IO_HUP|$G_IO_ERR);
377 $all_filter{$src} = 1;
378 $src->set_callback( sub {
380 my $n_read = POSIX::read($fd, $b, 1);
381 if (!defined $n_read) {
383 } elsif ($n_read == 0) {
384 delete $all_filter{$src};
387 if (!%all_filter and $restore_done) {
394 print STDERR "filter stderr: $line";
396 debug("filter stderr: $line");
403 my $xfer = Amanda::Xfer->new([ $src, @filters, $dest ]);
405 $xfer->get_source()->set_callback(sub {
406 my ($src, $msg, $xfer) = @_;
408 if ($msg->{'type'} == $XMSG_INFO) {
409 Amanda::Debug::info($msg->{'message'});
410 } elsif ($msg->{'type'} == $XMSG_ERROR) {
411 $got_err = $msg->{'message'};
412 } elsif ($msg->{'type'} == $XMSG_DONE) {
414 $steps->{'xfer_done'}->($got_err);
420 step xfer_done => sub {
422 return failure($err, $finished_cb) if $err;
424 $steps->{'next_file'}->('extracted');
427 step next_file => sub {
428 my ($extracted) = @_;
429 # amrestore does not loop over multiple files when reading from holding
430 # when outputting to a pipe amrestore extracts only the first file
431 if ($opt_holding or ($opt_pipe and $extracted)) {
432 return $steps->{'finished'}->();
435 # otherwise, try to read the next header from the device
436 $steps->{'read_header'}->();
439 step finished => sub {
441 $res->release(finished_cb => $steps->{'quit'});
443 $steps->{'quit'}->();
451 return failure($err, $finished_cb) if $err;
458 main(\&Amanda::MainLoop::quit);
459 Amanda::MainLoop::run();
460 Amanda::Util::finish_application();