-#!/bin/perl
+#!/usr/bin/perl
#============================================================= -*-perl-*-
#
# BackupPC_dump: Dump a single client.
#
# DESCRIPTION
#
-# Usage: BackupPC_dump [-i] [-f] [-d] [-e] [-v] <client>
+# Usage: BackupPC_dump [-i] [-f] [-F] [-I] [-d] [-e] [-v] <client>
#
# Flags:
#
#
# -f Do a full dump, overriding any scheduling.
#
+# -I Do an increment dump if the regular schedule requires a
+# full or incremental, otherwise do nothing (a full is done
+# if no dumps have yet succeeded)
+#
+# -F Do a full dump if the regular schedule requires a
+# full or incremental, otherwise do nothing
+#
# -d Host is a DHCP pool address, and the client argument
# just an IP address. We lookup the NetBios name from
# the IP address.
# 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.0, released 25 Nov 2007.
+# Version 3.2.0, released 31 Jul 2010.
#
# See http://backuppc.sourceforge.net.
#
use BackupPC::Lib;
use BackupPC::FileZIO;
use BackupPC::Storage;
-use BackupPC::Xfer::Smb;
-use BackupPC::Xfer::Tar;
-use BackupPC::Xfer::Rsync;
-use BackupPC::Xfer::BackupPCd;
+use BackupPC::Xfer;
use Encode;
use Socket;
use File::Path;
$bpc->ChildInit();
my %opts;
-if ( !getopts("defiv", \%opts) || @ARGV != 1 ) {
- print("usage: $0 [-d] [-e] [-f] [-i] [-v] <client>\n");
+if ( !getopts("defivFI", \%opts) || @ARGV != 1 ) {
+ print("usage: $0 [-d] [-e] [-f] [-i] [-F] [-I] [-v] <client>\n");
exit(1);
}
if ( $ARGV[0] !~ /^([\w\.\s-]+)$/ ) {
print("$0: bad client name '$ARGV[0]'\n");
exit(1);
}
+if ( (defined($opts{f}) + defined($opts{i}) + defined($opts{F}) + defined($opts{I})) > 1 ) {
+ print("$0: exiting because you can only use one of -f, -i, -F, and -I\n");
+ exit(1);
+}
+
my $client = $1; # BackupPC's client name (might not be real host name)
my $hostIP; # this is the IP address
my $host; # this is the real host name
my $Dir = "$TopDir/pc/$client";
my @xferPid = ();
my $tarPid = -1;
+my $completionPercent;
#
# Re-read config file, so we can include the PC-specific config
exit(0);
}
-if ( !$opts{d} ) {
- #
- # In the non-DHCP case, make sure the host can be looked up
- # via NS, or otherwise find the IP address via NetBios.
- #
- if ( $Conf{ClientNameAlias} ne "" ) {
- $host = $Conf{ClientNameAlias};
- } else {
- $host = $client;
- }
- if ( !defined(gethostbyname($host)) ) {
- #
- # Ok, NS doesn't know about it. Maybe it is a NetBios name
- # instead.
- #
- print(STDERR "Name server doesn't know about $host; trying NetBios\n")
- if ( $opts{v} );
- if ( !defined($hostIP = $bpc->NetBiosHostIPFind($host)) ) {
- print(LOG $bpc->timeStamp, "Can't find host $host via netbios\n");
- print("host not found\n");
- exit(1);
- }
- } else {
- $hostIP = $host;
- }
-}
-
###########################################################################
# Figure out what to do and do it
###########################################################################
|| !defined($p->{hourBegin})
|| !defined($p->{hourEnd})
);
+ my $matchWday = $wday;
if ( $p->{hourBegin} > $p->{hourEnd} ) {
$blackout = $p->{hourBegin} <= $currHours
|| $currHours <= $p->{hourEnd};
# weekday check (eg: Monday 11pm-1am means Monday 2300 to
# Tuesday 0100, not Monday 2300-2400 plus Monday 0000-0100).
#
- $wday--;
- $wday += 7 if ( $wday < 0 );
+ $matchWday--;
+ $matchWday += 7 if ( $matchWday < 0 );
}
} else {
$blackout = $p->{hourBegin} <= $currHours
&& $currHours <= $p->{hourEnd};
}
- if ( $blackout && grep($_ == $wday, @{$p->{weekDays}}) ) {
+ if ( $blackout && grep($_ == $matchWday, @{$p->{weekDays}}) ) {
# print(LOG $bpc->timeStamp, "skipping because of blackout"
# . " (alive $StatusHost{aliveCnt} times)\n");
print(STDERR "Skipping $client because of blackout\n")
#
# Decide whether we do nothing, or a full or incremental backup.
#
-if ( @Backups == 0
+my $needs_full = (time - $lastFullTime > $Conf{FullPeriod} * 24 * 3600
+ && time - $lastIncrTime > $Conf{IncrPeriod} * 24 * 3600);
+my $needs_incr = (time - $lastIncrTime > $Conf{IncrPeriod} * 24 * 3600
+ && time - $lastFullTime > $Conf{IncrPeriod} * 24 * 3600);
+
+if ( $lastFullTime == 0
|| $opts{f}
- || (!$opts{i} && (time - $lastFullTime > $Conf{FullPeriod} * 24*3600
- && time - $lastIncrTime > $Conf{IncrPeriod} * 24*3600)) ) {
+ || (!$opts{i} && !$opts{I} && $needs_full)
+ || ( $opts{F} && $needs_incr) ) {
$type = "full";
$incrLevel = 0;
$incrBaseBkupNum = $lastBkupNum;
-} elsif ( $opts{i} || (time - $lastIncrTime > $Conf{IncrPeriod} * 24*3600
- && time - $lastFullTime > $Conf{IncrPeriod} * 24*3600) ) {
+} elsif ( $opts{i}
+ || $needs_incr
+ || ($opts{I} && $needs_full) ) {
$type = "incr";
#
# For an incremental backup, figure out which level we should
NothingToDo($needLink);
}
+#
+# Create top-level directories if they don't exist
+#
+foreach my $dir ( (
+ "$Conf{TopDir}",
+ "$Conf{TopDir}/pool",
+ "$Conf{TopDir}/cpool",
+ "$Conf{TopDir}/pc",
+ "$Conf{TopDir}/trash",
+ ) ) {
+ next if ( -d $dir );
+ mkpath($dir, 0, 0750);
+ if ( !-d $dir ) {
+ print("Failed to create $dir\n");
+ printf(LOG "%sFailed to create directory %s\n", $bpc->timeStamp, $dir);
+ print("link $clientURI\n") if ( $needLink );
+ exit(1);
+ } else {
+ printf(LOG "%sCreated directory %s\n", $bpc->timeStamp, $dir);
+ }
+}
+
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"
exit(1);
}
+if ( !$opts{d} ) {
+ #
+ # In the non-DHCP case, make sure the host can be looked up
+ # via NS, or otherwise find the IP address via NetBios.
+ #
+ if ( $Conf{ClientNameAlias} ne "" ) {
+ $host = $Conf{ClientNameAlias};
+ } else {
+ $host = $client;
+ }
+ if ( !defined(gethostbyname($host)) ) {
+ #
+ # Ok, NS doesn't know about it. Maybe it is a NetBios name
+ # instead.
+ #
+ print(STDERR "Name server doesn't know about $host; trying NetBios\n")
+ if ( $opts{v} );
+ if ( !defined($hostIP = $bpc->NetBiosHostIPFind($host)) ) {
+ print(LOG $bpc->timeStamp, "Can't find host $host via netbios\n");
+ print("host not found\n");
+ exit(1);
+ }
+ } else {
+ $hostIP = $host;
+ }
+}
+
#
# Check if $host is alive
#
my($logMsg, %stat, $xfer, $ShareNames, $noFilesErr);
my $newFilesFH;
-if ( $Conf{XferMethod} eq "tar" ) {
- $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};
-}
-
-$ShareNames = [ $ShareNames ] unless ref($ShareNames) eq "ARRAY";
+$ShareNames = BackupPC::Xfer::getShareNames(\%Conf);
#
# Run an optional pre-dump command
if ( $? && $Conf{UserCmdCheckStatus} ) {
print(LOG $bpc->timeStamp,
"DumpPreUserCmd returned error status $?... exiting\n");
- print("dump failed: DumpPreUserCmd returned error status $?\n");
- exit(1);
+ $XferLOG->write(\"DumpPreUserCmd returned error status $?... exiting\n");
+ $stat{hostError} = "DumpPreUserCmd returned error status $?";
+ BackupFailCleanup();
}
$NeedPostCmd = 1;
#
# Now backup each of the shares
#
+my $shareDuplicate = {};
for my $shareName ( @$ShareNames ) {
local(*RH, *WH);
$shareName = encode("utf8", $shareName);
$stat{xferOK} = $stat{hostAbort} = undef;
$stat{hostError} = $stat{lastOutputLine} = undef;
- if ( -d "$Dir/new/$shareName" ) {
+ if ( $shareName eq "" ) {
+ print(LOG $bpc->timeStamp,
+ "unexpected empty share name skipped\n");
+ next;
+ }
+ if ( $shareDuplicate->{$shareName} ) {
print(LOG $bpc->timeStamp,
"unexpected repeated share name $shareName skipped\n");
next;
}
+ $shareDuplicate->{$shareName} = 1;
UserCommandRun("DumpPreShareCmd", $shareName);
if ( $? && $Conf{UserCmdCheckStatus} ) {
print(LOG $bpc->timeStamp,
"DumpPreShareCmd returned error status $?... exiting\n");
- print("dump failed: DumpPreShareCmd returned error status $?\n");
UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
- exit(1);
+ $XferLOG->write(\"DumpPreShareCmd returned error status $?... exiting\n");
+ $stat{hostError} = "DumpPreShareCmd returned error status $?";
+ BackupFailCleanup();
}
- if ( $Conf{XferMethod} eq "tar" ) {
- #
- # Use tar (eg: tar/ssh) as the transport program.
- #
- $xfer = BackupPC::Xfer::Tar->new($bpc);
- } elsif ( $Conf{XferMethod} eq "rsync" || $Conf{XferMethod} eq "rsyncd" ) {
- #
- # Use rsync as the transport program.
- #
- if ( !defined($xfer = BackupPC::Xfer::Rsync->new($bpc)) ) {
- 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);
- }
- } else {
- #
- # Default is to use smbclient (smb) as the transport program.
- #
- $xfer = BackupPC::Xfer::Smb->new($bpc);
+ $xfer = BackupPC::Xfer::create($Conf{XferMethod}, $bpc);
+ if ( !defined($xfer) ) {
+ my $errStr = BackupPC::Xfer::errStr();
+ print(LOG $bpc->timeStamp, "dump failed: $errStr\n");
+ UserCommandRun("DumpPostShareCmd", $shareName) if ( $NeedPostCmd );
+ UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
+ $XferLOG->write(\"BackupPC::Xfer::create failed: $errStr\n");
+ $stat{hostError} = $errStr;
+ BackupFailCleanup();
}
my $useTar = $xfer->useTar;
compress => $Conf{CompressLevel},
XferMethod => $Conf{XferMethod},
logLevel => $Conf{XferLogLevel},
- pidHandler => \&pidHandler,
partialNum => $partialNum,
+ pidHandler => \&pidHandler,
+ completionPercent => \&completionPercent,
});
if ( !defined($logMsg = $xfer->start()) ) {
- print(LOG $bpc->timeStamp, "xfer start failed: ", $xfer->errStr, "\n");
- print("dump failed: ", $xfer->errStr, "\n");
- print("link $clientURI\n") if ( $needLink );
+ my $errStr = "xfer start failed: " . $xfer->errStr . "\n";
+ print(LOG $bpc->timeStamp, $errStr);
#
# kill off the tar process, first nicely then forcefully
#
}
UserCommandRun("DumpPostShareCmd", $shareName) if ( $NeedPostCmd );
UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
- exit(1);
+ $XferLOG->write(\"xfer start failed: $errStr\n");
+ $stat{hostError} = $errStr;
+ BackupFailCleanup();
}
@xferPid = $xfer->xferPid;
# we use a select.
#
my($FDread, $tarOut, $mesg);
- vec($FDread, fileno(TAR), 1) = 1 if ( $useTar );
+ vec($FDread, fileno(TAR), 1) = 1;
$xfer->setSelectMask(\$FDread);
SCAN: while ( 1 ) {
my $ein = $FDread;
last if ( $FDread =~ /^\0*$/ );
select(my $rout = $FDread, undef, $ein, undef);
- if ( $useTar ) {
- if ( vec($rout, fileno(TAR), 1) ) {
- if ( sysread(TAR, $mesg, 8192) <= 0 ) {
- vec($FDread, fileno(TAR), 1) = 0;
- close(TAR);
- } else {
- $tarOut .= $mesg;
- }
- }
- while ( $tarOut =~ /(.*?)[\n\r]+(.*)/s ) {
- $_ = $1;
- $tarOut = $2;
- if ( /^ / ) {
- $XferLOG->write(\"$_\n");
- } else {
- $XferLOG->write(\"tarExtract: $_\n");
- }
- if ( /^BackupPC_tarExtact aborting \((.*)\)/ ) {
- $stat{hostError} = $1;
- }
- if ( /^Done: (\d+) errors, (\d+) filesExist, (\d+) sizeExist, (\d+) sizeExistComp, (\d+) filesTotal, (\d+) sizeTotal/ ) {
- $tarErrs += $1;
- $nFilesExist += $2;
- $sizeExist += $3;
- $sizeExistComp += $4;
- $nFilesTotal += $5;
- $sizeTotal += $6;
- }
- }
- }
+ if ( vec($rout, fileno(TAR), 1) ) {
+ if ( sysread(TAR, $mesg, 8192) <= 0 ) {
+ vec($FDread, fileno(TAR), 1) = 0;
+ close(TAR);
+ } else {
+ $tarOut .= $mesg;
+ }
+ }
+ while ( $tarOut =~ /(.*?)[\n\r]+(.*)/s ) {
+ $_ = $1;
+ $tarOut = $2;
+ if ( /^ / ) {
+ $XferLOG->write(\"$_\n");
+ } else {
+ $XferLOG->write(\"tarExtract: $_\n");
+ }
+ if ( /^BackupPC_tarExtact aborting \((.*)\)/ ) {
+ $stat{hostError} = $1;
+ }
+ if ( /^Done: (\d+) errors, (\d+) filesExist, (\d+) sizeExist, (\d+) sizeExistComp, (\d+) filesTotal, (\d+) sizeTotal/ ) {
+ $tarErrs += $1;
+ $nFilesExist += $2;
+ $sizeExist += $3;
+ $sizeExistComp += $4;
+ $nFilesTotal += $5;
+ $sizeTotal += $6;
+ }
+ }
last if ( !$xfer->readOutput(\$FDread, $rout) );
while ( my $str = $xfer->logMsgGet ) {
print(LOG $bpc->timeStamp, "xfer: $str\n");
#
last;
}
+ #
+ # Wait for any child processes to exit
+ #
+ # 1 while ( wait() >= 0 );
}
#
|| $Conf{NmbLookupCmd} eq "" );
my($netBiosHost, $netBiosUser) = $bpc->NetBiosInfoGet($hostIP);
return "host $host has mismatching netbios name $netBiosHost"
- if ( $netBiosHost ne $host );
+ if ( lc($netBiosHost) ne lc(substr($host, 0, 15)) );
return;
}
print("xferPids $str\n");
}
+#
+# The Xfer method might tell us from time to time about progress
+# in the backup or restore
+#
+sub completionPercent
+{
+ my($percent) = @_;
+
+ $percent = 100 if ( $percent > 100 );
+ $percent = 0 if ( $percent < 0 );
+ if ( !defined($completionPercent)
+ || int($completionPercent + 0.5) != int($percent) ) {
+ printf("completionPercent %.0f\n", $percent);
+ }
+ $completionPercent = $percent;
+}
+
#
# Run an optional pre- or post-dump command
#
$bpc->cmdSystemOrEval($cmd,
sub {
$XferLOG->write(\$_[0]);
+ print(LOG $bpc->timeStamp, "Output from $cmdType: ", $_[0]);
},
$vars, \%Conf);
}