-#!/bin/perl
+#!/usr/bin/perl
#============================================================= -*-perl-*-
#
# BackupPC: Main program for PC backups.
# Craig Barratt <cbarratt@users.sourceforge.net>
#
# COPYRIGHT
-# Copyright (C) 2001-2007 Craig Barratt
+# Copyright (C) 2001-2009 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
#
#========================================================================
#
-# Version 3.1.0beta0, released 3 Sep 2007.
+# Version 3.2.0, released 31 Jul 2010.
#
# See http://backuppc.sourceforge.net.
#
#
my $pid;
defined($pid = fork) or die("Can't fork: $!");
- exit if( $pid ); # parent exits
+ exit if ( $pid ); # parent exits
POSIX::setsid();
defined($pid = fork) or die("Can't fork: $!");
$CmdQueueOn{$bpc->trashJob} = 1;
}
if ( $RunNightlyWhenIdle == 1 ) {
-
#
# Queue multiple nightly jobs based on the configuration
#
$start = $start0 + int(($end - $start0)
* ($i + 1) / $Conf{MaxBackupPCNightlyJobs});
push(@$cmd, $start - 1);
-
my $job = $bpc->adminJob($i);
unshift(@CmdQueue, {
host => $job,
if ( !$pid ) {
setpgrp 0,0;
$ENV{BPC_REQUSER} = $req->{user};
+ POSIX::nice($Conf{CmdQueueNice}) if ( $Conf{CmdQueueNice} );
exec(@$cmd);
print(LOG $bpc->timeStamp, "can't exec @$cmd for $host\n");
exit(0);
push(@deferUserQueue, $req);
next;
}
- push(@args, $req->{doFull} ? "-f" : "-i")
- if (( !$req->{restore} ) && ( !$req->{archive} ));
$UserQueueOn{$req->{host}} = 0;
} elsif ( $nJobs < $Conf{MaxBackups}
&& (@CmdQueue + $nJobs)
} else {
$progName = "BackupPC_dump";
$type = "backup";
- push(@args, "-d") if ( $req->{dhcp} );
+ push(@args, "-I") if ( $req->{backupType} eq "autoIncr" );
+ push(@args, "-F") if ( $req->{backupType} eq "autoFull" );
+ push(@args, "-i") if ( $req->{backupType} eq "doIncr" );
+ push(@args, "-f") if ( $req->{backupType} eq "doFull" );
+ push(@args, "-d") if ( $req->{backupType} eq "dhcpPoll" );
push(@args, "-e") if ( $req->{dumpExpire} );
push(@args, $host);
}
# Remember to run the nightly script when the next CmdQueue
# job is done.
#
- $RunNightlyWhenIdle = 1;
+ if ( $RunNightlyWhenIdle == 2 ) {
+ print(LOG $bpc->timeStamp, "BackupPC_nightly is still running after 24 hours!!"
+ . " You should adjust the config settings; Skipping this run\n");
+ } else {
+ $RunNightlyWhenIdle = 1;
+ }
}
#
# Write out the current status and then queue all the PCs
if ( $Status{$host}{dhcpCheckCnt} > 0 );
} elsif ( $mesg =~ /^xferPids (.*)/ ) {
$Jobs{$host}{xferPid} = $1;
+ } elsif ( $mesg =~ /^completionPercent (.*)/ ) {
+ $Jobs{$host}{completionPercent} = $1;
} elsif ( $mesg =~ /^started_restore/ ) {
$Jobs{$host}{type} = "restore";
print(LOG $bpc->timeStamp,
} elsif ( $cmd =~ /^BackupPC_nightly run$/ ) {
$RunNightlyWhenIdle = 1;
} elsif ( $cmd =~ /^backup (\S+)\s+(\S+)\s+(\S+)\s+(\S+)/ ) {
- my $hostIP = $1;
- $host = $2;
- my $user = $3;
- my $doFull = $4;
- $host = $bpc->uriUnesc($host);
- $hostIP = $bpc->uriUnesc($hostIP);
- if ( !defined($Status{$host}) ) {
+ my $hostIP = $1;
+ $host = $2;
+ my $user = $3;
+ my $backupType = $4;
+ $host = $bpc->uriUnesc($host);
+ $hostIP = $bpc->uriUnesc($hostIP);
+ if ( !defined($Hosts->{$host}) ) {
print(LOG $bpc->timeStamp,
"User $user requested backup of unknown host"
. " $host\n");
$reply = "error: unknown host $host";
} else {
- print(LOG $bpc->timeStamp,
- "User $user requested backup of $host"
- . " ($hostIP)\n");
- if ( $BgQueueOn{$hostIP} ) {
- @BgQueue = grep($_->{host} ne $hostIP, @BgQueue);
- $BgQueueOn{$hostIP} = 0;
- }
- if ( $UserQueueOn{$hostIP} ) {
- @UserQueue = grep($_->{host} ne $hostIP, @UserQueue);
- $UserQueueOn{$hostIP} = 0;
+ #
+ # Handle numeric backupType for backward compatibility
+ # (technically -1 is a new feature for auto)
+ #
+ $backupType = 'auto' if ( $backupType eq '-1' );
+ $backupType = 'doIncr' if ( $backupType eq '0' );
+ $backupType = 'doFull' if ( $backupType eq '1' );
+ if ( $backupType !~ /^doIncr|doFull|autoIncr|autoFull|auto$/i ) {
+ $reply = "error: unknown backup type $backupType";
+ } else {
+ print(LOG $bpc->timeStamp,
+ "User $user requested backup of $host"
+ . " ($hostIP)\n");
+ if ( $BgQueueOn{$hostIP} ) {
+ @BgQueue = grep($_->{host} ne $hostIP, @BgQueue);
+ $BgQueueOn{$hostIP} = 0;
+ }
+ if ( $UserQueueOn{$hostIP} ) {
+ @UserQueue = grep($_->{host} ne $hostIP, @UserQueue);
+ $UserQueueOn{$hostIP} = 0;
+ }
+ my $status = QueueOnePC($host, $hostIP, $user, 'user', $backupType);
+ if ( $status == 0 ) {
+ $reply = "ok: requested backup of $host ($backupType)";
+ } elsif ( $status == 1 ) {
+ #should never see this we just dequeued it
+ $reply = "warning: $host was already queued."
+ . " Ignoring this request";
+ } elsif ( $status == 2 ) {
+ print(LOG $bpc->timeStamp,
+ "Disk too full ($Info{DUlastValue}%)."
+ . " Not queueing backup of $host\n");
+ $reply = "error: disk too full ($Info{DUlastValue}%)";
+ $Info{DUDailySkipHostCnt}++;
+ } elsif ( $status == 3 ) {
+ # should never reach this because
+ # it's set to "user" above
+ $reply = "error: unknown queue name";
+ } else {
+ $reply = "error: unknown queue status $status";
+ if ( $BgQueueOn{$hostIP} || $UserQueueOn{$hostIP} ) {
+ $reply .= ". Host is queued.";
+ } else {
+ $reply .= ". Host is not queued.";
+ }
+ }
}
- unshift(@UserQueue, {
- host => $hostIP,
- user => $user,
- reqTime => time,
- doFull => $doFull,
- userReq => 1,
- dhcp => $hostIP eq $host ? 0 : 1,
- });
- $UserQueueOn{$hostIP} = 1;
- $reply = "ok: requested backup of $host";
}
} elsif ( $cmd =~ /^archive (\S+)\s+(\S+)\s+(\S+)/ ) {
my $user = $1;
} else {
unshift(@UserQueue, {
host => $host,
- hostIP => $user,
+ user => $user,
reqFileName => $reqFileName,
reqTime => time,
dhcp => 0,
my $reqFileName = $4;
$host = $bpc->uriUnesc($host);
$hostIP = $bpc->uriUnesc($hostIP);
- if ( !defined($Status{$host}) ) {
+ if ( !defined($Hosts->{$host}) ) {
print(LOG $bpc->timeStamp,
"User $user requested restore to unknown host"
. " $host");
[ \%Info, \%Status],
[qw(*Info *Status)]);
$dump->Indent(1);
- $bpc->{storage}->TextFileWrite("$LogDir/status.pl", $dump->Dump);
+ my $text = $dump->Dump;
+ $bpc->{storage}->TextFileWrite("$LogDir/status.pl", $text);
}
#
#
# Hosts with no errors go after hosts with errors
#
-
return 1 if ( $Status{$a}{error} eq "" && $Status{$b}{error} ne "" );
#
}
#
-# Queue all the hosts for backup. This means queuing all the fixed
-# ip hosts and all the dhcp address ranges. We also additionally
-# queue the dhcp hosts with a -e flag to check for expired dumps.
+# Attempt to queue a host.
+# Returns 0 on success; 1 if host is already queued;
+# 2 if host was skipped; 3 on invalid queue name
#
-sub QueueAllPCs
+# $host is the client's host name
+# $hostIP is usually the client's host name too, or IP address
+# if the user specified it in the manual backup command
+# $user is the user name, or BackupPC by default
+# $queue is which queue to use ("bg" by default)
+# $backupType is the backup type (doIncr|doFull|autoIncr|autoFull|auto|dhcpPoll)
+#
+# Note: starting in 3.2.0, the PC is queued even if it has a current
+# job running
+#
+sub QueueOnePC
{
- my $nSkip = 0;
- foreach my $host ( sort(HostSortCompare keys(%$Hosts)) ) {
- delete($Status{$host}{backoffTime})
- if ( defined($Status{$host}{backoffTime})
+ my($host, $hostIP, $user, $queue, $backupType) = @_;
+ my $retVal = 0;
+ $user = "BackupPC" if ( $user eq '' );
+ $queue = "bg" if ( $queue eq '' && $user eq 'BackupPC' );
+ $backupType = "auto" if ( $backupType eq '' );
+
+ delete($Status{$host}{backoffTime})
+ if ( defined($Status{$host}{backoffTime})
&& $Status{$host}{backoffTime} < time );
- next if ( defined($Jobs{$host})
- || $BgQueueOn{$host}
- || $UserQueueOn{$host}
- || $CmdQueueOn{$host} );
- if ( $Hosts->{$host}{dhcp} ) {
- $Status{$host}{dhcpCheckCnt}++;
- if ( $RunNightlyWhenIdle ) {
- #
- # Once per night queue a check for DHCP hosts that just
- # checks for expired dumps. We need to do this to handle
- # the case when a DHCP host has not been on the network for
- # a long time, and some of the old dumps need to be expired.
- # Normally expiry checks are done by BackupPC_dump only
- # after the DHCP hosts has been detected on the network.
- #
- unshift(@BgQueue,
- {host => $host, user => "BackupPC", reqTime => time,
- dhcp => 0, dumpExpire => 1});
- $BgQueueOn{$host} = 1;
- }
- } else {
+ return 1 if ( $BgQueueOn{$host} || $UserQueueOn{$host} );
+ if ( $Hosts->{$host}{dhcp} ) {
+ $Status{$host}{dhcpCheckCnt}++;
+ if ( $RunNightlyWhenIdle ) {
#
- # this is a fixed ip host: queue it
+ # Once per night queue a check for DHCP hosts that just
+ # checks for expired dumps. We need to do this to handle
+ # the case when a DHCP host has not been on the network for
+ # a long time, and some of the old dumps need to be expired.
+ # Normally expiry checks are done by BackupPC_dump only
+ # after the DHCP hosts has been detected on the network.
#
- if ( $Info{DUlastValue} > $Conf{DfMaxUsagePct} ) {
- #
- # Since we are out of disk space, instead of queuing
- # a regular job, queue an expire check instead. That
- # way if the admin reduces the number of backups to
- # keep then we will actually delete them. Otherwise
- # BackupPC_dump will never run since we have exceeded
- # the limit.
- #
- $nSkip++;
- unshift(@BgQueue,
- {host => $host, user => "BackupPC", reqTime => time,
- dhcp => $Hosts->{$host}{dhcp}, dumpExpire => 1});
- } else {
- #
- # Queue regular background backup
- #
- unshift(@BgQueue,
- {host => $host, user => "BackupPC", reqTime => time,
- dhcp => $Hosts->{$host}{dhcp}});
- }
+ unshift(@BgQueue,
+ {host => $hostIP, user => $user, reqTime => time,
+ dhcp => 0, dumpExpire => 1});
+ $BgQueueOn{$host} = 1;
+ }
+ } else {
+ #
+ # this is a fixed ip host or DHCP ip address: queue it
+ #
+ if ( $Info{DUlastValue} > $Conf{DfMaxUsagePct} ) {
+ #
+ # Since we are out of disk space, instead of queuing
+ # a regular job, queue an expire check instead. That
+ # way if the admin reduces the number of backups to
+ # keep then we will actually delete them. Otherwise
+ # BackupPC_dump will never run since we have exceeded
+ # the limit.
+ #
+ $retVal = 2;
+ unshift(@BgQueue,
+ {host => $hostIP, user => $user, reqTime => time, dumpExpire => 1});
+ $BgQueueOn{$host} = 1;
+ } elsif( $queue eq 'bg' ) {
+ #
+ # Queue regular background backup
+ #
+ unshift(@BgQueue,
+ {host => $hostIP, user => $user, reqTime => time, backupType => $backupType});
$BgQueueOn{$host} = 1;
+ } elsif( $queue eq 'user' ) {
+ #
+ # Queue user backup
+ #
+ unshift(@UserQueue,
+ {host => $hostIP, user => $user, reqTime => time, backupType => $backupType});
+ $UserQueueOn{$host} = 1;
+ } else {
+ # unknown $queue type
+ $retVal = 3;
}
}
- if ( $nSkip ) {
- print(LOG $bpc->timeStamp,
- "Disk too full ($Info{DUlastValue}%); skipped $nSkip hosts\n");
- $Info{DUDailySkipHostCnt} += $nSkip;
+
+ return $retVal;
+}
+
+#
+# Queue all the hosts for backup. This means queuing all the fixed
+# ip hosts and all the dhcp address ranges. We also additionally
+# queue the dhcp hosts with a -e flag to check for expired dumps.
+#
+sub QueueAllPCs
+{
+ my $nSkip = 0;
+
+ foreach my $host ( sort HostSortCompare keys(%$Hosts) ) {
+ $nSkip++ if ( QueueOnePC($host, $host, 'BackupPC', 'bg', 'auto') == 2 );
}
foreach my $dhcp ( @{$Conf{DHCPAddressRanges}} ) {
for ( my $i = $dhcp->{first} ; $i <= $dhcp->{last} ; $i++ ) {
my $ipAddr = "$dhcp->{ipAddrBase}.$i";
- next if ( defined($Jobs{$ipAddr})
- || $BgQueueOn{$ipAddr}
- || $UserQueueOn{$ipAddr}
- || $CmdQueueOn{$ipAddr} );
- #
- # this is a potential dhcp ip address (we don't know the
- # host name yet): queue it
- #
- unshift(@BgQueue,
- {host => $ipAddr, user => "BackupPC", reqTime => time,
- dhcp => 1});
- $BgQueueOn{$ipAddr} = 1;
+ $nSkip++ if ( QueueOnePC($ipAddr, $ipAddr, 'BackupPC', 'bg', 'dhcpPoll') == 2 );
}
}
+ if ( $nSkip ) {
+ print(LOG $bpc->timeStamp,
+ "Disk too full ($Info{DUlastValue}%); skipped $nSkip hosts\n");
+ $Info{DUDailySkipHostCnt} += $nSkip;
+ }
}
#