* Completed support for rsync and rsyncd, including restore.
[BackupPC.git] / bin / BackupPC_dump
index 7fe11fe..fcb0bd7 100755 (executable)
@@ -91,6 +91,7 @@ die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
 my $TopDir = $bpc->TopDir();
 my $BinDir = $bpc->BinDir();
 my %Conf   = $bpc->Conf();
+my $NeedPostCmd;
 
 $bpc->ChildInit();
 
@@ -146,7 +147,7 @@ $SIG{TERM} = \&catch_signal;
 # Make sure we eventually timeout if there is no activity from
 # the data transport program.
 #
-alarm($Conf{SmbClientTimeout});
+alarm($Conf{ClientTimeout});
 
 mkpath($Dir, 0, 0777) if ( !-d $Dir );
 if ( !-f "$Dir/LOCK" ) {
@@ -331,6 +332,12 @@ if ( $Conf{XferMethod} eq "tar" ) {
 
 $ShareNames = [ $ShareNames ] unless ref($ShareNames) eq "ARRAY";
 
+#
+# Run an optional pre-dump command
+#
+UserCommandRun("DumpPreUserCmd");
+$NeedPostCmd = 1;
+
 #
 # Now backup each of the shares
 #
@@ -355,9 +362,10 @@ for my $shareName ( @$ShareNames ) {
         # Use rsync as the transport program.
         #
         if ( !defined($xfer = BackupPC::Xfer::Rsync->new($bpc)) ) {
-            print(LOG $bpc->timeStamp,
-                        "dump failed: File::RsyncP module is not installed\n");
-            print("dump failed: Rsync module is not installed\n");
+            my $errStr = BackupPC::Xfer::Rsync::errStr;
+            print(LOG $bpc->timeStamp, "dump failed: $errStr\n");
+            print("dump failed: $errStr\n");
+            UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
             exit(1);
         }
     } else {
@@ -366,6 +374,7 @@ for my $shareName ( @$ShareNames ) {
         #
         $xfer = BackupPC::Xfer::Smb->new($bpc);
     }
+
     my $useTar = $xfer->useTar;
 
     if ( $useTar ) {
@@ -438,7 +447,7 @@ for my $shareName ( @$ShareNames ) {
        lastFullBkupNum => $lastFullBkupNum,
        backups     => \@Backups,
        compress    => $Conf{CompressLevel},
-       XferMethod  => => $Conf{XferMethod},
+       XferMethod  => $Conf{XferMethod},
     });
 
     if ( !defined($logMsg = $xfer->start()) ) {
@@ -453,6 +462,7 @@ for my $shareName ( @$ShareNames ) {
            sleep(1);
            kill(9, $tarPid);
        }
+       UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
         exit(1);
     }
 
@@ -577,9 +587,6 @@ for my $shareName ( @$ShareNames ) {
         last;
     }
 }
-$XferLOG->close();
-close($newFilesFH) if ( defined($newFilesFH) );
-
 my $lastNum  = -1;
 
 #
@@ -589,6 +596,11 @@ if ( $stat{xferOK} && (my $errMsg = CorrectHostCheck($hostIP, $host)) ) {
     $stat{hostError} = $errMsg;
     $stat{xferOK} = 0;
 }
+
+UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
+$XferLOG->close();
+close($newFilesFH) if ( defined($newFilesFH) );
+
 if ( $stat{xferOK} ) {
     @Backups = $bpc->BackupInfoRead($host);
     for ( my $i = 0 ; $i < @Backups ; $i++ ) {
@@ -723,6 +735,7 @@ sub catch_signal
     my $fileExt = $Conf{CompressLevel} > 0 ? ".z" : "";
 
     print(LOG $bpc->timeStamp, "cleaning up after signal $signame\n");
+    UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
     $XferLOG->write(\"exiting after signal $signame\n");
     $XferLOG->close();
     if ( $xferPid > 0 ) {
@@ -846,3 +859,39 @@ sub CorrectHostCheck
             if ( $netBiosHost ne $host );
     return;
 }
+
+#
+# Run an optional pre- or post-dump command
+#
+sub UserCommandRun
+{
+    my($type) = @_;
+
+    return if ( !defined($Conf{$type}) );
+    my $vars = {
+        xfer    => $xfer,
+        host    => $host,
+        hostIP  => $hostIP,
+        share   => $ShareNames->[0],
+        shares  => $ShareNames,
+        XferMethod => $Conf{XferMethod},
+        LOG     => *LOG,
+        XferLOG => $XferLOG,
+        stat    => \%stat,
+        xferOK  => $stat{xferOK},
+       type    => $type,
+    };
+    my $cmd = $bpc->cmdVarSubstitute($Conf{$type}, $vars);
+    $XferLOG->write(\"Executing $type: @$cmd\n");
+    #
+    # Run the user's command, dumping the stdout/stderr into the
+    # Xfer log file.  Also supply the optional $vars and %Conf in
+    # case the command is really perl code instead of a shell
+    # command.
+    #
+    $bpc->cmdSystemOrEval($cmd,
+           sub {
+               $XferLOG->write(\$_[0]);
+           },
+           $vars, \%Conf);
+}