/[BackupPC]/trunk/bin/BackupPC_tarIncCreate
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/bin/BackupPC_tarIncCreate

Parent Directory Parent Directory | Revision Log Revision Log


Revision 252 - (hide annotations)
Mon Dec 12 13:41:04 2005 UTC (18 years, 5 months ago) by dpavlin
File size: 23181 byte(s)
 r11636@llin:  dpavlin | 2005-12-12 14:53:13 +0100
 fixes

1 dpavlin 234 #!/usr/bin/perl -w
2 dpavlin 100 #============================================================= -*-perl-*-
3     #
4 dpavlin 109 # BackupPC_tarIncCreate: create a tar archive of an existing incremental dump
5     #
6 dpavlin 100 #
7     # DESCRIPTION
8     #
9 dpavlin 112 # Usage: BackupPC_tarIncCreate [options]
10 dpavlin 100 #
11     # Flags:
12     # Required options:
13     #
14     # -h host Host from which the tar archive is created.
15     # -n dumpNum Dump number from which the tar archive is created.
16     # A negative number means relative to the end (eg -1
17     # means the most recent dump, -2 2nd most recent etc).
18     # -s shareName Share name from which the tar archive is created.
19     #
20     # Other options:
21     # -t print summary totals
22     # -r pathRemove path prefix that will be replaced with pathAdd
23     # -p pathAdd new path prefix
24     # -b BLOCKS BLOCKS x 512 bytes per record (default 20; same as tar)
25     # -w writeBufSz write buffer size (default 1MB)
26     #
27     # The -h, -n and -s options specify which dump is used to generate
28     # the tar archive. The -r and -p options can be used to relocate
29     # the paths in the tar archive so extracted files can be placed
30     # in a location different from their original location.
31     #
32     # AUTHOR
33     # Craig Barratt <cbarratt@users.sourceforge.net>
34 dpavlin 112 # Ivan Klaric <iklaric@gmail.com>
35     # Dobrica Pavlinusic <dpavlin@rot13.org>
36 dpavlin 100 #
37     # COPYRIGHT
38     # Copyright (C) 2001-2003 Craig Barratt
39     #
40     # This program is free software; you can redistribute it and/or modify
41     # it under the terms of the GNU General Public License as published by
42     # the Free Software Foundation; either version 2 of the License, or
43     # (at your option) any later version.
44     #
45     # This program is distributed in the hope that it will be useful,
46     # but WITHOUT ANY WARRANTY; without even the implied warranty of
47     # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48     # GNU General Public License for more details.
49     #
50     # You should have received a copy of the GNU General Public License
51     # along with this program; if not, write to the Free Software
52     # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
53     #
54     #========================================================================
55     #
56     # Version 2.1.0, released 20 Jun 2004.
57     #
58     # See http://backuppc.sourceforge.net.
59     #
60     #========================================================================
61    
62     use strict;
63     no utf8;
64     use lib "__INSTALLDIR__/lib";
65     use File::Path;
66     use Getopt::Std;
67     use DBI;
68     use BackupPC::Lib;
69     use BackupPC::Attrib qw(:all);
70     use BackupPC::FileZIO;
71     use BackupPC::View;
72     use BackupPC::SearchLib;
73 dpavlin 112 use Time::HiRes qw/time/;
74     use POSIX qw/strftime/;
75 dpavlin 234 use File::Which;
76     use File::Path;
77 dpavlin 235 use File::Slurp;
78 dpavlin 112 use Data::Dumper; ### FIXME
79 dpavlin 100
80     die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
81     my $TopDir = $bpc->TopDir();
82     my $BinDir = $bpc->BinDir();
83     my %Conf = $bpc->Conf();
84 dpavlin 234 %BackupPC::SearchLib::Conf = %Conf;
85 dpavlin 100 my %opts;
86 dpavlin 112 my $in_backup_increment;
87 dpavlin 100
88 dpavlin 112
89 dpavlin 252 if ( !getopts("th:n:p:r:s:b:w:vdf", \%opts) ) {
90 dpavlin 100 print STDERR <<EOF;
91 dpavlin 112 usage: $0 [options]
92 dpavlin 100 Required options:
93     -h host host from which the tar archive is created
94     -n dumpNum dump number from which the tar archive is created
95     A negative number means relative to the end (eg -1
96     means the most recent dump, -2 2nd most recent etc).
97     -s shareName share name from which the tar archive is created
98    
99     Other options:
100     -t print summary totals
101     -r pathRemove path prefix that will be replaced with pathAdd
102     -p pathAdd new path prefix
103     -b BLOCKS BLOCKS x 512 bytes per record (default 20; same as tar)
104     -w writeBufSz write buffer size (default 1048576 = 1MB)
105 dpavlin 252 -f overwrite existing parts
106 dpavlin 156 -v verbose output
107 dpavlin 234 -d debug output
108 dpavlin 100 EOF
109     exit(1);
110     }
111    
112     if ( $opts{h} !~ /^([\w\.\s-]+)$/ ) {
113 dpavlin 236 die "$0: bad host name '$opts{h}'\n";
114 dpavlin 100 }
115     my $Host = $opts{h};
116    
117     if ( $opts{n} !~ /^(-?\d+)$/ ) {
118 dpavlin 236 die "$0: bad dump number '$opts{n}'\n";
119 dpavlin 100 }
120     my $Num = $opts{n};
121    
122 dpavlin 234 my $bin;
123     foreach my $c (qw/gzip md5sum tee/) {
124     $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
125     }
126    
127 dpavlin 100 my @Backups = $bpc->BackupInfoRead($Host);
128     my $FileCnt = 0;
129     my $ByteCnt = 0;
130     my $DirCnt = 0;
131     my $SpecialCnt = 0;
132     my $ErrorCnt = 0;
133 dpavlin 234 my $current_tar_size = 0;
134 dpavlin 236 my $total_increment_size = 0;
135 dpavlin 100
136     my $i;
137     $Num = $Backups[@Backups + $Num]{num} if ( -@Backups <= $Num && $Num < 0 );
138     for ( $i = 0 ; $i < @Backups ; $i++ ) {
139     last if ( $Backups[$i]{num} == $Num );
140     }
141     if ( $i >= @Backups ) {
142 dpavlin 236 die "$0: bad backup number $Num for host $Host\n";
143 dpavlin 100 }
144    
145     my $PathRemove = $1 if ( $opts{r} =~ /(.+)/ );
146     my $PathAdd = $1 if ( $opts{p} =~ /(.+)/ );
147     if ( $opts{s} !~ /^([\w\s\.\/\$-]+)$/ && $opts{s} ne "*" ) {
148 dpavlin 236 die "$0: bad share name '$opts{s}'\n";
149 dpavlin 100 }
150     our $ShareName = $opts{s};
151     our $view = BackupPC::View->new($bpc, $Host, \@Backups);
152    
153 dpavlin 235 # database
154    
155     my $dsn = $Conf{SearchDSN};
156     my $db_user = $Conf{SearchUser} || '';
157    
158     my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 0} );
159    
160     my $sth_inc_size = $dbh->prepare(qq{
161     update backups set
162     inc_size = ?,
163     parts = ?,
164     inc_deleted = false
165 dpavlin 251 where id = ?
166     });
167 dpavlin 235 my $sth_backup_parts = $dbh->prepare(qq{
168     insert into backup_parts (
169     backup_id,
170     part_nr,
171     tar_size,
172     size,
173     md5,
174     items
175     ) values (?,?,?,?,?,?)
176     });
177    
178 dpavlin 100 #
179     # This constant and the line of code below that uses it are borrowed
180     # from Archive::Tar. Thanks to Calle Dybedahl and Stephen Zander.
181     # See www.cpan.org.
182     #
183     # Archive::Tar is Copyright 1997 Calle Dybedahl. All rights reserved.
184     # Copyright 1998 Stephen Zander. All rights reserved.
185     #
186     my $tar_pack_header
187     = 'a100 a8 a8 a8 a12 a12 A8 a1 a100 a6 a2 a32 a32 a8 a8 a155 x12';
188     my $tar_header_length = 512;
189    
190     my $BufSize = $opts{w} || 1048576; # 1MB or 2^20
191     my $WriteBuf = "";
192     my $WriteBufSz = ($opts{b} || 20) * $tar_header_length;
193    
194     my(%UidCache, %GidCache);
195     my(%HardLinkExtraFiles, @HardLinks);
196    
197     #
198     # Write out all the requested files/directories
199     #
200 dpavlin 112
201 dpavlin 234 my $max_file_size = $Conf{'MaxArchiveFileSize'} || die "problem with MaxArchiveFileSize parametar";
202     $max_file_size *= 1024;
203    
204     my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir};
205     die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir);
206    
207     my $tar_file = BackupPC::SearchLib::getGzipName($Host, $ShareName, $Num) || die "can't getGzipName($Host, $ShareName, $Num)";
208    
209 dpavlin 251 my $tar_path_final = $tar_dir . '/' . $tar_file;
210     my $tar_path = $tar_path_final . '.tmp';
211    
212 dpavlin 234 $tar_path =~ s#//#/#g;
213    
214 dpavlin 235 my $sth = $dbh->prepare(qq{
215     SELECT
216     backups.id
217     FROM backups
218     JOIN shares on shares.id = shareid
219     JOIN hosts on hosts.id = shares.hostid
220     WHERE hosts.name = ? and shares.name = ? and backups.num = ?
221     });
222     $sth->execute($Host, $ShareName, $Num);
223     my ($backup_id) = $sth->fetchrow_array;
224     $sth->finish;
225 dpavlin 234
226 dpavlin 235 print STDERR "backup_id: $backup_id working dir: $tar_dir, max uncompressed size $max_file_size bytes, tar $tar_file\n" if ($opts{d});
227    
228 dpavlin 252 if (-e $tar_path_final) {
229     if ($opts{f}) {
230     rmtree $tar_path_final || die "can't remove $tar_path_final: $!";
231     } else {
232     die "$tar_path_final allready exists\n";
233     }
234     }
235 dpavlin 235
236 dpavlin 234 my $fh;
237     my $part = 0;
238     my $no_files = 0;
239 dpavlin 235 my $items_in_part = 0;
240 dpavlin 234
241     sub new_tar_part {
242 dpavlin 236 my $arg = {@_};
243    
244 dpavlin 234 if ($fh) {
245     return if ($current_tar_size == 0);
246    
247 dpavlin 251 print STDERR " $part" if ($opts{v});
248 dpavlin 234
249 dpavlin 236 #
250     # Finish with two null 512 byte headers,
251     # and then round out a full block.
252     #
253 dpavlin 234 my $data = "\0" x ($tar_header_length * 2);
254     TarWrite($fh, \$data);
255     TarWrite($fh, undef);
256    
257     close($fh) || die "can't close archive part $part: $!";
258 dpavlin 235
259     my $file = $tar_path . '/' . $part;
260    
261     my $md5 = read_file( $file . '.md5' ) || die "can't read md5sum file ${file}.md5";
262     $md5 =~ s/\s.*$//;
263    
264     my $size = (stat( $file . '.tar.gz' ))[7] || die "can't stat ${file}.tar.gz";
265    
266     $sth_backup_parts->execute(
267     $backup_id,
268     $part,
269     $current_tar_size,
270     $size,
271     $md5,
272     $items_in_part,
273     );
274    
275 dpavlin 236 $total_increment_size += int( ( $size + 1023 ) / 1024 ) * 1024;
276    
277     if ($arg->{close}) {
278 dpavlin 251
279 dpavlin 252 sub move($$) {
280     my ($from,$to) = @_;
281     print STDERR "# rename $from -> $to\n" if ($opts{d});
282     rename $from, $to || die "can't move $from -> $to: $!\n";
283     }
284    
285 dpavlin 251 if ($part == 1) {
286     print STDERR " single";
287 dpavlin 252 move("${tar_path}/1.tar.gz", "${tar_path_final}.tar.gz");
288     move("${tar_path}/1.md5", "${tar_path_final}.md5");
289     rmtree $tar_path or die "can't remove temporary dir $tar_path: $!";
290 dpavlin 251 } else {
291     print STDERR " [last]";
292 dpavlin 252 move("${tar_path}", "${tar_path_final}");
293 dpavlin 251 }
294    
295 dpavlin 236 $sth_inc_size->execute(
296     $total_increment_size,
297     $part,
298     $backup_id
299     );
300 dpavlin 251
301     print STDERR ", $total_increment_size bytes\n" if ($opts{v});
302    
303 dpavlin 236 return;
304     }
305    
306 dpavlin 234 }
307    
308     $part++;
309    
310     # if this is first part, create directory
311    
312     if ($part == 1) {
313     if (-d $tar_path) {
314     print STDERR "# deleting existing $tar_path\n" if ($opts{d});
315     rmtree($tar_path);
316     }
317     mkdir($tar_path) || die "can't create directory $tar_path: $!";
318 dpavlin 236
319     sub abort_cleanup {
320     print STDERR "ABORTED: cleanup temp dir";
321     rmtree($tar_path);
322     $dbh->rollback;
323     exit 1;
324     }
325    
326     $SIG{'INT'} = \&abort_cleanup;
327     $SIG{'QUIT'} = \&abort_cleanup;
328     $SIG{'__DIE__'} = \&abort_cleanup;
329    
330 dpavlin 234 }
331    
332     my $file = $tar_path . '/' . $part;
333    
334     #
335     # create comprex pipe which will pass output through gzip
336     # for compression, create file on disk using tee
337     # and pipe same output to md5sum to create checksum
338     #
339    
340     my $cmd = '| ' . $bin->{'gzip'} . ' ' . $Conf{GzipLevel} . ' ' .
341     '| ' . $bin->{'tee'} . ' ' . $file . '.tar.gz' . ' ' .
342     '| ' . $bin->{'md5sum'} . ' - > ' . $file . '.md5';
343    
344     print STDERR "## $cmd\n" if ($opts{d});
345    
346     open($fh, $cmd) or die "can't open $cmd: $!";
347     binmode($fh);
348 dpavlin 235
349 dpavlin 234 $current_tar_size = 0;
350 dpavlin 235 $items_in_part = 0;
351 dpavlin 234 }
352    
353     new_tar_part();
354    
355 dpavlin 112 if (seedCache($Host, $ShareName, $Num)) {
356     archiveWrite($fh, '/');
357     archiveWriteHardLinks($fh);
358 dpavlin 252 new_tar_part( close => 1 );
359 dpavlin 100 } else {
360 dpavlin 170 print STDERR "NOTE: no files found for $Host:$ShareName, increment $Num\n" if ($opts{v});
361 dpavlin 252 # remove temporary files if there are no files
362 dpavlin 234 rmtree($tar_path);
363     }
364    
365 dpavlin 100 #
366     # print out totals if requested
367     #
368     if ( $opts{t} ) {
369     print STDERR "Done: $FileCnt files, $ByteCnt bytes, $DirCnt dirs,",
370     " $SpecialCnt specials, $ErrorCnt errors\n";
371     }
372     if ( $ErrorCnt && !$FileCnt && !$DirCnt ) {
373     #
374     # Got errors, with no files or directories; exit with non-zero
375     # status
376     #
377 dpavlin 236 die "got errors or no files\n";
378 dpavlin 100 }
379 dpavlin 234
380 dpavlin 235 $sth_inc_size->finish;
381     $sth_backup_parts->finish;
382    
383     $dbh->commit || die "can't commit changes to database";
384     $dbh->disconnect();
385    
386 dpavlin 236 exit;
387 dpavlin 100
388     ###########################################################################
389     # Subroutines
390     ###########################################################################
391    
392     sub archiveWrite
393     {
394     my($fh, $dir, $tarPathOverride) = @_;
395    
396     if ( $dir =~ m{(^|/)\.\.(/|$)} ) {
397     print(STDERR "$0: bad directory '$dir'\n");
398     $ErrorCnt++;
399     return;
400     }
401     $dir = "/" if ( $dir eq "." );
402     #print(STDERR "calling find with $Num, $ShareName, $dir\n");
403    
404     if ( $view->find($Num, $ShareName, $dir, 0, \&TarWriteFile,
405     $fh, $tarPathOverride) < 0 ) {
406     print(STDERR "$0: bad share or directory '$ShareName/$dir'\n");
407     $ErrorCnt++;
408     return;
409     }
410     }
411    
412     #
413     # Write out any hardlinks (if any)
414     #
415     sub archiveWriteHardLinks
416     {
417     my $fh = @_;
418     foreach my $hdr ( @HardLinks ) {
419     $hdr->{size} = 0;
420     if ( defined($PathRemove)
421     && substr($hdr->{linkname}, 0, length($PathRemove)+1)
422     eq ".$PathRemove" ) {
423     substr($hdr->{linkname}, 0, length($PathRemove)+1) = ".$PathAdd";
424     }
425     TarWriteFileInfo($fh, $hdr);
426     }
427     @HardLinks = ();
428     %HardLinkExtraFiles = ();
429     }
430    
431     sub UidLookup
432     {
433     my($uid) = @_;
434    
435     $UidCache{$uid} = (getpwuid($uid))[0] if ( !exists($UidCache{$uid}) );
436     return $UidCache{$uid};
437     }
438    
439     sub GidLookup
440     {
441     my($gid) = @_;
442    
443     $GidCache{$gid} = (getgrgid($gid))[0] if ( !exists($GidCache{$gid}) );
444     return $GidCache{$gid};
445     }
446    
447     sub TarWrite
448     {
449     my($fh, $dataRef) = @_;
450    
451 dpavlin 234
452 dpavlin 100 if ( !defined($dataRef) ) {
453     #
454     # do flush by padding to a full $WriteBufSz
455     #
456     my $data = "\0" x ($WriteBufSz - length($WriteBuf));
457     $dataRef = \$data;
458     }
459 dpavlin 234
460     # poor man's tell :-)
461     $current_tar_size += length($$dataRef);
462    
463 dpavlin 100 if ( length($WriteBuf) + length($$dataRef) < $WriteBufSz ) {
464     #
465     # just buffer and return
466     #
467     $WriteBuf .= $$dataRef;
468     return;
469     }
470     my $done = $WriteBufSz - length($WriteBuf);
471     if ( syswrite($fh, $WriteBuf . substr($$dataRef, 0, $done))
472     != $WriteBufSz ) {
473 dpavlin 236 die "Unable to write to output file ($!)\n";
474 dpavlin 100 }
475     while ( $done + $WriteBufSz <= length($$dataRef) ) {
476     if ( syswrite($fh, substr($$dataRef, $done, $WriteBufSz))
477     != $WriteBufSz ) {
478 dpavlin 236 die "Unable to write to output file ($!)\n";
479 dpavlin 100 }
480     $done += $WriteBufSz;
481     }
482     $WriteBuf = substr($$dataRef, $done);
483     }
484    
485     sub TarWritePad
486     {
487     my($fh, $size) = @_;
488    
489     if ( $size % $tar_header_length ) {
490     my $data = "\0" x ($tar_header_length - ($size % $tar_header_length));
491     TarWrite($fh, \$data);
492     }
493     }
494    
495     sub TarWriteHeader
496     {
497     my($fh, $hdr) = @_;
498    
499     $hdr->{uname} = UidLookup($hdr->{uid}) if ( !defined($hdr->{uname}) );
500     $hdr->{gname} = GidLookup($hdr->{gid}) if ( !defined($hdr->{gname}) );
501     my $devmajor = defined($hdr->{devmajor}) ? sprintf("%07o", $hdr->{devmajor})
502     : "";
503     my $devminor = defined($hdr->{devminor}) ? sprintf("%07o", $hdr->{devminor})
504     : "";
505     my $sizeStr;
506     if ( $hdr->{size} >= 2 * 65536 * 65536 ) {
507     #
508     # GNU extension for files >= 8GB: send size in big-endian binary
509     #
510     $sizeStr = pack("c4 N N", 0x80, 0, 0, 0,
511     $hdr->{size} / (65536 * 65536),
512     $hdr->{size} % (65536 * 65536));
513     } elsif ( $hdr->{size} >= 1 * 65536 * 65536 ) {
514     #
515     # sprintf octal only handles up to 2^32 - 1
516     #
517     $sizeStr = sprintf("%03o", $hdr->{size} / (1 << 24))
518     . sprintf("%08o", $hdr->{size} % (1 << 24));
519     } else {
520     $sizeStr = sprintf("%011o", $hdr->{size});
521     }
522     my $data = pack($tar_pack_header,
523     substr($hdr->{name}, 0, 99),
524     sprintf("%07o", $hdr->{mode}),
525     sprintf("%07o", $hdr->{uid}),
526     sprintf("%07o", $hdr->{gid}),
527     $sizeStr,
528     sprintf("%011o", $hdr->{mtime}),
529     "", #checksum field - space padded by pack("A8")
530     $hdr->{type},
531     substr($hdr->{linkname}, 0, 99),
532     $hdr->{magic} || 'ustar ',
533     $hdr->{version} || ' ',
534     $hdr->{uname},
535     $hdr->{gname},
536     $devmajor,
537     $devminor,
538     "" # prefix is empty
539     );
540     substr($data, 148, 7) = sprintf("%06o\0", unpack("%16C*",$data));
541     TarWrite($fh, \$data);
542     }
543    
544     sub TarWriteFileInfo
545     {
546     my($fh, $hdr) = @_;
547    
548     #
549     # Handle long link names (symbolic links)
550     #
551     if ( length($hdr->{linkname}) > 99 ) {
552     my %h;
553     my $data = $hdr->{linkname} . "\0";
554     $h{name} = "././\@LongLink";
555     $h{type} = "K";
556     $h{size} = length($data);
557     TarWriteHeader($fh, \%h);
558     TarWrite($fh, \$data);
559     TarWritePad($fh, length($data));
560     }
561     #
562     # Handle long file names
563     #
564     if ( length($hdr->{name}) > 99 ) {
565     my %h;
566     my $data = $hdr->{name} . "\0";
567     $h{name} = "././\@LongLink";
568     $h{type} = "L";
569     $h{size} = length($data);
570     TarWriteHeader($fh, \%h);
571     TarWrite($fh, \$data);
572     TarWritePad($fh, length($data));
573     }
574     TarWriteHeader($fh, $hdr);
575     }
576    
577     #
578 dpavlin 112 # seed cache of files in this increment
579 dpavlin 100 #
580 dpavlin 112 sub seedCache($$$) {
581     my ($host, $share, $dumpNo) = @_;
582    
583 dpavlin 251 print STDERR curr_time(), "$host:$share #$dumpNo" if ($opts{v});
584 dpavlin 112 my $sql = q{
585 dpavlin 234 SELECT path,size
586 dpavlin 112 FROM files
587     JOIN shares on shares.id = shareid
588     JOIN hosts on hosts.id = shares.hostid
589     WHERE hosts.name = ? and shares.name = ? and backupnum = ?
590     };
591 dpavlin 100
592 dpavlin 112 my $sth = $dbh->prepare($sql);
593     $sth->execute($host, $share, $dumpNo);
594     my $count = $sth->rows;
595 dpavlin 251 print STDERR " $count items, parts:" if ($opts{v});
596 dpavlin 112 while (my $row = $sth->fetchrow_arrayref) {
597 dpavlin 156 #print STDERR "+ ", $row->[0],"\n";
598 dpavlin 234 $in_backup_increment->{ $row->[0] } = $row->[1];
599 dpavlin 100 }
600 dpavlin 112
601     $sth->finish();
602 dpavlin 100
603 dpavlin 112 return $count;
604 dpavlin 100 }
605    
606 dpavlin 234 #
607     # calculate overhad for one file in tar
608     #
609     sub tar_overhead($) {
610     my $name = shift || '';
611    
612     # header, padding of file and two null blocks at end
613     my $len = 4 * $tar_header_length;
614    
615     # if filename is longer than 99 chars subtract blocks for
616     # long filename
617     if ( length($name) > 99 ) {
618     $len += int( ( length($name) + $tar_header_length ) / $tar_header_length ) * $tar_header_length;
619     }
620    
621     return $len;
622     }
623    
624 dpavlin 100 my $Attr;
625     my $AttrDir;
626    
627     sub TarWriteFile
628     {
629     my($hdr, $fh, $tarPathOverride) = @_;
630    
631     my $tarPath = $hdr->{relPath};
632     $tarPath = $tarPathOverride if ( defined($tarPathOverride) );
633    
634     $tarPath =~ s{//+}{/}g;
635 dpavlin 112
636 dpavlin 234 #print STDERR "? $tarPath\n" if ($opts{d});
637     my $size = $in_backup_increment->{$tarPath};
638     return unless (defined($size));
639 dpavlin 112
640 dpavlin 234 # is this file too large to fit into MaxArchiveFileSize?
641    
642     if ( ($current_tar_size + tar_overhead($tarPath) + $size) > $max_file_size ) {
643 dpavlin 251 print STDERR "# tar file $current_tar_size + $tar_header_length + $size > $max_file_size, splitting\n" if ($opts{d});
644 dpavlin 234 new_tar_part();
645     }
646    
647 dpavlin 235 #print STDERR "A $tarPath [$size] tell: $current_tar_size\n" if ($opts{d});
648     $items_in_part++;
649 dpavlin 234
650 dpavlin 100 if ( defined($PathRemove)
651     && substr($tarPath, 0, length($PathRemove)) eq $PathRemove ) {
652     substr($tarPath, 0, length($PathRemove)) = $PathAdd;
653     }
654     $tarPath = "./" . $tarPath if ( $tarPath !~ /^\.\// );
655     $tarPath =~ s{//+}{/}g;
656     $hdr->{name} = $tarPath;
657    
658     if ( $hdr->{type} == BPC_FTYPE_DIR ) {
659     #
660     # Directory: just write the header
661     #
662     $hdr->{name} .= "/" if ( $hdr->{name} !~ m{/$} );
663 dpavlin 112 TarWriteFileInfo($fh, $hdr);
664     $DirCnt++;
665 dpavlin 100 } elsif ( $hdr->{type} == BPC_FTYPE_FILE ) {
666     #
667     # Regular file: write the header and file
668     #
669     my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
670     if ( !defined($f) ) {
671     print(STDERR "Unable to open file $hdr->{fullPath}\n");
672     $ErrorCnt++;
673     return;
674     }
675 dpavlin 234 # do we need to split file?
676     if ($hdr->{size} < $max_file_size) {
677     TarWriteFileInfo($fh, $hdr);
678     my($data, $size);
679     while ( $f->read(\$data, $BufSize) > 0 ) {
680     TarWrite($fh, \$data);
681     $size += length($data);
682     }
683     $f->close;
684     TarWritePad($fh, $size);
685 dpavlin 100 $FileCnt++;
686     $ByteCnt += $size;
687 dpavlin 234 } else {
688     my $full_size = $hdr->{size};
689     my $orig_name = $hdr->{name};
690     my $max_part_size = $max_file_size - tar_overhead($hdr->{name});
691    
692     my $parts = int(($full_size + $max_part_size - 1) / $max_part_size);
693     print STDERR "# splitting $orig_name [$full_size bytes] into $parts parts\n" if ($opts{d});
694     foreach my $subpart ( 1 .. $parts ) {
695     new_tar_part();
696     if ($subpart < $parts) {
697     $hdr->{size} = $max_part_size;
698     } else {
699     $hdr->{size} = $full_size % $max_part_size;
700     }
701     $hdr->{name} = $orig_name . '/' . $subpart;
702     print STDERR "## creating part $subpart ",$hdr->{name}, " [", $hdr->{size}," bytes]\n";
703    
704     TarWriteFileInfo($fh, $hdr);
705     my($data, $size);
706     if (0) {
707     for ( 1 .. int($hdr->{size} / $BufSize) ) {
708     my $r_size = $f->read(\$data, $BufSize);
709     die "expected $BufSize bytes read, got $r_size bytes!" if ($r_size != $BufSize);
710     TarWrite($fh, \$data);
711     $size += length($data);
712     }
713     }
714     my $size_left = $hdr->{size} % $BufSize;
715     my $r_size = $f->read(\$data, $size_left);
716     die "expected $size_left bytes last read, got $r_size bytes!" if ($r_size != $size_left);
717    
718     TarWrite($fh, \$data);
719     $size += length($data);
720     TarWritePad($fh, $size);
721 dpavlin 235
722     $items_in_part++;
723 dpavlin 234 }
724     $f->close;
725     $FileCnt++;
726     $ByteCnt += $full_size;
727     new_tar_part();
728     }
729 dpavlin 100 } elsif ( $hdr->{type} == BPC_FTYPE_HARDLINK ) {
730     #
731     # Hardlink file: either write a hardlink or the complete file
732 dpavlin 234 # depending upon whether the linked-to file will be written
733     # to the archive.
734 dpavlin 100 #
735 dpavlin 234 # Start by reading the contents of the link.
736     #
737 dpavlin 100 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
738     if ( !defined($f) ) {
739     print(STDERR "Unable to open file $hdr->{fullPath}\n");
740     $ErrorCnt++;
741     return;
742     }
743     my $data;
744     while ( $f->read(\$data, $BufSize) > 0 ) {
745     $hdr->{linkname} .= $data;
746     }
747 dpavlin 234 $f->close;
748     my $done = 0;
749     my $name = $hdr->{linkname};
750     $name =~ s{^\./}{/};
751     if ( $HardLinkExtraFiles{$name} ) {
752     #
753     # Target file will be or was written, so just remember
754     # the hardlink so we can dump it later.
755     #
756     push(@HardLinks, $hdr);
757     $SpecialCnt++;
758     } else {
759     #
760     # Have to dump the original file. Just call the top-level
761     # routine, so that we save the hassle of dealing with
762     # mangling, merging and attributes.
763     #
764     $HardLinkExtraFiles{$hdr->{linkname}} = 1;
765     archiveWrite($fh, $hdr->{linkname}, $hdr->{name});
766     }
767 dpavlin 100 } elsif ( $hdr->{type} == BPC_FTYPE_SYMLINK ) {
768     #
769     # Symbolic link: read the symbolic link contents into the header
770     # and write the header.
771     #
772     my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
773     if ( !defined($f) ) {
774     print(STDERR "Unable to open symlink file $hdr->{fullPath}\n");
775     $ErrorCnt++;
776     return;
777     }
778     my $data;
779     while ( $f->read(\$data, $BufSize) > 0 ) {
780     $hdr->{linkname} .= $data;
781     }
782     $f->close;
783     $hdr->{size} = 0;
784     TarWriteFileInfo($fh, $hdr);
785     $SpecialCnt++;
786     } elsif ( $hdr->{type} == BPC_FTYPE_CHARDEV
787     || $hdr->{type} == BPC_FTYPE_BLOCKDEV
788     || $hdr->{type} == BPC_FTYPE_FIFO ) {
789     #
790     # Special files: for char and block special we read the
791     # major and minor numbers from a plain file.
792     #
793     if ( $hdr->{type} != BPC_FTYPE_FIFO ) {
794     my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0,
795     $hdr->{compress});
796     my $data;
797     if ( !defined($f) || $f->read(\$data, $BufSize) < 0 ) {
798     print(STDERR "Unable to open/read char/block special file"
799     . " $hdr->{fullPath}\n");
800     $f->close if ( defined($f) );
801     $ErrorCnt++;
802     return;
803     }
804     $f->close;
805     if ( $data =~ /(\d+),(\d+)/ ) {
806     $hdr->{devmajor} = $1;
807     $hdr->{devminor} = $2;
808     }
809     }
810     $hdr->{size} = 0;
811     TarWriteFileInfo($fh, $hdr);
812     $SpecialCnt++;
813     } else {
814     print(STDERR "Got unknown type $hdr->{type} for $hdr->{name}\n");
815     $ErrorCnt++;
816     }
817     }
818 dpavlin 112
819     my $t_fmt = '%Y-%m-%d %H:%M:%S';
820     sub curr_time {
821     return strftime($t_fmt,localtime());
822     }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26