Various changes, including changes in 2.1.1 and 2.1.2 releases.
[BackupPC.git] / bin / BackupPC_restore
index f63882b..2015df8 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/perl -T
+#!/bin/perl
 #============================================================= -*-perl-*-
 #
 # BackupPC_restore: Restore files to a client.
@@ -11,7 +11,7 @@
 #   Craig Barratt  <cbarratt@users.sourceforge.net>
 #
 # COPYRIGHT
-#   Copyright (C) 2001  Craig Barratt
+#   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
 #
 #========================================================================
 #
-# Version 2.0.0beta2, released 13 Apr 2003.
+# Version 2.1.0, released 20 Jun 2004.
 #
 # See http://backuppc.sourceforge.net.
 #
 #========================================================================
 
 use strict;
+no  utf8;
 use lib "/usr/local/BackupPC/lib";
 use BackupPC::Lib;
 use BackupPC::FileZIO;
 use BackupPC::Xfer::Smb;
 use BackupPC::Xfer::Tar;
 use BackupPC::Xfer::Rsync;
+use Socket;
 
 use File::Path;
 use Getopt::Std;
@@ -88,6 +90,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" ) {
@@ -179,7 +186,10 @@ 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});
@@ -224,13 +234,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);
+    }
 }
 
 #
@@ -247,6 +267,7 @@ my $xferArgs = {
     pipeWH       => *WH,
     XferLOG      => $RestoreLOG,
     XferMethod   => $Conf{XferMethod},
+    logLevel     => $Conf{XferLogLevel},
     bkupSrcHost  => $RestoreReq{hostSrc},
     bkupSrcShare => $RestoreReq{shareSrc},
     bkupSrcNum   => $RestoreReq{num},
@@ -304,6 +325,7 @@ if ( $useTar ) {
        $stat{hostError} = "Can't fork to run tar";
        exit(RestoreCleanup($client));
     }
+    binmode(TAR);
     if ( !$tarPid ) {
        #
        # This is the tarCreate child.  Clone STDERR to STDOUT,
@@ -314,6 +336,7 @@ if ( $useTar ) {
        open(STDERR, ">&STDOUT");
        close(STDOUT);
        open(STDOUT, ">&WH");
+       alarm(0);
        exec("$BinDir/BackupPC_tarCreate", @tarArgs);
        print(LOG $bpc->timeStamp, "can't exec $BinDir/BackupPC_tarCreate\n");
        # FIX: need to cleanup xfer
@@ -434,22 +457,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 ) {
-        kill(2, @xferPid);
-       sleep(1);
-       kill(9, @xferPid);
-    }
-    if ( $tarPid > 0 ) {
-        kill(2, $tarPid);
-       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));
 }
 
 #
@@ -467,17 +497,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);
        }
     }
 
@@ -502,7 +532,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();
@@ -523,8 +552,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
     #
@@ -550,7 +583,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 {
@@ -572,7 +605,7 @@ sub pidHandler
     my @pids = @xferPid;
     push(@pids, $tarPid) if ( $tarPid > 0 );
     my $str = join(",", @pids);
-    $XferLOG->write(\"Xfer PIDs are now $str\n") if ( defined($XferLOG) );
+    $RestoreLOG->write(\"Xfer PIDs are now $str\n") if ( defined($RestoreLOG) );
     print("xferPids $str\n");
 }
 
@@ -581,11 +614,12 @@ sub pidHandler
 #
 sub UserCommandRun
 {
-    my($type) = @_;
+    my($cmdType) = @_;
 
-    return if ( !defined($Conf{$type}) );
+    return if ( !defined($Conf{$cmdType}) );
     my $vars = {
         xfer         => $xfer,
+        client       => $client,
         host         => $host,
         hostIP       => $hostIP,
         share        => $RestoreReq{shareDest},
@@ -596,8 +630,9 @@ sub UserCommandRun
        moreUsers    => $Hosts->{$client}{moreUsers},
         XferLOG      => $RestoreLOG,
         stat         => \%stat,
-        xferOK       => $stat{xferOK},
-       type         => $type,
+        xferOK       => $stat{xferOK} || 0,
+       hostError    => $stat{hostError},
+       type         => "restore",
        bkupSrcHost  => $RestoreReq{hostSrc},
        bkupSrcShare => $RestoreReq{shareSrc},
        bkupSrcNum   => $RestoreReq{num},
@@ -605,9 +640,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