# 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:
#
#========================================================================
#
-# Version 2.1.0, released 20 Jun 2004.
+# Version 3.0.0alpha, released 23 Jan 2006.
#
# See http://backuppc.sourceforge.net.
#
use Socket;
use Carp;
use Digest::MD5;
+use POSIX qw(setsid);
###########################################################################
# Handle command line options
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();
#
#
# 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";
}
#
if ( $opts{d} ) {
#
- # daemonize by forking
+ # daemonize by forking; more robust method per:
+ # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=301057
#
- defined(my $pid = fork) or die "Can't fork: $!";
+ my $pid;
+ defined($pid = fork) or die("Can't fork: $!");
+ exit if( $pid ); # parent exits
+
+ POSIX::setsid();
+ defined($pid = fork) or die("Can't fork: $!");
exit if $pid; # parent exits
+
+ chdir ("/") or die("Cannot chdir to /: $!\n");
+ close(STDIN);
+ open(STDIN , ">/dev/null") or die("Cannot open /dev/null as stdin\n");
+ # STDOUT and STDERR are handled in LogFileOpen() right below,
+ # otherwise we would have to reopen them too.
}
#
# 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);
}
#
# 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++ ) {
#
# 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;
$CmdQueueOn{$job} = 1;
}
$RunNightlyWhenIdle = 2;
-
}
}
$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");
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();
#
###########################################################################
#
-# Write the current status to $TopDir/log/status.pl
+# Write the current status to $LogDir/status.pl
#
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);
}
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;
#
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");
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");
}
delete($Info{pid});
StatusWrite();
- unlink("$TopDir/log/BackupPC.pid");
+ unlink("$LogDir/BackupPC.pid");
exit(1);
}