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);
61 Getopt::Long::Configure(qw(bundling));
63 'version' => \&Amanda::Util::version_opt,
64 'help|usage|?' => \&usage,
65 'config=s' => \$opt_config,
66 'holding' => \$opt_holding,
67 'b=i' => \$opt_blocksize,
69 'c' => \$opt_compress,
70 'C' => \$opt_compress_best,
73 'f=i' => \$opt_filenum,
75 'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
78 $opt_compress = 1 if $opt_compress_best;
80 # see if we have a holding file or a device
81 usage("Must specify a device or holding-disk file") unless (@ARGV);
82 $opt_restore_src = shift @ARGV;
85 if (Amanda::Holding::get_header($opt_restore_src));
88 my @opt_dumpspecs = Amanda::Cmdline::parse_dumpspecs([@ARGV],
89 $Amanda::Cmdline::CMDLINE_PARSE_DATESTAMP);
91 usage("Cannot check a label on a holding-disk file")
92 if ($opt_holding and $opt_label);
93 usage("Cannot use both -r (raw) and -c/-C (compression) -- use -h instead")
94 if ($opt_raw and $opt_compress);
96 # -r implies -h, plus appending ".RAW" to filenames
97 $opt_header = 1 if $opt_raw;
99 set_config_overrides($config_overrides);
101 config_init($CONFIG_INIT_EXPLICIT_NAME, $opt_config);
103 config_init(0, undef);
105 my ($cfgerr_level, @cfgerr_errors) = config_errors();
106 if ($cfgerr_level >= $CFGERR_WARNINGS) {
107 config_print_errors();
108 if ($cfgerr_level >= $CFGERR_ERRORS) {
109 die("errors processing config file");
113 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
119 my ($msg, $finished_cb) = @_;
120 print STDERR "ERROR: $msg\n";
123 $res->release(finished_cb => sub {
133 my ($finished_cb) = @_;
138 my $filenum = $opt_filenum;
139 $filenum = 1 if (!$filenum);
140 $filenum = 0 + "$filenum"; # convert to integer
144 my $steps = define_steps
145 cb_ref => \$finished_cb,
146 finalize => sub { $chg->quit() if defined $chg };
149 # first, return to the original working directory we were started in
150 if (!chdir Amanda::Util::get_original_cwd()) {
151 return failure("Cannot chdir to original working directory", $finished_cb);
155 $steps->{'read_header'}->();
157 my $tlf = Amanda::Config::config_dir_relative(getconf($CNF_TAPELIST));
158 my $tl = Amanda::Tapelist->new($tlf);
159 $chg = Amanda::Changer->new($opt_restore_src, tapelist => $tl);
160 if ($chg->isa("Amanda::Changer::Error")) {
161 return failure($chg, $finished_cb);
164 $chg->load(relative_slot => "current", mode => "read",
165 res_cb => $steps->{'slot_loaded'});
169 step slot_loaded => sub {
170 (my $err, $res) = @_;
171 return failure($err, $finished_cb) if $err;
173 $dev = $res->{'device'};
174 if ($dev->status != $DEVICE_STATUS_SUCCESS) {
175 return failure($dev->error_or_status, $finished_cb);
178 $steps->{'check_label'}->();
181 step check_label => sub {
182 if ($dev->status != $DEVICE_STATUS_SUCCESS) {
183 return failure($dev->error_or_status, $finished_cb);
187 if ($dev->volume_label ne $opt_label) {
188 my $got = $dev->volume_label;
189 return failure("Found unexpected label '$got'", $finished_cb);
193 my $lbl = $dev->volume_label;
194 print STDERR "Restoring from tape $lbl starting with file $filenum.\n";
196 $steps->{'start_device'}->();
199 step start_device => sub {
200 if (!$dev->start($ACCESS_READ, undef, undef)) {
201 return failure($dev->error_or_status(), $finished_cb);
204 $steps->{'read_header'}->();
207 step read_header => sub {
209 print STDERR "Reading from '$opt_restore_src'\n";
210 $hdr = Amanda::Holding::get_header($opt_restore_src);
212 $hdr = $dev->seek_file($filenum);
214 return failure("while reading next header: " . $dev->error_or_status(),
216 } elsif ($hdr->{'type'} == $Amanda::Header::F_TAPEEND) {
217 return $steps->{'finished'}->();
220 # seek_file may have skipped ahead; plan accordingly
221 $filenum = $dev->file + 1;
224 $steps->{'filter_dumpspecs'}->();
227 step filter_dumpspecs => sub {
228 if (@opt_dumpspecs and not $hdr->matches_dumpspecs([@opt_dumpspecs])) {
230 my $dev_filenum = $dev->file;
231 print STDERR "amrestore: $dev_filenum: skipping ",
232 $hdr->summary(), "\n";
235 # skip to the next file without restoring this one
236 return $steps->{'next_file'}->();
240 my $dev_filenum = $dev->file;
241 print STDERR "amrestore: $dev_filenum: restoring ";
243 print STDERR $hdr->summary(), "\n";
245 $steps->{'xfer_dumpfile'}->();
248 step xfer_dumpfile => sub {
251 # set up the source..
253 $src = Amanda::Xfer::Source::Holding->new($opt_restore_src);
255 $src = Amanda::Xfer::Source::Device->new($dev);
258 # and set up the destination..
263 my $filename = sprintf("%s.%s.%s.%d",
265 Amanda::Util::sanitise_filename("".$hdr->{'disk'}), # workaround SWIG bug
267 $hdr->{'dumplevel'});
268 if ($hdr->{'partnum'} > 0) {
269 $filename .= sprintf(".%07d", $hdr->{'partnum'});
272 # add an appropriate suffix
275 } elsif ($opt_compress) {
276 $filename .= ($hdr->{'compressed'} && $hdr->{'comp_suffix'})?
277 $hdr->{'comp_suffix'} : $Amanda::Constants::COMPRESS_SUFFIX;
280 if (!open($dest_fh, ">", $filename)) {
281 return failure("Could not open '$filename' for writing: $!", $finished_cb);
284 $dest = Amanda::Xfer::Dest::Fd->new($dest_fh);
286 # set up any filters that need to be applied, decryption first
288 if ($hdr->{'encrypted'} and not $opt_raw) {
289 if ($hdr->{'srv_encrypt'}) {
291 Amanda::Xfer::Filter::Process->new(
292 [ $hdr->{'srv_encrypt'}, $hdr->{'srv_decrypt_opt'} ], 0);
293 } elsif ($hdr->{'clnt_encrypt'}) {
295 Amanda::Xfer::Filter::Process->new(
296 [ $hdr->{'clnt_encrypt'}, $hdr->{'clnt_decrypt_opt'} ], 0);
298 return failure("could not decrypt encrypted dump: no program specified",
302 $hdr->{'encrypted'} = 0;
303 $hdr->{'srv_encrypt'} = '';
304 $hdr->{'srv_decrypt_opt'} = '';
305 $hdr->{'clnt_encrypt'} = '';
306 $hdr->{'clnt_decrypt_opt'} = '';
307 $hdr->{'encrypt_suffix'} = 'N';
309 if (!$opt_raw and $hdr->{'compressed'} and not $opt_compress) {
310 # need to uncompress this file
312 if ($hdr->{'srvcompprog'}) {
313 # TODO: this assumes that srvcompprog takes "-d" to decompress
315 Amanda::Xfer::Filter::Process->new(
316 [ $hdr->{'srvcompprog'}, "-d" ], 0);
317 } elsif ($hdr->{'clntcompprog'}) {
318 # TODO: this assumes that clntcompprog takes "-d" to decompress
320 Amanda::Xfer::Filter::Process->new(
321 [ $hdr->{'clntcompprog'}, "-d" ], 0);
324 Amanda::Xfer::Filter::Process->new(
325 [ $Amanda::Constants::UNCOMPRESS_PATH,
326 $Amanda::Constants::UNCOMPRESS_OPT ], 0);
330 $hdr->{'compressed'} = 0;
331 $hdr->{'uncompress_cmd'} = '';
332 } elsif (!$opt_raw and !$hdr->{'compressed'} and $opt_compress) {
333 # need to compress this file
335 my $compress_opt = $opt_compress_best?
336 $Amanda::Constants::COMPRESS_BEST_OPT :
337 $Amanda::Constants::COMPRESS_FAST_OPT;
339 Amanda::Xfer::Filter::Process->new(
340 [ $Amanda::Constants::COMPRESS_PATH,
345 $hdr->{'compressed'} = 1;
346 $hdr->{'uncompress_cmd'} = " $Amanda::Constants::UNCOMPRESS_PATH " .
347 "$Amanda::Constants::UNCOMPRESS_OPT |";
348 $hdr->{'comp_suffix'} = $Amanda::Constants::COMPRESS_SUFFIX;
351 # write the header to the destination if requested
353 $hdr->{'blocksize'} = Amanda::Holding::DISK_BLOCK_BYTES;
354 $dest_fh->syswrite($hdr->to_string(32768, 32768));
357 # start reading all filter stderr
358 foreach my $filter (@filters) {
359 my $fd = $filter->get_stderr_fd();
362 my $src = Amanda::MainLoop::fd_source($fd,
363 $G_IO_IN|$G_IO_HUP|$G_IO_ERR);
365 $all_filter{$src} = 1;
366 $src->set_callback( sub {
368 my $n_read = POSIX::read($fd, $b, 1);
369 if (!defined $n_read) {
371 } elsif ($n_read == 0) {
372 delete $all_filter{$src};
375 if (!%all_filter and $restore_done) {
382 print STDERR "filter stderr: $line";
384 debug("filter stderr: $line");
391 my $xfer = Amanda::Xfer->new([ $src, @filters, $dest ]);
393 $xfer->get_source()->set_callback(sub {
394 my ($src, $msg, $xfer) = @_;
396 if ($msg->{'type'} == $XMSG_INFO) {
397 Amanda::Debug::info($msg->{'message'});
398 } elsif ($msg->{'type'} == $XMSG_ERROR) {
399 $got_err = $msg->{'message'};
400 } elsif ($msg->{'type'} == $XMSG_DONE) {
402 $steps->{'xfer_done'}->($got_err);
408 step xfer_done => sub {
410 return failure($err, $finished_cb) if $err;
412 $steps->{'next_file'}->('extracted');
415 step next_file => sub {
416 my ($extracted) = @_;
417 # amrestore does not loop over multiple files when reading from holding
418 # when outputting to a pipe amrestore extracts only the first file
419 if ($opt_holding or ($opt_pipe and $extracted)) {
420 return $steps->{'finished'}->();
423 # otherwise, try to read the next header from the device
424 $steps->{'read_header'}->();
427 step finished => sub {
429 $res->release(finished_cb => $steps->{'quit'});
431 $steps->{'quit'}->();
439 return failure($err, $finished_cb) if $err;
446 main(\&Amanda::MainLoop::quit);
447 Amanda::MainLoop::run();
448 Amanda::Util::finish_application();