- fixed configure.pl and makeDist.
[BackupPC.git] / bin / BackupPC_dump
index 3ac9ebc..72f992c 100755 (executable)
@@ -31,9 +31,9 @@
 #   full or incremental backup needs to be run.  If no backup is
 #   scheduled, or a ping to $client fails, then BackupPC_dump quits.
 #
 #   full or incremental backup needs to be run.  If no backup is
 #   scheduled, or a ping to $client fails, then BackupPC_dump quits.
 #
-#   The backup is done using the selected XferMethod (smb, tar, rsync etc),
-#   extracting the dump into $TopDir/pc/$client/new.  The xfer output is
-#   put into $TopDir/pc/$client/XferLOG.
+#   The backup is done using the selected XferMethod (smb, tar, rsync,
+#   backuppcd etc), extracting the dump into $TopDir/pc/$client/new.
+#   The xfer output is put into $TopDir/pc/$client/XferLOG.
 #
 #   If the dump succeeds (based on parsing the output of the XferMethod):
 #     - $TopDir/pc/$client/new is renamed to $TopDir/pc/$client/nnn, where
 #
 #   If the dump succeeds (based on parsing the output of the XferMethod):
 #     - $TopDir/pc/$client/new is renamed to $TopDir/pc/$client/nnn, where
@@ -70,7 +70,7 @@
 #
 #========================================================================
 #
 #
 #========================================================================
 #
-# Version 2.1.0beta1, released 9 Apr 2004.
+# Version 3.0.0alpha, released 23 Jan 2006.
 #
 # See http://backuppc.sourceforge.net.
 #
 #
 # See http://backuppc.sourceforge.net.
 #
@@ -81,9 +81,11 @@ no  utf8;
 use lib "/usr/local/BackupPC/lib";
 use BackupPC::Lib;
 use BackupPC::FileZIO;
 use lib "/usr/local/BackupPC/lib";
 use BackupPC::Lib;
 use BackupPC::FileZIO;
+use BackupPC::Storage;
 use BackupPC::Xfer::Smb;
 use BackupPC::Xfer::Tar;
 use BackupPC::Xfer::Rsync;
 use BackupPC::Xfer::Smb;
 use BackupPC::Xfer::Tar;
 use BackupPC::Xfer::Rsync;
+use BackupPC::Xfer::BackupPCd;
 use Socket;
 use File::Path;
 use File::Find;
 use Socket;
 use File::Path;
 use File::Find;
@@ -276,6 +278,7 @@ my $lastFull = 0;
 my $lastIncr = 0;
 my $partialIdx = -1;
 my $partialNum;
 my $lastIncr = 0;
 my $partialIdx = -1;
 my $partialNum;
+my $lastPartial = 0;
 
 if ( $Conf{FullPeriod} == -1 && !$opts{f} && !$opts{i}
         || $Conf{FullPeriod} == -2 ) {
 
 if ( $Conf{FullPeriod} == -1 && !$opts{f} && !$opts{i}
         || $Conf{FullPeriod} == -2 ) {
@@ -347,7 +350,7 @@ if ( !$opts{i} && !$opts{f} && $Conf{BlackoutGoodCnt} >= 0
 }
 
 if ( !$opts{i} && !$opts{f} && $StatusHost{backoffTime} > time ) {
 }
 
 if ( !$opts{i} && !$opts{f} && $StatusHost{backoffTime} > time ) {
-    printf(LOG "%sskipping because of user requested delay (%.1f hours left)",
+    printf(LOG "%sskipping because of user requested delay (%.1f hours left)\n",
                 $bpc->timeStamp, ($StatusHost{backoffTime} - time) / 3600);
     NothingToDo($needLink);
 }
                 $bpc->timeStamp, ($StatusHost{backoffTime} - time) / 3600);
     NothingToDo($needLink);
 }
@@ -375,8 +378,9 @@ for ( my $i = 0 ; $i < @Backups ; $i++ ) {
         $lastIncr = $Backups[$i]{startTime}
                 if ( $lastIncr < $Backups[$i]{startTime} );
     } elsif ( $Backups[$i]{type} eq "partial" ) {
         $lastIncr = $Backups[$i]{startTime}
                 if ( $lastIncr < $Backups[$i]{startTime} );
     } elsif ( $Backups[$i]{type} eq "partial" ) {
-        $partialIdx = $i;
-        $partialNum = $Backups[$i]{num};
+        $partialIdx  = $i;
+        $lastPartial = $Backups[$i]{startTime};
+        $partialNum  = $Backups[$i]{num};
     }
 }
 
     }
 }
 
@@ -432,7 +436,11 @@ $bpc->RmTreeDefer("$TopDir/trash", "$Dir/new") if ( -d "$Dir/new" );
 #
 # Setup file extension for compression and open XferLOG output file
 #
 #
 # Setup file extension for compression and open XferLOG output file
 #
-$Conf{CompressLevel} = 0 if ( !BackupPC::FileZIO->compOk );
+if ( $Conf{CompressLevel} && !BackupPC::FileZIO->compOk ) {
+    print(LOG $bpc->timeStamp, "dump failed: can't find Compress::Zlib\n");
+    print("dump failed: can't find Compress::Zlib\n");
+    exit(1);
+}
 my $fileExt = $Conf{CompressLevel} > 0 ? ".z" : "";
 my $XferLOG = BackupPC::FileZIO->open("$Dir/XferLOG$fileExt", 1,
                                      $Conf{CompressLevel});
 my $fileExt = $Conf{CompressLevel} > 0 ? ".z" : "";
 my $XferLOG = BackupPC::FileZIO->open("$Dir/XferLOG$fileExt", 1,
                                      $Conf{CompressLevel});
@@ -444,10 +452,10 @@ if ( !defined($XferLOG) ) {
 }
 
 #
 }
 
 #
-# Ignore the partial dump in the case of an incremental.
-# A partial is a partial full.
+# Ignore the partial dump in the case of an incremental
+# or when the partial is too old.  A partial is a partial full.
 #
 #
-if ( $type ne "full" ) {
+if ( $type ne "full" || time - $lastPartial > $Conf{PartialAgeMax} * 24*3600 ) {
     $partialNum = undef;
     $partialIdx = -1;
 }
     $partialNum = undef;
     $partialIdx = -1;
 }
@@ -490,6 +498,8 @@ if ( $Conf{XferMethod} eq "tar" ) {
     $ShareNames = $Conf{TarShareName};
 } elsif ( $Conf{XferMethod} eq "rsync" || $Conf{XferMethod} eq "rsyncd" ) {
     $ShareNames = $Conf{RsyncShareName};
     $ShareNames = $Conf{TarShareName};
 } elsif ( $Conf{XferMethod} eq "rsync" || $Conf{XferMethod} eq "rsyncd" ) {
     $ShareNames = $Conf{RsyncShareName};
+} elsif ( $Conf{XferMethod} eq "backuppcd" ) {
+    $ShareNames = $Conf{BackupPCdShareName};
 } else {
     $ShareNames = $Conf{SmbShareName};
 }
 } else {
     $ShareNames = $Conf{SmbShareName};
 }
@@ -516,6 +526,8 @@ for my $shareName ( @$ShareNames ) {
         next;
     }
 
         next;
     }
 
+    UserCommandRun("DumpPreShareCmd", $shareName);
+
     if ( $Conf{XferMethod} eq "tar" ) {
         #
         # Use tar (eg: tar/ssh) as the transport program.
     if ( $Conf{XferMethod} eq "tar" ) {
         #
         # Use tar (eg: tar/ssh) as the transport program.
@@ -529,6 +541,19 @@ for my $shareName ( @$ShareNames ) {
             my $errStr = BackupPC::Xfer::Rsync::errStr;
             print(LOG $bpc->timeStamp, "dump failed: $errStr\n");
             print("dump failed: $errStr\n");
             my $errStr = BackupPC::Xfer::Rsync::errStr;
             print(LOG $bpc->timeStamp, "dump failed: $errStr\n");
             print("dump failed: $errStr\n");
+           UserCommandRun("DumpPostShareCmd", $shareName) if ( $NeedPostCmd );
+            UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
+            exit(1);
+        }
+    } 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;
+            print(LOG $bpc->timeStamp, "dump failed: $errStr\n");
+            print("dump failed: $errStr\n");
+           UserCommandRun("DumpPostShareCmd", $shareName) if ( $NeedPostCmd );
             UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
             exit(1);
         }
             UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
             exit(1);
         }
@@ -648,6 +673,7 @@ for my $shareName ( @$ShareNames ) {
            sleep(1);
            kill($bpc->sigName2num("KILL"), @xferPid);
        }
            sleep(1);
            kill($bpc->sigName2num("KILL"), @xferPid);
        }
+       UserCommandRun("DumpPostShareCmd", $shareName) if ( $NeedPostCmd );
        UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
         exit(1);
     }
        UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
         exit(1);
     }
@@ -761,6 +787,9 @@ for my $shareName ( @$ShareNames ) {
             next;
         }
     }
             next;
         }
     }
+
+    UserCommandRun("DumpPostShareCmd", $shareName) if ( $NeedPostCmd );
+
     $stat{xferOK} = 0 if ( $stat{hostError} || $stat{hostAbort} );
     if ( !$stat{xferOK} ) {
         #
     $stat{xferOK} = 0 if ( $stat{hostError} || $stat{hostAbort} );
     if ( !$stat{xferOK} ) {
         #
@@ -1008,6 +1037,11 @@ sub BackupFailCleanup
        }
     }
 
        }
     }
 
+    #
+    # Don't keep partials if they are disabled
+    #
+    $keepPartial = 0 if ( $Conf{PartialAgeMax} < 0 );
+
     if ( !$keepPartial ) {
         #
         # No point in saving this dump; get rid of eveything.
     if ( !$keepPartial ) {
         #
         # No point in saving this dump; get rid of eveything.
@@ -1046,7 +1080,8 @@ sub BackupExpire
     my($client) = @_;
     my($Dir) = "$TopDir/pc/$client";
     my(@Backups) = $bpc->BackupInfoRead($client);
     my($client) = @_;
     my($Dir) = "$TopDir/pc/$client";
     my(@Backups) = $bpc->BackupInfoRead($client);
-    my($cntFull, $cntIncr, $firstFull, $firstIncr, $oldestIncr, $oldestFull);
+    my($cntFull, $cntIncr, $firstFull, $firstIncr, $oldestIncr,
+       $oldestFull, $changes);
 
     if ( $Conf{FullKeepCnt} <= 0 ) {
         print(LOG $bpc->timeStamp,
 
     if ( $Conf{FullKeepCnt} <= 0 ) {
         print(LOG $bpc->timeStamp,
@@ -1088,6 +1123,7 @@ sub BackupExpire
            print(LOG $bpc->timeStamp,
                       "removing incr backup $Backups[$firstIncr]{num}\n");
             BackupRemove($client, \@Backups, $firstIncr);
            print(LOG $bpc->timeStamp,
                       "removing incr backup $Backups[$firstIncr]{num}\n");
             BackupRemove($client, \@Backups, $firstIncr);
+            $changes++;
             next;
         }
 
             next;
         }
 
@@ -1095,8 +1131,26 @@ sub BackupExpire
         # Delete any old full backups, according to $Conf{FullKeepCntMin}
        # and $Conf{FullAgeMax}.
         #
         # Delete any old full backups, according to $Conf{FullKeepCntMin}
        # and $Conf{FullAgeMax}.
         #
+       # First make sure that $Conf{FullAgeMax} is at least bigger
+       # than $Conf{FullPeriod} * $Conf{FullKeepCnt}, including
+       # the exponential array case.
+        #
+       my $fullKeepCnt = $Conf{FullKeepCnt};
+       $fullKeepCnt = [$fullKeepCnt] if ( ref($fullKeepCnt) ne "ARRAY" );
+       my $fullAgeMax;
+       my $fullPeriod = int(0.5 + $Conf{FullPeriod});
+        $fullPeriod = 7 if ( $fullPeriod <= 0 );
+       for ( my $i = 0 ; $i < @$fullKeepCnt ; $i++ ) {
+           $fullAgeMax += $fullKeepCnt->[$i] * $fullPeriod;
+           $fullPeriod *= 2;
+       }
+       $fullAgeMax += $fullPeriod;     # add some buffer
+
         if ( $cntFull > $Conf{FullKeepCntMin}
                && $oldestFull > $Conf{FullAgeMax}
         if ( $cntFull > $Conf{FullKeepCntMin}
                && $oldestFull > $Conf{FullAgeMax}
+               && $oldestFull > $fullAgeMax
+              && $Conf{FullKeepCntMin} > 0
+              && $Conf{FullAgeMax} > 0
                && (@Backups <= $firstFull + 1
                         || !$Backups[$firstFull + 1]{noFill}) ) {
             #
                && (@Backups <= $firstFull + 1
                         || !$Backups[$firstFull + 1]{noFill}) ) {
             #
@@ -1109,6 +1163,7 @@ sub BackupExpire
            print(LOG $bpc->timeStamp,
                    "removing old full backup $Backups[$firstFull]{num}\n");
             BackupRemove($client, \@Backups, $firstFull);
            print(LOG $bpc->timeStamp,
                    "removing old full backup $Backups[$firstFull]{num}\n");
             BackupRemove($client, \@Backups, $firstFull);
+            $changes++;
             next;
         }
 
             next;
         }
 
@@ -1118,7 +1173,7 @@ sub BackupExpire
         #
         last if ( !BackupFullExpire($client, \@Backups) );
     }
         #
         last if ( !BackupFullExpire($client, \@Backups) );
     }
-    $bpc->BackupInfoWrite($client, @Backups);
+    $bpc->BackupInfoWrite($client, @Backups) if ( $changes );
 }
 
 #
 }
 
 #
@@ -1172,8 +1227,8 @@ sub BackupFullExpire
             #
             # Delete the full backup
             #
             #
             # Delete the full backup
             #
-            #printf("Deleting backup $i ($prevFull)\n");
-            push(@delete, $i);
+            #print("Deleting backup $i ($prevFull)\n");
+            unshift(@delete, $i);
         } else {
             $fullCnt++;
             while ( $fullKeepIdx < @$fullKeepCnt
         } else {
             $fullCnt++;
             while ( $fullKeepIdx < @$fullKeepCnt
@@ -1212,6 +1267,7 @@ sub BackupSave
 {
     my @Backups = $bpc->BackupInfoRead($client);
     my $num  = -1;
 {
     my @Backups = $bpc->BackupInfoRead($client);
     my $num  = -1;
+    my $newFilesFH;
 
     #
     # Since we got a good backup we should remove any partial dumps
 
     #
     # Since we got a good backup we should remove any partial dumps
@@ -1254,7 +1310,18 @@ sub BackupSave
     $Backups[$i]{noFill}        = $type eq "incr" ? 1 : 0;
     $Backups[$i]{level}         = $type eq "incr" ? 1 : 0;
     $Backups[$i]{mangle}        = 1;        # name mangling always on for v1.04+
     $Backups[$i]{noFill}        = $type eq "incr" ? 1 : 0;
     $Backups[$i]{level}         = $type eq "incr" ? 1 : 0;
     $Backups[$i]{mangle}        = 1;        # name mangling always on for v1.04+
+    $Backups[$i]{xferMethod}    = $Conf{XferMethod};
+    $Backups[$i]{charset}       = $Conf{ClientCharset};
+    #
+    # Save the main backups file
+    #
     $bpc->BackupInfoWrite($client, @Backups);
     $bpc->BackupInfoWrite($client, @Backups);
+    #
+    # Save just this backup's info in case the main backups file
+    # gets corrupted
+    #
+    BackupPC::Storage->backupInfoWrite($Dir, $Backups[$i]{num},
+                                             $Backups[$i]);
 
     unlink("$Dir/timeStamp.level0") if ( -f "$Dir/timeStamp.level0" );
     foreach my $ext ( qw(bad bad.z) ) {
 
     unlink("$Dir/timeStamp.level0") if ( -f "$Dir/timeStamp.level0" );
     foreach my $ext ( qw(bad bad.z) ) {
@@ -1280,11 +1347,34 @@ sub BackupSave
                $file = "$f->{share}/$f->{file}";
            }
            next if ( !-f "$Dir/$Backups[$j]{num}/$file" );
                $file = "$f->{share}/$f->{file}";
            }
            next if ( !-f "$Dir/$Backups[$j]{num}/$file" );
-           if ( !link("$Dir/$Backups[$j]{num}/$file",
-                               "$Dir/$num/$shareM/$fileM") ) {
-               my $str = \"Unable to link $num/$f->{share}/$f->{file} to"
-                         . " $Backups[$j]{num}/$f->{share}/$f->{file}\n";
-               $XferLOG->write(\$str);
+
+            my($exists, $digest, $origSize, $outSize, $errs)
+                                = BackupPC::PoolWrite::LinkOrCopy(
+                                      $bpc,
+                                      "$Dir/$Backups[$j]{num}/$file",
+                                      $Backups[$j]{compress},
+                                      "$Dir/$num/$shareM/$fileM",
+                                      $Conf{CompressLevel});
+            if ( !$exists ) {
+                #
+                # the hard link failed, most likely because the target
+                # file has too many links.  We have copied the file
+                # instead, so add this to the new file list.
+                #
+                if ( !defined($newFilesFH) ) {
+                    my $str = "Appending to NewFileList for $shareM/$fileM\n";
+                    $XferLOG->write(\$str);
+                    open($newFilesFH, ">>", "$TopDir/pc/$client/NewFileList")
+                         || die("can't open $TopDir/pc/$client/NewFileList");
+                    binmode($newFilesFH);
+                }
+                if ( -f "$Dir/$num/$shareM/$fileM" ) {
+                    print($newFilesFH "$digest $origSize $shareM/$fileM\n");
+                } else {
+                    my $str = "Unable to link/copy $num/$f->{share}/$f->{file}"
+                            . " to $Backups[$j]{num}/$f->{share}/$f->{file}\n";
+                    $XferLOG->write(\$str);
+                }
            } else {
                my $str = "Bad file $num/$f->{share}/$f->{file} replaced"
                         . " by link to"
            } else {
                my $str = "Bad file $num/$f->{share}/$f->{file} replaced"
                         . " by link to"
@@ -1299,6 +1389,7 @@ sub BackupSave
            $XferLOG->write(\$str);
        }
     }
            $XferLOG->write(\$str);
        }
     }
+    close($newFilesFH) if ( defined($newFilesFH) );
     $XferLOG->close();
     rename("$Dir/XferLOG$fileExt", "$Dir/XferLOG.$num$fileExt");
     rename("$Dir/NewFileList", "$Dir/NewFileList.$num");
     $XferLOG->close();
     rename("$Dir/XferLOG$fileExt", "$Dir/XferLOG.$num$fileExt");
     rename("$Dir/NewFileList", "$Dir/NewFileList.$num");
@@ -1314,6 +1405,11 @@ sub BackupRemove
     my($client, $Backups, $idx) = @_;
     my($Dir) = "$TopDir/pc/$client";
 
     my($client, $Backups, $idx) = @_;
     my($Dir) = "$TopDir/pc/$client";
 
+    if ( $Backups->[$idx]{num} eq "" ) {
+        print("BackupRemove: ignoring empty backup number for idx $idx\n");
+        return;
+    }
+
     $bpc->RmTreeDefer("$TopDir/trash",
                       "$Dir/$Backups->[$idx]{num}");
     unlink("$Dir/SmbLOG.$Backups->[$idx]{num}")
     $bpc->RmTreeDefer("$TopDir/trash",
                       "$Dir/$Backups->[$idx]{num}");
     unlink("$Dir/SmbLOG.$Backups->[$idx]{num}")
@@ -1360,9 +1456,9 @@ sub pidHandler
 #
 sub UserCommandRun
 {
 #
 sub UserCommandRun
 {
-    my($type) = @_;
+    my($cmdType, $sharename) = @_;
 
 
-    return if ( !defined($Conf{$type}) );
+    return if ( !defined($Conf{$cmdType}) );
     my $vars = {
         xfer       => $xfer,
         client     => $client,
     my $vars = {
         xfer       => $xfer,
         client     => $client,
@@ -1380,9 +1476,15 @@ sub UserCommandRun
         xferOK     => $stat{xferOK} || 0,
        hostError  => $stat{hostError},
        type       => $type,
         xferOK     => $stat{xferOK} || 0,
        hostError  => $stat{hostError},
        type       => $type,
+       cmdType    => $cmdType,
     };
     };
-    my $cmd = $bpc->cmdVarSubstitute($Conf{$type}, $vars);
-    $XferLOG->write(\"Executing $type: @$cmd\n");
+
+    if ($cmdType eq 'DumpPreShareCmd' || $cmdType eq 'DumpPostShareCmd') {
+       $vars->{share} = $sharename;
+    }
+
+    my $cmd = $bpc->cmdVarSubstitute($Conf{$cmdType}, $vars);
+    $XferLOG->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
     #
     # Run the user's command, dumping the stdout/stderr into the
     # Xfer log file.  Also supply the optional $vars and %Conf in