/[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 253 - (hide annotations)
Mon Dec 12 13:41:08 2005 UTC (18 years, 5 months ago) by dpavlin
File size: 23216 byte(s)
 r11637@llin:  dpavlin | 2005-12-12 15:40:59 +0100
 create increments using new BackupPC_tarIncCreate

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 253 #$total_increment_size += int( ( $size + 1023 ) / 1024 ) * 1024;
276     $total_increment_size += $size;
277 dpavlin 236
278     if ($arg->{close}) {
279 dpavlin 251
280 dpavlin 252 sub move($$) {
281     my ($from,$to) = @_;
282     print STDERR "# rename $from -> $to\n" if ($opts{d});
283     rename $from, $to || die "can't move $from -> $to: $!\n";
284     }
285    
286 dpavlin 251 if ($part == 1) {
287     print STDERR " single";
288 dpavlin 252 move("${tar_path}/1.tar.gz", "${tar_path_final}.tar.gz");
289     move("${tar_path}/1.md5", "${tar_path_final}.md5");
290     rmtree $tar_path or die "can't remove temporary dir $tar_path: $!";
291 dpavlin 251 } else {
292     print STDERR " [last]";
293 dpavlin 252 move("${tar_path}", "${tar_path_final}");
294 dpavlin 251 }
295    
296 dpavlin 236 $sth_inc_size->execute(
297     $total_increment_size,
298     $part,
299     $backup_id
300     );
301 dpavlin 251
302     print STDERR ", $total_increment_size bytes\n" if ($opts{v});
303    
304 dpavlin 236 return;
305     }
306    
307 dpavlin 234 }
308    
309     $part++;
310    
311     # if this is first part, create directory
312    
313     if ($part == 1) {
314 dpavlin 253 if (-e $tar_path) {
315 dpavlin 234 print STDERR "# deleting existing $tar_path\n" if ($opts{d});
316     rmtree($tar_path);
317     }
318     mkdir($tar_path) || die "can't create directory $tar_path: $!";
319 dpavlin 236
320     sub abort_cleanup {
321     print STDERR "ABORTED: cleanup temp dir";
322     rmtree($tar_path);
323     $dbh->rollback;
324     exit 1;
325     }
326    
327     $SIG{'INT'} = \&abort_cleanup;
328     $SIG{'QUIT'} = \&abort_cleanup;
329     $SIG{'__DIE__'} = \&abort_cleanup;
330    
331 dpavlin 234 }
332    
333     my $file = $tar_path . '/' . $part;
334    
335     #
336     # create comprex pipe which will pass output through gzip
337     # for compression, create file on disk using tee
338     # and pipe same output to md5sum to create checksum
339     #
340    
341     my $cmd = '| ' . $bin->{'gzip'} . ' ' . $Conf{GzipLevel} . ' ' .
342     '| ' . $bin->{'tee'} . ' ' . $file . '.tar.gz' . ' ' .
343     '| ' . $bin->{'md5sum'} . ' - > ' . $file . '.md5';
344    
345     print STDERR "## $cmd\n" if ($opts{d});
346    
347     open($fh, $cmd) or die "can't open $cmd: $!";
348     binmode($fh);
349 dpavlin 235
350 dpavlin 234 $current_tar_size = 0;
351 dpavlin 235 $items_in_part = 0;
352 dpavlin 234 }
353    
354     new_tar_part();
355    
356 dpavlin 112 if (seedCache($Host, $ShareName, $Num)) {
357     archiveWrite($fh, '/');
358     archiveWriteHardLinks($fh);
359 dpavlin 252 new_tar_part( close => 1 );
360 dpavlin 100 } else {
361 dpavlin 170 print STDERR "NOTE: no files found for $Host:$ShareName, increment $Num\n" if ($opts{v});
362 dpavlin 252 # remove temporary files if there are no files
363 dpavlin 234 rmtree($tar_path);
364     }
365    
366 dpavlin 100 #
367     # print out totals if requested
368     #
369     if ( $opts{t} ) {
370     print STDERR "Done: $FileCnt files, $ByteCnt bytes, $DirCnt dirs,",
371     " $SpecialCnt specials, $ErrorCnt errors\n";
372     }
373     if ( $ErrorCnt && !$FileCnt && !$DirCnt ) {
374     #
375     # Got errors, with no files or directories; exit with non-zero
376     # status
377     #
378 dpavlin 236 die "got errors or no files\n";
379 dpavlin 100 }
380 dpavlin 234
381 dpavlin 235 $sth_inc_size->finish;
382     $sth_backup_parts->finish;
383    
384     $dbh->commit || die "can't commit changes to database";
385     $dbh->disconnect();
386    
387 dpavlin 236 exit;
388 dpavlin 100
389     ###########################################################################
390     # Subroutines
391     ###########################################################################
392    
393     sub archiveWrite
394     {
395     my($fh, $dir, $tarPathOverride) = @_;
396    
397     if ( $dir =~ m{(^|/)\.\.(/|$)} ) {
398     print(STDERR "$0: bad directory '$dir'\n");
399     $ErrorCnt++;
400     return;
401     }
402     $dir = "/" if ( $dir eq "." );
403     #print(STDERR "calling find with $Num, $ShareName, $dir\n");
404    
405     if ( $view->find($Num, $ShareName, $dir, 0, \&TarWriteFile,
406     $fh, $tarPathOverride) < 0 ) {
407     print(STDERR "$0: bad share or directory '$ShareName/$dir'\n");
408     $ErrorCnt++;
409     return;
410     }
411     }
412    
413     #
414     # Write out any hardlinks (if any)
415     #
416     sub archiveWriteHardLinks
417     {
418     my $fh = @_;
419     foreach my $hdr ( @HardLinks ) {
420     $hdr->{size} = 0;
421     if ( defined($PathRemove)
422     && substr($hdr->{linkname}, 0, length($PathRemove)+1)
423     eq ".$PathRemove" ) {
424     substr($hdr->{linkname}, 0, length($PathRemove)+1) = ".$PathAdd";
425     }
426     TarWriteFileInfo($fh, $hdr);
427     }
428     @HardLinks = ();
429     %HardLinkExtraFiles = ();
430     }
431    
432     sub UidLookup
433     {
434     my($uid) = @_;
435    
436     $UidCache{$uid} = (getpwuid($uid))[0] if ( !exists($UidCache{$uid}) );
437     return $UidCache{$uid};
438     }
439    
440     sub GidLookup
441     {
442     my($gid) = @_;
443    
444     $GidCache{$gid} = (getgrgid($gid))[0] if ( !exists($GidCache{$gid}) );
445     return $GidCache{$gid};
446     }
447    
448     sub TarWrite
449     {
450     my($fh, $dataRef) = @_;
451    
452 dpavlin 234
453 dpavlin 100 if ( !defined($dataRef) ) {
454     #
455     # do flush by padding to a full $WriteBufSz
456     #
457     my $data = "\0" x ($WriteBufSz - length($WriteBuf));
458     $dataRef = \$data;
459     }
460 dpavlin 234
461     # poor man's tell :-)
462     $current_tar_size += length($$dataRef);
463    
464 dpavlin 100 if ( length($WriteBuf) + length($$dataRef) < $WriteBufSz ) {
465     #
466     # just buffer and return
467     #
468     $WriteBuf .= $$dataRef;
469     return;
470     }
471     my $done = $WriteBufSz - length($WriteBuf);
472     if ( syswrite($fh, $WriteBuf . substr($$dataRef, 0, $done))
473     != $WriteBufSz ) {
474 dpavlin 236 die "Unable to write to output file ($!)\n";
475 dpavlin 100 }
476     while ( $done + $WriteBufSz <= length($$dataRef) ) {
477     if ( syswrite($fh, substr($$dataRef, $done, $WriteBufSz))
478     != $WriteBufSz ) {
479 dpavlin 236 die "Unable to write to output file ($!)\n";
480 dpavlin 100 }
481     $done += $WriteBufSz;
482     }
483     $WriteBuf = substr($$dataRef, $done);
484     }
485    
486     sub TarWritePad
487     {
488     my($fh, $size) = @_;
489    
490     if ( $size % $tar_header_length ) {
491     my $data = "\0" x ($tar_header_length - ($size % $tar_header_length));
492     TarWrite($fh, \$data);
493     }
494     }
495    
496     sub TarWriteHeader
497     {
498     my($fh, $hdr) = @_;
499    
500     $hdr->{uname} = UidLookup($hdr->{uid}) if ( !defined($hdr->{uname}) );
501     $hdr->{gname} = GidLookup($hdr->{gid}) if ( !defined($hdr->{gname}) );
502     my $devmajor = defined($hdr->{devmajor}) ? sprintf("%07o", $hdr->{devmajor})
503     : "";
504     my $devminor = defined($hdr->{devminor}) ? sprintf("%07o", $hdr->{devminor})
505     : "";
506     my $sizeStr;
507     if ( $hdr->{size} >= 2 * 65536 * 65536 ) {
508     #
509     # GNU extension for files >= 8GB: send size in big-endian binary
510     #
511     $sizeStr = pack("c4 N N", 0x80, 0, 0, 0,
512     $hdr->{size} / (65536 * 65536),
513     $hdr->{size} % (65536 * 65536));
514     } elsif ( $hdr->{size} >= 1 * 65536 * 65536 ) {
515     #
516     # sprintf octal only handles up to 2^32 - 1
517     #
518     $sizeStr = sprintf("%03o", $hdr->{size} / (1 << 24))
519     . sprintf("%08o", $hdr->{size} % (1 << 24));
520     } else {
521     $sizeStr = sprintf("%011o", $hdr->{size});
522     }
523     my $data = pack($tar_pack_header,
524     substr($hdr->{name}, 0, 99),
525     sprintf("%07o", $hdr->{mode}),
526     sprintf("%07o", $hdr->{uid}),
527     sprintf("%07o", $hdr->{gid}),
528     $sizeStr,
529     sprintf("%011o", $hdr->{mtime}),
530     "", #checksum field - space padded by pack("A8")
531     $hdr->{type},
532     substr($hdr->{linkname}, 0, 99),
533     $hdr->{magic} || 'ustar ',
534     $hdr->{version} || ' ',
535     $hdr->{uname},
536     $hdr->{gname},
537     $devmajor,
538     $devminor,
539     "" # prefix is empty
540     );
541     substr($data, 148, 7) = sprintf("%06o\0", unpack("%16C*",$data));
542     TarWrite($fh, \$data);
543     }
544    
545     sub TarWriteFileInfo
546     {
547     my($fh, $hdr) = @_;
548    
549     #
550     # Handle long link names (symbolic links)
551     #
552     if ( length($hdr->{linkname}) > 99 ) {
553     my %h;
554     my $data = $hdr->{linkname} . "\0";
555     $h{name} = "././\@LongLink";
556     $h{type} = "K";
557     $h{size} = length($data);
558     TarWriteHeader($fh, \%h);
559     TarWrite($fh, \$data);
560     TarWritePad($fh, length($data));
561     }
562     #
563     # Handle long file names
564     #
565     if ( length($hdr->{name}) > 99 ) {
566     my %h;
567     my $data = $hdr->{name} . "\0";
568     $h{name} = "././\@LongLink";
569     $h{type} = "L";
570     $h{size} = length($data);
571     TarWriteHeader($fh, \%h);
572     TarWrite($fh, \$data);
573     TarWritePad($fh, length($data));
574     }
575     TarWriteHeader($fh, $hdr);
576     }
577    
578     #
579 dpavlin 112 # seed cache of files in this increment
580 dpavlin 100 #
581 dpavlin 112 sub seedCache($$$) {
582     my ($host, $share, $dumpNo) = @_;
583    
584 dpavlin 251 print STDERR curr_time(), "$host:$share #$dumpNo" if ($opts{v});
585 dpavlin 112 my $sql = q{
586 dpavlin 234 SELECT path,size
587 dpavlin 112 FROM files
588     JOIN shares on shares.id = shareid
589     JOIN hosts on hosts.id = shares.hostid
590     WHERE hosts.name = ? and shares.name = ? and backupnum = ?
591     };
592 dpavlin 100
593 dpavlin 112 my $sth = $dbh->prepare($sql);
594     $sth->execute($host, $share, $dumpNo);
595     my $count = $sth->rows;
596 dpavlin 251 print STDERR " $count items, parts:" if ($opts{v});
597 dpavlin 112 while (my $row = $sth->fetchrow_arrayref) {
598 dpavlin 156 #print STDERR "+ ", $row->[0],"\n";
599 dpavlin 234 $in_backup_increment->{ $row->[0] } = $row->[1];
600 dpavlin 100 }
601 dpavlin 112
602     $sth->finish();
603 dpavlin 100
604 dpavlin 112 return $count;
605 dpavlin 100 }
606    
607 dpavlin 234 #
608     # calculate overhad for one file in tar
609     #
610     sub tar_overhead($) {
611     my $name = shift || '';
612    
613     # header, padding of file and two null blocks at end
614     my $len = 4 * $tar_header_length;
615    
616     # if filename is longer than 99 chars subtract blocks for
617     # long filename
618     if ( length($name) > 99 ) {
619     $len += int( ( length($name) + $tar_header_length ) / $tar_header_length ) * $tar_header_length;
620     }
621    
622     return $len;
623     }
624    
625 dpavlin 100 my $Attr;
626     my $AttrDir;
627    
628     sub TarWriteFile
629     {
630     my($hdr, $fh, $tarPathOverride) = @_;
631    
632     my $tarPath = $hdr->{relPath};
633     $tarPath = $tarPathOverride if ( defined($tarPathOverride) );
634    
635     $tarPath =~ s{//+}{/}g;
636 dpavlin 112
637 dpavlin 234 #print STDERR "? $tarPath\n" if ($opts{d});
638     my $size = $in_backup_increment->{$tarPath};
639     return unless (defined($size));
640 dpavlin 112
641 dpavlin 234 # is this file too large to fit into MaxArchiveFileSize?
642    
643     if ( ($current_tar_size + tar_overhead($tarPath) + $size) > $max_file_size ) {
644 dpavlin 251 print STDERR "# tar file $current_tar_size + $tar_header_length + $size > $max_file_size, splitting\n" if ($opts{d});
645 dpavlin 234 new_tar_part();
646     }
647    
648 dpavlin 235 #print STDERR "A $tarPath [$size] tell: $current_tar_size\n" if ($opts{d});
649     $items_in_part++;
650 dpavlin 234
651 dpavlin 100 if ( defined($PathRemove)
652     && substr($tarPath, 0, length($PathRemove)) eq $PathRemove ) {
653     substr($tarPath, 0, length($PathRemove)) = $PathAdd;
654     }
655     $tarPath = "./" . $tarPath if ( $tarPath !~ /^\.\// );
656     $tarPath =~ s{//+}{/}g;
657     $hdr->{name} = $tarPath;
658    
659     if ( $hdr->{type} == BPC_FTYPE_DIR ) {
660     #
661     # Directory: just write the header
662     #
663     $hdr->{name} .= "/" if ( $hdr->{name} !~ m{/$} );
664 dpavlin 112 TarWriteFileInfo($fh, $hdr);
665     $DirCnt++;
666 dpavlin 100 } elsif ( $hdr->{type} == BPC_FTYPE_FILE ) {
667     #
668     # Regular file: write the header and file
669     #
670     my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
671     if ( !defined($f) ) {
672     print(STDERR "Unable to open file $hdr->{fullPath}\n");
673     $ErrorCnt++;
674     return;
675     }
676 dpavlin 234 # do we need to split file?
677     if ($hdr->{size} < $max_file_size) {
678     TarWriteFileInfo($fh, $hdr);
679     my($data, $size);
680     while ( $f->read(\$data, $BufSize) > 0 ) {
681     TarWrite($fh, \$data);
682     $size += length($data);
683     }
684     $f->close;
685     TarWritePad($fh, $size);
686 dpavlin 100 $FileCnt++;
687     $ByteCnt += $size;
688 dpavlin 234 } else {
689     my $full_size = $hdr->{size};
690     my $orig_name = $hdr->{name};
691     my $max_part_size = $max_file_size - tar_overhead($hdr->{name});
692    
693     my $parts = int(($full_size + $max_part_size - 1) / $max_part_size);
694     print STDERR "# splitting $orig_name [$full_size bytes] into $parts parts\n" if ($opts{d});
695     foreach my $subpart ( 1 .. $parts ) {
696     new_tar_part();
697     if ($subpart < $parts) {
698     $hdr->{size} = $max_part_size;
699     } else {
700     $hdr->{size} = $full_size % $max_part_size;
701     }
702     $hdr->{name} = $orig_name . '/' . $subpart;
703     print STDERR "## creating part $subpart ",$hdr->{name}, " [", $hdr->{size}," bytes]\n";
704    
705     TarWriteFileInfo($fh, $hdr);
706     my($data, $size);
707     if (0) {
708     for ( 1 .. int($hdr->{size} / $BufSize) ) {
709     my $r_size = $f->read(\$data, $BufSize);
710     die "expected $BufSize bytes read, got $r_size bytes!" if ($r_size != $BufSize);
711     TarWrite($fh, \$data);
712     $size += length($data);
713     }
714     }
715     my $size_left = $hdr->{size} % $BufSize;
716     my $r_size = $f->read(\$data, $size_left);
717     die "expected $size_left bytes last read, got $r_size bytes!" if ($r_size != $size_left);
718    
719     TarWrite($fh, \$data);
720     $size += length($data);
721     TarWritePad($fh, $size);
722 dpavlin 235
723     $items_in_part++;
724 dpavlin 234 }
725     $f->close;
726     $FileCnt++;
727     $ByteCnt += $full_size;
728     new_tar_part();
729     }
730 dpavlin 100 } elsif ( $hdr->{type} == BPC_FTYPE_HARDLINK ) {
731     #
732     # Hardlink file: either write a hardlink or the complete file
733 dpavlin 234 # depending upon whether the linked-to file will be written
734     # to the archive.
735 dpavlin 100 #
736 dpavlin 234 # Start by reading the contents of the link.
737     #
738 dpavlin 100 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
739     if ( !defined($f) ) {
740     print(STDERR "Unable to open file $hdr->{fullPath}\n");
741     $ErrorCnt++;
742     return;
743     }
744     my $data;
745     while ( $f->read(\$data, $BufSize) > 0 ) {
746     $hdr->{linkname} .= $data;
747     }
748 dpavlin 234 $f->close;
749     my $done = 0;
750     my $name = $hdr->{linkname};
751     $name =~ s{^\./}{/};
752     if ( $HardLinkExtraFiles{$name} ) {
753     #
754     # Target file will be or was written, so just remember
755     # the hardlink so we can dump it later.
756     #
757     push(@HardLinks, $hdr);
758     $SpecialCnt++;
759     } else {
760     #
761     # Have to dump the original file. Just call the top-level
762     # routine, so that we save the hassle of dealing with
763     # mangling, merging and attributes.
764     #
765     $HardLinkExtraFiles{$hdr->{linkname}} = 1;
766     archiveWrite($fh, $hdr->{linkname}, $hdr->{name});
767     }
768 dpavlin 100 } elsif ( $hdr->{type} == BPC_FTYPE_SYMLINK ) {
769     #
770     # Symbolic link: read the symbolic link contents into the header
771     # and write the header.
772     #
773     my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
774     if ( !defined($f) ) {
775     print(STDERR "Unable to open symlink file $hdr->{fullPath}\n");
776     $ErrorCnt++;
777     return;
778     }
779     my $data;
780     while ( $f->read(\$data, $BufSize) > 0 ) {
781     $hdr->{linkname} .= $data;
782     }
783     $f->close;
784     $hdr->{size} = 0;
785     TarWriteFileInfo($fh, $hdr);
786     $SpecialCnt++;
787     } elsif ( $hdr->{type} == BPC_FTYPE_CHARDEV
788     || $hdr->{type} == BPC_FTYPE_BLOCKDEV
789     || $hdr->{type} == BPC_FTYPE_FIFO ) {
790     #
791     # Special files: for char and block special we read the
792     # major and minor numbers from a plain file.
793     #
794     if ( $hdr->{type} != BPC_FTYPE_FIFO ) {
795     my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0,
796     $hdr->{compress});
797     my $data;
798     if ( !defined($f) || $f->read(\$data, $BufSize) < 0 ) {
799     print(STDERR "Unable to open/read char/block special file"
800     . " $hdr->{fullPath}\n");
801     $f->close if ( defined($f) );
802     $ErrorCnt++;
803     return;
804     }
805     $f->close;
806     if ( $data =~ /(\d+),(\d+)/ ) {
807     $hdr->{devmajor} = $1;
808     $hdr->{devminor} = $2;
809     }
810     }
811     $hdr->{size} = 0;
812     TarWriteFileInfo($fh, $hdr);
813     $SpecialCnt++;
814     } else {
815     print(STDERR "Got unknown type $hdr->{type} for $hdr->{name}\n");
816     $ErrorCnt++;
817     }
818     }
819 dpavlin 112
820     my $t_fmt = '%Y-%m-%d %H:%M:%S';
821     sub curr_time {
822     return strftime($t_fmt,localtime());
823     }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26