/[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 297 - (hide annotations)
Thu Jan 26 17:13:55 2006 UTC (18 years, 3 months ago) by dpavlin
File size: 23683 byte(s)
correctly update parts = 0, inc_size = 0, inc_deleted = true for zero-sized increments

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26