Imported Upstream version 2.6.0p2
[debian/amanda] / dumper-src / amgtar.pl
1 #!@PERL@ -T
2 #
3
4 # Run perl.
5 eval '(exit $?0)' && eval 'exec @PERL@ -S $0 ${1+"$@"}'
6         & eval 'exec @PERL@ -S $0 $argv:q'
7                 if 0;
8
9 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV', 'PATH'};
10 $ENV{'PATH'} = "/usr/bin:/usr/sbin:/sbin:/bin";
11
12 $debug=1;
13 push(@INC, ".", "@DUMPER_DIR@");
14
15 use File::Copy;
16 use IPC::Open3;
17 use Sys::Hostname;
18
19
20 open(DEBUG,">>@AMANDA_DBGDIR@/amgtar.$$.debug") if ($debug==1);
21
22 $prefix='@prefix@';
23 $prefix = $prefix;
24 $exec_prefix="@exec_prefix@";
25 $exec_prefix=$exec_prefix;
26 $amlibexecdir="@amlibexecdir@";
27 $amlibexecdir=$amlibexecdir;
28
29 $USE_VERSION_SUFFIXES='@USE_VERSION_SUFFIXES@';
30 $suf = '';
31 if ( $USE_VERSION_SUFFIXES =~ /^yes$/i ) {
32    $suf='-@VERSION@';
33 }
34
35 $myhost = hostname;
36 $myhost =~ s/\..*$//;
37 $runtar="${amlibexecdir}/runtar${suf}";
38 $gnulist = '@GNUTAR_LISTED_INCREMENTAL_DIR@';
39 $gnutar = '@GNUTAR@';
40
41
42
43 $max_level    = 9;
44 $index_line   = 1;
45 $index_xml    = 0;
46 $message_line = 1;
47 $message_xml  = 0;
48 $record       = 1;
49 $include_file = 1;
50 $include_list = 1;
51 $exclude_file = 1;
52 $exclude_list = 1;
53 $collection   = 0;
54
55 #$user_support  = "";
56 #$group_support = "";
57
58 #$user_selfcheck  = "";
59 #$group_selfcheck = "";
60
61 #$user_estimate  = "";
62 #$group_estimate = "";
63
64 #$user_estimate_parse  = "";
65 #$group_estimate_parse = "";
66
67 $user_backup  = "root";
68 #$group_backup = "";
69
70 #$user_backup_parse  = "";
71 #$group_backup_parse = "";
72
73 #$user_index_from_output  = "";
74 #$group_index_from_output = "";
75
76 #$user_index_from_image  = "";
77 #$group_index_from_image = "";
78
79 #$user_restore  = "";
80 #$group_restore = "";
81
82 #$user_print_command  = "";
83 #$group_print_command = "";
84
85 $user_default  = "amanda";
86 $group_default = "amanda";
87
88
89 sub command_support {
90    my($config, $host, $disk, $device, $level) = @_;
91    print "CONFIG YES\n";
92    print DEBUG "STDOUT: CONFIG YES\n" if ($debug == 1);
93    print "HOST YES\n";
94    print DEBUG "STDOUT: HOST YES\n" if ($debug == 1);
95    print "DISK YES\n";
96    print DEBUG "STDOUT: DISK YES\n" if ($debug == 1);
97    print "MAX-LEVEL 9\n";
98    print DEBUG "STDOUT: MAX-LEVEL 9\n" if ($debug == 1);
99    print "INDEX-LINE YES\n";
100    print DEBUG "STDOUT: INDEX-LINE YES\n" if ($debug == 1);
101    print "INDEX-XML NO\n";
102    print DEBUG "STDOUT: INDEX-XML NO\n" if ($debug == 1);
103    print "MESSAGE-LINE YES\n";
104    print DEBUG "STDOUT: MESSAGE-LINE YES\n" if ($debug == 1);
105    print "MESSAGE-XML NO\n";
106    print DEBUG "STDOUT: MESSAGE-XML NO\n" if ($debug == 1);
107    print "RECORD YES\n";
108    print DEBUG "STDOUT: RECORD YES\n" if ($debug == 1);
109    print "INCLUDE-FILE YES\n";
110    print "INCLUDE-LIST YES\n";
111    print "EXCLUDE-FILE YES\n";
112    print "EXCLUDE-LIST YES\n";
113    print "COLLECTION NO\n";
114 }
115
116 sub command_selfcheck {
117    my($config, $host, $disk, $device, $level) = @_;
118    print DEBUG "STDOUT: OK $disk\n" if ($debug == 1);
119    print DEBUG "STDOUT: OK $device\n" if ($debug == 1);
120    print "OK $disk\n";
121    print "OK $device\n";
122    #check binary
123    #check statefile
124    #check amdevice
125    #check property include/exclude
126 }
127
128 sub command_estimate {
129    my($config, $host, $disk, $device, $level) = @_;
130    my($listdir) = "$host$disk";
131    $listdir     =~ s/\//_/g;
132    if($level == 0) {
133       open(GNULIST, ">${gnulist}/${listdir}_${level}.new") || die();
134       close(GNULIST) || die();
135    }
136    else {
137       my($prev_level) = $level - 1;
138       if (-f "${gnulist}/${listdir}_${prev_level}") {
139         copy("${gnulist}/${listdir}_${prev_level}", "${gnulist}/${listdir}_${level}.new");
140       } else {
141         open(GNULIST, ">${gnulist}/${listdir}_${level}.new") || die();
142         close(GNULIST) || die();
143         #print "ERROR file ${gnulist}/${listdir}_${level}.new doesn't exist\n";
144       }
145    }
146    command_estimate_opt_direct($config, $host, $disk, $device, $level, $listdir);
147 }
148
149
150 sub command_estimate_opt_direct {
151    my($config, $host, $disk, $device, $level, $listdir) = @_;
152    my($size) = -1;
153    my(@cmd) = ($runtar, $config, $gnutar, "--create", "--directory", $device, "--listed-incremental", "${gnulist}/${listdir}_${level}.new", "--sparse", "--one-file-system", "--ignore-failed-read", "--totals", "--file", "/dev/null", ".");
154    #my(@cmd) = ($gnutar, "--create", "--directory", $device, "--listed-incremental", "${gnulist}/${listdir}_${level}.new", "--sparse", "--one-file-system", "--ignore-failed-read", "--totals", "--file", "/dev/null", ".");
155    print DEBUG "cmd:" , join(" ", @cmd), "\n" if ($debug == 1);
156    open3(\*WTRFH, '>&STDOUT', \*ESTIMATE, @cmd);
157
158    $size = parse_estimate(ESTIMATE);
159    close(ESTIMATE);
160    output_size($size);
161    unlink "${gnulist}/${listdir}_${level}.new";
162    exit 0;
163 }
164
165 sub parse_estimate {
166    my($fh) = @_;
167    my($size) = -1;
168    while(<$fh>) {
169 print DEBUG "READ 2: $_" if ($debug == 1);
170       if ($_ =~ /^Total bytes written: (\d*)/) {
171          $size = $1;
172          last;
173       }
174    }
175    return $size;
176 }
177
178 sub output_size {
179    my($size) = @_;
180    if($size == -1) {
181       print DEBUG "STDOUT A: -1 -1\n" if ($debug == 1);
182       print "-1 -1\n";
183       exit 2;
184    }
185    else {
186       my($ksize) = int $size / (1024);
187       $ksize=32 if ($ksize<32);
188       print DEBUG "STDOUT B: $ksize 1\n" if ($debug == 1);
189       print "$ksize 1\n";
190    }
191 }
192
193 sub command_backup {
194    my($config, $host, $disk, $device, $level) = @_;
195    my($listdir) = "$host$disk";
196    my($verbose) = "";
197    $listdir     =~ s/\//_/g;
198
199 print DEBUG "config =" . $config . "\n" if ($debug == 1);
200 print DEBUG "host   =" . $host   . "\n" if ($debug == 1);
201 print DEBUG "disk   =" . $disk   . "\n" if ($debug == 1);
202 print DEBUG "device =" . $device . "\n" if ($debug == 1);
203 print DEBUG "level  =" . $level  . "\n" if ($debug == 1);
204
205    if($level == 0) {
206       open(GNULIST, ">${gnulist}/${listdir}_${level}.new") || die();
207       close(GNULIST) || die();
208    }
209    else {
210       my($prev_level) = $level - 1;
211       copy("${gnulist}/${listdir}_${prev_level}", 
212            "${gnulist}/${listdir}_${level}.new");
213    }
214
215    if(defined($opt_index)) {
216       $verbose = "--verbose";
217    }
218    my(@cmd) = ($runtar, $config, $gnutar, "--create", $verbose, "--directory", $device, "--listed-incremental", "${gnulist}/${listdir}_${level}.new", "--sparse", "--one-file-system", "--ignore-failed-read", "--totals", "--file", "-", ".");
219    #my(@cmd) = ($gnutar, "--create", $verbose, "--directory", $device, "--listed-incremental", "${gnulist}/${listdir}_${level}.new", "--sparse", "--one-file-system", "--ignore-failed-read", "--totals", "--file", "-", ".");
220
221    print DEBUG "cmd:" , join(" ", @cmd), "\n" if ($debug == 1);
222
223    open3(\*WTRFH, '>&STDOUT', \*INDEX, @cmd) || die();
224
225    if(defined($opt_index)) {
226       open(INDEXOUT, '>&=3') || die();
227       parse_backup(INDEX, STDERR, INDEXOUT);
228       close(INDEXOUT);
229    }
230    else {
231       parse_backup(INDEX, STDERR, undef);
232    }
233    close(INDEX);
234    close(WTRFH);
235
236    if(defined($opt_record)) {
237       rename "${gnulist}/${listdir}_${level}.new", 
238              "${gnulist}/${listdir}_${level}";
239    }
240    else {
241       unlink "${gnulist}/${listdir}_${level}.new";
242    }
243    exit 0;
244 }
245
246 sub parse_backup {
247    my($fhin, $fhout, $indexout) = @_;
248    my($size) = -1;
249    while(<$fhin>) {
250 print DEBUG "READ 3: $_" if ($debug == 1);
251       if ( /^\.\//) {
252          if(defined($indexout)) {
253             if(defined($opt_index)) {
254                s/^\.//;
255 print DEBUG "INDEXOUT: $_" if ($debug == 1);
256                print $indexout $_;
257             }
258          }
259       }
260       else {
261             if (/^Total bytes written: (\d*)/) {
262                $size = $1;
263                $ksize = int ($size / 1024);
264             }
265             elsif(defined($fhout)) {
266                next if /: Directory is new$/;
267 print DEBUG "FHOUT 2: $_" if ($debug == 1);
268                print $fhout $_;
269             }
270       }
271    }
272    if(defined($fhout)) {
273       if ($size == -1) {
274 print DEBUG "FHOUT 4: $command -1 -1\n" if ($debug == 1);
275          print $fhout "$command -1 -1\n";
276       }
277       else {
278          my($ksize) = int ($size/1024);
279 print DEBUG "FHOUT 5: sendbackup: size $ksize\n" if ($debug == 1);
280          print $fhout "sendbackup: size $ksize\n";
281 print DEBUG "FHOUT 5: sendbackup: end\n" if ($debug == 1);
282                print $fhout "sendbackup: end\n";
283       }
284    }
285 }
286
287 sub command_index_from_output {
288    index_from_output(STDIN, STDOUT);
289    exit 0;
290 }
291
292 sub index_from_output {
293    my($fhin, $fhout) = @_;
294    my($size) = -1;
295    while(<$fhin>) {
296 print DEBUG "READ 4: $_" if ($debug == 1);
297       next if /^Total bytes written:/;
298       next if !/^\.\//;
299       s/^\.//;
300 print DEBUG "FHOUT 6: $_" if ($debug == 1);
301       print $fhout $_;
302    }
303 }
304
305 sub command_index_from_image {
306    my($config, $host, $disk, $device, $level) = @_;
307    open(INDEX, "$gnutar --list --file - |") || die();
308    index_from_output(INDEX, STDOUT);
309 }
310
311 sub command_restore {
312    my($config, $host, $disk, $device, $level) = @_;
313
314 #   $ARGV[0] = undef;   
315    my(@cmd) = ($gnutar, "--numeric-owner", "-xpGvf", "-");
316    for($i=1;defined $ARGV[$i]; $i++) {
317       push @cmd, $ARGV[$i];
318    }
319    print DEBUG "cmd:" , join(" ", @cmd), "\n" if ($debug == 1);
320    exec @cmd;
321 }
322
323 sub command_print_command {
324 }
325
326 require "generic-dumper"