b5c7e6a80a909487a4bea936c6611f4aaff2738b
[debian/amanda] / dumper-src / gnutar.pl.in
1 #!@PERL@
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 $debug=1;
10 push(@INC, "@DUMPER_DIR@");
11
12 use File::Copy;
13 use IPC::Open3;
14 use Sys::Hostname;
15
16
17 open(DEBUG,">>@AMANDA_DBGDIR@/gnutar.$$.debug") if ($debug==1);
18
19 $prefix='@prefix@';
20 $exec_prefix="@exec_prefix@";
21 $libexecdir="@libexecdir@";
22 $USE_VERSION_SUFFIXES='@USE_VERSION_SUFFIXES@';
23 $suf = '';
24 if ( $USE_VERSION_SUFFIXES =~ /^yes$/i ) {
25    $suf='-@VERSION@';
26 }
27
28 $host = hostname;
29 $host =~ s/\..*$//;
30 $runtar="${libexecdir}/runtar${suf}";
31 $gnulist = '@GNUTAR_LISTED_INCREMENTAL_DIR@';
32 $gnutar = $runtar;
33
34
35
36 $level_incrementals = 9;
37
38 @index_generation = ("direct","parse","output","image");
39 @parse_estimate   = ("direct","parse");
40 @parse_backup     = ("direct","parse");
41 @backup_type      = ("full","level","diff");
42
43
44 #$user_support  = "";
45 #$group_support = "";
46
47 #$user_selfcheck  = "";
48 #$group_selfcheck = "";
49
50 #$user_estimate  = "";
51 #$group_estimate = "";
52
53 #$user_estimate_parse  = "";
54 #$group_estimate_parse = "";
55
56 $user_backup  = "root";
57 #$group_backup = "";
58
59 #$user_backup_parse  = "";
60 #$group_backup_parse = "";
61
62 #$user_index_from_output  = "";
63 #$group_index_from_output = "";
64
65 #$user_index_from_image  = "";
66 #$group_index_from_image = "";
67
68 #$user_restore  = "";
69 #$group_restore = "";
70
71 #$user_print_command  = "";
72 #$group_print_command = "";
73
74 $user_default  = "amanda";
75 $group_default = "amanda";
76
77
78 sub command_selfcheck {
79 print DEBUG "STDOUT: OK selfcheck\n" if ($debug == 1);
80    print "OK selfcheck\n";
81 }
82
83 sub command_estimate_full {
84    my($disk)    = @_;
85    command_estimate_level(0, $disk);
86 }
87
88 sub command_estimate_level {
89    my($level, $disk) = @_;
90    my($listdir) = "$host$disk";
91    $listdir     =~ s/\//_/g;
92    if($level == 0) {
93       open(GNULIST, ">${gnulist}/${listdir}_${level}.new") || die();
94       close(GNULIST) || die();
95    }
96    else {
97       my($prev_level) = $level - 1;
98       copy("${gnulist}/${listdir}_${prev_level}", "${gnulist}/${listdir}_${level}.new");
99    }
100    if($option_estimate_direct == 1) {
101       command_estimate_opt_direct($disk, $level, $listdir);
102    }
103    else {
104       command_estimate_opt_parse($disk, $level, $listdir);
105    }
106 }
107
108 #sub command_estimate_diff {
109 #}
110
111 sub command_estimate_opt_direct {
112    my($disk, $level, $listdir) = @_;
113    my($size) = -1;
114 print DEBUG "$gnutar --create --directory $disk --listed-incremental ${gnulist}/${listdir}_${level}.new --sparse --one-file-system --ignore-failed-read --totals --file /dev/null .\n" if ($debug == 1);
115    open3(\*WTRFH, '>&STDOUT', \*ESTIMATE, "$gnutar --create --directory $disk --listed-incremental ${gnulist}/${listdir}_${level}.new --sparse --one-file-system --ignore-failed-read --totals --file /dev/null .") || die();
116
117    $size = parse_estimate(ESTIMATE);
118    close(ESTIMATE);
119    output_size($size);
120    unlink "${gnulist}/${listdir}_${level}.new";
121    exit 0;
122 }
123
124 sub command_estimate_opt_parse {
125    my($disk, $level, $listdir) = @_;
126
127    open3(\*WTRFH, '>&STDOUT',\*ESTIMATE,"$gnutar --create --directory $disk --listed-incremental ${gnulist}/${listdir}_${level}.new --sparse --one-file-system --ignore-failed-read --totals --file /dev/null .");
128
129    while (<ESTIMATE>) {
130 print DEBUG "READ: $_" if ($debug == 1);
131 print DEBUG "STDOUT: $_" if ($debug == 1);
132       print $_;
133    }
134    close(ESTIMATE);
135    close(WTRFH);
136
137    unlink "${gnulist}/${listdir}_${level}.new";
138 }
139
140 sub command_estimate_parse {
141    my($size) = parse_estimate(STDIN);
142    output_size($size);
143    exit 0;
144 }
145
146 sub parse_estimate {
147    my($fh) = @_;
148    my($size) = -1;
149    while(<$fh>) {
150 print DEBUG "READ: $_" if ($debug == 1);
151       if ($_ =~ /^Total bytes written: (\d*)$/) {
152          $size = $1;
153          last;
154       }
155    }
156    return $size;
157 }
158
159 sub output_size {
160    my($size) = @_;
161    if($size == -1) {
162 print DEBUG "STDOUT: -1 -1\n" if ($debug == 1);
163       print "-1 -1\n";
164       exit 2;
165    }
166    else {
167       my($ksize) = $size / 1024;
168 print DEBUG "STDOUT: $ksize 1024\n" if ($debug == 1);
169       print "$ksize 1024\n";
170    }
171 }
172
173 sub command_backup_full {
174    my($disk)    = @_;
175    command_backup_level(0, $disk);
176 }
177
178 sub command_backup_level {
179    my($level, $disk) = @_;
180    my($listdir) = "$host$disk";
181    my($verbose) = "";
182    $listdir     =~ s/\//_/g;
183
184    if($level == 0) {
185       open(GNULIST, ">${gnulist}/${listdir}_${level}.new") || die();
186       close(GNULIST) || die();
187    }
188    else {
189       my($prev_level) = $level - 1;
190       copy("${gnulist}/${listdir}_${prev_level}", 
191            "${gnulist}/${listdir}_${level}.new");
192    }
193
194    if($option_index_direct == 1 || $option_index_parse == 1) {
195       $verbose = "--verbose";
196    }
197    my($cmd) = "$gnutar --create $verbose --directory $disk --listed-incremental ${gnulist}/${listdir}_${level}.new --sparse --one-file-system --ignore-failed-read --totals --file - .";
198
199    if($option_backup_parse == 1 && $option_index_direct == 0 &&
200                                    $option_index_parse  == 0) {
201       system($cmd);
202    }
203    else {
204       open3(\*WTRFH, '>&STDOUT', \*INDEX, $cmd) || die();
205
206       if($option_index_direct == 1 || $option_index_parse == 1) {
207          open(INDEXOUT, '>&=3') || die();
208          parse_backup(INDEX, STDERR, INDEXOUT);
209          close(INDEXOUT);
210       }
211       else {
212          parse_backup(INDEX, STDERR, undef);
213       }
214       close(INDEX);
215       close(WTRFH);
216    }
217
218    if(!defined($option_no_record)) {
219       rename "${gnulist}/${listdir}_${level}.new", 
220              "${gnulist}/${listdir}_${level}";
221    }
222    else {
223       unlink "${gnulist}/${listdir}_${level}.new";
224    }
225    exit 0;
226 }
227
228 #sub command_backup_diff {
229 #}
230
231 sub command_backup_parse {
232    $option_backup_direct = 1;
233    $option_backup_parse  = 0;
234    parse_backup(STDIN, STDOUT, undef);
235    exit 0;
236 }
237
238 sub parse_backup {
239    my($fhin, $fhout, $indexout) = @_;
240    my($size) = -1;
241    while(<$fhin>) {
242 print DEBUG "READ: $_" if ($debug == 1);
243       if ( /^\.\//) {
244          if(defined($indexout)) {
245             if($option_index_direct == 1) {
246                s/^\.//;
247 print DEBUG "INDEXOUT: $_" if ($debug == 1);
248                print $indexout $_;
249             }
250             elsif($option_index_parse == 1) {
251 print DEBUG "INDEXOUT: $_" if ($debug == 1);
252                print $indexout $_;
253             }
254          }
255       }
256       else {
257          if (/^Total bytes written: (\d*)$/) {
258             $size = $1;
259             if(defined($fhout) && $option_backup_parse == 1) {
260 print DEBUG "FHOUT: $_" if ($debug == 1);
261                print $fhout $_;
262             }
263          }
264          elsif(defined($fhout)) {
265             if($option_backup_direct == 1) {
266 print DEBUG "FHOUT: $_" if ($debug == 1);
267                print $fhout $_;
268             }
269             elsif($option_backup_parse == 1) {
270 print DEBUG "FHOUT: $_" if ($debug == 1);
271                print $fhout $_;
272             }
273          }
274       }
275    }
276    if(defined($fhout) && $option_backup_direct == 1) {
277       if ($size == -1) {
278 print DEBUG "FHOUT: $command -1 -1\n" if ($debug == 1);
279          print $fhout "$command -1 -1\n";
280       }
281       else {
282          my($ksize) = $size/1024;
283 print DEBUG "FHOUT: $ksize 1024\n" if ($debug == 1);
284          print $fhout "$ksize 1024\n";
285       }
286    }
287 }
288
289 sub command_index_from_output {
290    index_from_output(STDIN, STDOUT);
291    exit 0;
292 }
293
294 sub index_from_output {
295    my($fhin, $fhout) = @_;
296    my($size) = -1;
297    while(<$fhin>) {
298 print DEBUG "READ: $_" if ($debug == 1);
299       next if /^Total bytes written:/;
300       next if !/^\.\//;
301       s/^\.//;
302 print DEBUG "FHOUT: $_" if ($debug == 1);
303       print $fhout $_;
304    }
305 }
306
307 sub command_index_from_image {
308    open(INDEX, "$gnutar --list --file - |") || die();
309    index_from_output(INDEX, STDOUT);
310 }
311
312 #sub command_restore {
313 #}
314
315 sub command_print_command {
316 }
317
318 require "generic-dumper"