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@';
25 package Amanda::Application::Amsuntar;
26 use base qw(Amanda::Application);
28 use File::Temp qw( tempfile );
34 use Amanda::Constants;
35 use Amanda::Config qw( :init :getconf config_dir_relative );
36 use Amanda::Debug qw( :logging );
38 use Amanda::Util qw( :constants );
42 my ($config, $host, $disk, $device, $level, $index, $message, $collection, $record, $exclude_list, $exclude_optional, $include_list, $include_optional, $bsize, $ext_attrib, $ext_header, $ignore, $normal, $strange, $error_exp, $directory, $suntar_path) = @_;
43 my $self = $class->SUPER::new($config);
45 $self->{suntar} = $Amanda::Constants::SUNTAR;
46 if (defined $suntar_path) {
47 $self->{suntar} = $suntar_path;
49 $self->{pfexec} = "/usr/bin/pfexec";
50 $self->{gnutar} = $Amanda::Constants::GNUTAR;
51 $self->{teecount} = $Amanda::Paths::amlibexecdir."/teecount";
53 $self->{config} = $config;
54 $self->{host} = $host;
56 $self->{disk} = $disk;
58 $self->{disk} = $device;
60 if (defined $device) {
61 $self->{device} = $device;
63 $self->{device} = $disk;
65 $self->{level} = $level;
66 $self->{index} = $index;
67 $self->{message} = $message;
68 $self->{collection} = $collection;
69 $self->{record} = $record;
70 $self->{exclude_list} = [ @{$exclude_list} ];
71 $self->{exclude_optional} = $exclude_optional;
72 $self->{include_list} = [ @{$include_list} ];
73 $self->{include_optional} = $include_optional;
74 $self->{block_size} = $bsize;
75 $self->{extended_header} = $ext_header;
76 $self->{extended_attrib} = $ext_attrib;
77 $self->{directory} = $directory;
81 for $regex (@{$ignore}) {
82 my $a = { regex => $regex, type => "IGNORE" };
83 push @{$self->{regex}}, $a;
86 for $regex (@{$normal}) {
87 my $a = { regex => $regex, type => "NORMAL" };
88 push @{$self->{regex}}, $a;
91 for $regex (@{$strange}) {
92 my $a = { regex => $regex, type => "STRANGE" };
93 push @{$self->{regex}}, $a;
96 for $regex (@{$error_exp}) {
97 my $a = { regex => $regex, type => "ERROR" };
98 push @{$self->{regex}}, $a;
101 #type can be IGNORE/NORMAL/STRANGE/ERROR
102 push @{$self->{regex}}, { regex => "is not a file. Not dumped\$",
104 push @{$self->{regex}}, { regex => "same as archive file\$",
106 push @{$self->{regex}}, { regex => ": invalid character in UTF-8 conversion of ",
108 push @{$self->{regex}}, { regex => ": UTF-8 conversion failed.\$",
110 push @{$self->{regex}}, { regex => ": Permission denied\$",
113 for $regex (@{$self->{regex}}) {
114 debug ($regex->{type} . ": " . $regex->{regex});
120 sub command_support {
123 print "CONFIG YES\n";
126 print "MAX-LEVEL 0\n";
127 print "INDEX-LINE YES\n";
128 print "INDEX-XML NO\n";
129 print "MESSAGE-LINE YES\n";
130 print "MESSAGE-XML NO\n";
131 print "RECORD YES\n";
132 print "EXCLUDE-FILE NO\n";
133 print "EXCLUDE-LIST YES\n";
134 print "EXCLUDE-OPTIONAL YES\n";
135 print "INCLUDE-FILE NO\n";
136 print "INCLUDE-LIST YES\n";
137 print "INCLUDE-OPTIONAL YES\n";
138 print "COLLECTION NO\n";
139 print "MULTI-ESTIMATE NO\n";
140 print "CALCSIZE NO\n";
141 print "CLIENT-ESTIMATE YES\n";
144 sub command_selfcheck {
147 if (!-e $self->{suntar}) {
148 $self->print_to_server_and_die(
149 "application binary $self->{suntar} doesn't exist",
150 $Amanda::Script_App::ERROR);
152 if (!-x $self->{suntar}) {
153 $self->print_to_server_and_die(
154 "application binary $self->{suntar} is not a executable",
155 $Amanda::Script_App::ERROR);
157 if (!defined $self->{disk} || !defined $self->{device}) {
160 print "OK " . $self->{disk} . "\n";
161 print "OK " . $self->{device} . "\n";
162 print "OK " . $self->{directory} . "\n" if defined $self->{directory};
163 $self->validate_inexclude();
166 sub command_estimate() {
169 my $level = $self->{level};
171 $self->{index} = undef; #remove verbose flag to suntar.
172 my(@cmd) = $self->build_command();
173 my(@cmdwc) = ("/usr/bin/wc", "-c");
175 debug("cmd:" . join(" ", @cmd) . " | " . join(" ", @cmdwc));
176 my($wtr, $rdr, $err, $pid, $rdrwc, $pidwc);
177 $err = Symbol::gensym;
178 $pid = open3($wtr, \*DATA, $err, @cmd);
179 $pidwc = open2($rdrwc, '>&DATA', @cmdwc);
182 my ($msgsize) = <$rdrwc>;
187 for my $regex (@{$self->{regex}}) {
188 my $regex1 = $regex->{regex};
190 $result = 1 if ($regex->{type} eq "ERROR");
195 $result = 1 if ($matched == 0);
196 $errmsg = $_ if (!defined $errmsg);
202 if (defined $errmsg) {
203 $self->print_to_server_and_die($errmsg, $Amanda::Script_App::ERROR);
205 $self->print_to_server_and_die(
206 "cannot estimate archive size': unknown reason",
207 $Amanda::Script_App::ERROR);
210 output_size($level, $msgsize);
219 print "$level -1 -1\n";
223 my($ksize) = int $size / (1024);
224 $ksize=32 if ($ksize<32);
225 print "$level $ksize 1\n";
232 $self->validate_inexclude();
234 my(@cmd) = $self->build_command();
235 my(@cmdtc) = $self->{teecount};
237 debug("cmd:" . join(" ", @cmd) . " | " . join(" ", @cmdtc));
239 my($wtr, $pid, $rdrtc, $errtc, $pidtc);
240 my $index_fd = Symbol::gensym;
241 $errtc = Symbol::gensym;
243 $pid = open3($wtr, \*DATA, $index_fd, @cmd) ||
244 $self->print_to_server_and_die("Can't run $cmd[0]: $!",
245 $Amanda::Script_App::ERROR);
246 $pidtc = open3('<&DATA', '>&STDOUT', $errtc, @cmdtc) ||
247 $self->print_to_server_and_die("Can't run $cmdtc[0]: $!",
248 $Amanda::Script_App::ERROR);
251 unlink($self->{include_tmp}) if(-e $self->{include_tmp});
252 unlink($self->{exclude_tmp}) if(-e $self->{exclude_tmp});
255 if(defined($self->{index})) {
257 open($indexout_fd, '>&=4') ||
258 $self->print_to_server_and_die("Can't open indexout_fd: $!",
259 $Amanda::Script_App::ERROR);
260 $result = $self->parse_backup($index_fd, $self->{mesgout}, $indexout_fd);
264 $result = $self->parse_backup($index_fd, $self->{mesgout}, undef);
273 debug("exit status $status ?" );
277 debug("$self->{suntar} returned error" );
278 $self->print_to_server("$self->{suntar} returned error",
279 $Amanda::Script_App::ERROR);
282 my($ksize) = int ($size/1024);
283 print {$self->{mesgout}} "sendbackup: size $ksize\n";
284 print {$self->{mesgout}} "sendbackup: end\n";
285 debug("sendbackup: size $ksize ");
292 my($fhin, $fhout, $indexout) = @_;
296 if ( /^ ?a\s+(\.\/.*) \d*K/ ||
297 /^a\s+(\.\/.*) symbolic link to/ ||
298 /^a\s+(\.\/.*) link to/ ) {
300 if(defined($indexout)) {
301 if(defined($self->{index})) {
303 print $indexout $name, "\n";
309 for my $regex (@{$self->{regex}}) {
310 my $regex1 = $regex->{regex};
312 $result = 1 if ($regex->{type} eq "ERROR");
313 if (defined($fhout)) {
314 if ($regex->{type} eq "IGNORE") {
315 } elsif ($regex->{type} eq "NORMAL") {
317 } elsif ($regex->{type} eq "STRANGE") {
329 if (defined($fhout)) {
338 sub validate_inexclude {
343 if ($#{$self->{exclude_list}} >= 0 && $#{$self->{include_list}} >= 0 ) {
344 $self->print_to_server_and_die("Can't have both include and exclude",
345 $Amanda::Script_App::ERROR);
348 foreach my $file (@{$self->{exclude_list}}){
349 if (!open($fh, $file)) {
350 if ($self->{action} eq "check" && !$self->{exclude_optional}) {
351 $self->print_to_server("Open of '$file' failed: $!",
352 $Amanda::Script_App::ERROR);
362 #Merging list into a single file
363 if($self->{action} eq 'backup' && $#{$self->{exculde_list}} >= 0) {
364 ($fh, $self->{exclude_tmp}) = tempfile(DIR => $Amanda::paths::AMANDA_TMPDIR);
366 $self->print_to_server_and_die(
367 "Open of tmp file '$self->{exclude_tmp}' failed: $!",
368 $Amanda::Script_App::ERROR);
375 foreach my $file (@{$self->{include_list}}) {
376 if (!open($fh, $file)) {
377 if ($self->{action} eq "check" && !$self->{include_optional}) {
378 $self->print_to_server("Open of '$file' failed: $!",
379 $Amanda::Script_App::ERROR);
389 if($self->{action} eq 'backup' && $#{$self->{include_list}} >= 0) {
390 ($fh, $self->{include_tmp}) = tempfile(DIR => $Amanda::paths::AMANDA_TMPDIR);
392 $self->print_to_server_and_die(
393 "Open of tmp file '$self->{include_tmp}' failed: $!",
394 $Amanda::Script_App::ERROR);
402 sub command_index_from_output {
403 index_from_output(0, 1);
407 sub index_from_output {
408 my($fhin, $fhout) = @_;
411 next if /^Total bytes written:/;
418 sub command_index_from_image {
421 open($index_fd, "$self->{suntar} -tf - |") ||
422 $self->print_to_server_and_die("Can't run $self->{suntar}: $!",
423 $Amanda::Script_App::ERROR);
424 index_from_output($index_fd, 1);
427 sub command_restore {
430 chdir(Amanda::Util::get_original_cwd());
431 if (defined $self->{directory}) {
432 if (!-d $self->{directory}) {
433 $self->print_to_server_and_die("Directory $self->{directory}: $!",
434 $Amanda::Script_App::ERROR);
436 if (!-w $self->{directory}) {
437 $self->print_to_server_and_die("Directory $self->{directory}: $!",
438 $Amanda::Script_App::ERROR);
440 chdir($self->{directory});
445 if($self->{extended_header} eq "YES") {
448 if($self->{extended_attrib} eq "YES") {
454 if (defined($self->{exclude_list}) && (-e $self->{exclude_list}[0])) {
458 my(@cmd) = ($self->{pfexec},$self->{suntar}, $cmd);
460 push @cmd, "-"; # for f argument
461 if (defined($self->{exclude_list}) && (-e $self->{exclude_list}[0])) {
462 push @cmd, $self->{exclude_list}[0]; # for X argument
465 if(defined($self->{include_list}) && (-e $self->{include_list}[0])) {
466 push @cmd, "-I", $self->{include_list}[0];
469 for(my $i=1;defined $ARGV[$i]; $i++) {
470 my $param = $ARGV[$i];
474 debug("cmd:" . join(" ", @cmd));
475 exec { $cmd[0] } @cmd;
476 die("Can't exec '", $cmd[0], "'");
479 sub command_validate {
484 if (-e $self->{suntar}) {
485 $program = $self->{suntar};
486 } elsif (-e $self->{gnutar}) {
487 $program = $self->{gnutar};
489 return $self->default_validate();
491 @cmd = ($program, "-tf", "-");
492 debug("cmd:" . join(" ", @cmd));
493 my $pid = open3('>&STDIN', '>&STDOUT', '>&STDERR', @cmd) ||
494 $self->print_to_server_and_die("Unable to run @cmd",
495 $Amanda::Script_App::ERROR);
498 $self->print_to_server_and_die("$program returned error",
499 $Amanda::Script_App::ERROR);
507 #Careful sun tar options and ordering is very very tricky
512 $self->validate_inexclude();
514 if($self->{extended_header} =~ /^YES$/i) {
517 if($self->{extended_attrib} =~ /^YES$/i) {
520 if(defined($self->{index})) {
524 if(defined($self->{block_size})) {
526 push @optparams, $self->{block_size};
529 if (defined($self->{exclude_tmp})) {
531 push @optparams,"-",$self->{exclude_tmp};
536 if ($self->{directory}) {
537 push @optparams, "-C", $self->{directory};
539 push @optparams, "-C", $self->{device};
542 if(defined($self->{include_tmp})) {
543 push @optparams,"-I", $self->{include_tmp};
548 my(@cmd) = ($self->{pfexec}, $self->{suntar}, $cmd, @optparams);
556 Usage: Amsuntar <command> --config=<config> --host=<host> --disk=<disk> --device=<device> --level=<level> --index=<yes|no> --message=<text> --collection=<no> --record=<yes|no> --exclude-list=<fileList> --include-list=<fileList> --block-size=<size> --extended_attributes=<yes|no> --extended_headers<yes|no> --ignore=<regex> --normal=<regex> --strange=<regex> --error=<regex> --lang=<lang>.
570 my @opt_exclude_list;
571 my $opt_exclude_optional;
572 my @opt_include_list;
573 my $opt_include_optional;
575 my $opt_ext_attrib = "YES";
576 my $opt_ext_head = "YES";
585 Getopt::Long::Configure(qw{bundling});
587 'config=s' => \$opt_config,
588 'host=s' => \$opt_host,
589 'disk=s' => \$opt_disk,
590 'device=s' => \$opt_device,
591 'level=s' => \$opt_level,
592 'index=s' => \$opt_index,
593 'message=s' => \$opt_message,
594 'collection=s' => \$opt_collection,
595 'exclude-list=s' => \@opt_exclude_list,
596 'exclude-optional=s' => \$opt_exclude_optional,
597 'include-list=s' => \@opt_include_list,
598 'include-optional=s' => \$opt_include_optional,
599 'record' => \$opt_record,
600 'block-size=s' => \$opt_bsize,
601 'extended-attributes=s' => \$opt_ext_attrib,
602 'extended-headers=s' => \$opt_ext_head,
603 'ignore=s' => \@opt_ignore,
604 'normal=s' => \@opt_normal,
605 'strange=s' => \@opt_strange,
606 'error=s' => \@opt_error,
607 'lang=s' => \$opt_lang,
608 'directory=s' => \$opt_directory,
609 'suntar-path=s' => \$opt_suntar_path,
612 if (defined $opt_lang) {
613 $ENV{LANG} = $opt_lang;
616 my $application = Amanda::Application::Amsuntar->new($opt_config, $opt_host, $opt_disk, $opt_device, $opt_level, $opt_index, $opt_message, $opt_collection, $opt_record, \@opt_exclude_list, $opt_exclude_optional, \@opt_include_list, $opt_include_optional,$opt_bsize,$opt_ext_attrib,$opt_ext_head, \@opt_ignore, \@opt_normal, \@opt_strange, \@opt_error, $opt_directory, $opt_suntar_path);
618 $application->do($ARGV[0]);