Debian Codegraph @

application-src/amsuntar.pl

raw
1 #!@PERL@
2 # Copyright (c) 2009-2012 Zmanda, Inc. All Rights Reserved.
3 # Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved.
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #
19 # Contact information: Carbonite Inc., 756 N Pastoria Ave
20 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
21
22 use lib '@amperldir@';
23 use strict;
24 use warnings;
25 use Getopt::Long;
26
27 package Amanda::Application::Amsuntar;
28 use base qw(Amanda::Application);
29 use File::Copy;
30 use File::Temp qw( tempfile );
31 use File::Path;
32 use IPC::Open2;
33 use IPC::Open3;
34 use Sys::Hostname;
35 use Symbol;
36 use Amanda::Constants;
37 use Amanda::Config qw( :init :getconf config_dir_relative );
38 use Amanda::Debug qw( :logging );
39 use Amanda::Paths;
40 use Amanda::Util qw( :constants quote_string );
41
42 sub new {
43 my $class = shift;
44 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, $target, $suntar_path) = @_;
45 my $self = $class->SUPER::new($config);
46
47 $self->{suntar} = $Amanda::Constants::SUNTAR;
48 if (defined $suntar_path) {
49 $self->{suntar} = $suntar_path;
50 }
51 $self->{pfexec} = "/usr/bin/pfexec";
52 $self->{gnutar} = $Amanda::Constants::GNUTAR;
53 $self->{teecount} = $Amanda::Paths::amlibexecdir."/teecount";
54
55 $self->{config} = $config;
56 $self->{host} = $host;
57 if (defined $disk) {
58 $self->{disk} = $disk;
59 } else {
60 $self->{disk} = $device;
61 }
62 if (defined $device) {
63 $self->{device} = $device;
64 } else {
65 $self->{device} = $disk;
66 }
67 $self->{level} = $level;
68 $self->{index} = $index;
69 $self->{message} = $message;
70 $self->{collection} = $collection;
71 $self->{record} = $record;
72 $self->{exclude_list} = [ @{$exclude_list} ];
73 $self->{exclude_optional} = $exclude_optional;
74 $self->{include_list} = [ @{$include_list} ];
75 $self->{include_optional} = $include_optional;
76 $self->{block_size} = $bsize;
77 $self->{extended_header} = $ext_header;
78 $self->{extended_attrib} = $ext_attrib;
79 $self->{target} = $target;
80
81 $self->{regex} = ();
82 my $regex;
83 for $regex (@{$ignore}) {
84 my $a = { regex => $regex, type => "IGNORE" };
85 push @{$self->{regex}}, $a;
86 }
87
88 for $regex (@{$normal}) {
89 my $a = { regex => $regex, type => "NORMAL" };
90 push @{$self->{regex}}, $a;
91 }
92
93 for $regex (@{$strange}) {
94 my $a = { regex => $regex, type => "STRANGE" };
95 push @{$self->{regex}}, $a;
96 }
97
98 for $regex (@{$error_exp}) {
99 my $a = { regex => $regex, type => "ERROR" };
100 push @{$self->{regex}}, $a;
101 }
102
103 #type can be IGNORE/NORMAL/STRANGE/ERROR
104 push @{$self->{regex}}, { regex => "is not a file. Not dumped\$",
105 type => "NORMAL" };
106 push @{$self->{regex}}, { regex => "same as archive file\$",
107 type => "NORMAL" };
108 push @{$self->{regex}}, { regex => ": invalid character in UTF-8 conversion of ",
109 type => "STRANGE" };
110 push @{$self->{regex}}, { regex => ": UTF-8 conversion failed.\$",
111 type => "STRANGE" };
112 push @{$self->{regex}}, { regex => ": Permission denied\$",
113 type => "ERROR" };
114
115 for $regex (@{$self->{regex}}) {
116 debug ($regex->{type} . ": " . $regex->{regex});
117 }
118
119 return $self;
120 }
121
122 sub command_support {
123 my $self = shift;
124
125 print "CONFIG YES\n";
126 print "HOST YES\n";
127 print "DISK YES\n";
128 print "MAX-LEVEL 0\n";
129 print "INDEX-LINE YES\n";
130 print "INDEX-XML NO\n";
131 print "MESSAGE-LINE YES\n";
132 print "MESSAGE-XML NO\n";
133 print "RECORD YES\n";
134 print "EXCLUDE-FILE NO\n";
135 print "EXCLUDE-LIST YES\n";
136 print "EXCLUDE-OPTIONAL YES\n";
137 print "INCLUDE-FILE NO\n";
138 print "INCLUDE-LIST YES\n";
139 print "INCLUDE-OPTIONAL YES\n";
140 print "COLLECTION NO\n";
141 print "MULTI-ESTIMATE NO\n";
142 print "CALCSIZE NO\n";
143 print "CLIENT-ESTIMATE YES\n";
144 }
145
146 sub command_selfcheck {
147 my $self = shift;
148
149 $self->print_to_server("disk " . quote_string($self->{disk}));
150
151 $self->print_to_server("amsuntar version " . $Amanda::Constants::VERSION,
152 $Amanda::Script_App::GOOD);
153
154 if (!-e $self->{suntar}) {
155 $self->print_to_server_and_die(
156 "application binary $self->{suntar} doesn't exist",
157 $Amanda::Script_App::ERROR);
158 }
159 if (!-x $self->{suntar}) {
160 $self->print_to_server_and_die(
161 "application binary $self->{suntar} is not a executable",
162 $Amanda::Script_App::ERROR);
163 }
164 if (!defined $self->{disk} || !defined $self->{device}) {
165 return;
166 }
167 print "OK " . $self->{device} . "\n";
168 print "OK " . $self->{target} . "\n" if defined $self->{target};
169 $self->validate_inexclude();
170 }
171
172 sub command_estimate() {
173 my $self = shift;
174 my $size = "-1";
175 my $level = $self->{level};
176
177 $self->{index} = undef; #remove verbose flag to suntar.
178 my(@cmd) = $self->build_command();
179 my(@cmdwc) = ("/usr/bin/wc", "-c");
180
181 debug("cmd:" . join(" ", @cmd) . " | " . join(" ", @cmdwc));
182 my($wtr, $rdr, $err, $pid, $rdrwc, $pidwc);
183 $err = Symbol::gensym;
184 $pid = open3($wtr, \*DATA, $err, @cmd);
185 $pidwc = open2($rdrwc, '>&DATA', @cmdwc);
186 close $wtr;
187
188 my $errmsg;
189 my $result = 0;
190 while (<$err>) {
191 my $matched = 0;
192 for my $regex (@{$self->{regex}}) {
193 my $regex1 = $regex->{regex};
194 if (/$regex1/) {
195 $result = 1 if ($regex->{type} eq "ERROR");
196 $matched = 1;
197 last;
198 }
199 }
200 $result = 1 if ($matched == 0);
201 $errmsg = $_ if (!defined $errmsg);
202 }
203 my ($msgsize) = <$rdrwc>;
204 waitpid $pid, 0;
205 close $rdrwc;
206 close $err;
207 if ($result == 1) {
208 if (defined $errmsg) {
209 $self->print_to_server_and_die($errmsg, $Amanda::Script_App::ERROR);
210 } else {
211 $self->print_to_server_and_die(
212 "cannot estimate archive size': unknown reason",
213 $Amanda::Script_App::ERROR);
214 }
215 }
216 output_size($level, $msgsize);
217 }
218
219
220 sub output_size {
221 my($level) = shift;
222 my($size) = shift;
223 if($size == -1) {
224 print "$level -1 -1\n";
225 #exit 2;
226 }
227 else {
228 my($ksize) = int $size / (1024);
229 $ksize=32 if ($ksize<32);
230 print "$level $ksize 1\n";
231 }
232 }
233
234 sub command_backup {
235 my $self = shift;
236
237 $self->validate_inexclude();
238
239 my(@cmd) = $self->build_command();
240 my(@cmdtc) = $self->{teecount};
241
242 debug("cmd:" . join(" ", @cmd) . " | " . join(" ", @cmdtc));
243
244 my($wtr, $pid, $rdrtc, $errtc, $pidtc);
245 my $index_fd = Symbol::gensym;
246 $errtc = Symbol::gensym;
247
248 $pid = open3($wtr, \*DATA, $index_fd, @cmd) ||
249 $self->print_to_server_and_die("Can't run $cmd[0]: $!",
250 $Amanda::Script_App::ERROR);
251 $pidtc = open3('<&DATA', '>&STDOUT', $errtc, @cmdtc) ||
252 $self->print_to_server_and_die("Can't run $cmdtc[0]: $!",
253 $Amanda::Script_App::ERROR);
254 close($wtr);
255
256 unlink($self->{include_tmp}) if defined $self->{include_tmp} and -e $self->{include_tmp};
257 unlink($self->{exclude_tmp}) if defined $self->{exclude_tmp} and -e $self->{exclude_tmp};
258
259 my $result;
260 if(defined($self->{index})) {
261 my $indexout_fd;
262 open($indexout_fd, '>&=4') ||
263 $self->print_to_server_and_die("Can't open indexout_fd: $!",
264 $Amanda::Script_App::ERROR);
265 $result = $self->parse_backup($index_fd, $self->{mesgout}, $indexout_fd);
266 close($indexout_fd);
267 }
268 else {
269 $result = $self->parse_backup($index_fd, $self->{mesgout}, undef);
270 }
271 close($index_fd);
272 my $size = <$errtc>;
273
274 waitpid $pid, 0;
275
276 my $status = $?;
277 if( $status != 0 ){
278 debug("exit status $status ?" );
279 }
280
281 if ($result == 1) {
282 debug("$self->{suntar} returned error" );
283 $self->print_to_server("$self->{suntar} returned error",
284 $Amanda::Script_App::ERROR);
285 }
286
287 my($ksize) = int ($size/1024);
288 print {$self->{mesgout}} "sendbackup: size $ksize\n";
289 debug("sendbackup: size $ksize ");
290 }
291
292 sub parse_backup {
293 my $self = shift;
294 my($fhin, $fhout, $indexout) = @_;
295 my $size = -1;
296 my $result = 0;
297 while(<$fhin>) {
298 if ( /^ ?a\s+(\.\/.*) \d*K/ ||
299 /^a\s+(\.\/.*) symbolic link to/ ||
300 /^a\s+(\.\/.*) link to/ ) {
301 my $name = $1;
302 if(defined($indexout)) {
303 if(defined($self->{index})) {
304 $name =~ s/^\.//;
305 print $indexout $name, "\n";
306 }
307 }
308 }
309 else {
310 my $matched = 0;
311 for my $regex (@{$self->{regex}}) {
312 my $regex1 = $regex->{regex};
313 if (/$regex1/) {
314 $result = 1 if ($regex->{type} eq "ERROR");
315 if (defined($fhout)) {
316 if ($regex->{type} eq "IGNORE") {
317 } elsif ($regex->{type} eq "NORMAL") {
318 print $fhout "| $_";
319 } elsif ($regex->{type} eq "STRANGE") {
320 print $fhout "? $_";
321 } else {
322 print $fhout "? $_";
323 }
324 }
325 $matched = 1;
326 last;
327 }
328 }
329 if ($matched == 0) {
330 $result = 1;
331 if (defined($fhout)) {
332 print $fhout "? $_";
333 }
334 }
335 }
336 }
337 return $result;
338 }
339
340 sub validate_inexclude {
341 my $self = shift;
342 my $fh;
343 my @tmp;
344
345 if ($#{$self->{exclude_list}} >= 0 && $#{$self->{include_list}} >= 0 ) {
346 $self->print_to_server_and_die("Can't have both include and exclude",
347 $Amanda::Script_App::ERROR);
348 }
349
350 foreach my $file (@{$self->{exclude_list}}){
351 if (!open($fh, $file)) {
352 if ($self->{action} eq "check" && !$self->{exclude_optional}) {
353 $self->print_to_server("Open of '$file' failed: $!",
354 $Amanda::Script_App::ERROR);
355 }
356 next;
357 }
358 while (<$fh>) {
359 push @tmp, $_;
360 }
361 close($fh);
362 }
363
364 #Merging list into a single file
365 if($self->{action} eq 'backup' && $#{$self->{exculde_list}} >= 0) {
366 ($fh, $self->{exclude_tmp}) = tempfile(DIR => $Amanda::paths::AMANDA_TMPDIR);
367 unless($fh) {
368 $self->print_to_server_and_die(
369 "Open of tmp file '$self->{exclude_tmp}' failed: $!",
370 $Amanda::Script_App::ERROR);
371 }
372 print $fh @tmp;
373 close $fh;
374 undef (@tmp);
375 }
376
377 foreach my $file (@{$self->{include_list}}) {
378 if (!open($fh, $file)) {
379 if ($self->{action} eq "check" && !$self->{include_optional}) {
380 $self->print_to_server("Open of '$file' failed: $!",
381 $Amanda::Script_App::ERROR);
382 }
383 next;
384 }
385 while (<$fh>) {
386 push @tmp, $_;
387 }
388 close($fh);
389 }
390
391 if($self->{action} eq 'backup' && $#{$self->{include_list}} >= 0) {
392 ($fh, $self->{include_tmp}) = tempfile(DIR => $Amanda::paths::AMANDA_TMPDIR);
393 unless($fh) {
394 $self->print_to_server_and_die(
395 "Open of tmp file '$self->{include_tmp}' failed: $!",
396 $Amanda::Script_App::ERROR);
397 }
398 print $fh @tmp;
399 close $fh;
400 undef (@tmp);
401 }
402 }
403
404 sub command_index_from_output {
405 index_from_output(0, 1);
406 }
407
408 sub index_from_output {
409 my($fhin, $fhout) = @_;
410 my($size) = -1;
411 while(<$fhin>) {
412 next if /^Total bytes written:/;
413 next if !/^\.\//;
414 s/^\.//;
415 print $fhout $_;
416 }
417 }
418
419 sub command_index {
420 my $self = shift;
421 my $program;
422 my $index_fd;
423
424 if (-e $self->{suntar}) {
425 $program = $self->{suntar};
426 } elsif (-e $self->{gnutar}) {
427 $program = $self->{gnutar};
428 }
429
430 open2($index_fd, ">&0", $program, "tf", "-") ||
431 $self->print_to_server_and_die("Can't run $program: $!",
432 $Amanda::Script_App::ERROR);
433 index_from_output($index_fd, \*STDOUT);
434
435 }
436
437 sub command_restore {
438 my $self = shift;
439
440 chdir(Amanda::Util::get_original_cwd());
441 if (defined $self->{target}) {
442 if (!-d $self->{target}) {
443 $self->print_to_server_and_die("Directory $self->{target}: $!",
444 $Amanda::Script_App::ERROR);
445 }
446 if (!-w $self->{target}) {
447 $self->print_to_server_and_die("Directory $self->{target}: $!",
448 $Amanda::Script_App::ERROR);
449 }
450 chdir($self->{target});
451 }
452
453 my $cmd = "-xpv";
454
455 if($self->{extended_header} eq "YES") {
456 $cmd .= "E";
457 }
458 if($self->{extended_attrib} eq "YES") {
459 $cmd .= "\@";
460 }
461
462 $cmd .= "f";
463
464 if (defined($self->{exclude_list}) && defined($self->{exclude_list}[0]) && (-e $self->{exclude_list}[0])) {
465 $cmd .= "X";
466 }
467
468 my(@cmd) = ($self->{pfexec},$self->{suntar}, $cmd);
469
470 push @cmd, "-"; # for f argument
471 if (defined($self->{exclude_list}) && defined($self->{exclude_list}[0]) && (-e $self->{exclude_list}[0])) {
472 push @cmd, $self->{exclude_list}[0]; # for X argument
473 }
474
475 if(defined($self->{include_list}) && defined($self->{include_list}[0]) && (-e $self->{include_list}[0])) {
476 push @cmd, "-I", $self->{include_list}[0];
477 }
478
479 for(my $i=1;defined $ARGV[$i]; $i++) {
480 my $param = $ARGV[$i];
481 $param =~ /^(.*)$/;
482 push @cmd, $1;
483 }
484 debug("cmd:" . join(" ", @cmd));
485 exec { $cmd[0] } @cmd;
486 die("Can't exec '", $cmd[0], "': $!");
487 }
488
489 sub command_validate {
490 my $self = shift;
491 my @cmd;
492 my $program;
493
494 if (-e $self->{suntar}) {
495 $program = $self->{suntar};
496 } elsif (-e $self->{gnutar}) {
497 $program = $self->{gnutar};
498 } else {
499 return $self->default_validate();
500 }
501 @cmd = ($program, "-tf", "-");
502 debug("cmd:" . join(" ", @cmd));
503 my $pid = open3('>&STDIN', '>&STDOUT', '>&STDERR', @cmd) ||
504 $self->print_to_server_and_die("Unable to run @cmd",
505 $Amanda::Script_App::ERROR);
506 waitpid $pid, 0;
507 if( $? != 0 ){
508 $self->print_to_server_and_die("$program returned error",
509 $Amanda::Script_App::ERROR);
510 }
511 }
512
513 sub build_command {
514 my $self = shift;
515
516 #Careful sun tar options and ordering is very very tricky
517
518 my($cmd) = "-cp";
519 my(@optparams) = ();
520
521 $self->validate_inexclude();
522
523 if($self->{extended_header} =~ /^YES$/i) {
524 $cmd .= "E";
525 }
526 if($self->{extended_attrib} =~ /^YES$/i) {
527 $cmd .= "\@";
528 }
529 if(defined($self->{index})) {
530 $cmd .= "v";
531 }
532
533 if(defined($self->{block_size})) {
534 $cmd .= "b";
535 push @optparams, $self->{block_size};
536 }
537
538 if (defined($self->{exclude_tmp})) {
539 $cmd .= "fX";
540 push @optparams,"-",$self->{exclude_tmp};
541 } else {
542 $cmd .= "f";
543 push @optparams,"-";
544 }
545 if ($self->{target}) {
546 push @optparams, "-C", $self->{target};
547 } else {
548 push @optparams, "-C", $self->{device};
549 }
550
551 if(defined($self->{include_tmp})) {
552 push @optparams,"-I", $self->{include_tmp};
553 } else {
554 push @optparams,".";
555 }
556
557 my(@cmd) = ($self->{pfexec}, $self->{suntar}, $cmd, @optparams);
558 return (@cmd);
559 }
560
561 package main;
562
563 sub usage {
564 print <<EOF;
565 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>.
566 EOF
567 exit(1);
568 }
569
570 my $opt_version;
571 my $opt_config;
572 my $opt_host;
573 my $opt_disk;
574 my $opt_device;
575 my $opt_level;
576 my $opt_index;
577 my $opt_message;
578 my $opt_collection;
579 my $opt_record;
580 my @opt_exclude_list;
581 my $opt_exclude_optional;
582 my @opt_include_list;
583 my $opt_include_optional;
584 my $opt_bsize = 256;
585 my $opt_ext_attrib = "YES";
586 my $opt_ext_head = "YES";
587 my @opt_ignore;
588 my @opt_normal;
589 my @opt_strange;
590 my @opt_error;
591 my $opt_lang;
592 my $opt_target;
593 my $opt_suntar_path;
594
595 my @orig_argv = @ARGV;
596
597 Getopt::Long::Configure(qw{bundling});
598 GetOptions(
599 'version' => \$opt_version,
600 'config=s' => \$opt_config,
601 'host=s' => \$opt_host,
602 'disk=s' => \$opt_disk,
603 'device=s' => \$opt_device,
604 'level=s' => \$opt_level,
605 'index=s' => \$opt_index,
606 'message=s' => \$opt_message,
607 'collection=s' => \$opt_collection,
608 'exclude-list=s' => \@opt_exclude_list,
609 'exclude-optional=s' => \$opt_exclude_optional,
610 'include-list=s' => \@opt_include_list,
611 'include-optional=s' => \$opt_include_optional,
612 'record' => \$opt_record,
613 'block-size=s' => \$opt_bsize,
614 'extended-attributes=s' => \$opt_ext_attrib,
615 'extended-headers=s' => \$opt_ext_head,
616 'ignore=s' => \@opt_ignore,
617 'normal=s' => \@opt_normal,
618 'strange=s' => \@opt_strange,
619 'error=s' => \@opt_error,
620 'lang=s' => \$opt_lang,
621 'target|directory=s' => \$opt_target,
622 'suntar-path=s' => \$opt_suntar_path,
623 ) or usage();
624
625 if (defined $opt_version) {
626 print "amsuntar-" . $Amanda::Constants::VERSION , "\n";
627 exit(0);
628 }
629
630 if (defined $opt_lang) {
631 $ENV{LANG} = $opt_lang;
632 }
633
634 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_target, $opt_suntar_path);
635
636 Amanda::Debug::debug("Arguments: " . join(' ', @orig_argv));
637
638 $application->do($ARGV[0]);
639 # NOTREACHED