1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
|
#!/bin/perl
#============================================================= -*-perl-*-
#
# BackupPC_compressPool: Compress existing pool
#
# DESCRIPTION
#
# Usage: BackupPC_compressPool [-t] [-r] <host>
#
# Flags:
# -t test mode: do everything except actually replace the pool files.
# Useful for estimating total run time without making any real
# changes.
# -r read check: re-read the compressed file and compare it against
# the original uncompressed file. Can only be used in test mode.
# -c # number of children to fork. BackupPC_compressPool can take
# a long time to run, so to speed things up it spawns four children,
# each working on a different part of the pool. You can change
# the number of children with the -c option.
#
# BackupPC_compressPool is used to convert an uncompressed pool to
# a compressed pool. If BackupPC compression is enabled after
# uncompressed backups already exist, BackupPC_compressPool can
# be used to compress all the old uncompressed backups.
#
# It is important that BackupPC not run while BackupPC_compressPool
# runs. Also, BackupPC_compressPool must run to completion before
# BackupPC is restarted.
#
# AUTHOR
# Craig Barratt <cbarratt@users.sourceforge.net>
#
# COPYRIGHT
# Copyright (C) 2001-2003 Craig Barratt
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#========================================================================
#
# Version 2.1.1, released 13 Mar 2005.
#
# See http://backuppc.sourceforge.net.
#
#========================================================================
use strict;
no utf8;
use File::Find;
use File::Path;
use Compress::Zlib;
use Getopt::Std;
use lib "__INSTALLDIR__/lib";
use BackupPC::Lib;
use BackupPC::FileZIO;
die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
$bpc->ChildInit();
my $TopDir = $bpc->TopDir();
my $BinDir = $bpc->BinDir();
my %Conf = $bpc->Conf();
my $PoolDir = "$TopDir/pool";
my $CPoolDir = "$TopDir/cpool";
my $Compress = $Conf{CompressLevel};
my %opts;
my $SigName = "";
#
# Catch various signals
#
foreach my $sig ( qw(INT BUS SEGV PIPE TERM ALRM HUP) ) {
$SIG{$sig} = \&catch_signal;
}
$| = 1;
my $CompMaxRead = 131072; # 128K
my $CompMaxWrite = 6291456; # 6MB
if ( !getopts("trc:", \%opts) || @ARGV != 0 ) {
print("usage: $0 [-c nChild] [-r] [-t]\n");
exit(1);
}
my $TestMode = $opts{t};
my $ReadCheck = $opts{r};
my $nChild = $opts{c} || 4;
if ( $ReadCheck && !$TestMode ) {
print(STDERR "$0: -r (read check) option must have -t (test)\n");
exit(1);
}
if ( $nChild < 1 || $nChild >= 16 ) {
print(STDERR "$0: number of children (-c option) must be from 1 to 16\n");
exit(1);
}
if ( !BackupPC::FileZIO->compOk ) {
print STDERR <<EOF;
$0: Compress::Zlib is not installed. You need to install it
before running this script.
EOF
exit(1);
}
if ( $Compress <= 0 ) {
print STDERR <<EOF;
$0: compression is not enabled. \%Conf{CompressLevel} needs
to be set to a value from 1 to 9. Please edit the config.pl file and
re-start $0.
EOF
exit(1);
}
my $Errors = 0;
my $SubDirDone = 0;
my $SubDirCnt = 0;
my $SubDirCurr = 0;
my $FileCnt = 0;
my $FileOrigSz = 0;
my $FileCompressSz = 0;
my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort});
if ( $err eq "" ) {
print <<EOF;
BackupPC is running on $Conf{ServerHost}. You need to stop BackupPC
before you can upgrade the code. Depending upon your installation,
you could run "/etc/init.d/backuppc stop".
EOF
exit(1);
}
umask($Conf{UmaskMode});
sub cpoolFileName
{
my($new) = @_;
if ( $new !~ m{/(\w/\w/\w)/(\w{32})(_\d+)?$} ) {
print("Error: Can't parse filename from $new\n");
$Errors++;
return;
}
my $dir = "$CPoolDir/$1";
$new = "$dir/$2";
mkpath($dir, 0, 0777) if ( !-d $dir );
return $new if ( !-f $new );
for ( my $i = 0 ; ; $i++ ) {
return "${new}_$i" if ( !-f "${new}_$i" );
}
}
sub doCompress
{
my $file = ($File::Find::name =~ /(.*)/ && $1);
local(*FH, *OUT);
my(@s) = stat($file);
my($n, $dataIn, $dataOut, $flush, $copy);
if ( $SigName ) {
print("Child got signal $SigName; quitting\n");
reportStats();
exit(0);
}
return if ( !-f $file );
my $defl = deflateInit(
-Bufsize => 65536,
-Level => $Compress,
);
if ( !open(FH, $TestMode ? "<" : "+<", $file) ) {
print("Error: Can't open $file for read/write\n");
$Errors++;
return;
}
binmode(FH);
while ( sysread(FH, $dataIn, $CompMaxWrite) > 0 ) {
$flush = 0;
$FileOrigSz += length($dataIn);
my $fragOut = $defl->deflate($dataIn);
if ( length($fragOut) < $CompMaxRead ) {
#
# Compression is too high: to avoid huge memory requirements
# on read we need to flush().
#
$fragOut .= $defl->flush();
$flush = 1;
$defl = deflateInit(
-Bufsize => 65536,
-Level => $Compress,
);
}
$dataOut .= $fragOut;
if ( !$copy && length($dataOut) > $CompMaxWrite ) {
if ( !open(OUT, "+>", "$file.__z") ) {
print("Error: Can't open $file.__z for write\n");
$Errors++;
close(FH);
return;
}
binmode(OUT);
$copy = 1;
}
if ( $copy && $dataOut ne "" ) {
if ( syswrite(OUT, $dataOut) != length($dataOut) ) {
printf("Error: Can't write %d bytes to %s\n",
length($dataOut), "$file.__z");
$Errors++;
close(OUT);
close(FH);
unlink("$file.__z");
return;
}
$FileCompressSz += length($dataOut);
$dataOut = undef;
}
}
if ( !$flush ) {
$dataOut .= $defl->flush();
if ( $copy && $dataOut ne "" ) {
if ( syswrite(OUT, $dataOut) != length($dataOut) ) {
printf("Error: Can't write %d bytes to %s\n",
length($dataOut), "$file.__z");
$Errors++;
close(OUT);
close(FH);
unlink("$file.__z");
return;
}
$FileCompressSz += length($dataOut);
$dataOut = undef;
}
}
my $newFile = cpoolFileName($file);
if ( $TestMode ) {
close(FH);
if ( !open(FH, ">", $newFile) ) {
print("Error: Can't open $newFile for write\n");
$Errors++;
close(FH);
unlink("$file.__z");
return;
}
binmode(FH);
}
if ( $copy ) {
if ( !sysseek(OUT, 0, 0) ) {
print("Error: Can't seek $file.__z to 0\n");
$Errors++;
}
if ( !sysseek(FH, 0, 0) ) {
print("Error: Can't seek $newFile to 0\n");
$Errors++;
}
while ( sysread(OUT, $dataIn, $CompMaxWrite) > 0 ) {
if ( syswrite(FH, $dataIn) != length($dataIn) ) {
printf("Error: Can't write %d bytes to %s\n",
length($dataIn), $file);
$Errors++;
}
}
if ( !truncate(FH, sysseek(OUT, 0, 1)) ) {
printf("Error: Can't truncate %s to %d\n",
$file, sysseek(OUT, 0, 1));
$Errors++;
}
close(OUT);
close(FH);
unlink("$file.__z");
} else {
if ( !sysseek(FH, 0, 0) ) {
print("Error: Can't seek $file to 0\n");
$Errors++;
}
if ( syswrite(FH, $dataOut) != length($dataOut) ) {
printf("Error: Can't write %d bytes to %s\n",
length($dataOut), $file);
$Errors++;
}
$FileCompressSz += length($dataOut);
if ( !truncate(FH, length($dataOut)) ) {
printf("Error: Can't truncate %s to %d\n", $file, length($dataOut));
$Errors++;
}
close(FH);
}
if ( $TestMode ) {
if ( $ReadCheck ) {
checkRead($file, $newFile);
}
unlink($newFile);
} else {
rename($file, $newFile);
my $atime = $s[8] =~ /(.*)/ && $1;
my $mtime = $s[9] =~ /(.*)/ && $1;
utime($atime, $mtime, $newFile);
}
(my $dir = $file) =~ s{/[^/]*$}{};
$FileCnt++;
if ( $SubDirCurr ne "" && $SubDirCurr ne $dir ) {
$SubDirDone++;
$SubDirCurr = $dir;
reportStats();
} elsif ( $SubDirCurr eq "" ) {
$SubDirCurr = $dir;
}
}
sub reportStats
{
print("stats: $SubDirDone $SubDirCnt $FileCnt $FileOrigSz"
. " $FileCompressSz $Errors\n");
}
sub checkRead
{
my($file, $cfile) = @_;
return if ( !-f $file || !-f $cfile );
my $f = BackupPC::FileZIO->open($cfile, 0, $Compress)
|| die("can't open $cfile for read\n");
my($n, $nd, $r, $d, $d0);
local(*FH);
if ( !open(FH, "<", $file) ) {
print("can't open $file for check\n");
$Errors++;
$f->close();
return;
}
binmode(FH);
#print("comparing $file to $cfile\n");
while ( 1 ) {
$n = 1 + int(rand($CompMaxRead) + rand(100));
$r = $f->read(\$d, $n);
sysread(FH, $d0, $n);
if ( $d ne $d0 ) {
print("Botch read data on $cfile\n");
}
last if ( length($d) == 0 );
}
if ( ($r = $f->read(\$d, 100)) != 0 || ($r = $f->read(\$d, 100)) != 0 ) {
printf("Botch at EOF on $cfile got $r (%d,%d)\n",
sysseek(FH, 0, 1), $n);
$Errors++;
}
$f->close;
close(FH);
}
sub checkReadLine
{
my($file, $cfile) = @_;
return if ( !-f $file || !-f $cfile );
my $f = BackupPC::FileZIO->open($cfile, 0, $Compress)
|| die("can't open $cfile for read\n");
my($n, $nd, $r, $d, $d0);
local(*FH);
if ( !open(FH, "<", $file) ) {
print("can't open $file for check\n");
$Errors++;
$f->close();
return;
}
binmode(FH);
while ( 1 ) {
$d0 = <FH>;
$d = $f->readLine();
if ( $d ne $d0 ) {
print("Botch read data on $cfile\n");
}
last if ( length($d) == 0 );
}
if ( ($r = $f->read(\$d, 100)) != 0 || ($r = $f->read(\$d, 100)) != 0 ) {
printf("Botch at EOF on $cfile got $r (%d,%d)\n",
sysseek(FH, 0, 1), $n);
$Errors++;
}
$f->close;
close(FH);
}
sub catch_signal
{
$SigName = shift;
}
sub compressHostFiles
{
my($host) = @_;
my(@Files, @Backups, $fh, $data);
local(*FH);
if ( !defined($host) ) {
for ( my $i = 0 ; ; $i++ ) {
last if ( !-f "$TopDir/log/LOG.$i" );
push(@Files, "$TopDir/log/LOG.$i");
}
} else {
@Backups = $bpc->BackupInfoRead($host);
for ( my $i = 0 ; $i < @Backups ; $i++ ) {
next if ( $Backups[$i]{compress} );
push(@Files, "$TopDir/pc/$host/SmbLOG.$Backups[$i]{num}");
push(@Files, "$TopDir/pc/$host/XferLOG.$Backups[$i]{num}");
}
push(@Files, "$TopDir/pc/$host/SmbLOG.bad");
push(@Files, "$TopDir/pc/$host/XferLOG.bad");
for ( my $i = 0 ; ; $i++ ) {
last if ( !-f "$TopDir/pc/$host/LOG.$i" );
push(@Files, "$TopDir/pc/$host/LOG.$i");
}
}
foreach my $file ( @Files ) {
if ( $SigName ) {
print("Child got signal $SigName; quitting\n");
reportStats();
exit(0);
}
next if ( !-f $file );
if ( !BackupPC::FileZIO->compressCopy($file, "$file.z", undef,
$Compress, !$TestMode) ) {
print("compressCopy($file, $file.z, $Compress, !$TestMode)"
. " failed\n");
$Errors++;
} elsif ( $TestMode ) {
checkReadLine($file, "$file.z") if ( $ReadCheck );
unlink("$file.z");
}
}
}
sub updateHostBackupInfo
{
my($host) = @_;
if ( !$TestMode ) {
my @Backups = $bpc->BackupInfoRead($host);
for ( my $i = 0 ; $i < @Backups ; $i++ ) {
$Backups[$i]{compress} = $Compress;
}
$bpc->BackupInfoWrite($host, @Backups);
}
}
my @Dirs = split(//, "0123456789abcdef");
my @Hosts = sort(keys(%{$bpc->HostInfoRead()}));
my $FDread;
my @Jobs;
#
# First make sure there are no existing compressed backups
#
my(%compHosts, $compCnt);
for ( my $j = 0 ; $j < @Hosts ; $j++ ) {
my $host = $Hosts[$j];
my @Backups = $bpc->BackupInfoRead($host);
for ( my $i = 0 ; $i < @Backups ; $i++ ) {
next if ( !$Backups[$i]{compress} );
$compHosts{$host}++;
$compCnt++;
}
}
if ( $compCnt ) {
my $compHostStr = join("\n + ", sort(keys(%compHosts)));
print STDERR <<EOF;
BackupPC_compressPool: there are $compCnt compressed backups.
BackupPC_compressPool can only be run when there are no existing
compressed backups. The following hosts have compressed backups:
+ $compHostStr
If you really want to run BackupPC_compressPool you will need to remove
all the existing compressed backups (and /home/pcbackup/data/cpool).
Think carefully before you do this. Otherwise, you can just let new
compressed backups run and the old uncompressed backups and pool will
steadily expire.
EOF
exit(0);
}
#
# Next spawn $nChild children that actually do all the work.
#
for ( my $i = 0 ; $i < $nChild ; $i++ ) {
local(*CHILD);
my $pid;
if ( !defined($pid = open(CHILD, "-|")) ) {
print("Can't fork\n");
next;
}
my $nDirs = @Dirs / ($nChild - $i);
my $nHosts = @Hosts / ($nChild - $i);
if ( !$pid ) {
#
# This is the child.
# First process each of the hosts (compress per-pc log files etc).
#
for ( my $j = 0 ; $j < $nHosts ; $j++ ) {
compressHostFiles($Hosts[$j]);
}
#
# Count the total number of directories so we can estimate the
# completion time. We ignore empty directories by reading each
# directory and making sure it has at least 3 entries (ie, ".",
# ".." and a file).
#
for ( my $j = 0 ; $j < $nDirs ; $j++ ) {
my $thisDir = $Dirs[$j];
next if ( !-d "$PoolDir/$thisDir" );
foreach my $dir ( <$PoolDir/$thisDir/*/*> ) {
next if ( !opendir(DIR, $dir) );
my @files = readdir(DIR);
closedir(DIR);
$SubDirCnt++ if ( @files > 2 );
}
}
#
# Now process each of the directories
#
for ( my $j = 0 ; $j < $nDirs ; $j++ ) {
my $thisDir = shift(@Dirs);
next if ( !-d "$PoolDir/$thisDir" );
find({wanted => sub { doCompress($File::Find::name); },
no_chdir => 1}, "$PoolDir/$thisDir");
}
#
# Last, update the backup info file for each of the hosts
#
for ( my $j = 0 ; $j < $nHosts ; $j++ ) {
updateHostBackupInfo($Hosts[$j]);
}
$SubDirDone = $SubDirCnt;
reportStats();
exit(0);
}
#
# This is the parent. Peel off $nDirs directories, $nHosts hosts,
# and continue
#
$Jobs[$i]{fh} = *CHILD;
$Jobs[$i]{pid} = $pid;
vec($FDread, fileno($Jobs[$i]{fh}), 1) = 1;
splice(@Dirs, 0, $nDirs);
splice(@Hosts, 0, $nHosts);
}
#
# compress the main log files (in the parents)
#
compressHostFiles(undef);
#
# Now wait for all the children to report results and finish up
#
my $TimeStart = time;
my $DonePct = 0;
my $GotSignal = "";
while ( $FDread !~ /^\0*$/ ) {
my $ein = $FDread;
select(my $rout = $FDread, undef, $ein, undef);
if ( $SigName ne $GotSignal ) {
print("Got signal $SigName; waiting for $nChild children to cleanup\n");
$GotSignal = $SigName;
}
for ( my $i = 0 ; $i < $nChild ; $i++ ) {
next if ( !vec($rout, fileno($Jobs[$i]{fh}), 1) );
my $data;
if ( sysread($Jobs[$i]{fh}, $data, 1024) <= 0 ) {
vec($FDread, fileno($Jobs[$i]{fh}), 1) = 0;
close($Jobs[$i]{fh});
next;
}
$Jobs[$i]{mesg} .= $data;
while ( $Jobs[$i]{mesg} =~ /(.*?)[\n\r]+(.*)/s ) {
my $mesg = $1;
$Jobs[$i]{mesg} = $2;
if ( $mesg =~ /^stats: (\d+) (\d+) (\d+) (\d+) (\d+) (\d+)/ ) {
$Jobs[$i]{SubDirDone} = $1;
$Jobs[$i]{SubDirCnt} = $2;
$Jobs[$i]{FileCnt} = $3;
$Jobs[$i]{FileOrigSz} = $4;
$Jobs[$i]{FileCompressSz} = $5;
$Jobs[$i]{Errors} = $6;
$SubDirDone = $SubDirCnt = $FileCnt = $FileOrigSz = 0;
$FileCompressSz = $Errors = 0;
my $numReports = 0;
for ( my $j = 0 ; $j < $nChild ; $j++ ) {
next if ( !defined($Jobs[$j]{SubDirDone}) );
$SubDirDone += $Jobs[$j]{SubDirDone};
$SubDirCnt += $Jobs[$j]{SubDirCnt};
$FileCnt += $Jobs[$j]{FileCnt};
$FileOrigSz += $Jobs[$j]{FileOrigSz};
$FileCompressSz += $Jobs[$j]{FileCompressSz};
$Errors += $Jobs[$j]{Errors};
$numReports++;
}
$SubDirCnt ||= 1;
$FileOrigSz ||= 1;
my $pctDone = 100 * $SubDirDone / $SubDirCnt;
if ( $numReports == $nChild && $pctDone >= $DonePct + 1 ) {
$DonePct = int($pctDone);
my $estSecLeft = 1.2 * (time - $TimeStart)
* (100 / $pctDone - 1);
my $timeStamp = $bpc->timeStamp;
printf("%sDone %2.0f%% (%d of %d dirs, %d files,"
. " %.2fGB raw, %.1f%% reduce, %d errors)\n",
$timeStamp,
$pctDone, $SubDirDone, $SubDirCnt, $FileCnt,
$FileOrigSz / (1024 * 1024 * 1000),
100 * (1 - $FileCompressSz / $FileOrigSz));
printf("%s Est complete in %.1f hours (around %s)\n",
$timeStamp, $estSecLeft / 3600,
$bpc->timeStamp(time + $estSecLeft, 1))
if ( $DonePct < 100 );
}
} else {
print($mesg, "\n");
}
}
}
}
if ( $Errors ) {
print("Finished with $Errors errors!!!!\n");
exit(1);
}
|