X-Git-Url: http://git.rot13.org/?p=BackupPC.git;a=blobdiff_plain;f=bin%2FBackupPC_dump;h=0971f9ec71f51be4339592e6b065ca2420a938c7;hp=f47b6e3a505fc8a7a1d749be49778732d5e3a876;hb=f076585d3ff9dfe6de32292b897e293008efe74e;hpb=df717077755b9331fa245f72933ef03add7a3710 diff --git a/bin/BackupPC_dump b/bin/BackupPC_dump index f47b6e3..0971f9e 100755 --- a/bin/BackupPC_dump +++ b/bin/BackupPC_dump @@ -52,7 +52,7 @@ # Craig Barratt # # COPYRIGHT -# Copyright (C) 2001-2003 Craig Barratt +# Copyright (C) 2001-2007 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 @@ -70,7 +70,7 @@ # #======================================================================== # -# Version 3.0.0beta1, released 30 Jul 2006. +# Version 3.2.0, released 31 Dec 2008. # # See http://backuppc.sourceforge.net. # @@ -82,10 +82,7 @@ use lib "/usr/local/BackupPC/lib"; use BackupPC::Lib; use BackupPC::FileZIO; use BackupPC::Storage; -use BackupPC::Xfer::Smb; -use BackupPC::Xfer::Tar; -use BackupPC::Xfer::Rsync; -use BackupPC::Xfer::BackupPCd; +use BackupPC::Xfer; use Encode; use Socket; use File::Path; @@ -237,7 +234,7 @@ if ($Conf{XferMethod} eq "archive" ) { exit(0); } -if ( !$opts{d} ) { +if ( !$opts{d} && !$Conf{BackupsDisable} ) { # # In the non-DHCP case, make sure the host can be looked up # via NS, or otherwise find the IP address via NetBios. @@ -303,6 +300,7 @@ my $lastFullTime = 0; my $lastIncrTime = 0; my $partialIdx = -1; my $partialNum; +my $partialFileCnt; my $lastBkupNum; my $lastPartial = 0; @@ -388,7 +386,6 @@ my(@lastIdxByLevel, $incrCntSinceFull); # as a starting point for an incremental. # @Backups = $bpc->BackupInfoRead($client); -## @Backups = sort( { $a->{startTime} <=> $b->{startTime} }, @Backups); for ( my $i = 0 ; $i < @Backups ; $i++ ) { $needLink = 1 if ( $Backups[$i]{nFilesNew} eq "" || -f "$Dir/NewFileList.$Backups[$i]{num}" ); @@ -406,9 +403,10 @@ for ( my $i = 0 ; $i < @Backups ; $i++ ) { $lastIncrTime = $Backups[$i]{startTime} if ( $lastIncrTime < $Backups[$i]{startTime} ); } elsif ( $Backups[$i]{type} eq "partial" ) { - $partialIdx = $i; - $lastPartial = $Backups[$i]{startTime}; - $partialNum = $Backups[$i]{num}; + $partialIdx = $i; + $lastPartial = $Backups[$i]{startTime}; + $partialNum = $Backups[$i]{num}; + $partialFileCnt = $Backups[$i]{nFiles}; } } @@ -457,6 +455,41 @@ if ( @Backups == 0 NothingToDo($needLink); } +# +# Create top-level directories if they don't exist +# +foreach my $dir ( ( + "$Conf{TopDir}", + "$Conf{TopDir}/pool", + "$Conf{TopDir}/cpool", + "$Conf{TopDir}/pc", + "$Conf{TopDir}/trash", + ) ) { + next if ( -d $dir ); + mkpath($dir, 0, 0750); + if ( !-d $dir ) { + print("Failed to create $dir\n"); + printf(LOG "%sFailed to create directory %s\n", $bpc->timeStamp, $dir); + print("link $clientURI\n") if ( $needLink ); + exit(1); + } else { + printf(LOG "%sCreated directory %s\n", $bpc->timeStamp, $dir); + } +} + +if ( !$bpc->HardlinkTest($Dir, "$TopDir/cpool") ) { + print(LOG $bpc->timeStamp, "Can't create a test hardlink between a file" + . " in $Dir and $TopDir/cpool. Either these are different" + . " file systems, or this file system doesn't support hardlinks," + . " or these directories don't exist, or there is a permissions" + . " problem, or the file system is out of inodes or full. Use" + . " df, df -i, and ls -ld to check each of these possibilities." + . " Quitting...\n"); + print("test hardlink between $Dir and $TopDir/cpool failed\n"); + print("link $clientURI\n") if ( $needLink ); + exit(1); +} + # # Check if $host is alive # @@ -552,17 +585,7 @@ my $sizeTotal = 0; my($logMsg, %stat, $xfer, $ShareNames, $noFilesErr); my $newFilesFH; -if ( $Conf{XferMethod} eq "tar" ) { - $ShareNames = $Conf{TarShareName}; -} elsif ( $Conf{XferMethod} eq "rsync" || $Conf{XferMethod} eq "rsyncd" ) { - $ShareNames = $Conf{RsyncShareName}; -} elsif ( $Conf{XferMethod} eq "backuppcd" ) { - $ShareNames = $Conf{BackupPCdShareName}; -} else { - $ShareNames = $Conf{SmbShareName}; -} - -$ShareNames = [ $ShareNames ] unless ref($ShareNames) eq "ARRAY"; +$ShareNames = BackupPC::Xfer::getShareNames(\%Conf); # # Run an optional pre-dump command @@ -603,40 +626,14 @@ for my $shareName ( @$ShareNames ) { exit(1); } - if ( $Conf{XferMethod} eq "tar" ) { - # - # Use tar (eg: tar/ssh) as the transport program. - # - $xfer = BackupPC::Xfer::Tar->new($bpc); - } elsif ( $Conf{XferMethod} eq "rsync" || $Conf{XferMethod} eq "rsyncd" ) { - # - # Use rsync as the transport program. - # - if ( !defined($xfer = BackupPC::Xfer::Rsync->new($bpc)) ) { - my $errStr = BackupPC::Xfer::Rsync::errStr; - print(LOG $bpc->timeStamp, "dump failed: $errStr\n"); - print("dump failed: $errStr\n"); - UserCommandRun("DumpPostShareCmd", $shareName) if ( $NeedPostCmd ); - UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd ); - exit(1); - } - } elsif ( $Conf{XferMethod} eq "backuppcd" ) { - # - # Use backuppcd as the transport program. - # - if ( !defined($xfer = BackupPC::Xfer::BackupPCd->new($bpc)) ) { - my $errStr = BackupPC::Xfer::BackupPCd::errStr; - print(LOG $bpc->timeStamp, "dump failed: $errStr\n"); - print("dump failed: $errStr\n"); - UserCommandRun("DumpPostShareCmd", $shareName) if ( $NeedPostCmd ); - UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd ); - exit(1); - } - } else { - # - # Default is to use smbclient (smb) as the transport program. - # - $xfer = BackupPC::Xfer::Smb->new($bpc); + $xfer = BackupPC::Xfer::create($Conf{XferMethod}, $bpc); + if ( !defined($xfer) ) { + my $errStr = BackupPC::Xfer::errStr(); + print(LOG $bpc->timeStamp, "dump failed: $errStr\n"); + print("dump failed: $errStr\n"); + UserCommandRun("DumpPostShareCmd", $shareName) if ( $NeedPostCmd ); + UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd ); + exit(1); } my $useTar = $xfer->useTar; @@ -775,43 +772,41 @@ for my $shareName ( @$ShareNames ) { # we use a select. # my($FDread, $tarOut, $mesg); - vec($FDread, fileno(TAR), 1) = 1 if ( $useTar ); + vec($FDread, fileno(TAR), 1) = 1; $xfer->setSelectMask(\$FDread); SCAN: while ( 1 ) { my $ein = $FDread; last if ( $FDread =~ /^\0*$/ ); select(my $rout = $FDread, undef, $ein, undef); - if ( $useTar ) { - if ( vec($rout, fileno(TAR), 1) ) { - if ( sysread(TAR, $mesg, 8192) <= 0 ) { - vec($FDread, fileno(TAR), 1) = 0; - close(TAR); - } else { - $tarOut .= $mesg; - } - } - while ( $tarOut =~ /(.*?)[\n\r]+(.*)/s ) { - $_ = $1; - $tarOut = $2; - if ( /^ / ) { - $XferLOG->write(\"$_\n"); - } else { - $XferLOG->write(\"tarExtract: $_\n"); - } - if ( /^BackupPC_tarExtact aborting \((.*)\)/ ) { - $stat{hostError} = $1; - } - if ( /^Done: (\d+) errors, (\d+) filesExist, (\d+) sizeExist, (\d+) sizeExistComp, (\d+) filesTotal, (\d+) sizeTotal/ ) { - $tarErrs += $1; - $nFilesExist += $2; - $sizeExist += $3; - $sizeExistComp += $4; - $nFilesTotal += $5; - $sizeTotal += $6; - } - } - } + if ( vec($rout, fileno(TAR), 1) ) { + if ( sysread(TAR, $mesg, 8192) <= 0 ) { + vec($FDread, fileno(TAR), 1) = 0; + close(TAR); + } else { + $tarOut .= $mesg; + } + } + while ( $tarOut =~ /(.*?)[\n\r]+(.*)/s ) { + $_ = $1; + $tarOut = $2; + if ( /^ / ) { + $XferLOG->write(\"$_\n"); + } else { + $XferLOG->write(\"tarExtract: $_\n"); + } + if ( /^BackupPC_tarExtact aborting \((.*)\)/ ) { + $stat{hostError} = $1; + } + if ( /^Done: (\d+) errors, (\d+) filesExist, (\d+) sizeExist, (\d+) sizeExistComp, (\d+) filesTotal, (\d+) sizeTotal/ ) { + $tarErrs += $1; + $nFilesExist += $2; + $sizeExist += $3; + $sizeExistComp += $4; + $nFilesTotal += $5; + $sizeTotal += $6; + } + } last if ( !$xfer->readOutput(\$FDread, $rout) ); while ( my $str = $xfer->logMsgGet ) { print(LOG $bpc->timeStamp, "xfer: $str\n"); @@ -875,7 +870,7 @@ for my $shareName ( @$ShareNames ) { $stat{xferOK} = 0 if ( $stat{hostError} || $stat{hostAbort} ); if ( !$stat{xferOK} ) { # - # kill off the tranfer program, first nicely then forcefully + # kill off the transfer program, first nicely then forcefully # if ( @xferPid ) { kill($bpc->sigName2num("INT"), @xferPid); @@ -908,6 +903,15 @@ if ( $type eq "full" && $stat{hostError} eq "" $stat{xferOK} = 0 if ( $Abort ); +# +# If there is no "new" directory then the backup is bad +# +if ( $stat{xferOK} && !-d "$Dir/new" ) { + $stat{hostError} = "No backup directory $Dir/new" + if ( $stat{hostError} eq "" ); + $stat{xferOK} = 0; +} + # # Do one last check to make sure it is still the machine we expect. # @@ -964,6 +968,11 @@ my $newNum = BackupSave(); my $otherCount = $stat{xferErrCnt} - $stat{xferBadFileCnt} - $stat{xferBadShareCnt}; +$stat{fileCnt} ||= 0; +$stat{byteCnt} ||= 0; +$stat{xferErrCnt} ||= 0; +$stat{xferBadFileCnt} ||= 0; +$stat{xferBadShareCnt} ||= 0; print(LOG $bpc->timeStamp, "$type backup $newNum complete, $stat{fileCnt} files," . " $stat{byteCnt} bytes," @@ -1012,9 +1021,9 @@ sub catch_signal "Aborting backup up after signal $sigName\n"); # - # Tell xfer to abort + # Tell xfer to abort, but only if we actually started one # - $xfer->abort($reason); + $xfer->abort($reason) if ( defined($xfer) ); # # Send ALRMs to BackupPC_tarExtract if we are using it @@ -1107,7 +1116,9 @@ sub BackupFailCleanup # # We keep this backup if it is a full and we actually backed - # up some files. + # up some files. If the prior backup was a partial too, we + # only keep this backup if it has more files than the previous + # partial. # if ( $type eq "full" ) { if ( $nFilesTotal == 0 && $xfer->getStats->{fileCnt} == 0 ) { @@ -1116,13 +1127,29 @@ sub BackupFailCleanup # directory just in case. # find(\&CheckForNewFiles, "$Dir/new"); - $keepPartial = 1 if ( $nFilesTotal ); - } else { - # - # Xfer reported some files - # + } + my $str; + if ( $nFilesTotal > $partialFileCnt + || $xfer->getStats->{fileCnt} > $partialFileCnt ) { + # + # If the last backup wasn't a partial then + # $partialFileCnt is undefined, so the above + # test is simply $nFilesTotal > 0 + # $keepPartial = 1; - } + if ( $partialFileCnt ) { + $str = "Saving this as a partial backup\n"; + } else { + $str = sprintf("Saving this as a partial backup, replacing the" + . " prior one (got %d and %d files versus %d)\n", + $nFilesTotal, $xfer->getStats->{fileCnt}, $partialFileCnt); + } + } else { + $str = sprintf("Not saving this as a partial backup since it has fewer" + . " files than the prior one (got %d and %d files versus %d)\n", + $nFilesTotal, $xfer->getStats->{fileCnt}, $partialFileCnt); + } + $XferLOG->write(\$str); } # @@ -1379,6 +1406,7 @@ sub BackupSave # (the new backup might also be a partial, but that's ok). # BackupPartialRemove($client, \@Backups); + $needLink = 1 if ( -f "$Dir/NewFileList" ); # # Number the new backup @@ -1391,8 +1419,8 @@ sub BackupSave if ( !rename("$Dir/new", "$Dir/$num") ) { print(LOG $bpc->timeStamp, "Rename $Dir/new -> $Dir/$num failed\n"); $stat{xferOK} = 0; + return; } - $needLink = 1 if ( -f "$Dir/NewFileList" ); # # Add the new backup information to the backup file @@ -1417,6 +1445,7 @@ sub BackupSave $Backups[$i]{mangle} = 1; # name mangling always on for v1.04+ $Backups[$i]{xferMethod} = $Conf{XferMethod}; $Backups[$i]{charset} = $Conf{ClientCharset}; + $Backups[$i]{version} = $bpc->Version(); # # Save the main backups file # @@ -1599,6 +1628,7 @@ sub UserCommandRun $bpc->cmdSystemOrEval($cmd, sub { $XferLOG->write(\$_[0]); + print(LOG $bpc->timeStamp, "Output from $cmdType: ", $_[0]); }, $vars, \%Conf); }