2 #============================================================= -*-perl-*-
4 # BackupPC_tarPCCopy: create a tar archive of the PC directory
5 # for copying the entire PC data directory. The archive will
6 # contain hardlinks to the pool directory, which should be copied
7 # before BackupPC_tarPCCopy is run.
9 # See the documentation for use.
13 # Usage: BackupPC_tarPCCopy [options] files/directories...
16 # -c don't cache inode data (reduces memory usage at the
17 # expense of longer run time)
20 # Craig Barratt <cbarratt@users.sourceforge.net>
23 # Copyright (C) 2005-2009 Craig Barratt
25 # This program is free software; you can redistribute it and/or modify
26 # it under the terms of the GNU General Public License as published by
27 # the Free Software Foundation; either version 2 of the License, or
28 # (at your option) any later version.
30 # This program is distributed in the hope that it will be useful,
31 # but WITHOUT ANY WARRANTY; without even the implied warranty of
32 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 # GNU General Public License for more details.
35 # You should have received a copy of the GNU General Public License
36 # along with this program; if not, write to the Free Software
37 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 #========================================================================
41 # Version 3.2.0, released 31 Jul 2010.
43 # See http://backuppc.sourceforge.net.
45 #========================================================================
49 use lib "/usr/local/BackupPC/lib";
55 use BackupPC::Attrib qw(:all);
56 use BackupPC::FileZIO;
59 use constant S_IFMT => 0170000; # type of file
61 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
62 my $TopDir = $bpc->TopDir();
63 my $BinDir = $bpc->BinDir();
64 my %Conf = $bpc->Conf();
68 if ( !getopts("c", \%opts) || @ARGV < 1 ) {
70 usage: $0 [options] files/directories...
72 -c don't cache inode data (reduces memory usage at the
73 expense of longer run time)
79 # This constant and the line of code below that uses it are borrowed
80 # from Archive::Tar. Thanks to Calle Dybedahl and Stephen Zander.
83 # Archive::Tar is Copyright 1997 Calle Dybedahl. All rights reserved.
84 # Copyright 1998 Stephen Zander. All rights reserved.
87 = 'a100 a8 a8 a8 a12 a12 A8 a1 a100 a6 a2 a32 a32 a8 a8 a155 x12';
88 my $tar_header_length = 512;
90 my $BufSize = 1048576; # 1MB or 2^20
92 my $WriteBufSz = ($opts{b} || 20) * $tar_header_length;
94 my(%UidCache, %GidCache);
96 my($ClientName, $ClientBackups, $ClientBkupNum, $ClientDirAttr, $ClientDir);
103 my $ClientBkupCompress = 1;
104 my $ClientBkupMangle = 1;
109 # Write out all the requested files/directories
118 my $path = shift(@ARGV);
120 if ( $path !~ m{^\Q$TopDir/\E} ) {
121 print STDERR "Argument $path must be an absolute path starting with $TopDir\n";
125 print STDERR "Argument $path does not exist\n";
129 find({wanted => sub { archiveFile($fh) } }, $path);
132 # To avoid using too much memory for the inode cache,
133 # remove it after each top-level directory is done.
140 print STDERR "Done $path ($argCnt of $argMax): $DirCnt dirs,"
141 . " $FileCnt files, $HLinkCnt hardlinks\n";
152 # Finish with two null 512 byte headers, and then round out a full
155 my $data = "\0" x ($tar_header_length * 2);
156 TarWrite($fh, \$data);
157 TarWrite($fh, undef);
161 # Got errors so exit with a non-zero status
163 print STDERR "Got $ErrorCnt warnings/errors\n";
168 ###########################################################################
170 ###########################################################################
180 # Default type - we'll update later if it is a symlink, hardlink etc
182 $hdr->{type} = -d _ ? BPC_FTYPE_DIR
183 : -f _ ? BPC_FTYPE_FILE
185 $hdr->{fullPath} = $File::Find::name;
186 $hdr->{inode} = $s[1];
187 $hdr->{nlink} = $s[3];
188 $hdr->{size} = $s[7];
189 $hdr->{devmajor} = $s[6] >> 8;
190 $hdr->{devminor} = $s[6] & 0xff;
193 $hdr->{mode} = $s[2];
194 $hdr->{mtime} = $s[9];
195 $hdr->{compress} = 1;
197 if ( $hdr->{fullPath} !~ m{\Q$TopDir\E/pc/(.*)} ) {
198 print STDERR "Can't extract TopDir ($TopDir) from"
199 . " $hdr->{fullPath}\n";
203 $hdr->{relPath} = $1;
204 if ( $hdr->{relPath} =~ m{(.*)/(.*)} ) {
207 $hdr->{name} = $hdr->{relPath};
210 if ( $hdr->{relPath} =~ m{(.*?)/} ) {
212 if ( $ClientName ne $clientName ) {
213 $ClientName = $clientName;
214 $ClientBackups = [ $bpc->BackupInfoRead($ClientName) ];
215 #print STDERR "Setting Client to $ClientName\n";
217 if ( $hdr->{relPath} =~ m{(.*?)/(\d+)/}
218 || $hdr->{relPath} =~ m{(.*?)/(\d+)$} ) {
220 if ( $ClientBkupNum != $backupNum ) {
222 $ClientBkupNum = $backupNum;
223 # print STDERR "Setting ClientBkupNum to $ClientBkupNum\n";
224 for ( $i = 0 ; $i < @$ClientBackups ; $i++ ) {
225 if ( $ClientBackups->[$i]{num} == $ClientBkupNum ) {
226 $ClientBkupCompress = $ClientBackups->[$i]{compress};
227 $ClientBkupMangle = $ClientBackups->[$i]{mangle};
228 # print STDERR "Setting $ClientBkupNum compress to $ClientBkupCompress, mangle to $ClientBkupMangle\n";
233 $hdr->{compress} = $ClientBkupCompress;
234 if ( $hdr->{type} == BPC_FTYPE_FILE && $hdr->{name} =~ /^f/ ) {
235 (my $dir = $hdr->{fullPath}) =~ s{(.*)/.*}{$1};
236 if ( $ClientDir ne $dir ) {
238 $ClientDirAttr = BackupPC::Attrib->new(
239 { compress => $ClientBkupCompress }
241 if ( -f $ClientDirAttr->fileName($dir)
242 && !$ClientDirAttr->read($dir) ) {
243 print STDERR "Can't read attrib file in $dir\n";
247 my $name = $hdr->{name};
248 $name = $bpc->fileNameUnmangle($name) if ( $ClientBkupMangle );
249 my $attr = $ClientDirAttr->get($name);
250 if ( defined($attr) ) {
251 $hdr->{type} = $attr->{type};
252 $hdr->{realSize} = $attr->{size}
253 if ( $attr->{type} == BPC_FTYPE_FILE );
255 #print STDERR "$hdr->{fullPath} has type $hdr->{type} and real size $hdr->{realSize}\n";
259 $hdr->{compress} = 0;
260 $hdr->{realSize} = $hdr->{size};
263 #print STDERR "$File::Find::name\n";
265 TarWriteFile($hdr, $fh);
272 $UidCache{$uid} = (getpwuid($uid))[0] if ( !exists($UidCache{$uid}) );
273 return $UidCache{$uid};
280 $GidCache{$gid} = (getgrgid($gid))[0] if ( !exists($GidCache{$gid}) );
281 return $GidCache{$gid};
286 my($fh, $dataRef) = @_;
288 if ( !defined($dataRef) ) {
290 # do flush by padding to a full $WriteBufSz
292 my $data = "\0" x ($WriteBufSz - length($WriteBuf));
295 if ( length($WriteBuf) + length($$dataRef) < $WriteBufSz ) {
297 # just buffer and return
299 $WriteBuf .= $$dataRef;
302 my $done = $WriteBufSz - length($WriteBuf);
303 if ( (my $n = syswrite($fh, $WriteBuf . substr($$dataRef, 0, $done)))
305 print(STDERR "Unable to write to output file ($!) ($n vs $WriteBufSz)\n");
308 while ( $done + $WriteBufSz <= length($$dataRef) ) {
309 if ( (my $n = syswrite($fh, substr($$dataRef, $done, $WriteBufSz)))
311 print(STDERR "Unable to write to output file ($!) ($n v $WriteBufSz)\n");
314 $done += $WriteBufSz;
316 $WriteBuf = substr($$dataRef, $done);
323 if ( $size % $tar_header_length ) {
324 my $data = "\0" x ($tar_header_length - ($size % $tar_header_length));
325 TarWrite($fh, \$data);
333 $hdr->{uname} = UidLookup($hdr->{uid}) if ( !defined($hdr->{uname}) );
334 $hdr->{gname} = GidLookup($hdr->{gid}) if ( !defined($hdr->{gname}) );
335 my $devmajor = defined($hdr->{devmajor}) ? sprintf("%07o", $hdr->{devmajor})
337 my $devminor = defined($hdr->{devminor}) ? sprintf("%07o", $hdr->{devminor})
340 if ( $hdr->{size} >= 2 * 65536 * 65536 ) {
342 # GNU extension for files >= 8GB: send size in big-endian binary
344 $sizeStr = pack("c4 N N", 0x80, 0, 0, 0,
345 $hdr->{size} / (65536 * 65536),
346 $hdr->{size} % (65536 * 65536));
347 } elsif ( $hdr->{size} >= 1 * 65536 * 65536 ) {
349 # sprintf octal only handles up to 2^32 - 1
351 $sizeStr = sprintf("%03o", $hdr->{size} / (1 << 24))
352 . sprintf("%08o", $hdr->{size} % (1 << 24));
354 $sizeStr = sprintf("%011o", $hdr->{size});
356 my $data = pack($tar_pack_header,
357 substr($hdr->{name}, 0, 99),
358 sprintf("%07o", $hdr->{mode}),
359 sprintf("%07o", $hdr->{uid}),
360 sprintf("%07o", $hdr->{gid}),
362 sprintf("%011o", $hdr->{mtime}),
363 "", #checksum field - space padded by pack("A8")
365 substr($hdr->{linkname}, 0, 99),
366 $hdr->{magic} || 'ustar ',
367 $hdr->{version} || ' ',
374 substr($data, 148, 7) = sprintf("%06o\0", unpack("%16C*",$data));
375 TarWrite($fh, \$data);
383 # Handle long link names (symbolic links)
385 if ( length($hdr->{linkname}) > 99 ) {
387 my $data = $hdr->{linkname} . "\0";
388 $h{name} = "././\@LongLink";
390 $h{size} = length($data);
391 TarWriteHeader($fh, \%h);
392 TarWrite($fh, \$data);
393 TarWritePad($fh, length($data));
396 # Handle long file names
398 if ( length($hdr->{name}) > 99 ) {
400 my $data = $hdr->{name} . "\0";
401 $h{name} = "././\@LongLink";
403 $h{size} = length($data);
404 TarWriteHeader($fh, \%h);
405 TarWrite($fh, \$data);
406 TarWritePad($fh, length($data));
408 TarWriteHeader($fh, $hdr);
418 my $tarPath = $hdr->{relPath};
420 $tarPath =~ s{//+}{/}g;
421 $tarPath = "./" . $tarPath if ( $tarPath !~ /^\.\// );
422 $tarPath =~ s{//+}{/}g;
423 $hdr->{name} = $tarPath;
425 if ( $hdr->{type} == BPC_FTYPE_DIR ) {
427 # Directory: just write the header
429 $hdr->{name} .= "/" if ( $hdr->{name} !~ m{/$} );
430 TarWriteFileInfo($fh, $hdr);
432 } elsif ( $hdr->{type} == BPC_FTYPE_FILE
433 || $hdr->{type} == BPC_FTYPE_HARDLINK
434 || $hdr->{type} == BPC_FTYPE_SYMLINK
435 || $hdr->{type} == BPC_FTYPE_CHARDEV
436 || $hdr->{type} == BPC_FTYPE_BLOCKDEV
437 || $hdr->{type} == BPC_FTYPE_FIFO
438 || $hdr->{type} == BPC_FTYPE_SOCKET ) {
440 # Underlying file is a regular file: write the header and file
442 my($data, $dataMD5, $size, $linkName);
444 if ( defined($Inode2Path{$hdr->{inode}}) ) {
445 $linkName = $Inode2Path{$hdr->{inode}};
446 #print STDERR "Got cache hit for $linkName\n";
448 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0,
450 if ( !defined($f) ) {
451 print(STDERR "Unable to open file $hdr->{fullPath}\n");
456 # Try to find the hardlink it points to by computing
457 # the pool file digest.
459 $f->read(\$dataMD5, $BufSize);
460 if ( !defined($hdr->{realSize}) ) {
462 # Need to get the real size
464 $size = length($dataMD5);
465 while ( $f->read(\$data, $BufSize) > 0 ) {
466 $size += length($data);
468 $hdr->{realSize} = $size;
471 my $md5 = Digest::MD5->new;
472 my $len = length($dataMD5);
473 if ( $hdr->{realSize} < 1048576
474 && length($dataMD5) != $hdr->{realSize} ) {
475 print(STDERR "File $hdr->{fullPath} has bad size"
476 . " (expect $hdr->{realSize}, got $len)\n");
478 my $digest = $bpc->Buffer2MD5($md5, $hdr->{realSize},
480 my $path = $bpc->MD52Path($digest, $hdr->{compress});
483 # print(STDERR "Looking up $hdr->{fullPath} at $path\n");
485 my $testPath = $path;
486 $testPath .= "_$i" if ( $i >= 0 );
487 last if ( !-f $testPath );
488 my $inode = (stat(_))[1];
489 if ( $inode == $hdr->{inode} ) {
491 # Found it! Just emit a tar hardlink
493 $testPath =~ s{\Q$TopDir\E}{..};
494 $linkName = $testPath;
501 if ( defined($linkName) ) {
502 $hdr->{type} = BPC_FTYPE_HARDLINK;
503 $hdr->{linkname} = $linkName;
504 TarWriteFileInfo($fh, $hdr);
506 #print STDERR "$hdr->{relPath} matches $testPath\n";
507 if ( !$opts{c} && $hdr->{nlink} > 2 ) {
509 # add it to the cache if there are more
510 # than 2 links (pool + current file),
511 # since there are more to go
513 $Inode2Path{$hdr->{inode}} = $linkName;
518 if ( $hdr->{nlink} > 1 ) {
519 print STDERR "Can't find $hdr->{relPath} in pool, will copy file\n";
522 $hdr->{type} = BPC_FTYPE_FILE;
524 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, 0);
525 if ( !defined($f) ) {
526 print(STDERR "Unable to open file $hdr->{fullPath}\n");
530 TarWriteFileInfo($fh, $hdr);
531 while ( $f->read(\$data, $BufSize) > 0 ) {
532 if ( $size + length($data) > $hdr->{size} ) {
533 print(STDERR "Error: truncating $hdr->{fullPath} to"
534 . " $hdr->{size} bytes\n");
535 $data = substr($data, 0, $hdr->{size} - $size);
538 TarWrite($fh, \$data);
539 $size += length($data);
542 if ( $size != $hdr->{size} ) {
543 print(STDERR "Error: padding $hdr->{fullPath} to $hdr->{size}"
544 . " bytes from $size bytes\n");
546 while ( $size < $hdr->{size} ) {
547 my $len = $hdr->{size} - $size;
548 $len = $BufSize if ( $len > $BufSize );
550 TarWrite($fh, \$data);
554 TarWritePad($fh, $size);
558 print(STDERR "Got unknown type $hdr->{type} for $hdr->{name}\n");