1 |
#!/usr/bin/perl -w |
2 |
#============================================================= -*-perl-*- |
3 |
# |
4 |
# BackupPC_tarIncCreate: create a tar archive of an existing incremental dump |
5 |
# |
6 |
# |
7 |
# DESCRIPTION |
8 |
# |
9 |
# Usage: BackupPC_tarIncCreate [options] |
10 |
# |
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 |
# Ivan Klaric <iklaric@gmail.com> |
35 |
# Dobrica Pavlinusic <dpavlin@rot13.org> |
36 |
# |
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 |
use Time::HiRes qw/time/; |
74 |
use POSIX qw/strftime/; |
75 |
use File::Which; |
76 |
use File::Path; |
77 |
use File::Slurp; |
78 |
use Data::Dumper; ### FIXME |
79 |
|
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 |
%BackupPC::SearchLib::Conf = %Conf; |
85 |
my %opts; |
86 |
my $in_backup_increment; |
87 |
|
88 |
|
89 |
if ( !getopts("th:n:p:r:s:b:w:vdf", \%opts) ) { |
90 |
print STDERR <<EOF; |
91 |
usage: $0 [options] |
92 |
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 |
-f overwrite existing parts |
106 |
-v verbose output |
107 |
-d debug output |
108 |
EOF |
109 |
exit(1); |
110 |
} |
111 |
|
112 |
if ( $opts{h} !~ /^([\w\.\s-]+)$/ ) { |
113 |
die "$0: bad host name '$opts{h}'\n"; |
114 |
} |
115 |
my $Host = $opts{h}; |
116 |
|
117 |
if ( $opts{n} !~ /^(-?\d+)$/ ) { |
118 |
die "$0: bad dump number '$opts{n}'\n"; |
119 |
} |
120 |
my $Num = $opts{n}; |
121 |
|
122 |
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 |
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 |
my $current_tar_size = 0; |
134 |
my $total_increment_size = 0; |
135 |
|
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 |
die "$0: bad backup number $Num for host $Host\n"; |
143 |
} |
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 |
die "$0: bad share name '$opts{s}'\n"; |
149 |
} |
150 |
our $ShareName = $opts{s}; |
151 |
our $view = BackupPC::View->new($bpc, $Host, \@Backups); |
152 |
|
153 |
# 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 |
where id = ? |
166 |
}); |
167 |
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 |
# |
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 |
|
201 |
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 |
my $tar_path_final = $tar_dir . '/' . $tar_file; |
209 |
my $tar_path = $tar_path_final . '.tmp'; |
210 |
|
211 |
$tar_path =~ s#//#/#g; |
212 |
|
213 |
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 |
|
225 |
|
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 |
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 |
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 |
|
244 |
my $fh; |
245 |
my $part = 0; |
246 |
my $no_files = 0; |
247 |
my $items_in_part = 0; |
248 |
|
249 |
sub new_tar_part { |
250 |
my $arg = {@_}; |
251 |
|
252 |
if ($fh) { |
253 |
return if ($current_tar_size == 0); |
254 |
|
255 |
print STDERR "\n\t+ $part:"; |
256 |
|
257 |
# |
258 |
# Finish with two null 512 byte headers, |
259 |
# and then round out a full block. |
260 |
# |
261 |
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 |
|
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 |
print "$file, $size bytes, $items_in_part items"; |
275 |
|
276 |
$sth_backup_parts->execute( |
277 |
$backup_id, |
278 |
$part, |
279 |
$current_tar_size, |
280 |
$size, |
281 |
$md5, |
282 |
$items_in_part, |
283 |
); |
284 |
|
285 |
$total_increment_size += $size; |
286 |
|
287 |
if ($arg->{close}) { |
288 |
|
289 |
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 |
if ($part == 1) { |
296 |
print STDERR " single" if ($opts{v}); |
297 |
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 |
} else { |
301 |
print STDERR " [last]" if ($opts{v}); |
302 |
move("${tar_path}", "${tar_path_final}"); |
303 |
|
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 |
} |
310 |
|
311 |
$sth_inc_size->execute( |
312 |
$total_increment_size, |
313 |
$part, |
314 |
$backup_id |
315 |
); |
316 |
|
317 |
print "\n\ttotal $total_increment_size bytes"; |
318 |
|
319 |
return; |
320 |
} |
321 |
|
322 |
} |
323 |
|
324 |
$part++; |
325 |
|
326 |
# if this is first part, create directory |
327 |
|
328 |
if ($part == 1) { |
329 |
if (-e $tar_path) { |
330 |
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 |
|
335 |
sub abort_cleanup { |
336 |
print STDERR "ABORTED: cleanup temp dir "; |
337 |
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 |
} |
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 |
|
365 |
$current_tar_size = 0; |
366 |
$items_in_part = 0; |
367 |
} |
368 |
|
369 |
new_tar_part(); |
370 |
|
371 |
if (seedCache($Host, $ShareName, $Num)) { |
372 |
archiveWrite($fh, '/'); |
373 |
archiveWriteHardLinks($fh); |
374 |
new_tar_part( close => 1 ); |
375 |
} else { |
376 |
print STDERR "NOTE: no files found for $Host:$ShareName, increment $Num\n" if ($opts{v}); |
377 |
# remove temporary files if there are no files |
378 |
rmtree($tar_path); |
379 |
|
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 |
} |
387 |
|
388 |
# |
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 |
die "got errors or no files\n"; |
401 |
} |
402 |
|
403 |
$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 |
exit; |
410 |
|
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 |
|
475 |
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 |
|
483 |
# poor man's tell :-) |
484 |
$current_tar_size += length($$dataRef); |
485 |
|
486 |
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 |
die "Unable to write to output file ($!)\n"; |
497 |
} |
498 |
while ( $done + $WriteBufSz <= length($$dataRef) ) { |
499 |
if ( syswrite($fh, substr($$dataRef, $done, $WriteBufSz)) |
500 |
!= $WriteBufSz ) { |
501 |
die "Unable to write to output file ($!)\n"; |
502 |
} |
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 |
# seed cache of files in this increment |
602 |
# |
603 |
sub seedCache($$$) { |
604 |
my ($host, $share, $dumpNo) = @_; |
605 |
|
606 |
print STDERR curr_time(), "$host:$share #$dumpNo" if ($opts{v}); |
607 |
my $sql = q{ |
608 |
SELECT path,size |
609 |
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 |
|
615 |
my $sth = $dbh->prepare($sql); |
616 |
$sth->execute($host, $share, $dumpNo); |
617 |
my $count = $sth->rows; |
618 |
print STDERR " $count items, parts:" if ($opts{v}); |
619 |
while (my $row = $sth->fetchrow_arrayref) { |
620 |
#print STDERR "+ ", $row->[0],"\n"; |
621 |
$in_backup_increment->{ $row->[0] } = $row->[1]; |
622 |
} |
623 |
|
624 |
$sth->finish(); |
625 |
|
626 |
return $count; |
627 |
} |
628 |
|
629 |
# |
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 |
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 |
|
659 |
#print STDERR "? $tarPath\n" if ($opts{d}); |
660 |
my $size = $in_backup_increment->{$tarPath}; |
661 |
return unless (defined($size)); |
662 |
|
663 |
# is this file too large to fit into MaxArchiveFileSize? |
664 |
|
665 |
if ( ($current_tar_size + tar_overhead($tarPath) + $size) > $max_file_size ) { |
666 |
print STDERR "# tar file $current_tar_size + $tar_header_length + $size > $max_file_size, splitting\n" if ($opts{d}); |
667 |
new_tar_part(); |
668 |
} |
669 |
|
670 |
#print STDERR "A $tarPath [$size] tell: $current_tar_size\n" if ($opts{d}); |
671 |
$items_in_part++; |
672 |
|
673 |
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 |
TarWriteFileInfo($fh, $hdr); |
687 |
$DirCnt++; |
688 |
} 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 |
# 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 |
$FileCnt++; |
709 |
$ByteCnt += $size; |
710 |
} 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 |
|
745 |
$items_in_part++; |
746 |
} |
747 |
$f->close; |
748 |
$FileCnt++; |
749 |
$ByteCnt += $full_size; |
750 |
new_tar_part(); |
751 |
} |
752 |
} elsif ( $hdr->{type} == BPC_FTYPE_HARDLINK ) { |
753 |
# |
754 |
# Hardlink file: either write a hardlink or the complete file |
755 |
# depending upon whether the linked-to file will be written |
756 |
# to the archive. |
757 |
# |
758 |
# Start by reading the contents of the link. |
759 |
# |
760 |
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 |
$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 |
} 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 |
|
842 |
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
843 |
sub curr_time { |
844 |
return strftime($t_fmt,localtime()); |
845 |
} |