BackupPC_restore (BackupPC-4.3.2) | : | BackupPC_restore (BackupPC-4.4.0) | ||
---|---|---|---|---|
skipping to change at line 31 | skipping to change at line 31 | |||
# This program is distributed in the hope that it will be useful, | # This program is distributed in the hope that it will be useful, | |||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
# GNU General Public License for more details. | # GNU General Public License for more details. | |||
# | # | |||
# You should have received a copy of the GNU General Public License | # You should have received a copy of the GNU General Public License | |||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
# | # | |||
#======================================================================== | #======================================================================== | |||
# | # | |||
# Version 4.3.2, released 17 Feb 2020. | # Version 4.4.0, released 20 Jun 2020. | |||
# | # | |||
# See http://backuppc.sourceforge.net. | # See http://backuppc.sourceforge.net. | |||
# | # | |||
#======================================================================== | #======================================================================== | |||
use strict; | use strict; | |||
no utf8; | no utf8; | |||
use lib "__INSTALLDIR__/lib"; | use lib "__INSTALLDIR__/lib"; | |||
use BackupPC::Lib; | use BackupPC::Lib; | |||
use BackupPC::XS; | use BackupPC::XS; | |||
use BackupPC::Xfer; | use BackupPC::Xfer; | |||
use Socket; | use Socket; | |||
use File::Path; | use File::Path; | |||
use Getopt::Std; | use Getopt::Std; | |||
use vars qw( %RestoreReq ); | use vars qw( %RestoreReq ); | |||
########################################################################### | ########################################################################### | |||
# Initialize | # Initialize | |||
########################################################################### | ########################################################################### | |||
die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) ); | die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) ); | |||
my $TopDir = $bpc->TopDir(); | my $TopDir = $bpc->TopDir(); | |||
my $BinDir = $bpc->BinDir(); | my $BinDir = $bpc->BinDir(); | |||
my %Conf = $bpc->Conf(); | my %Conf = $bpc->Conf(); | |||
my $NeedPostCmd; | my $NeedPostCmd; | |||
my($hostIP, $host, $client, $reqFileName, %stat); | my($hostIP, $host, $client, $reqFileName, %stat); | |||
$bpc->ChildInit(); | $bpc->ChildInit(); | |||
my %opts; | my %opts; | |||
skipping to change at line 77 | skipping to change at line 79 | |||
exit(1); | exit(1); | |||
} | } | |||
$hostIP = $1 if ( $ARGV[0] =~ /(.+)/ ); | $hostIP = $1 if ( $ARGV[0] =~ /(.+)/ ); | |||
$client = $1 if ( $ARGV[1] =~ /(.+)/ ); | $client = $1 if ( $ARGV[1] =~ /(.+)/ ); | |||
if ( $ARGV[2] !~ /^([\w.]+)$/ ) { | if ( $ARGV[2] !~ /^([\w.]+)$/ ) { | |||
print("$0: bad reqFileName (arg #3): $ARGV[2]\n"); | print("$0: bad reqFileName (arg #3): $ARGV[2]\n"); | |||
exit(1); | exit(1); | |||
} | } | |||
$reqFileName = $1; | $reqFileName = $1; | |||
if ( !$opts{m} && !defined($bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPo | if ( !$opts{m} | |||
rt})) | && !defined($bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort})) | |||
&& (my $status = $bpc->ServerMesg("hostMutex $client 1 BackupPC_re | && (my $status = $bpc->ServerMesg("hostMutex $client 1 BackupPC_restore")) = | |||
store")) =~ /fail/ ) { | ~ /fail/ ) { | |||
print(STDERR "$0: $status (use -m option to force running)\n"); | print(STDERR "$0: $status (use -m option to force running)\n"); | |||
exit(1); | exit(1); | |||
} | } | |||
$bpc->verbose(1) if ( $opts{v} ); | $bpc->verbose(1) if ( $opts{v} ); | |||
my $startTime = time(); | my $startTime = time(); | |||
my $Hosts = $bpc->HostInfoRead($client); | my $Hosts = $bpc->HostInfoRead($client); | |||
skipping to change at line 125 | skipping to change at line 128 | |||
select($LogFd); $| = 1; select(STDOUT); | select($LogFd); $| = 1; select(STDOUT); | |||
# | # | |||
# Read the request file | # Read the request file | |||
# | # | |||
if ( !(my $ret = do "$Dir/$reqFileName") ) { | if ( !(my $ret = do "$Dir/$reqFileName") ) { | |||
my $err; | my $err; | |||
if ( $@ ) { | if ( $@ ) { | |||
$err = "couldn't parse $Dir/$reqFileName: $@"; | $err = "couldn't parse $Dir/$reqFileName: $@"; | |||
} elsif ( !defined($ret) ) { | } elsif ( !defined($ret) ) { | |||
$err = "couldn't do $Dir/$reqFileName: $!"; | $err = "couldn't do $Dir/$reqFileName: $!"; | |||
} else { | } else { | |||
$err = "couldn't run $Dir/$reqFileName"; | $err = "couldn't run $Dir/$reqFileName"; | |||
} | } | |||
$stat{hostError} = $err; | $stat{hostError} = $err; | |||
exit(RestoreCleanup($client)); | exit(RestoreCleanup($client)); | |||
} | } | |||
# | # | |||
# Re-read config file, so we can include the PC-specific config | # Re-read config file, so we can include the PC-specific config | |||
# | # | |||
if ( defined(my $error = $bpc->ConfigRead($client)) ) { | if ( defined(my $error = $bpc->ConfigRead($client)) ) { | |||
$stat{hostError} = "Can't read PC's config file: $error"; | $stat{hostError} = "Can't read PC's config file: $error"; | |||
skipping to change at line 154 | skipping to change at line 157 | |||
# the data transport program. | # the data transport program. | |||
# | # | |||
alarm($Conf{ClientTimeout}); | alarm($Conf{ClientTimeout}); | |||
# | # | |||
# Build the list of hosts to check | # Build the list of hosts to check | |||
# | # | |||
my @hostsToCheck = (); | my @hostsToCheck = (); | |||
my @validHosts = (); | my @validHosts = (); | |||
if ( ref($Conf{ClientNameAlias}) eq 'ARRAY' ) { | if ( ref($Conf{ClientNameAlias}) eq 'ARRAY' ) { | |||
push(@hostsToCheck, @{ $Conf{ClientNameAlias} }); | push(@hostsToCheck, @{$Conf{ClientNameAlias}}); | |||
} elsif ( $Conf{ClientNameAlias} ne "" ) { | } elsif ( $Conf{ClientNameAlias} ne "" ) { | |||
push(@hostsToCheck, $Conf{ClientNameAlias}); | push(@hostsToCheck, $Conf{ClientNameAlias}); | |||
} else { | } else { | |||
push(@hostsToCheck, $client); | push(@hostsToCheck, $client); | |||
} | } | |||
if ( $hostIP !~ /^\d+\.\d+\.\d+\.\d+$/ ) { | if ( $hostIP !~ /^\d+\.\d+\.\d+\.\d+$/ ) { | |||
foreach my $hostToCheck ( @hostsToCheck ) { | foreach my $hostToCheck ( @hostsToCheck ) { | |||
$hostIP = HostLookupCheck($hostToCheck); | $hostIP = HostLookupCheck($hostToCheck); | |||
push(@validHosts, [$hostToCheck, $hostIP]) if ( defined($hostIP) ); | push(@validHosts, [$hostToCheck, $hostIP]) if ( defined($hostIP) ); | |||
} | } | |||
} else { | } else { | |||
push(@validHosts, [$client, $hostIP]); | push(@validHosts, [$client, $hostIP]); | |||
} | } | |||
# | # | |||
# Find the first entry in @validHosts that we can ping | # Find the first entry in @validHosts that we can ping | |||
# | # | |||
foreach my $h ( @validHosts ) { | foreach my $h ( @validHosts ) { | |||
($host, $hostIP) = @$h; | ($host, $hostIP) = @$h; | |||
my $delay = $bpc->CheckHostAlive($hostIP); | my $delay = $bpc->CheckHostAlive($hostIP); | |||
if ( $delay < 0 ) { | if ( $delay < 0 ) { | |||
print($LogFd $bpc->timeStamp, "no ping response from $hostIP ($host)\n"); | print($LogFd $bpc->timeStamp, "no ping response from $hostIP ($host)\n") | |||
$hostIP = undef; | ; | |||
next; | $hostIP = undef; | |||
next; | ||||
} elsif ( $delay > $Conf{PingMaxMsec} ) { | } elsif ( $delay > $Conf{PingMaxMsec} ) { | |||
printf($LogFd "%sping too slow on $hostIP ($host) %.4gmsec\n", $bpc->time | printf($LogFd "%sping too slow on $hostIP ($host) %.4gmsec\n", $bpc->tim | |||
Stamp, $delay); | eStamp, $delay); | |||
$hostIP = undef; | $hostIP = undef; | |||
next; | next; | |||
} | } | |||
last; | last; | |||
} | } | |||
$host = $hostIP if ( !defined($host) ); | $host = $hostIP if ( !defined($host) ); | |||
if ( !defined($hostIP) ) { | if ( !defined($hostIP) ) { | |||
$stat{hostError} = "can't ping $host (client = $client)"; | $stat{hostError} = "can't ping $host (client = $client)"; | |||
print("can't ping $host (client = $client); exiting\n"); | print("can't ping $host (client = $client); exiting\n"); | |||
exit(RestoreCleanup($client)); | exit(RestoreCleanup($client)); | |||
} | } | |||
# | # | |||
# Make sure it is really the machine we expect | # Make sure it is really the machine we expect | |||
# | # | |||
if ( (my $errMsg = CorrectHostCheck($hostIP, $host)) ) { | if ( (my $errMsg = CorrectHostCheck($hostIP, $host)) ) { | |||
$stat{hostError} = $errMsg; | $stat{hostError} = $errMsg; | |||
exit(RestoreCleanup($client)); | exit(RestoreCleanup($client)); | |||
} | } | |||
my $fileExt = $Conf{CompressLevel} > 0 ? ".z" : ""; | my $fileExt = $Conf{CompressLevel} > 0 ? ".z" : ""; | |||
my $RestoreLOG = BackupPC::XS::FileZIO::open("$Dir/RestoreLOG$fileExt", 1, | my $RestoreLOG = BackupPC::XS::FileZIO::open("$Dir/RestoreLOG$fileExt", 1, $Conf | |||
$Conf{CompressLevel}); | {CompressLevel}); | |||
if ( !defined($RestoreLOG) ) { | if ( !defined($RestoreLOG) ) { | |||
$stat{hostError} = "unable to open/create $Dir/RestoreLOG.$fileExt"; | $stat{hostError} = "unable to open/create $Dir/RestoreLOG.$fileExt"; | |||
exit(RestoreCleanup($client)); | exit(RestoreCleanup($client)); | |||
} | } | |||
my $xferCreateFileCnt = 0; | my $xferCreateFileCnt = 0; | |||
my $xferCreateByteCnt = 0; | my $xferCreateByteCnt = 0; | |||
my $xferCreateDirCnt = 0; | my $xferCreateDirCnt = 0; | |||
my $xferCreateErrCnt = 1; # assume not ok until we learn otherwise | my $xferCreateErrCnt = 1; # assume not ok until we learn otherwise | |||
my $xferCreateErr; | my $xferCreateErr; | |||
my($logMsg, $xfer); | my($logMsg, $xfer); | |||
$RestoreLOG->writeTeeStderr(1) if ( $opts{v} ); | $RestoreLOG->writeTeeStderr(1) if ( $opts{v} ); | |||
$stat{xferOK} = $stat{hostAbort} = undef; | $stat{xferOK} = $stat{hostAbort} = undef; | |||
$stat{hostError} = $stat{lastOutputLine} = undef; | $stat{hostError} = $stat{lastOutputLine} = undef; | |||
local(*RH, *WH); | local(*RH, *WH); | |||
# | # | |||
# Run an optional pre-restore command | # Run an optional pre-restore command | |||
# | # | |||
UserCommandRun("RestorePreUserCmd"); | UserCommandRun("RestorePreUserCmd"); | |||
if ( $? && $Conf{UserCmdCheckStatus} ) { | if ( $? && $Conf{UserCmdCheckStatus} ) { | |||
$stat{hostError} = "RestorePreUserCmd returned error status $?"; | $stat{hostError} = "RestorePreUserCmd returned error status $?"; | |||
exit(RestoreCleanup($client)); | exit(RestoreCleanup($client)); | |||
skipping to change at line 254 | skipping to change at line 256 | |||
if ( $useTar ) { | if ( $useTar ) { | |||
# | # | |||
# Create a socketpair to connect BackupPC_tarCreate to the transport | # Create a socketpair to connect BackupPC_tarCreate to the transport | |||
# program (smbclient, tar, etc). | # program (smbclient, tar, etc). | |||
# WH is the write handle for writing, provided to BackupPC_tarCreate | # WH is the write handle for writing, provided to BackupPC_tarCreate | |||
# and RH is the other end of the pipe for reading provided to the | # and RH is the other end of the pipe for reading provided to the | |||
# transport program. | # transport program. | |||
# | # | |||
if ( socketpair(RH, WH, AF_UNIX, SOCK_STREAM, PF_UNSPEC) ) { | if ( socketpair(RH, WH, AF_UNIX, SOCK_STREAM, PF_UNSPEC) ) { | |||
shutdown(RH, 1); # no writing to this socket | shutdown(RH, 1); # no writing to this socket | |||
shutdown(WH, 0); # no reading from this socket | shutdown(WH, 0); # no reading from this socket | |||
setsockopt(RH, SOL_SOCKET, SO_RCVBUF, 8 * 65536); | setsockopt(RH, SOL_SOCKET, SO_RCVBUF, 8 * 65536); | |||
setsockopt(WH, SOL_SOCKET, SO_SNDBUF, 8 * 65536); | setsockopt(WH, SOL_SOCKET, SO_SNDBUF, 8 * 65536); | |||
} else { | } else { | |||
# | # | |||
# Default to pipe() if socketpair() doesn't work. | # Default to pipe() if socketpair() doesn't work. | |||
# | # | |||
pipe(RH, WH); | pipe(RH, WH); | |||
} | } | |||
} | } | |||
# | # | |||
# Run the transport program, which reads from RH and extracts the data. | # Run the transport program, which reads from RH and extracts the data. | |||
# | # | |||
my @Backups = $bpc->BackupInfoRead($RestoreReq{hostSrc}); | my @Backups = $bpc->BackupInfoRead($RestoreReq{hostSrc}); | |||
my $xferArgs = { | my $xferArgs = { | |||
client => $client, | client => $client, | |||
host => $host, | host => $host, | |||
hostIP => $hostIP, | hostIP => $hostIP, | |||
type => "restore", | type => "restore", | |||
shareName => $RestoreReq{shareDest}, | shareName => $RestoreReq{shareDest}, | |||
pipeRH => *RH, | pipeRH => *RH, | |||
pipeWH => *WH, | pipeWH => *WH, | |||
XferLOG => $RestoreLOG, | XferLOG => $RestoreLOG, | |||
XferMethod => $Conf{XferMethod}, | XferMethod => $Conf{XferMethod}, | |||
logLevel => $Conf{XferLogLevel}, | logLevel => $Conf{XferLogLevel}, | |||
bkupSrcHost => $RestoreReq{hostSrc}, | bkupSrcHost => $RestoreReq{hostSrc}, | |||
bkupSrcShare => $RestoreReq{shareSrc}, | bkupSrcShare => $RestoreReq{shareSrc}, | |||
bkupSrcNum => $RestoreReq{num}, | bkupSrcNum => $RestoreReq{num}, | |||
backups => \@Backups, | backups => \@Backups, | |||
pathHdrSrc => $RestoreReq{pathHdrSrc}, | pathHdrSrc => $RestoreReq{pathHdrSrc}, | |||
pathHdrDest => $RestoreReq{pathHdrDest}, | pathHdrDest => $RestoreReq{pathHdrDest}, | |||
fileList => $RestoreReq{fileList}, | fileList => $RestoreReq{fileList}, | |||
pidHandler => \&pidHandler, | pidHandler => \&pidHandler, | |||
noProgressPrint => $opts{p}, | noProgressPrint => $opts{p}, | |||
}; | }; | |||
$xfer->args($xferArgs); | $xfer->args($xferArgs); | |||
if ( !defined($logMsg = $xfer->start()) ) { | if ( !defined($logMsg = $xfer->start()) ) { | |||
UserCommandRun("RestorePostUserCmd") if ( $NeedPostCmd ); | UserCommandRun("RestorePostUserCmd") if ( $NeedPostCmd ); | |||
$stat{hostError} = "xfer start failed: ", $xfer->errStr; | $stat{hostError} = "xfer start failed: ", $xfer->errStr; | |||
exit(RestoreCleanup($client)); | exit(RestoreCleanup($client)); | |||
} | } | |||
skipping to change at line 315 | skipping to change at line 317 | |||
# is using it. | # is using it. | |||
# | # | |||
close(RH); | close(RH); | |||
# | # | |||
# fork a child for BackupPC_tarCreate. TAR is a file handle | # fork a child for BackupPC_tarCreate. TAR is a file handle | |||
# on which we (the parent) read the stderr from BackupPC_tarCreate. | # on which we (the parent) read the stderr from BackupPC_tarCreate. | |||
# | # | |||
my @tarPathOpts; | my @tarPathOpts; | |||
if ( defined($RestoreReq{pathHdrDest}) | if ( defined($RestoreReq{pathHdrDest}) | |||
&& $RestoreReq{pathHdrDest} ne $RestoreReq{pathHdrSrc} ) { | && $RestoreReq{pathHdrDest} ne $RestoreReq{pathHdrSrc} ) { | |||
@tarPathOpts = ("-r", $RestoreReq{pathHdrSrc}, | @tarPathOpts = ("-r", $RestoreReq{pathHdrSrc}, "-p", $RestoreReq{pathHdr | |||
"-p", $RestoreReq{pathHdrDest} | Dest}); | |||
); | ||||
} | } | |||
my @tarArgs = ( | my @tarArgs = ( | |||
"-h", $RestoreReq{hostSrc}, | "-h", $RestoreReq{hostSrc}, "-n", $RestoreReq{num}, "-s", $RestoreReq{sh | |||
"-n", $RestoreReq{num}, | areSrc}, "-t", | |||
"-s", $RestoreReq{shareSrc}, | @tarPathOpts, @{$RestoreReq{fileList}}, | |||
"-t", | ||||
@tarPathOpts, | ||||
@{$RestoreReq{fileList}}, | ||||
); | ); | |||
my $runMsg = "Running: " | my $runMsg = "Running: " . $bpc->execCmd2ShellCmd("$BinDir/BackupPC_tarCreat | |||
. $bpc->execCmd2ShellCmd("$BinDir/BackupPC_tarCreate", @tarArgs) | e", @tarArgs) . "\n"; | |||
. "\n"; | ||||
$RestoreLOG->write(\$runMsg); | $RestoreLOG->write(\$runMsg); | |||
if ( !defined($tarPid = open(TAR, "-|")) ) { | if ( !defined($tarPid = open(TAR, "-|")) ) { | |||
close(WH); | close(WH); | |||
# FIX: need to cleanup xfer | ||||
UserCommandRun("RestorePostUserCmd") if ( $NeedPostCmd ); | # FIX: need to cleanup xfer | |||
$stat{hostError} = "Can't fork to run tar"; | UserCommandRun("RestorePostUserCmd") if ( $NeedPostCmd ); | |||
exit(RestoreCleanup($client)); | $stat{hostError} = "Can't fork to run tar"; | |||
exit(RestoreCleanup($client)); | ||||
} | } | |||
binmode(TAR); | binmode(TAR); | |||
if ( !$tarPid ) { | if ( !$tarPid ) { | |||
# | # | |||
# This is the tarCreate child. Clone STDERR to STDOUT, | # This is the tarCreate child. Clone STDERR to STDOUT, | |||
# STDOUT to WH, and then exec BackupPC_tarCreate. | # STDOUT to WH, and then exec BackupPC_tarCreate. | |||
# | # | |||
setpgrp 0,0; | setpgrp 0, 0; | |||
close(STDERR); | close(STDERR); | |||
open(STDERR, ">&STDOUT"); | open(STDERR, ">&STDOUT"); | |||
close(STDOUT); | close(STDOUT); | |||
open(STDOUT, ">&WH"); | open(STDOUT, ">&WH"); | |||
alarm(0); | alarm(0); | |||
exec("$BinDir/BackupPC_tarCreate", @tarArgs); | exec("$BinDir/BackupPC_tarCreate", @tarArgs); | |||
print($LogFd $bpc->timeStamp, "can't exec $BinDir/BackupPC_tarCreate\n"); | print($LogFd $bpc->timeStamp, "can't exec $BinDir/BackupPC_tarCreate\n") | |||
print(STDERR "$0: can't exec $BinDir/BackupPC_tarCreate\n") if ( $opts{v} | ; | |||
); | print(STDERR "$0: can't exec $BinDir/BackupPC_tarCreate\n") if ( $opts{v | |||
# FIX: need to cleanup xfer | } ); | |||
exit(0); | ||||
# FIX: need to cleanup xfer | ||||
exit(0); | ||||
} | } | |||
# | # | |||
# The parent must close the write handle since BackupPC_tarCreate | # The parent must close the write handle since BackupPC_tarCreate | |||
# is using it. | # is using it. | |||
# | # | |||
close(WH); | close(WH); | |||
@xferPid = $xfer->xferPid; | @xferPid = $xfer->xferPid; | |||
print($LogFd $bpc->timeStamp, $logMsg, "\n"); | print($LogFd $bpc->timeStamp, $logMsg, "\n"); | |||
skipping to change at line 380 | skipping to change at line 376 | |||
# | # | |||
# Parse the output of the transfer program and BackupPC_tarCreate | # Parse the output of the transfer program and BackupPC_tarCreate | |||
# while they run. Since we are reading from two or more children | # while they run. Since we are reading from two or more children | |||
# we use a select. | # we use a select. | |||
# | # | |||
my($FDread, $tarOut, $mesg); | my($FDread, $tarOut, $mesg); | |||
vec($FDread, fileno(TAR), 1) = 1; | vec($FDread, fileno(TAR), 1) = 1; | |||
$xfer->setSelectMask(\$FDread); | $xfer->setSelectMask(\$FDread); | |||
SCAN: while ( 1 ) { | SCAN: while ( 1 ) { | |||
my $ein = $FDread; | my $ein = $FDread; | |||
last if ( $FDread =~ /^\0*$/ ); | last if ( $FDread =~ /^\0*$/ ); | |||
alarm($Conf{ClientTimeout}); | alarm($Conf{ClientTimeout}); | |||
select(my $rout = $FDread, undef, $ein, undef); | select(my $rout = $FDread, undef, $ein, undef); | |||
if ( vec($rout, fileno(TAR), 1) ) { | if ( vec($rout, fileno(TAR), 1) ) { | |||
if ( sysread(TAR, $mesg, 8192) <= 0 ) { | if ( sysread(TAR, $mesg, 8192) <= 0 ) { | |||
vec($FDread, fileno(TAR), 1) = 0; | vec($FDread, fileno(TAR), 1) = 0; | |||
if ( !close(TAR) ) { | if ( !close(TAR) ) { | |||
$xferCreateErrCnt = 1; | $xferCreateErrCnt = 1; | |||
$xferCreateErr = "BackupPC_tarCreate failed"; | $xferCreateErr = "BackupPC_tarCreate failed"; | |||
} | } | |||
} else { | } else { | |||
$tarOut .= $mesg; | $tarOut .= $mesg; | |||
} | } | |||
} | } | |||
while ( $tarOut =~ /(.*?)[\n\r]+(.*)/s ) { | while ( $tarOut =~ /(.*?)[\n\r]+(.*)/s ) { | |||
$_ = $1; | $_ = $1; | |||
$tarOut = $2; | $tarOut = $2; | |||
$RestoreLOG->write(\"tarCreate: $_\n"); | $RestoreLOG->write(\"tarCreate: $_\n"); | |||
if ( /^Done: (\d+) files, (\d+) bytes, (\d+) dirs, (\d+) specials, (\ | if ( /^Done: (\d+) files, (\d+) bytes, (\d+) dirs, (\d+) specials, ( | |||
d+) errors/ ) { | \d+) errors/ ) { | |||
$xferCreateFileCnt = $1; | $xferCreateFileCnt = $1; | |||
$xferCreateByteCnt = $2; | $xferCreateByteCnt = $2; | |||
$xferCreateDirCnt = $3; | $xferCreateDirCnt = $3; | |||
$xferCreateErrCnt = $5; | $xferCreateErrCnt = $5; | |||
} | } | |||
} | } | |||
last if ( !$xfer->readOutput(\$FDread, $rout) ); | last if ( !$xfer->readOutput(\$FDread, $rout) ); | |||
while ( my $str = $xfer->logMsgGet ) { | while ( my $str = $xfer->logMsgGet ) { | |||
print($LogFd $bpc->timeStamp, "xfer: $str\n"); | print($LogFd $bpc->timeStamp, "xfer: $str\n"); | |||
print(STDERR "xfer: $str\n") if ( $opts{v} ); | print(STDERR "xfer: $str\n") if ( $opts{v} ); | |||
} | } | |||
if ( $xfer->getStats->{fileCnt} == 1 ) { | if ( $xfer->getStats->{fileCnt} == 1 ) { | |||
# | # | |||
# Make sure it is still the machine we expect. We do this while | # Make sure it is still the machine we expect. We do this while | |||
# the transfer is running to avoid a potential race condition if | # the transfer is running to avoid a potential race condition if | |||
# the ip address was reassigned by dhcp just before we started | # the ip address was reassigned by dhcp just before we started | |||
# the transfer. | # the transfer. | |||
# | # | |||
if ( my $errMsg = CorrectHostCheck($hostIP, $host) ) { | if ( my $errMsg = CorrectHostCheck($hostIP, $host) ) { | |||
$stat{hostError} = $errMsg; | $stat{hostError} = $errMsg; | |||
last SCAN; | last SCAN; | |||
} | } | |||
} | } | |||
} | } | |||
} else { | } else { | |||
# | # | |||
# otherwise the xfer module does everything for us | # otherwise the xfer module does everything for us | |||
# | # | |||
print($LogFd $bpc->timeStamp, $logMsg . "\n"); | print($LogFd $bpc->timeStamp, $logMsg . "\n"); | |||
print(STDERR $logMsg, "\n") if ( $opts{v} ); | print(STDERR $logMsg, "\n") if ( $opts{v} ); | |||
print("started_restore\n"); | print("started_restore\n"); | |||
($xferCreateFileCnt, $xferCreateByteCnt, | ($xferCreateFileCnt, $xferCreateByteCnt, $xferCreateErrCnt, $xferCreateErr) | |||
$xferCreateErrCnt, $xferCreateErr) = $xfer->run(); | = $xfer->run(); | |||
} | } | |||
alarm(0); | alarm(0); | |||
# | # | |||
# Merge the xfer status (need to accumulate counts) | # Merge the xfer status (need to accumulate counts) | |||
# | # | |||
my $newStat = $xfer->getStats; | my $newStat = $xfer->getStats; | |||
foreach my $k ( (keys(%stat), keys(%$newStat)) ) { | foreach my $k ( (keys(%stat), keys(%$newStat)) ) { | |||
next if ( !defined($newStat->{$k}) ); | next if ( !defined($newStat->{$k}) ); | |||
if ( $k =~ /Cnt$/ ) { | if ( $k =~ /Cnt$/ ) { | |||
skipping to change at line 464 | skipping to change at line 459 | |||
exit(RestoreCleanup($client)); | exit(RestoreCleanup($client)); | |||
########################################################################### | ########################################################################### | |||
# Subroutines | # Subroutines | |||
########################################################################### | ########################################################################### | |||
sub CorrectHostCheck | sub CorrectHostCheck | |||
{ | { | |||
my($hostIP, $host) = @_; | my($hostIP, $host) = @_; | |||
return if ( $hostIP eq $host && !$Conf{FixedIPNetBiosNameCheck} | return if ( $hostIP eq $host && !$Conf{FixedIPNetBiosNameCheck} || $Conf{Nmb | |||
|| $Conf{NmbLookupCmd} eq "" ); | LookupCmd} eq "" ); | |||
if (ref($Conf{ClientNameAlias}) eq 'ARRAY') { | if ( ref($Conf{ClientNameAlias}) eq 'ARRAY' ) { | |||
return if ( grep /^$hostIP$/, @{ $Conf{ClientNameAlias} } ); | return if ( grep /^$hostIP$/, @{$Conf{ClientNameAlias}} ); | |||
} | } | |||
my($netBiosHost, $netBiosUser) = $bpc->NetBiosInfoGet($hostIP); | my($netBiosHost, $netBiosUser) = $bpc->NetBiosInfoGet($hostIP); | |||
return "host $host has mismatching netbios name $netBiosHost" | return "host $host has mismatching netbios name $netBiosHost" | |||
if ( lc($netBiosHost) ne lc(substr($host, 0, 15)) ); | if ( lc($netBiosHost) ne lc(substr($host, 0, 15)) ); | |||
return; | return; | |||
} | } | |||
# | # | |||
# Returns $host if $bpc->getHostAddrInfo() knows about it. | # Returns $host if $bpc->getHostAddrInfo() knows about it. | |||
# Otherwise tries to find the hostIP via NetBios, and returns | # Otherwise tries to find the hostIP via NetBios, and returns | |||
# the hostIP if successful. | # the hostIP if successful. | |||
# | # | |||
# Returns undef if both $bpc->getHostAddrInfo() and NetBios fail | # Returns undef if both $bpc->getHostAddrInfo() and NetBios fail | |||
# | # | |||
skipping to change at line 523 | skipping to change at line 517 | |||
# | # | |||
# Note: needs to be tested for each kind of XferMethod | # Note: needs to be tested for each kind of XferMethod | |||
# | # | |||
print($LogFd $bpc->timeStamp, "cleaning up after signal $signame\n"); | print($LogFd $bpc->timeStamp, "cleaning up after signal $signame\n"); | |||
print(STDERR "$0: cleaning up after signal $signame\n") if ( $opts{v} ); | print(STDERR "$0: cleaning up after signal $signame\n") if ( $opts{v} ); | |||
$SIG{$signame} = 'IGNORE'; | $SIG{$signame} = 'IGNORE'; | |||
$RestoreLOG->write(\"exiting after signal $signame\n"); | $RestoreLOG->write(\"exiting after signal $signame\n"); | |||
$stat{xferOK} = 0; | $stat{xferOK} = 0; | |||
if ( $signame eq "INT" ) { | if ( $signame eq "INT" ) { | |||
$stat{hostError} = "aborted by user (signal=$signame)"; | $stat{hostError} = "aborted by user (signal=$signame)"; | |||
} else { | } else { | |||
$stat{hostError} = "aborted by signal=$signame"; | $stat{hostError} = "aborted by signal=$signame"; | |||
} | } | |||
exit(RestoreCleanup($client)); | exit(RestoreCleanup($client)); | |||
} | } | |||
# | # | |||
# Cleanup and update the restore status | # Cleanup and update the restore status | |||
# | # | |||
sub RestoreCleanup | sub RestoreCleanup | |||
{ | { | |||
my($client) = @_; | my($client) = @_; | |||
$stat{xferOK} = 0 if ( $stat{hostError} || $stat{hostAbort} | $stat{xferOK} = 0 if ( $stat{hostError} || $stat{hostAbort} || $xferCreateEr | |||
|| $xferCreateErr ); | r ); | |||
if ( !$stat{xferOK} ) { | if ( !$stat{xferOK} ) { | |||
# | # | |||
# kill off the transfer program, first nicely then forcefully | # kill off the transfer program, first nicely then forcefully | |||
# | # | |||
if ( @xferPid ) { | if ( @xferPid ) { | |||
kill($bpc->sigName2num("INT"), @xferPid); | kill($bpc->sigName2num("INT"), @xferPid); | |||
sleep(1); | sleep(1); | |||
kill($bpc->sigName2num("KILL"), @xferPid); | kill($bpc->sigName2num("KILL"), @xferPid); | |||
} | } | |||
# | # | |||
# kill off the tar process, first nicely then forcefully | # kill off the tar process, first nicely then forcefully | |||
# | # | |||
if ( $tarPid > 0 ) { | if ( $tarPid > 0 ) { | |||
kill($bpc->sigName2num("INT"), $tarPid); | kill($bpc->sigName2num("INT"), $tarPid); | |||
sleep(1); | sleep(1); | |||
kill($bpc->sigName2num("KILL"), $tarPid); | kill($bpc->sigName2num("KILL"), $tarPid); | |||
} | } | |||
} | } | |||
my $lastNum = -1; | my $lastNum = -1; | |||
my @Restores; | my @Restores; | |||
# | # | |||
# Do one last check to make sure it is still the machine we expect. | # Do one last check to make sure it is still the machine we expect. | |||
# | # | |||
if ( $stat{xferOK} && (my $errMsg = CorrectHostCheck($hostIP, $host)) ) { | if ( $stat{xferOK} && (my $errMsg = CorrectHostCheck($hostIP, $host)) ) { | |||
$stat{hostError} = $errMsg; | $stat{hostError} = $errMsg; | |||
$stat{xferOK} = 0; | $stat{xferOK} = 0; | |||
} | } | |||
@Restores = $bpc->RestoreInfoRead($client); | @Restores = $bpc->RestoreInfoRead($client); | |||
for ( my $i = 0 ; $i < @Restores ; $i++ ) { | for ( my $i = 0 ; $i < @Restores ; $i++ ) { | |||
$lastNum = $Restores[$i]{num} if ( $lastNum < $Restores[$i]{num} ); | $lastNum = $Restores[$i]{num} if ( $lastNum < $Restores[$i]{num} ); | |||
} | } | |||
$lastNum++; | $lastNum++; | |||
# | # | |||
# Run an optional post-restore command | # Run an optional post-restore command | |||
# | # | |||
if ( $NeedPostCmd ) { | if ( $NeedPostCmd ) { | |||
UserCommandRun("RestorePostUserCmd"); | UserCommandRun("RestorePostUserCmd"); | |||
if ( $? && $Conf{UserCmdCheckStatus} ) { | if ( $? && $Conf{UserCmdCheckStatus} ) { | |||
$stat{hostError} = "RestorePostUserCmd returned error status $?"; | $stat{hostError} = "RestorePostUserCmd returned error status $?"; | |||
$stat{xferOK} = 0; | $stat{xferOK} = 0; | |||
} | } | |||
} | } | |||
rename("$Dir/RestoreLOG$fileExt", "$Dir/RestoreLOG.$lastNum$fileExt"); | rename("$Dir/RestoreLOG$fileExt", "$Dir/RestoreLOG.$lastNum$fileExt"); | |||
rename("$Dir/$reqFileName", "$Dir/RestoreInfo.$lastNum"); | rename("$Dir/$reqFileName", "$Dir/RestoreInfo.$lastNum"); | |||
my $endTime = time(); | my $endTime = time(); | |||
# | # | |||
# If the restore failed, clean up | # If the restore failed, clean up | |||
# | # | |||
if ( !$stat{xferOK} ) { | if ( !$stat{xferOK} ) { | |||
# | # | |||
# wait a short while and see if the system is still alive | # wait a short while and see if the system is still alive | |||
# | # | |||
$stat{hostError} ||= $xferCreateErr if ( $xferCreateErr ne "" ); | $stat{hostError} ||= $xferCreateErr if ( $xferCreateErr ne "" ); | |||
$stat{hostError} = $stat{lastOutputLine} if ( $stat{hostError} eq "" ); | $stat{hostError} = $stat{lastOutputLine} if ( $stat{hostError} eq "" ); | |||
sleep(2); | sleep(2); | |||
if ( $bpc->CheckHostAlive($hostIP) < 0 ) { | if ( $bpc->CheckHostAlive($hostIP) < 0 ) { | |||
$stat{hostAbort} = 1; | $stat{hostAbort} = 1; | |||
} | } | |||
if ( $stat{hostAbort} && $stat{hostError} eq "" ) { | if ( $stat{hostAbort} && $stat{hostError} eq "" ) { | |||
$stat{hostError} = "lost network connection during restore"; | $stat{hostError} = "lost network connection during restore"; | |||
} | } | |||
$RestoreLOG->write(\"restore failed: $stat{hostError}\n") | $RestoreLOG->write(\"restore failed: $stat{hostError}\n") | |||
if ( defined($RestoreLOG) ); | if ( defined($RestoreLOG) ); | |||
} | } | |||
$RestoreLOG->close() if ( defined($RestoreLOG) ); | $RestoreLOG->close() if ( defined($RestoreLOG) ); | |||
# | # | |||
# Add the new restore information to the restore file | # Add the new restore information to the restore file | |||
# | # | |||
@Restores = $bpc->RestoreInfoRead($client); | @Restores = $bpc->RestoreInfoRead($client); | |||
my $i = @Restores; | my $i = @Restores; | |||
$Restores[$i]{num} = $lastNum; | $Restores[$i]{num} = $lastNum; | |||
$Restores[$i]{startTime} = $startTime; | $Restores[$i]{startTime} = $startTime; | |||
$Restores[$i]{endTime} = $endTime; | $Restores[$i]{endTime} = $endTime; | |||
$Restores[$i]{result} = $stat{xferOK} ? "ok" : "failed"; | $Restores[$i]{result} = $stat{xferOK} ? "ok" : "failed"; | |||
$Restores[$i]{errorMsg} = $stat{hostError}; | $Restores[$i]{errorMsg} = $stat{hostError}; | |||
$Restores[$i]{nFiles} = $xferCreateFileCnt; | $Restores[$i]{nFiles} = $xferCreateFileCnt; | |||
$Restores[$i]{size} = $xferCreateByteCnt; | $Restores[$i]{size} = $xferCreateByteCnt; | |||
$Restores[$i]{tarCreateErrs} = $xferCreateErrCnt; | $Restores[$i]{tarCreateErrs} = $xferCreateErrCnt; | |||
$Restores[$i]{xferErrs} = $stat{xferErrCnt} || 0; | $Restores[$i]{xferErrs} = $stat{xferErrCnt} || 0; | |||
while ( @Restores > $Conf{RestoreInfoKeepCnt} ) { | while ( @Restores > $Conf{RestoreInfoKeepCnt} ) { | |||
my $num = $Restores[0]{num}; | my $num = $Restores[0]{num}; | |||
unlink("$Dir/RestoreLOG.$num.z"); | unlink("$Dir/RestoreLOG.$num.z"); | |||
unlink("$Dir/RestoreLOG.$num"); | unlink("$Dir/RestoreLOG.$num"); | |||
unlink("$Dir/RestoreInfo.$num"); | unlink("$Dir/RestoreInfo.$num"); | |||
shift(@Restores); | shift(@Restores); | |||
} | } | |||
$bpc->RestoreInfoWrite($client, @Restores); | $bpc->RestoreInfoWrite($client, @Restores); | |||
if ( !$stat{xferOK} ) { | if ( !$stat{xferOK} ) { | |||
print($LogFd $bpc->timeStamp, "restore failed ($stat{hostError})\n"); | print($LogFd $bpc->timeStamp, "restore failed ($stat{hostError})\n"); | |||
print("restore failed: $stat{hostError}\n"); | print("restore failed: $stat{hostError}\n"); | |||
return 1; | return 1; | |||
} else { | } else { | |||
$stat{xferErrCnt} ||= 0; | $stat{xferErrCnt} ||= 0; | |||
print($LogFd $bpc->timeStamp, "restore $lastNum complete" | print($LogFd $bpc->timeStamp, | |||
. " ($xferCreateFileCnt files, $xferCreateByteCnt bytes," | "restore $lastNum complete" | |||
. " $xferCreateDirCnt dirs, $stat{xferErrCnt} xferErrs)\n"); | . " ($xferCreateFileCnt files, $xferCreateByteCnt bytes," | |||
print("restore complete\n"); | . " $xferCreateDirCnt dirs, $stat{xferErrCnt} xferErrs)\n" | |||
return; | ); | |||
print("restore complete\n"); | ||||
return; | ||||
} | } | |||
} | } | |||
# | # | |||
# The Xfer method might tell us from time to time about processes | # The Xfer method might tell us from time to time about processes | |||
# it forks. We tell BackupPC about this (for status displays) and | # it forks. We tell BackupPC about this (for status displays) and | |||
# keep track of the pids in case we cancel the backup | # keep track of the pids in case we cancel the backup | |||
# | # | |||
sub pidHandler | sub pidHandler | |||
{ | { | |||
skipping to change at line 684 | skipping to change at line 679 | |||
return if ( !defined($Conf{$cmdType}) ); | return if ( !defined($Conf{$cmdType}) ); | |||
my $vars = { | my $vars = { | |||
xfer => $xfer, | xfer => $xfer, | |||
client => $client, | client => $client, | |||
host => $host, | host => $host, | |||
hostIP => $hostIP, | hostIP => $hostIP, | |||
share => $RestoreReq{shareDest}, | share => $RestoreReq{shareDest}, | |||
XferMethod => $Conf{XferMethod}, | XferMethod => $Conf{XferMethod}, | |||
sshPath => $Conf{SshPath}, | sshPath => $Conf{SshPath}, | |||
LOG => $LogFd, | LOG => $LogFd, | |||
user => $Hosts->{$client}{user}, | user => $Hosts->{$client}{user}, | |||
moreUsers => $Hosts->{$client}{moreUsers}, | moreUsers => $Hosts->{$client}{moreUsers}, | |||
XferLOG => $RestoreLOG, | XferLOG => $RestoreLOG, | |||
stat => \%stat, | stat => \%stat, | |||
xferOK => $stat{xferOK} || 0, | xferOK => $stat{xferOK} || 0, | |||
hostError => $stat{hostError}, | hostError => $stat{hostError}, | |||
type => "restore", | type => "restore", | |||
bkupSrcHost => $RestoreReq{hostSrc}, | bkupSrcHost => $RestoreReq{hostSrc}, | |||
bkupSrcShare => $RestoreReq{shareSrc}, | bkupSrcShare => $RestoreReq{shareSrc}, | |||
bkupSrcNum => $RestoreReq{num}, | bkupSrcNum => $RestoreReq{num}, | |||
backups => \@Backups, | backups => \@Backups, | |||
pathHdrSrc => $RestoreReq{pathHdrSrc}, | pathHdrSrc => $RestoreReq{pathHdrSrc}, | |||
pathHdrDest => $RestoreReq{pathHdrDest}, | pathHdrDest => $RestoreReq{pathHdrDest}, | |||
fileList => $RestoreReq{fileList}, | fileList => $RestoreReq{fileList}, | |||
cmdType => $cmdType, | cmdType => $cmdType, | |||
}; | }; | |||
if ( $cmdType eq "RestorePostUserCmd" ) { | if ( $cmdType eq "RestorePostUserCmd" ) { | |||
print("__bpc_progress_state__ post-cmd\n") if ( !$opts{p} ); | print("__bpc_progress_state__ post-cmd\n") if ( !$opts{p} ); | |||
} else { | } else { | |||
print("__bpc_progress_state__ pre-cmd\n") if ( !$opts{p} ); | print("__bpc_progress_state__ pre-cmd\n") if ( !$opts{p} ); | |||
} | } | |||
my $cmd = $bpc->cmdVarSubstitute($Conf{$cmdType}, $vars); | my $cmd = $bpc->cmdVarSubstitute($Conf{$cmdType}, $vars); | |||
$RestoreLOG->write(\"Executing $cmdType: @$cmd\n"); | $RestoreLOG->write(\"Executing $cmdType: @$cmd\n"); | |||
# | # | |||
# Run the user's command, dumping the stdout/stderr into the | # Run the user's command, dumping the stdout/stderr into the | |||
# Xfer log file. Also supply the optional $vars and %Conf in | # Xfer log file. Also supply the optional $vars and %Conf in | |||
# case the command is really perl code instead of a shell | # case the command is really perl code instead of a shell | |||
# command. | # command. | |||
# | # | |||
$bpc->cmdSystemOrEval($cmd, | $bpc->cmdSystemOrEval( | |||
sub { | $cmd, | |||
if ( $RestoreLOG && length($_[0]) ) { | sub { | |||
$RestoreLOG->write(\$_[0]); | if ( $RestoreLOG && length($_[0]) ) { | |||
} elsif ( $LogFd && length($_[0]) ) { | $RestoreLOG->write(\$_[0]); | |||
print($LogFd $bpc->timeStamp, "Output from $cmdType: ", $_[0 | } elsif ( $LogFd && length($_[0]) ) { | |||
]); | print($LogFd $bpc->timeStamp, "Output from $cmdType: ", $_[0]); | |||
} | } | |||
}, | }, | |||
$vars, \%Conf); | $vars, | |||
\%Conf | ||||
); | ||||
} | } | |||
End of changes. 44 change blocks. | ||||
211 lines changed or deleted | 216 lines changed or added |