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) = @_;
43 my $self = $class->SUPER::new($config);
45 $self->{suntar} = "/usr/sbin/tar";
46 $self->{pfexec} = "/usr/bin/pfexec";
47 $self->{gnutar} = $Amanda::Constants::GNUTAR;
48 $self->{teecount} = $Amanda::Paths::amlibexecdir."/teecount";
50 $self->{config} = $config;
51 $self->{host} = $host;
53 $self->{disk} = $disk;
55 $self->{disk} = $device;
57 if (defined $device) {
58 $self->{device} = $device;
60 $self->{device} = $disk;
62 $self->{level} = $level;
63 $self->{index} = $index;
64 $self->{message} = $message;
65 $self->{collection} = $collection;
66 $self->{record} = $record;
67 $self->{exclude_list} = [ @{$exclude_list} ];
68 $self->{exclude_optional} = $exclude_optional;
69 $self->{include_list} = [ @{$include_list} ];
70 $self->{include_optional} = $include_optional;
71 $self->{block_size} = $bsize;
72 $self->{extended_header} = $ext_header;
73 $self->{extended_attrib} = $ext_attrib;
74 $self->{directory} = $directory;
78 for $regex (@{$ignore}) {
79 my $a = { regex => $regex, type => "IGNORE" };
80 push @{$self->{regex}}, $a;
83 for $regex (@{$normal}) {
84 my $a = { regex => $regex, type => "NORMAL" };
85 push @{$self->{regex}}, $a;
88 for $regex (@{$strange}) {
89 my $a = { regex => $regex, type => "STRANGE" };
90 push @{$self->{regex}}, $a;
93 for $regex (@{$error_exp}) {
94 my $a = { regex => $regex, type => "ERROR" };
95 push @{$self->{regex}}, $a;
98 #type can be IGNORE/NORMAL/STRANGE/ERROR
99 push @{$self->{regex}}, { regex => "is not a file. Not dumped\$",
101 push @{$self->{regex}}, { regex => "same as archive file\$",
103 push @{$self->{regex}}, { regex => ": invalid character in UTF-8 conversion of ",
105 push @{$self->{regex}}, { regex => ": UTF-8 conversion failed.\$",
107 push @{$self->{regex}}, { regex => ": Permission denied\$",
110 for $regex (@{$self->{regex}}) {
111 debug ($regex->{type} . ": " . $regex->{regex});
117 sub command_support {
120 print "CONFIG YES\n";
123 print "MAX-LEVEL 0\n";
124 print "INDEX-LINE YES\n";
125 print "INDEX-XML NO\n";
126 print "MESSAGE-LINE YES\n";
127 print "MESSAGE-XML NO\n";
128 print "RECORD YES\n";
129 print "EXCLUDE-FILE NO\n";
130 print "EXCLUDE-LIST YES\n";
131 print "EXCLUDE-OPTIONAL YES\n";
132 print "INCLUDE-FILE NO\n";
133 print "INCLUDE-LIST YES\n";
134 print "INCLUDE-OPTIONAL YES\n";
135 print "COLLECTION NO\n";
136 print "MULTI-ESTIMATE NO\n";
137 print "CALCSIZE NO\n";
138 print "CLIENT-ESTIMATE YES\n";
141 sub command_selfcheck {
144 if (!-e $self->{suntar}) {
145 $self->print_to_server_and_die(
146 "application binary $self->{suntar} doesn't exist",
147 $Amanda::Script_App::ERROR);
149 if (!-x $self->{suntar}) {
150 $self->print_to_server_and_die(
151 "application binary $self->{suntar} is not a executable",
152 $Amanda::Script_App::ERROR);
154 if (!defined $self->{disk} || !defined $self->{device}) {
157 print "OK " . $self->{disk} . "\n";
158 print "OK " . $self->{device} . "\n";
159 print "OK " . $self->{directory} . "\n" if defined $self->{directory};
160 $self->validate_inexclude();
163 sub command_estimate() {
166 my $level = $self->{level};
168 $self->{index} = undef; #remove verbose flag to suntar.
169 my(@cmd) = $self->build_command();
170 my(@cmdwc) = ("/usr/bin/wc", "-c");
172 debug("cmd:" . join(" ", @cmd) . " | " . join(" ", @cmdwc));
173 my($wtr, $rdr, $err, $pid, $rdrwc, $pidwc);
174 $err = Symbol::gensym;
175 $pid = open3($wtr, \*DATA, $err, @cmd);
176 $pidwc = open2($rdrwc, '>&DATA', @cmdwc);
179 my ($msgsize) = <$rdrwc>;
184 for my $regex (@{$self->{regex}}) {
185 my $regex1 = $regex->{regex};
187 $result = 1 if ($regex->{type} eq "ERROR");
192 $result = 1 if ($matched == 0);
193 $errmsg = $_ if (!defined $errmsg);
199 if (defined $errmsg) {
200 $self->print_to_server_and_die($errmsg, $Amanda::Script_App::ERROR);
202 $self->print_to_server_and_die(
203 "cannot estimate archive size': unknown reason",
204 $Amanda::Script_App::ERROR);
207 output_size($level, $msgsize);
216 print "$level -1 -1\n";
220 my($ksize) = int $size / (1024);
221 $ksize=32 if ($ksize<32);
222 print "$level $ksize 1\n";
229 $self->validate_inexclude();
231 my(@cmd) = $self->build_command();
232 my(@cmdtc) = $self->{teecount};
234 debug("cmd:" . join(" ", @cmd) . " | " . join(" ", @cmdtc));
236 my($wtr, $pid, $rdrtc, $errtc, $pidtc);
237 my $index_fd = Symbol::gensym;
238 $errtc = Symbol::gensym;
240 $pid = open3($wtr, \*DATA, $index_fd, @cmd) ||
241 $self->print_to_server_and_die("Can't run $cmd[0]: $!",
242 $Amanda::Script_App::ERROR);
243 $pidtc = open3('<&DATA', '>&STDOUT', $errtc, @cmdtc) ||
244 $self->print_to_server_and_die("Can't run $cmdtc[0]: $!",
245 $Amanda::Script_App::ERROR);
248 unlink($self->{include_tmp}) if(-e $self->{include_tmp});
249 unlink($self->{exclude_tmp}) if(-e $self->{exclude_tmp});
252 if(defined($self->{index})) {
254 open($indexout_fd, '>&=4') ||
255 $self->print_to_server_and_die("Can't open indexout_fd: $!",
256 $Amanda::Script_App::ERROR);
257 $result = $self->parse_backup($index_fd, $self->{mesgout}, $indexout_fd);
261 $result = $self->parse_backup($index_fd, $self->{mesgout}, undef);
270 debug("exit status $status ?" );
274 debug("$self->{suntar} returned error" );
275 $self->print_to_server("$self->{suntar} returned error",
276 $Amanda::Script_App::ERROR);
279 my($ksize) = int ($size/1024);
280 print {$self->{mesgout}} "sendbackup: size $ksize\n";
281 print {$self->{mesgout}} "sendbackup: end\n";
282 debug("sendbackup: size $ksize ");
289 my($fhin, $fhout, $indexout) = @_;
293 if ( /^ ?a\s+(\.\/.*) \d*K/ ||
294 /^a\s+(\.\/.*) symbolic link to/ ||
295 /^a\s+(\.\/.*) link to/ ) {
297 if(defined($indexout)) {
298 if(defined($self->{index})) {
300 print $indexout $name, "\n";
306 for my $regex (@{$self->{regex}}) {
307 my $regex1 = $regex->{regex};
309 $result = 1 if ($regex->{type} eq "ERROR");
310 if (defined($fhout)) {
311 if ($regex->{type} eq "IGNORE") {
312 } elsif ($regex->{type} eq "NORMAL") {
314 } elsif ($regex->{type} eq "STRANGE") {
326 if (defined($fhout)) {
335 sub validate_inexclude {
340 if ($#{$self->{exclude_list}} >= 0 && $#{$self->{include_list}} >= 0 ) {
341 $self->print_to_server_and_die("Can't have both include and exclude",
342 $Amanda::Script_App::ERROR);
345 foreach my $file (@{$self->{exclude_list}}){
346 if (!open($fh, $file)) {
347 if ($self->{action} eq "check" && !$self->{exclude_optional}) {
348 $self->print_to_server("Open of '$file' failed: $!",
349 $Amanda::Script_App::ERROR);
359 #Merging list into a single file
360 if($self->{action} eq 'backup' && $#{$self->{exculde_list}} >= 0) {
361 ($fh, $self->{exclude_tmp}) = tempfile(DIR => $Amanda::paths::AMANDA_TMPDIR);
363 $self->print_to_server_and_die(
364 "Open of tmp file '$self->{exclude_tmp}' failed: $!",
365 $Amanda::Script_App::ERROR);
372 foreach my $file (@{$self->{include_list}}) {
373 if (!open($fh, $file)) {
374 if ($self->{action} eq "check" && !$self->{include_optional}) {
375 $self->print_to_server("Open of '$file' failed: $!",
376 $Amanda::Script_App::ERROR);
386 if($self->{action} eq 'backup' && $#{$self->{include_list}} >= 0) {
387 ($fh, $self->{include_tmp}) = tempfile(DIR => $Amanda::paths::AMANDA_TMPDIR);
389 $self->print_to_server_and_die(
390 "Open of tmp file '$self->{include_tmp}' failed: $!",
391 $Amanda::Script_App::ERROR);
399 sub command_index_from_output {
400 index_from_output(0, 1);
404 sub index_from_output {
405 my($fhin, $fhout) = @_;
408 next if /^Total bytes written:/;
415 sub command_index_from_image {
418 open($index_fd, "$self->{suntar} -tf - |") ||
419 $self->print_to_server_and_die("Can't run $self->{suntar}: $!",
420 $Amanda::Script_App::ERROR);
421 index_from_output($index_fd, 1);
424 sub command_restore {
427 chdir(Amanda::Util::get_original_cwd());
428 if (defined $self->{directory}) {
429 if (!-d $self->{directory}) {
430 $self->print_to_server_and_die("Directory $self->{directory}: $!",
431 $Amanda::Script_App::ERROR);
433 if (!-w $self->{directory}) {
434 $self->print_to_server_and_die("Directory $self->{directory}: $!",
435 $Amanda::Script_App::ERROR);
437 chdir($self->{directory});
442 if($self->{extended_header} eq "YES") {
445 if($self->{extended_attrib} eq "YES") {
451 if (defined($self->{exclude_list}) && (-e $self->{exclude_list}[0])) {
455 my(@cmd) = ($self->{pfexec},$self->{suntar}, $cmd);
457 push @cmd, "-"; # for f argument
458 if (defined($self->{exclude_list}) && (-e $self->{exclude_list}[0])) {
459 push @cmd, $self->{exclude_list}[0]; # for X argument
462 if(defined($self->{include_list}) && (-e $self->{include_list}[0])) {
463 push @cmd, "-I", $self->{include_list}[0];
466 for(my $i=1;defined $ARGV[$i]; $i++) {
467 my $param = $ARGV[$i];
471 debug("cmd:" . join(" ", @cmd));
472 exec { $cmd[0] } @cmd;
473 die("Can't exec '", $cmd[0], "'");
476 sub command_validate {
481 if (-e $self->{suntar}) {
482 $program = $self->{suntar};
483 } elsif (-e $self->{gnutar}) {
484 $program = $self->{gnutar};
486 return $self->default_validate();
488 @cmd = ($program, "-tf", "-");
489 debug("cmd:" . join(" ", @cmd));
490 my $pid = open3('>&STDIN', '>&STDOUT', '>&STDERR', @cmd) ||
491 $self->print_to_server_and_die("Unable to run @cmd",
492 $Amanda::Script_App::ERROR);
495 $self->print_to_server_and_die("$program returned error",
496 $Amanda::Script_App::ERROR);
504 #Careful sun tar options and ordering is very very tricky
509 $self->validate_inexclude();
511 if($self->{extended_header} =~ /^YES$/i) {
514 if($self->{extended_attrib} =~ /^YES$/i) {
517 if(defined($self->{index})) {
521 if(defined($self->{block_size})) {
523 push @optparams, $self->{block_size};
526 if (defined($self->{exclude_tmp})) {
528 push @optparams,"-",$self->{exclude_tmp};
533 if ($self->{directory}) {
534 push @optparams, "-C", $self->{directory};
536 push @optparams, "-C", $self->{device};
539 if(defined($self->{include_tmp})) {
540 push @optparams,"-I", $self->{include_tmp};
545 my(@cmd) = ($self->{pfexec}, $self->{suntar}, $cmd, @optparams);
553 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>.
567 my @opt_exclude_list;
568 my $opt_exclude_optional;
569 my @opt_include_list;
570 my $opt_include_optional;
572 my $opt_ext_attrib = "YES";
573 my $opt_ext_head = "YES";
581 Getopt::Long::Configure(qw{bundling});
583 'config=s' => \$opt_config,
584 'host=s' => \$opt_host,
585 'disk=s' => \$opt_disk,
586 'device=s' => \$opt_device,
587 'level=s' => \$opt_level,
588 'index=s' => \$opt_index,
589 'message=s' => \$opt_message,
590 'collection=s' => \$opt_collection,
591 'exclude-list=s' => \@opt_exclude_list,
592 'exclude-optional=s' => \$opt_exclude_optional,
593 'include-list=s' => \@opt_include_list,
594 'include-optional=s' => \$opt_include_optional,
595 'record' => \$opt_record,
596 'block-size=s' => \$opt_bsize,
597 'extended-attributes=s' => \$opt_ext_attrib,
598 'extended-headers=s' => \$opt_ext_head,
599 'ignore=s' => \@opt_ignore,
600 'normal=s' => \@opt_normal,
601 'strange=s' => \@opt_strange,
602 'error=s' => \@opt_error,
603 'lang=s' => \$opt_lang,
604 'directory=s' => \$opt_directory,
607 if (defined $opt_lang) {
608 $ENV{LANG} = $opt_lang;
611 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);
613 $application->do($ARGV[0]);