* Various changes for 3.0.0beta1
[BackupPC.git] / bin / BackupPC
index 4c13633..3e55f7f 100755 (executable)
@@ -6,7 +6,7 @@
 # DESCRIPTION
 #
 #   BackupPC reads the configuration and status information from
-#   $TopDir/conf.  It then runs and manages all the backup activity.
+#   $ConfDir/conf.  It then runs and manages all the backup activity.
 #
 #   As specified by $Conf{WakeupSchedule}, BackupPC wakes up periodically
 #   to queue backups on all the PCs.  This is a four step process:
@@ -47,7 +47,7 @@
 #
 #========================================================================
 #
-# Version 2.1.0, released 20 Jun 2004.
+# Version 3.0.0beta1, released 30 Jul 2006.
 #
 # See http://backuppc.sourceforge.net.
 #
@@ -87,6 +87,7 @@ if ( !getopts("d", \%opts) || @ARGV != 0 ) {
 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
 my $TopDir = $bpc->TopDir();
 my $BinDir = $bpc->BinDir();
+my $LogDir = $bpc->LogDir();
 my %Conf   = $bpc->Conf();
 
 #
@@ -111,10 +112,10 @@ if ( $Conf{BackupPCUserVerify}
 #
 # Read old status
 #
-if ( -f "$TopDir/log/status.pl" && !(my $ret = do "$TopDir/log/status.pl") ) {
-   die "couldn't parse $TopDir/log/status.pl: $@" if $@;
-   die "couldn't do $TopDir/log/status.pl: $!"    unless defined $ret;
-   die "couldn't run $TopDir/log/status.pl";
+if ( -f "$LogDir/status.pl" && !(my $ret = do "$LogDir/status.pl") ) {
+   die "couldn't parse $LogDir/status.pl: $@" if $@;
+   die "couldn't do $LogDir/status.pl: $!"    unless defined $ret;
+   die "couldn't run $LogDir/status.pl";
 }
 
 #
@@ -344,7 +345,7 @@ sub Main_Initialize
     # Write out our initial status and save our PID
     #
     StatusWrite();
-    if ( open(PID, ">", "$TopDir/log/BackupPC.pid") ) {
+    if ( open(PID, ">", "$LogDir/BackupPC.pid") ) {
         print(PID $$);
         close(PID);
     }
@@ -387,7 +388,7 @@ sub Main_TryToRun_nightly
            });
        $CmdQueueOn{$bpc->trashJob} = 1;
     }
-    if ( keys(%Jobs) == $trashCleanRunning && $RunNightlyWhenIdle == 1 ) {
+    if ( $RunNightlyWhenIdle == 1 ) {
 
         #
         # Queue multiple nightly jobs based on the configuration
@@ -416,9 +417,6 @@ sub Main_TryToRun_nightly
 
        #
        # Zero out the data we expect to get from BackupPC_nightly.
-       # In the future if we want to split BackupPC_nightly over
-       # more than one night we will only zero out the portion
-       # that we are running right now.
        #
        for my $p ( qw(pool cpool) ) {
            for ( my $i = $start ; $i < $end ; $i++ ) {
@@ -442,7 +440,7 @@ sub Main_TryToRun_nightly
 
        #
        # Now queue the $Conf{MaxBackupPCNightlyJobs} jobs.
-       # The granularity on start and end is now 0..256.
+       # The granularity on start and end is now 0..255.
        #
        $start *= 16;
        $end   *= 16;
@@ -470,7 +468,6 @@ sub Main_TryToRun_nightly
             $CmdQueueOn{$job} = 1;
         }
         $RunNightlyWhenIdle = 2;
-
     }
 }
 
@@ -609,7 +606,14 @@ sub Main_TryToRun_Bg_or_User_Queue
         }
     }
 
-    while ( $RunNightlyWhenIdle == 0 ) {
+    #
+    # Run background jobs anytime.  Previously they were locked out
+    # when BackupPC_nightly was running or pending with this
+    # condition on the while loop:
+    #
+    #    while ( $RunNightlyWhenIdle == 0 )
+    #
+    while ( 1 ) {
         local(*FH);
         my(@args, $progName, $type);
         my $nJobs = keys(%Jobs);
@@ -807,15 +811,15 @@ sub Main_Check_Timeout
         $Info{DUDailySkipHostCntPrev} = $Info{DUDailySkipHostCnt};
         $Info{DUDailySkipHostCnt}     = 0;
         my $lastLog = $Conf{MaxOldLogFiles} - 1;
-        if ( -f "$TopDir/log/LOG.$lastLog" ) {
+        if ( -f "$LogDir/LOG.$lastLog" ) {
             print(LOG $bpc->timeStamp,
-                       "Removing $TopDir/log/LOG.$lastLog\n");
-            unlink("$TopDir/log/LOG.$lastLog");
+                       "Removing $LogDir/LOG.$lastLog\n");
+            unlink("$LogDir/LOG.$lastLog");
         }
-        if ( -f "$TopDir/log/LOG.$lastLog.z" ) {
+        if ( -f "$LogDir/LOG.$lastLog.z" ) {
             print(LOG $bpc->timeStamp,
-                       "Removing $TopDir/log/LOG.$lastLog.z\n");
-            unlink("$TopDir/log/LOG.$lastLog.z");
+                       "Removing $LogDir/LOG.$lastLog.z\n");
+            unlink("$LogDir/LOG.$lastLog.z");
         }
         print(LOG $bpc->timeStamp, "Aging LOG files, LOG -> LOG.0 -> "
                    . "LOG.1 -> ... -> LOG.$lastLog\n");
@@ -824,22 +828,23 @@ sub Main_Check_Timeout
         close(LOG);
         for ( my $i = $lastLog - 1 ; $i >= 0 ; $i-- ) {
             my $j = $i + 1;
-            rename("$TopDir/log/LOG.$i", "$TopDir/log/LOG.$j")
-                            if ( -f "$TopDir/log/LOG.$i" );
-            rename("$TopDir/log/LOG.$i.z", "$TopDir/log/LOG.$j.z")
-                            if ( -f "$TopDir/log/LOG.$i.z" );
+            rename("$LogDir/LOG.$i", "$LogDir/LOG.$j")
+                            if ( -f "$LogDir/LOG.$i" );
+            rename("$LogDir/LOG.$i.z", "$LogDir/LOG.$j.z")
+                            if ( -f "$LogDir/LOG.$i.z" );
         }
        #
        # Compress the log file LOG -> LOG.0.z (if enabled).
        # Otherwise, just rename LOG -> LOG.0.
        #
-       BackupPC::FileZIO->compressCopy("$TopDir/log/LOG",
-                                        "$TopDir/log/LOG.0.z",
-                                        "$TopDir/log/LOG.0",
+       BackupPC::FileZIO->compressCopy("$LogDir/LOG",
+                                        "$LogDir/LOG.0.z",
+                                        "$LogDir/LOG.0",
                                         $Conf{CompressLevel}, 1);
         LogFileOpen();
         #
-        # Remember to run nightly script after current jobs are done
+        # Remember to run the nightly script when the next CmdQueue
+        # job is done.
         #
         $RunNightlyWhenIdle = 1;
     }
@@ -1070,7 +1075,8 @@ sub Main_Check_Job_Messages
                     #
                     # This means the last BackupPC_nightly is done with
                    # the pool clean, so it's ok to start running regular
-                   # backups again.
+                   # backups again.  But starting in 3.0 regular jobs
+                    # are decoupled from BackupPC_nightly.
                     #
                     $RunNightlyWhenIdle = 0;
                }
@@ -1100,6 +1106,9 @@ sub Main_Check_Job_Messages
                    #print(LOG $bpc->timeStamp, "BackupPC_nightly done; now"
                    #         . " have $BackupPCNightlyJobs running\n");
                     if ( $BackupPCNightlyJobs <= 0 ) {
+                        #
+                        # Last BackupPC_nightly has finished
+                        #
                         $BackupPCNightlyJobs = 0;
                         $RunNightlyWhenIdle = 0;
                         $CmdJob = "";
@@ -1552,7 +1561,7 @@ sub Main_Check_Client_Messages
 ###########################################################################
 
 #
-# Write the current status to $TopDir/log/status.pl
+# Write the current status to $LogDir/status.pl
 #
 sub StatusWrite
 {
@@ -1560,7 +1569,7 @@ sub StatusWrite
              [  \%Info, \%Status],
              [qw(*Info   *Status)]);
     $dump->Indent(1);
-    if ( open(STATUS, ">", "$TopDir/log/status.pl") ) {
+    if ( open(STATUS, ">", "$LogDir/status.pl") ) {
         print(STATUS $dump->Dump);
         close(STATUS);
     }
@@ -1761,7 +1770,7 @@ sub catch_signal
         close(LOG);
         LogFileOpen();
         print(LOG "Fatal error: unhandled signal $SigName\n");
-        unlink("$TopDir/log/BackupPC.pid");
+        unlink("$LogDir/BackupPC.pid");
         confess("Got new signal $SigName... quitting\n");
     } else {
        $SigName = shift;
@@ -1773,9 +1782,9 @@ sub catch_signal
 #
 sub LogFileOpen
 {
-    mkpath("$TopDir/log", 0, 0777) if ( !-d "$TopDir/log" );
-    open(LOG, ">>$TopDir/log/LOG")
-            || die("Can't create LOG file $TopDir/log/LOG");
+    mkpath($LogDir, 0, 0777) if ( !-d $LogDir );
+    open(LOG, ">>$LogDir/LOG")
+            || die("Can't create LOG file $LogDir/LOG");
     close(STDOUT);
     close(STDERR);
     open(STDOUT, ">&LOG");
@@ -1800,7 +1809,7 @@ sub ServerSocketInit
             print(LOG $bpc->timeStamp, "unix socket() failed: $!\n");
             exit(1);
         }
-        my $sockFile = "$TopDir/log/BackupPC.sock";
+        my $sockFile = "$LogDir/BackupPC.sock";
         unlink($sockFile);
         if ( !bind(SERVER_UNIX, sockaddr_un($sockFile)) ) {
             print(LOG $bpc->timeStamp, "unix bind() failed: $!\n");
@@ -1883,7 +1892,7 @@ sub ServerShutdown
     }
     delete($Info{pid});
     StatusWrite();
-    unlink("$TopDir/log/BackupPC.pid");
+    unlink("$LogDir/BackupPC.pid");
     exit(1);
 }