* When there is an existing partial, a new partials is only saved
[BackupPC.git] / bin / BackupPC_dump
index d0925f0..7c3e33c 100755 (executable)
@@ -52,7 +52,7 @@
 #   Craig Barratt  <cbarratt@users.sourceforge.net>
 #
 # COPYRIGHT
-#   Copyright (C) 2001-2003  Craig Barratt
+#   Copyright (C) 2001-2007  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
@@ -70,7 +70,7 @@
 #
 #========================================================================
 #
-# Version 3.0.0, released 28 Jan 2007.
+# Version 3.1.0beta0, released 3 Sep 2007.
 #
 # See http://backuppc.sourceforge.net.
 #
@@ -303,6 +303,7 @@ my $lastFullTime = 0;
 my $lastIncrTime = 0;
 my $partialIdx = -1;
 my $partialNum;
+my $partialFileCnt;
 my $lastBkupNum;
 my $lastPartial = 0;
 
@@ -388,7 +389,6 @@ my(@lastIdxByLevel, $incrCntSinceFull);
 # as a starting point for an incremental.
 #
 @Backups = $bpc->BackupInfoRead($client);
-## @Backups = sort( { $a->{startTime} <=> $b->{startTime} }, @Backups);
 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
     $needLink = 1 if ( $Backups[$i]{nFilesNew} eq ""
                         || -f "$Dir/NewFileList.$Backups[$i]{num}" );
@@ -406,9 +406,10 @@ for ( my $i = 0 ; $i < @Backups ; $i++ ) {
         $lastIncrTime = $Backups[$i]{startTime}
                 if ( $lastIncrTime < $Backups[$i]{startTime} );
     } elsif ( $Backups[$i]{type} eq "partial" ) {
-        $partialIdx  = $i;
-        $lastPartial = $Backups[$i]{startTime};
-        $partialNum  = $Backups[$i]{num};
+        $partialIdx     = $i;
+        $lastPartial    = $Backups[$i]{startTime};
+        $partialNum     = $Backups[$i]{num};
+        $partialFileCnt = $Backups[$i]{nFiles};
     }
 }
 
@@ -457,6 +458,19 @@ if ( @Backups == 0
     NothingToDo($needLink);
 }
 
+if ( !$bpc->HardlinkTest($Dir, "$TopDir/cpool") ) {
+    print(LOG $bpc->timeStamp, "Can't create a test hardlink between a file"
+               . " in $Dir and $TopDir/cpool.  Either these are different"
+               . " file systems, or this file system doesn't support hardlinks,"
+               . " or these directories don't exist, or there is a permissions"
+               . " problem, or the file system is out of inodes or full.  Use"
+               . " df, df -i, and ls -ld to check each of these possibilities."
+               . " Quitting...\n");
+    print("test hardlink between $Dir and $TopDir/cpool failed\n");
+    print("link $clientURI\n") if ( $needLink );
+    exit(1);
+}
+
 #
 # Check if $host is alive
 #
@@ -908,6 +922,15 @@ if ( $type eq "full" && $stat{hostError} eq ""
 
 $stat{xferOK} = 0 if ( $Abort );
 
+#
+# If there is no "new" directory then the backup is bad
+#
+if ( $stat{xferOK} && !-d "$Dir/new" ) {
+    $stat{hostError} = "No backup directory $Dir/new"
+                            if ( $stat{hostError} eq "" );
+    $stat{xferOK} = 0;
+}
+
 #
 # Do one last check to make sure it is still the machine we expect.
 #
@@ -1107,7 +1130,9 @@ sub BackupFailCleanup
 
     #
     # We keep this backup if it is a full and we actually backed
-    # up some files.
+    # up some files.  If the prior backup was a partial too, we
+    # only keep this backup if it has more files than the previous
+    # partial.
     #
     if ( $type eq "full" ) {
        if ( $nFilesTotal == 0 && $xfer->getStats->{fileCnt} == 0 ) {
@@ -1116,13 +1141,29 @@ sub BackupFailCleanup
            # directory just in case.
            #
            find(\&CheckForNewFiles, "$Dir/new");
-           $keepPartial = 1 if ( $nFilesTotal );
-       } else {
-           #
-           # Xfer reported some files
-           #
+        }
+        my $str;
+        if ( $nFilesTotal > $partialFileCnt
+                || $xfer->getStats->{fileCnt} > $partialFileCnt ) {
+            #
+            # If the last backup wasn't a partial then
+            # $partialFileCnt is undefined, so the above
+            # test is simply $nFilesTotal > 0
+            #
            $keepPartial = 1;
-       }
+            if ( $partialFileCnt ) {
+                $str = "Saving this as a partial backup\n";
+            } else {
+                $str = sprintf("Saving this as a partial backup, replacing the"
+                         . " prior one (got %d and %d files versus %d)\n",
+                         $nFilesTotal, $xfer->getStats->{fileCnt}, $partialFileCnt);
+            }
+       } else {
+            $str = sprintf("Not saving this as a partial backup since it has fewer"
+                     . " files than the prior one (got %d and %d files versus %d)\n",
+                     $nFilesTotal, $xfer->getStats->{fileCnt}, $partialFileCnt);
+        }
+        $XferLOG->write(\$str);
     }
 
     #
@@ -1379,6 +1420,7 @@ sub BackupSave
     # (the new backup might also be a partial, but that's ok).
     #
     BackupPartialRemove($client, \@Backups);
+    $needLink = 1 if ( -f "$Dir/NewFileList" );
 
     #
     # Number the new backup
@@ -1391,8 +1433,8 @@ sub BackupSave
     if ( !rename("$Dir/new", "$Dir/$num") ) {
         print(LOG $bpc->timeStamp, "Rename $Dir/new -> $Dir/$num failed\n");
         $stat{xferOK} = 0;
+        return;
     }
-    $needLink = 1 if ( -f "$Dir/NewFileList" );
 
     #
     # Add the new backup information to the backup file