X-Git-Url: http://git.rot13.org/?p=BackupPC.git;a=blobdiff_plain;f=bin%2FBackupPC_restore;h=1186cac89294516bbf548a4d272593041f017cac;hp=27d4c946eefe4ac76efce9bbcea940ff3bc9b6b4;hb=74dc9d456332757127d5eda4ce32f29377133fa2;hpb=9175f9157f0d54b50ebf11d2036c20f50ffc6d9d diff --git a/bin/BackupPC_restore b/bin/BackupPC_restore index 27d4c94..1186cac 100755 --- a/bin/BackupPC_restore +++ b/bin/BackupPC_restore @@ -29,7 +29,7 @@ # #======================================================================== # -# Version 2.0.0_CVS, released 3 Feb 2003. +# Version 2.0.0beta3, released 1 Jun 2003. # # See http://backuppc.sourceforge.net. # @@ -42,6 +42,7 @@ use BackupPC::FileZIO; use BackupPC::Xfer::Smb; use BackupPC::Xfer::Tar; use BackupPC::Xfer::Rsync; +use Socket; use File::Path; use Getopt::Std; @@ -76,10 +77,10 @@ $reqFileName = $1; my $startTime = time(); -my $Hosts = $bpc->HostInfoRead(); +my $Hosts = $bpc->HostInfoRead($client); my $Dir = "$TopDir/pc/$client"; -my $xferPid = -1; +my @xferPid = (); my $tarPid = -1; # @@ -88,6 +89,11 @@ my $tarPid = -1; $SIG{INT} = \&catch_signal; $SIG{ALRM} = \&catch_signal; $SIG{TERM} = \&catch_signal; +$SIG{PIPE} = \&catch_signal; +$SIG{STOP} = \&catch_signal; +$SIG{TSTP} = \&catch_signal; +$SIG{TTIN} = \&catch_signal; +my $Pid = $$; mkpath($Dir, 0, 0777) if ( !-d $Dir ); if ( !-f "$Dir/LOCK" ) { @@ -96,6 +102,7 @@ if ( !-f "$Dir/LOCK" ) { open(LOG, ">>", "$Dir/LOG"); select(LOG); $| = 1; select(STDOUT); + # # Read the request file # @@ -223,13 +230,23 @@ my $useTar = $xfer->useTar; if ( $useTar ) { # - # Create a pipe to connect BackupPC_tarCreate to the transport program - # (smbclient, tar, etc). + # Create a socketpair to connect BackupPC_tarCreate to the transport + # program (smbclient, tar, etc). # 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 # transport program. # - pipe(RH, WH); + if ( socketpair(RH, WH, AF_UNIX, SOCK_STREAM, PF_UNSPEC) ) { + shutdown(RH, 1); # no writing to this socket + shutdown(WH, 0); # no reading from this socket + setsockopt(RH, SOL_SOCKET, SO_RCVBUF, 8 * 65536); + setsockopt(WH, SOL_SOCKET, SO_SNDBUF, 8 * 65536); + } else { + # + # Default to pipe() if socketpair() doesn't work. + # + pipe(RH, WH); + } } # @@ -253,6 +270,7 @@ my $xferArgs = { pathHdrSrc => $RestoreReq{pathHdrSrc}, pathHdrDest => $RestoreReq{pathHdrDest}, fileList => $RestoreReq{fileList}, + pidHandler => \&pidHandler, }; $xfer->args($xferArgs); @@ -323,9 +341,12 @@ if ( $useTar ) { # close(WH); - $xferPid = $xfer->xferPid; - print(LOG $bpc->timeStamp, $logMsg, " (tarPid=$tarPid, xferPid=$xferPid)\n"); - print("started restore, tarPid=$tarPid, xferPid=$xferPid\n"); + @xferPid = $xfer->xferPid; + + print(LOG $bpc->timeStamp, $logMsg, "\n"); + print("started_restore\n"); + + pidHandler(@xferPid); # # Parse the output of the transfer program and BackupPC_tarCreate @@ -383,8 +404,8 @@ if ( $useTar ) { # # otherwise the xfer module does everything for us # - print(LOG $bpc->timeStamp, "Starting restore (tarPid=-1, xferPid=-1)\n"); - print("started restore, tarPid=-1, xferPid=-1\n"); + print(LOG $bpc->timeStamp, "Starting restore\n"); + print("started_restore\n"); ($tarCreateFileCnt, $tarCreateByteCnt, $tarCreateErrCnt, $tarCreateErr) = $xfer->run(); } @@ -429,24 +450,29 @@ sub catch_signal { my $signame = shift; + # + # Children quit quietly on ALRM + # + exit(1) if ( $Pid != $$ && $signame eq "ALRM" ); + + # + # Ignore signals in children + # + return if ( $Pid != $$ ); + # # Note: needs to be tested for each kind of XferMethod # print(LOG $bpc->timeStamp, "cleaning up after signal $signame\n"); - if ( $xferPid > 0 ) { - if ( kill(2, $xferPid) <= 0 ) { - sleep(1); - kill(9, $xferPid); - } - } - if ( $tarPid > 0 ) { - if ( kill(2, $tarPid) <= 0 ) { - sleep(1); - kill(9, $tarPid); - } - } + $SIG{$signame} = 'IGNORE'; + $RestoreLOG->write(\"exiting after signal $signame\n"); $stat{xferOK} = 0; - $stat{hostError} = "aborted by signal $signame"; + if ( $signame eq "INT" ) { + $stat{hostError} = "aborted by user (signal=$signame)"; + } else { + $stat{hostError} = "aborted by signal=$signame"; + } + exit(RestoreCleanup($client)); } # @@ -463,15 +489,19 @@ sub RestoreCleanup # # kill off the tranfer program, first nicely then forcefully # - kill(2, $xferPid) if ( $xferPid > 0 ); - sleep(1); - kill(9, $xferPid) if ( $xferPid > 0 ); + if ( @xferPid ) { + kill(2, @xferPid); + sleep(1); + kill(9, @xferPid); + } # # kill off the tar process, first nicely then forcefully # - kill(2, $tarPid) if ( $tarPid > 0 ); - sleep(1); - kill(9, $tarPid) if ( $tarPid > 0 ); + if ( $tarPid > 0 ) { + kill(2, $tarPid); + sleep(1); + kill(9, $tarPid); + } } my $lastNum = -1; @@ -495,7 +525,6 @@ sub RestoreCleanup # UserCommandRun("RestorePostUserCmd") if ( $NeedPostCmd ); - $RestoreLOG->close() if ( defined($RestoreLOG) ); rename("$Dir/RestoreLOG$fileExt", "$Dir/RestoreLOG.$lastNum$fileExt"); rename("$Dir/$reqFileName", "$Dir/RestoreInfo.$lastNum"); my $endTime = time(); @@ -516,8 +545,12 @@ sub RestoreCleanup if ( $stat{hostAbort} && $stat{hostError} eq "" ) { $stat{hostError} = "lost network connection during restore"; } + $RestoreLOG->write(\"Restore failed: $stat{hostError}\n") + if ( defined($RestoreLOG) ); } + $RestoreLOG->close() if ( defined($RestoreLOG) ); + # # Add the new restore information to the restore file # @@ -543,7 +576,7 @@ sub RestoreCleanup $bpc->RestoreInfoWrite($client, @Restores); if ( !$stat{xferOK} ) { - print(LOG $bpc->timeStamp, "Restore aborted ($stat{hostError})\n"); + print(LOG $bpc->timeStamp, "Restore failed ($stat{hostError})\n"); print("restore failed: $stat{hostError}\n"); return 1; } else { @@ -552,6 +585,23 @@ sub RestoreCleanup } } +# +# The Xfer method might tell us from time to time about processes +# it forks. We tell BackupPC about this (for status displays) and +# keep track of the pids in case we cancel the backup +# +sub pidHandler +{ + @xferPid = @_; + @xferPid = grep(/./, @xferPid); + return if ( !@xferPid && $tarPid < 0 ); + my @pids = @xferPid; + push(@pids, $tarPid) if ( $tarPid > 0 ); + my $str = join(",", @pids); + $RestoreLOG->write(\"Xfer PIDs are now $str\n") if ( defined($RestoreLOG) ); + print("xferPids $str\n"); +} + # # Run an optional pre- or post-dump command # @@ -568,6 +618,8 @@ sub UserCommandRun XferMethod => $Conf{XferMethod}, sshPath => $Conf{SshPath}, LOG => *LOG, + user => $Hosts->{$client}{user}, + moreUsers => $Hosts->{$client}{moreUsers}, XferLOG => $RestoreLOG, stat => \%stat, xferOK => $stat{xferOK},