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.
11
# Usage: BackupPC_tarPCCopy [options] files/directories...
14
# -c don't cache inode data (reduces memory usage at the
15
# expense of longer run time)
18
# Craig Barratt <cbarratt@users.sourceforge.net>
21
# Copyright (C) 2005 Craig Barratt
23
# This program is free software; you can redistribute it and/or modify
24
# it under the terms of the GNU General Public License as published by
25
# the Free Software Foundation; either version 2 of the License, or
26
# (at your option) any later version.
28
# This program is distributed in the hope that it will be useful,
29
# but WITHOUT ANY WARRANTY; without even the implied warranty of
30
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31
# GNU General Public License for more details.
33
# You should have received a copy of the GNU General Public License
34
# along with this program; if not, write to the Free Software
35
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37
#========================================================================
39
# Version 3.0.0, released 28 Jan 2007.
41
# See http://backuppc.sourceforge.net.
43
#========================================================================
47
use lib "__INSTALLDIR__/lib";
53
use BackupPC::Attrib qw(:all);
54
use BackupPC::FileZIO;
57
use constant S_IFMT => 0170000; # type of file
59
die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
60
my $TopDir = $bpc->TopDir();
61
my $BinDir = $bpc->BinDir();
62
my %Conf = $bpc->Conf();
66
if ( !getopts("c", \%opts) || @ARGV < 1 ) {
68
usage: $0 [options] files/directories...
70
-c don't cache inode data (reduces memory usage at the
71
expense of longer run time)
77
# This constant and the line of code below that uses it are borrowed
78
# from Archive::Tar. Thanks to Calle Dybedahl and Stephen Zander.
81
# Archive::Tar is Copyright 1997 Calle Dybedahl. All rights reserved.
82
# Copyright 1998 Stephen Zander. All rights reserved.
85
= 'a100 a8 a8 a8 a12 a12 A8 a1 a100 a6 a2 a32 a32 a8 a8 a155 x12';
86
my $tar_header_length = 512;
88
my $BufSize = 1048576; # 1MB or 2^20
90
my $WriteBufSz = ($opts{b} || 20) * $tar_header_length;
92
my(%UidCache, %GidCache);
94
my($ClientName, $ClientBackups, $ClientBkupNum, $ClientDirAttr, $ClientDir);
101
my $ClientBkupCompress = 1;
102
my $ClientBkupMangle = 1;
107
# Write out all the requested files/directories
116
my $path = shift(@ARGV);
118
if ( $path !~ m{^\Q$TopDir/\E} ) {
119
print STDERR "Argument $path must be an absolute path starting with $TopDir\n";
123
print STDERR "Argument $path does not exist\n";
127
find({wanted => sub { archiveFile($fh) } }, $path);
130
# To avoid using too much memory for the inode cache,
131
# remove it after each top-level directory is done.
138
print STDERR "Done $path ($argCnt of $argMax): $DirCnt dirs,"
139
. " $FileCnt files, $HLinkCnt hardlinks\n";
150
# Finish with two null 512 byte headers, and then round out a full
153
my $data = "\0" x ($tar_header_length * 2);
154
TarWrite($fh, \$data);
155
TarWrite($fh, undef);
159
# Got errors so exit with a non-zero status
161
print STDERR "Got $ErrorCnt warnings/errors\n";
166
###########################################################################
168
###########################################################################
178
# Default type - we'll update later if it is a symlink, hardlink etc
180
$hdr->{type} = -d _ ? BPC_FTYPE_DIR
181
: -f _ ? BPC_FTYPE_FILE
183
$hdr->{fullPath} = $File::Find::name;
184
$hdr->{inode} = $s[1];
185
$hdr->{nlink} = $s[3];
186
$hdr->{size} = $s[7];
187
$hdr->{devmajor} = $s[6] >> 8;
188
$hdr->{devminor} = $s[6] & 0xff;
191
$hdr->{mode} = $s[2];
192
$hdr->{mtime} = $s[9];
193
$hdr->{compress} = 1;
195
if ( $hdr->{fullPath} !~ m{\Q$TopDir\E/pc/(.*)} ) {
196
print STDERR "Can't extract TopDir ($TopDir) from"
197
. " $hdr->{fullPath}\n";
201
$hdr->{relPath} = $1;
202
if ( $hdr->{relPath} =~ m{(.*)/(.*)} ) {
205
$hdr->{name} = $hdr->{relPath};
208
if ( $hdr->{relPath} =~ m{(.*?)/} ) {
210
if ( $ClientName ne $clientName ) {
211
$ClientName = $clientName;
212
$ClientBackups = [ $bpc->BackupInfoRead($ClientName) ];
213
#print STDERR "Setting Client to $ClientName\n";
215
if ( $hdr->{relPath} =~ m{(.*?)/(\d+)/}
216
|| $hdr->{relPath} =~ m{(.*?)/(\d+)$} ) {
218
if ( $ClientBkupNum != $backupNum ) {
220
$ClientBkupNum = $backupNum;
221
# print STDERR "Setting ClientBkupNum to $ClientBkupNum\n";
222
for ( $i = 0 ; $i < @$ClientBackups ; $i++ ) {
223
if ( $ClientBackups->[$i]{num} == $ClientBkupNum ) {
224
$ClientBkupCompress = $ClientBackups->[$i]{compress};
225
$ClientBkupMangle = $ClientBackups->[$i]{mangle};
226
# print STDERR "Setting $ClientBkupNum compress to $ClientBkupCompress, mangle to $ClientBkupMangle\n";
231
$hdr->{compress} = $ClientBkupCompress;
232
if ( $hdr->{type} == BPC_FTYPE_FILE && $hdr->{name} =~ /^f/ ) {
233
(my $dir = $hdr->{fullPath}) =~ s{(.*)/.*}{$1};
234
if ( $ClientDir ne $dir ) {
236
$ClientDirAttr = BackupPC::Attrib->new(
237
{ compress => $ClientBkupCompress }
239
if ( -f $ClientDirAttr->fileName($dir)
240
&& !$ClientDirAttr->read($dir) ) {
241
print STDERR "Can't read attrib file in $dir\n";
245
my $name = $hdr->{name};
246
$name = $bpc->fileNameUnmangle($name) if ( $ClientBkupMangle );
247
my $attr = $ClientDirAttr->get($name);
248
if ( defined($attr) ) {
249
$hdr->{type} = $attr->{type};
250
$hdr->{realSize} = $attr->{size}
251
if ( $attr->{type} == BPC_FTYPE_FILE );
253
#print STDERR "$hdr->{fullPath} has type $hdr->{type} and real size $hdr->{realSize}\n";
257
$hdr->{compress} = 0;
258
$hdr->{realSize} = $hdr->{size};
261
#print STDERR "$File::Find::name\n";
263
TarWriteFile($hdr, $fh);
270
$UidCache{$uid} = (getpwuid($uid))[0] if ( !exists($UidCache{$uid}) );
271
return $UidCache{$uid};
278
$GidCache{$gid} = (getgrgid($gid))[0] if ( !exists($GidCache{$gid}) );
279
return $GidCache{$gid};
284
my($fh, $dataRef) = @_;
286
if ( !defined($dataRef) ) {
288
# do flush by padding to a full $WriteBufSz
290
my $data = "\0" x ($WriteBufSz - length($WriteBuf));
293
if ( length($WriteBuf) + length($$dataRef) < $WriteBufSz ) {
295
# just buffer and return
297
$WriteBuf .= $$dataRef;
300
my $done = $WriteBufSz - length($WriteBuf);
301
if ( (my $n = syswrite($fh, $WriteBuf . substr($$dataRef, 0, $done)))
303
print(STDERR "Unable to write to output file ($!) ($n vs $WriteBufSz)\n");
306
while ( $done + $WriteBufSz <= length($$dataRef) ) {
307
if ( (my $n = syswrite($fh, substr($$dataRef, $done, $WriteBufSz)))
309
print(STDERR "Unable to write to output file ($!) ($n v $WriteBufSz)\n");
312
$done += $WriteBufSz;
314
$WriteBuf = substr($$dataRef, $done);
321
if ( $size % $tar_header_length ) {
322
my $data = "\0" x ($tar_header_length - ($size % $tar_header_length));
323
TarWrite($fh, \$data);
331
$hdr->{uname} = UidLookup($hdr->{uid}) if ( !defined($hdr->{uname}) );
332
$hdr->{gname} = GidLookup($hdr->{gid}) if ( !defined($hdr->{gname}) );
333
my $devmajor = defined($hdr->{devmajor}) ? sprintf("%07o", $hdr->{devmajor})
335
my $devminor = defined($hdr->{devminor}) ? sprintf("%07o", $hdr->{devminor})
338
if ( $hdr->{size} >= 2 * 65536 * 65536 ) {
340
# GNU extension for files >= 8GB: send size in big-endian binary
342
$sizeStr = pack("c4 N N", 0x80, 0, 0, 0,
343
$hdr->{size} / (65536 * 65536),
344
$hdr->{size} % (65536 * 65536));
345
} elsif ( $hdr->{size} >= 1 * 65536 * 65536 ) {
347
# sprintf octal only handles up to 2^32 - 1
349
$sizeStr = sprintf("%03o", $hdr->{size} / (1 << 24))
350
. sprintf("%08o", $hdr->{size} % (1 << 24));
352
$sizeStr = sprintf("%011o", $hdr->{size});
354
my $data = pack($tar_pack_header,
355
substr($hdr->{name}, 0, 99),
356
sprintf("%07o", $hdr->{mode}),
357
sprintf("%07o", $hdr->{uid}),
358
sprintf("%07o", $hdr->{gid}),
360
sprintf("%011o", $hdr->{mtime}),
361
"", #checksum field - space padded by pack("A8")
363
substr($hdr->{linkname}, 0, 99),
364
$hdr->{magic} || 'ustar ',
365
$hdr->{version} || ' ',
372
substr($data, 148, 7) = sprintf("%06o\0", unpack("%16C*",$data));
373
TarWrite($fh, \$data);
381
# Handle long link names (symbolic links)
383
if ( length($hdr->{linkname}) > 99 ) {
385
my $data = $hdr->{linkname} . "\0";
386
$h{name} = "././\@LongLink";
388
$h{size} = length($data);
389
TarWriteHeader($fh, \%h);
390
TarWrite($fh, \$data);
391
TarWritePad($fh, length($data));
394
# Handle long file names
396
if ( length($hdr->{name}) > 99 ) {
398
my $data = $hdr->{name} . "\0";
399
$h{name} = "././\@LongLink";
401
$h{size} = length($data);
402
TarWriteHeader($fh, \%h);
403
TarWrite($fh, \$data);
404
TarWritePad($fh, length($data));
406
TarWriteHeader($fh, $hdr);
416
my $tarPath = $hdr->{relPath};
418
$tarPath =~ s{//+}{/}g;
419
$tarPath = "./" . $tarPath if ( $tarPath !~ /^\.\// );
420
$tarPath =~ s{//+}{/}g;
421
$hdr->{name} = $tarPath;
423
if ( $hdr->{type} == BPC_FTYPE_DIR ) {
425
# Directory: just write the header
427
$hdr->{name} .= "/" if ( $hdr->{name} !~ m{/$} );
428
TarWriteFileInfo($fh, $hdr);
430
} elsif ( $hdr->{type} == BPC_FTYPE_FILE
431
|| $hdr->{type} == BPC_FTYPE_HARDLINK
432
|| $hdr->{type} == BPC_FTYPE_SYMLINK
433
|| $hdr->{type} == BPC_FTYPE_CHARDEV
434
|| $hdr->{type} == BPC_FTYPE_BLOCKDEV
435
|| $hdr->{type} == BPC_FTYPE_FIFO
436
|| $hdr->{type} == BPC_FTYPE_SOCKET ) {
438
# Underlying file is a regular file: write the header and file
440
my($data, $dataMD5, $size, $linkName);
442
if ( defined($Inode2Path{$hdr->{inode}}) ) {
443
$linkName = $Inode2Path{$hdr->{inode}};
444
#print STDERR "Got cache hit for $linkName\n";
446
my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0,
448
if ( !defined($f) ) {
449
print(STDERR "Unable to open file $hdr->{fullPath}\n");
454
# Try to find the hardlink it points to by computing
455
# the pool file digest.
457
$f->read(\$dataMD5, $BufSize);
458
if ( !defined($hdr->{realSize}) ) {
460
# Need to get the real size
462
$size = length($dataMD5);
463
while ( $f->read(\$data, $BufSize) > 0 ) {
464
$size += length($data);
466
$hdr->{realSize} = $size;
469
my $md5 = Digest::MD5->new;
470
my $len = length($dataMD5);
471
if ( $hdr->{realSize} < 1048576
472
&& length($dataMD5) != $hdr->{realSize} ) {
473
print(STDERR "File $hdr->{fullPath} has bad size"
474
. " (expect $hdr->{realSize}, got $len)\n");
476
my $digest = $bpc->Buffer2MD5($md5, $hdr->{realSize},
478
my $path = $bpc->MD52Path($digest, $hdr->{compress});
481
# print(STDERR "Looking up $hdr->{fullPath} at $path\n");
483
my $testPath = $path;
484
$testPath .= "_$i" if ( $i >= 0 );
485
last if ( !-f $testPath );
486
my $inode = (stat(_))[1];
487
if ( $inode == $hdr->{inode} ) {
489
# Found it! Just emit a tar hardlink
491
$testPath =~ s{\Q$TopDir\E}{..};
492
$linkName = $testPath;
499
if ( defined($linkName) ) {
500
$hdr->{type} = BPC_FTYPE_HARDLINK;
501
$hdr->{linkname} = $linkName;
502
TarWriteFileInfo($fh, $hdr);
504
#print STDERR "$hdr->{relPath} matches $testPath\n";
505
if ( !$opts{c} && $hdr->{nlink} > 2 ) {
507
# add it to the cache if there are more
508
# than 2 links (pool + current file),
509
# since there are more to go
511
$Inode2Path{$hdr->{inode}} = $linkName;
516
if ( $hdr->{nlink} > 1 ) {
517
print STDERR "Can't find $hdr->{relPath} in pool, will copy file\n";
520
$hdr->{type} = BPC_FTYPE_FILE;
522
my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, 0);
523
if ( !defined($f) ) {
524
print(STDERR "Unable to open file $hdr->{fullPath}\n");
528
TarWriteFileInfo($fh, $hdr);
529
while ( $f->read(\$data, $BufSize) > 0 ) {
530
if ( $size + length($data) > $hdr->{size} ) {
531
print(STDERR "Error: truncating $hdr->{fullPath} to"
532
. " $hdr->{size} bytes\n");
533
$data = substr($data, 0, $hdr->{size} - $size);
536
TarWrite($fh, \$data);
537
$size += length($data);
540
if ( $size != $hdr->{size} ) {
541
print(STDERR "Error: padding $hdr->{fullPath} to $hdr->{size}"
542
. " bytes from $size bytes\n");
544
while ( $size < $hdr->{size} ) {
545
my $len = $hdr->{size} - $size;
546
$len = $BufSize if ( $len > $BufSize );
548
TarWrite($fh, \$data);
552
TarWritePad($fh, $size);
556
print(STDERR "Got unknown type $hdr->{type} for $hdr->{name}\n");