X-Git-Url: http://git.rot13.org/?p=BackupPC.git;a=blobdiff_plain;f=bin%2FBackupPC_restore;h=f23afe8635b792b591c967b4da3110c756d79a75;hp=9ebd8c45c831dca03ba6104261be4a849e2cfedc;hb=fda25dc88a63ccac1c80efa2e4994bf0725ca9b7;hpb=2c14784ad71874ec850d189060fe63d6eb9eba95 diff --git a/bin/BackupPC_restore b/bin/BackupPC_restore index 9ebd8c4..f23afe8 100755 --- a/bin/BackupPC_restore +++ b/bin/BackupPC_restore @@ -11,7 +11,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 @@ -29,7 +29,7 @@ # #======================================================================== # -# Version 2.1.0_CVS, released 8 Feb 2004. +# Version 3.1.0, released 25 Nov 2007. # # See http://backuppc.sourceforge.net. # @@ -43,6 +43,7 @@ use BackupPC::FileZIO; use BackupPC::Xfer::Smb; use BackupPC::Xfer::Tar; use BackupPC::Xfer::Rsync; +use BackupPC::Xfer::BackupPCd; use Socket; use File::Path; @@ -100,9 +101,30 @@ mkpath($Dir, 0, 0777) if ( !-d $Dir ); if ( !-f "$Dir/LOCK" ) { open(LOCK, ">", "$Dir/LOCK") && close(LOCK); } -open(LOG, ">>", "$Dir/LOG"); -select(LOG); $| = 1; select(STDOUT); +my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); +my $logPath = sprintf("$Dir/LOG.%02d%04d", $mon + 1, $year + 1900); + +if ( !-f $logPath ) { + # + # Compress and prune old log files + # + my $lastLog = $Conf{MaxOldPerPCLogFiles} - 1; + foreach my $file ( $bpc->sortedPCLogFiles($client) ) { + if ( $lastLog <= 0 ) { + unlink($file); + next; + } + $lastLog--; + next if ( $file =~ /\.z$/ || !$Conf{CompressLevel} ); + BackupPC::FileZIO->compressCopy($file, + "$file.z", + undef, + $Conf{CompressLevel}, 1); + } +} +open(LOG, ">>", $logPath); +select(LOG); $| = 1; select(STDOUT); # # Read the request file @@ -186,12 +208,16 @@ if ( (my $errMsg = CorrectHostCheck($hostIP, $host)) ) { # # Setup file extension for compression and open RestoreLOG output file # -$Conf{CompressLevel} = 0 if ( !BackupPC::FileZIO->compOk ); +if ( $Conf{CompressLevel} && !BackupPC::FileZIO->compOk ) { + $stat{hostError} = "Compress:Zlib not found"; + exit(RestoreCleanup($client)); +} my $fileExt = $Conf{CompressLevel} > 0 ? ".z" : ""; my $RestoreLOG = BackupPC::FileZIO->open("$Dir/RestoreLOG$fileExt", 1, $Conf{CompressLevel}); my $tarCreateFileCnt = 0; my $tarCreateByteCnt = 0; +my $tarCreateDirCnt = 0; my $tarCreateErrCnt = 1; # assume not ok until we learn otherwise my $tarCreateErr; my($logMsg, $xfer); @@ -204,6 +230,10 @@ local(*RH, *WH); # Run an optional pre-restore command # UserCommandRun("RestorePreUserCmd"); +if ( $? && $Conf{UserCmdCheckStatus} ) { + $stat{hostError} = "RestorePreUserCmd returned error status $?"; + exit(RestoreCleanup($client)); +} $NeedPostCmd = 1; if ( $Conf{XferMethod} eq "tar" ) { @@ -221,6 +251,16 @@ if ( $Conf{XferMethod} eq "tar" ) { $stat{hostError} = $errStr; exit(RestoreCleanup($client)); } +} 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; + UserCommandRun("RestorePostUserCmd") if ( $NeedPostCmd ); + $stat{hostError} = $errStr; + exit(RestoreCleanup($client)); + } } else { # # Default is to use smbclient (smb) as the transport program. @@ -264,6 +304,7 @@ my $xferArgs = { pipeWH => *WH, XferLOG => $RestoreLOG, XferMethod => $Conf{XferMethod}, + logLevel => $Conf{XferLogLevel}, bkupSrcHost => $RestoreReq{hostSrc}, bkupSrcShare => $RestoreReq{shareSrc}, bkupSrcNum => $RestoreReq{num}, @@ -310,10 +351,10 @@ if ( $useTar ) { @tarPathOpts, @{$RestoreReq{fileList}}, ); - my $logMsg = "Running: " + my $runMsg = "Running: " . $bpc->execCmd2ShellCmd("$BinDir/BackupPC_tarCreate", @tarArgs) . "\n"; - $RestoreLOG->write(\$logMsg); + $RestoreLOG->write(\$runMsg); if ( !defined($tarPid = open(TAR, "-|")) ) { close(WH); # FIX: need to cleanup xfer @@ -383,6 +424,7 @@ if ( $useTar ) { if ( /^Done: (\d+) files, (\d+) bytes, (\d+) dirs, (\d+) specials, (\d+) errors/ ) { $tarCreateFileCnt = $1; $tarCreateByteCnt = $2; + $tarCreateDirCnt = $3; $tarCreateErrCnt = $5; } } @@ -407,7 +449,7 @@ if ( $useTar ) { # # otherwise the xfer module does everything for us # - print(LOG $bpc->timeStamp, "Starting restore\n"); + print(LOG $bpc->timeStamp, $logMsg . "\n"); print("started_restore\n"); ($tarCreateFileCnt, $tarCreateByteCnt, $tarCreateErrCnt, $tarCreateErr) = $xfer->run(); @@ -493,17 +535,17 @@ sub RestoreCleanup # kill off the tranfer program, first nicely then forcefully # if ( @xferPid ) { - kill(2, @xferPid); + kill($bpc->sigName2num("INT"), @xferPid); sleep(1); - kill(9, @xferPid); + kill($bpc->sigName2num("KILL"), @xferPid); } # # kill off the tar process, first nicely then forcefully # if ( $tarPid > 0 ) { - kill(2, $tarPid); + kill($bpc->sigName2num("INT"), $tarPid); sleep(1); - kill(9, $tarPid); + kill($bpc->sigName2num("KILL"), $tarPid); } } @@ -526,7 +568,13 @@ sub RestoreCleanup # # Run an optional post-restore command # - UserCommandRun("RestorePostUserCmd") if ( $NeedPostCmd ); + if ( $NeedPostCmd ) { + UserCommandRun("RestorePostUserCmd"); + if ( $? && $Conf{UserCmdCheckStatus} ) { + $stat{hostError} = "RestorePostUserCmd returned error status $?"; + $stat{xferOK} = 0; + } + } rename("$Dir/RestoreLOG$fileExt", "$Dir/RestoreLOG.$lastNum$fileExt"); rename("$Dir/$reqFileName", "$Dir/RestoreInfo.$lastNum"); @@ -548,7 +596,7 @@ sub RestoreCleanup if ( $stat{hostAbort} && $stat{hostError} eq "" ) { $stat{hostError} = "lost network connection during restore"; } - $RestoreLOG->write(\"Restore failed: $stat{hostError}\n") + $RestoreLOG->write(\"restore failed: $stat{hostError}\n") if ( defined($RestoreLOG) ); } @@ -579,10 +627,14 @@ sub RestoreCleanup $bpc->RestoreInfoWrite($client, @Restores); if ( !$stat{xferOK} ) { - print(LOG $bpc->timeStamp, "Restore failed ($stat{hostError})\n"); + print(LOG $bpc->timeStamp, "restore failed ($stat{hostError})\n"); print("restore failed: $stat{hostError}\n"); return 1; } else { + $stat{xferErrCnt} ||= 0; + print(LOG $bpc->timeStamp, "restore $lastNum complete" + . " ($tarCreateFileCnt files, $tarCreateByteCnt bytes," + . " $tarCreateDirCnt dirs, $stat{xferErrCnt} xferErrs)\n"); print("restore complete\n"); return; } @@ -610,9 +662,9 @@ sub pidHandler # sub UserCommandRun { - my($type) = @_; + my($cmdType) = @_; - return if ( !defined($Conf{$type}) ); + return if ( !defined($Conf{$cmdType}) ); my $vars = { xfer => $xfer, client => $client, @@ -628,7 +680,7 @@ sub UserCommandRun stat => \%stat, xferOK => $stat{xferOK} || 0, hostError => $stat{hostError}, - type => $type, + type => "restore", bkupSrcHost => $RestoreReq{hostSrc}, bkupSrcShare => $RestoreReq{shareSrc}, bkupSrcNum => $RestoreReq{num}, @@ -636,9 +688,10 @@ sub UserCommandRun pathHdrSrc => $RestoreReq{pathHdrSrc}, pathHdrDest => $RestoreReq{pathHdrDest}, fileList => $RestoreReq{fileList}, + cmdType => $cmdType, }; - my $cmd = $bpc->cmdVarSubstitute($Conf{$type}, $vars); - $RestoreLOG->write(\"Executing $type: @$cmd\n"); + my $cmd = $bpc->cmdVarSubstitute($Conf{$cmdType}, $vars); + $RestoreLOG->write(\"Executing $cmdType: @$cmd\n"); # # Run the user's command, dumping the stdout/stderr into the # Xfer log file. Also supply the optional $vars and %Conf in