2 #============================================================= -*-perl-*-
4 # BackupPC_dump: Dump a single client.
8 # Usage: BackupPC_dump [-i] [-f] [-d] [-e] <client>
12 # -i Do an incremental dump, overriding any scheduling (but a full
13 # dump will be done if no dumps have yet succeeded)
15 # -f Do a full dump, overriding any scheduling.
17 # -d Host is a DHCP pool address, and the client argument
18 # just an IP address. We lookup the NetBios name from
21 # -e Just do an dump expiry check for the client. Don't do anything
22 # else. This is used periodically by BackupPC to make sure that
23 # dhcp hosts have correctly expired old backups. Without this,
24 # dhcp hosts that are no longer on the network will not expire
27 # BackupPC_dump is run periodically by BackupPC to backup $client.
28 # The file $TopDir/pc/$client/backups is read to decide whether a
29 # full or incremental backup needs to be run. If no backup is
30 # scheduled, or a ping to $client fails, then BackupPC_dump quits.
32 # The backup is done using the selected XferMethod (smb, tar, rsync etc),
33 # extracting the dump into $TopDir/pc/$client/new. The xfer output is
34 # put into $TopDir/pc/$client/XferLOG.
36 # If the dump succeeds (based on parsing the output of the XferMethod):
37 # - $TopDir/pc/$client/new is renamed to $TopDir/pc/$client/nnn, where
38 # nnn is the next sequential dump number.
39 # - $TopDir/pc/$client/XferLOG is renamed to $TopDir/pc/$client/XferLOG.nnn.
40 # - $TopDir/pc/$client/backups is updated.
43 # - $TopDir/pc/$client/new is moved to $TopDir/trash for later removal.
44 # - $TopDir/pc/$client/XferLOG is renamed to $TopDir/pc/$client/XferLOG.bad
47 # BackupPC_dump communicates to BackupPC via printing to STDOUT.
50 # Craig Barratt <cbarratt@users.sourceforge.net>
53 # Copyright (C) 2001 Craig Barratt
55 # This program is free software; you can redistribute it and/or modify
56 # it under the terms of the GNU General Public License as published by
57 # the Free Software Foundation; either version 2 of the License, or
58 # (at your option) any later version.
60 # This program is distributed in the hope that it will be useful,
61 # but WITHOUT ANY WARRANTY; without even the implied warranty of
62 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
63 # GNU General Public License for more details.
65 # You should have received a copy of the GNU General Public License
66 # along with this program; if not, write to the Free Software
67 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
69 #========================================================================
71 # Version 2.0.0_CVS, released 18 Jan 2003.
73 # See http://backuppc.sourceforge.net.
75 #========================================================================
78 use lib "/usr/local/BackupPC/lib";
80 use BackupPC::FileZIO;
81 use BackupPC::Xfer::Smb;
82 use BackupPC::Xfer::Tar;
83 use BackupPC::Xfer::Rsync;
87 ###########################################################################
89 ###########################################################################
91 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
92 my $TopDir = $bpc->TopDir();
93 my $BinDir = $bpc->BinDir();
94 my %Conf = $bpc->Conf();
100 getopts("defi", \%opts);
102 print("usage: $0 [-d] [-e] [-f] [-i] <client>\n");
105 if ( $ARGV[0] !~ /^([\w\.-\s]+)$/ ) {
106 print("$0: bad client name '$ARGV[0]'\n");
109 my $client = $1; # BackupPC's client name (might not be real host name)
110 my $hostIP; # this is the IP address
111 my $host; # this is the real host name
113 my($clientURI, $user);
117 # The client name $client is simply a DHCP address. We need to check
118 # if there is any machine at this address, and if so, get the actual
119 # host name via NetBios using nmblookup.
122 exit(1) if ( $bpc->CheckHostAlive($hostIP) < 0 );
123 ($client, $user) = $bpc->NetBiosInfoGet($hostIP);
124 exit(1) if ( $host !~ /^([\w\.-]+)$/ );
125 my $hosts = $bpc->HostInfoRead($client);
126 exit(1) if ( !defined($hosts->{$client}) );
130 my $Dir = "$TopDir/pc/$client";
135 # Re-read config file, so we can include the PC-specific config
137 $clientURI = $bpc->uriEsc($client);
138 if ( defined(my $error = $bpc->ConfigRead($client)) ) {
139 print("dump failed: Can't read PC's config file: $error\n");
142 %Conf = $bpc->Conf();
145 # Catch various signals
147 $SIG{INT} = \&catch_signal;
148 $SIG{ALRM} = \&catch_signal;
149 $SIG{TERM} = \&catch_signal;
152 # Make sure we eventually timeout if there is no activity from
153 # the data transport program.
155 alarm($Conf{ClientTimeout});
157 mkpath($Dir, 0, 0777) if ( !-d $Dir );
158 if ( !-f "$Dir/LOCK" ) {
159 open(LOCK, ">", "$Dir/LOCK") && close(LOCK);
161 open(LOG, ">>", "$Dir/LOG");
162 select(LOG); $| = 1; select(STDOUT);
166 # In the non-DHCP case, make sure the host can be looked up
167 # via NS, or otherwise find the IP address via NetBios.
169 if ( $Conf{ClientNameAlias} ne "" ) {
170 $host = $Conf{ClientNameAlias};
174 if ( !defined(gethostbyname($host)) ) {
176 # Ok, NS doesn't know about it. Maybe it is a NetBios name
179 if ( !defined($hostIP = $bpc->NetBiosHostIPFind($host)) ) {
180 print(LOG $bpc->timeStamp,
181 "dump failed: Can't find host $host\n");
182 print("dump failed: Can't find host $host\n");
190 ###########################################################################
191 # Figure out what to do and do it
192 ###########################################################################
195 # For the -e option we just expire backups and quit
198 BackupExpire($client);
203 # See if we should skip this host during a certain range
206 my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort});
208 print("Can't connect to server ($err)\n");
209 print(LOG $bpc->timeStamp, "Can't connect to server ($err)\n");
212 my $reply = $bpc->ServerMesg("status host($clientURI)");
213 $reply = $1 if ( $reply =~ /(.*)/s );
216 $bpc->ServerDisconnect();
219 # For DHCP tell BackupPC which host this is
222 if ( $StatusHost{activeJob} ) {
223 # oops, something is already running for this host
226 print("DHCP $hostIP $clientURI\n");
229 my($needLink, @Backups, $type, $lastBkupNum, $lastFullBkupNum);
233 if ( $Conf{FullPeriod} == -1 && !$opts{f} && !$opts{i}
234 || $Conf{FullPeriod} == -2 ) {
235 NothingToDo($needLink);
238 if ( !$opts{i} && !$opts{f} && $Conf{BlackoutGoodCnt} >= 0
239 && $StatusHost{aliveCnt} >= $Conf{BlackoutGoodCnt} ) {
240 my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
241 my($currHours) = $hour + $min / 60 + $sec / 3600;
242 if ( $Conf{BlackoutHourBegin} <= $currHours
243 && $currHours <= $Conf{BlackoutHourEnd}
244 && grep($_ == $wday, @{$Conf{BlackoutWeekDays}}) ) {
245 # print(LOG $bpc->timeStamp, "skipping because of blackout"
246 # . " (alive $StatusHost{aliveCnt} times)\n");
247 NothingToDo($needLink);
251 if ( !$opts{i} && !$opts{f} && $StatusHost{backoffTime} > time ) {
252 printf(LOG "%sskipping because of user requested delay (%.1f hours left)",
253 $bpc->timeStamp, ($StatusHost{backoffTime} - time) / 3600);
254 NothingToDo($needLink);
258 # Now see if there are any old backups we should delete
260 BackupExpire($client);
263 # Read Backup information, and find times of the most recent full and
264 # incremental backups
266 @Backups = $bpc->BackupInfoRead($client);
267 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
268 $needLink = 1 if ( $Backups[$i]{nFilesNew} eq ""
269 || -f "$Dir/NewFileList.$Backups[$i]{num}" );
270 $lastBkupNum = $Backups[$i]{num};
271 if ( $Backups[$i]{type} eq "full" ) {
272 if ( $lastFull < $Backups[$i]{startTime} ) {
273 $lastFull = $Backups[$i]{startTime};
274 $lastFullBkupNum = $Backups[$i]{num};
277 $lastIncr = $Backups[$i]{startTime}
278 if ( $lastIncr < $Backups[$i]{startTime} );
283 # Decide whether we do nothing, or a full or incremental backup.
287 || (!$opts{i} && (time - $lastFull > $Conf{FullPeriod} * 24*3600
288 && time - $lastIncr > $Conf{IncrPeriod} * 24*3600)) ) {
290 } elsif ( $opts{i} || (time - $lastIncr > $Conf{IncrPeriod} * 24*3600
291 && time - $lastFull > $Conf{IncrPeriod} * 24*3600) ) {
294 NothingToDo($needLink);
298 # Check if $host is alive
300 my $delay = $bpc->CheckHostAlive($hostIP);
302 print(LOG $bpc->timeStamp, "no ping response\n");
303 print("no ping response\n");
304 print("link $clientURI\n") if ( $needLink );
306 } elsif ( $delay > $Conf{PingMaxMsec} ) {
307 printf(LOG "%sping too slow: %.4gmsec\n", $bpc->timeStamp, $delay);
308 printf("ping too slow: %.4gmsec (threshold is %gmsec)\n",
309 $delay, $Conf{PingMaxMsec});
310 print("link $clientURI\n") if ( $needLink );
315 # Make sure it is really the machine we expect (only for fixed addresses,
316 # since we got the DHCP address above).
318 if ( !$opts{d} && (my $errMsg = CorrectHostCheck($hostIP, $host)) ) {
319 print(LOG $bpc->timeStamp, "dump failed: $errMsg\n");
320 print("dump failed: $errMsg\n");
322 } elsif ( $opts{d} ) {
323 print(LOG $bpc->timeStamp, "$host is dhcp $hostIP, user is $user\n");
327 # Get a clean directory $Dir/new
329 $bpc->RmTreeDefer("$TopDir/trash", "$Dir/new") if ( -d "$Dir/new" );
332 # Setup file extension for compression and open XferLOG output file
334 $Conf{CompressLevel} = 0 if ( !BackupPC::FileZIO->compOk );
335 my $fileExt = $Conf{CompressLevel} > 0 ? ".z" : "";
336 my $XferLOG = BackupPC::FileZIO->open("$Dir/XferLOG$fileExt", 1,
337 $Conf{CompressLevel});
338 if ( !defined($XferLOG) ) {
339 print(LOG $bpc->timeStamp, "dump failed: unable to open/create"
340 . " $Dir/XferLOG$fileExt\n");
341 print("dump failed: unable to open/create $Dir/XferLOG$fileExt\n");
344 unlink("$Dir/NewFileList");
345 my $startTime = time();
350 my $sizeExistComp = 0;
353 my($logMsg, %stat, $xfer, $ShareNames);
356 if ( $Conf{XferMethod} eq "tar" ) {
357 $ShareNames = $Conf{TarShareName};
358 } elsif ( $Conf{XferMethod} eq "rsync" || $Conf{XferMethod} eq "rsyncd" ) {
359 $ShareNames = $Conf{RsyncShareName};
361 $ShareNames = $Conf{SmbShareName};
364 $ShareNames = [ $ShareNames ] unless ref($ShareNames) eq "ARRAY";
367 # Run an optional pre-dump command
369 UserCommandRun("DumpPreUserCmd");
373 # Now backup each of the shares
375 for my $shareName ( @$ShareNames ) {
378 $stat{xferOK} = $stat{hostAbort} = undef;
379 $stat{hostError} = $stat{lastOutputLine} = undef;
380 if ( -d "$Dir/new/$shareName" ) {
381 print(LOG $bpc->timeStamp,
382 "unexpected repeated share name $shareName skipped\n");
386 if ( $Conf{XferMethod} eq "tar" ) {
388 # Use tar (eg: tar/ssh) as the transport program.
390 $xfer = BackupPC::Xfer::Tar->new($bpc);
391 } elsif ( $Conf{XferMethod} eq "rsync" || $Conf{XferMethod} eq "rsyncd" ) {
393 # Use rsync as the transport program.
395 if ( !defined($xfer = BackupPC::Xfer::Rsync->new($bpc)) ) {
396 my $errStr = BackupPC::Xfer::Rsync::errStr;
397 print(LOG $bpc->timeStamp, "dump failed: $errStr\n");
398 print("dump failed: $errStr\n");
399 UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
404 # Default is to use smbclient (smb) as the transport program.
406 $xfer = BackupPC::Xfer::Smb->new($bpc);
409 my $useTar = $xfer->useTar;
413 # This xfer method outputs a tar format file, so we start a
414 # BackupPC_tarExtract to extract the data.
416 # Create a pipe to connect the Xfer method to BackupPC_tarExtract
417 # WH is the write handle for writing, provided to the transport
418 # program, and RH is the other end of the pipe for reading,
419 # provided to BackupPC_tarExtract.
424 # fork a child for BackupPC_tarExtract. TAR is a file handle
425 # on which we (the parent) read the stdout & stderr from
426 # BackupPC_tarExtract.
428 if ( !defined($tarPid = open(TAR, "-|")) ) {
429 print(LOG $bpc->timeStamp, "can't fork to run tar\n");
430 print("can't fork to run tar\n");
437 # This is the tar child. Close the write end of the pipe,
438 # clone STDERR to STDOUT, clone STDIN from RH, and then
439 # exec BackupPC_tarExtract.
444 open(STDERR, ">&STDOUT");
447 exec("$BinDir/BackupPC_tarExtract", $client, $shareName,
448 $Conf{CompressLevel});
449 print(LOG $bpc->timeStamp,
450 "can't exec $BinDir/BackupPC_tarExtract\n");
453 } elsif ( !defined($newFilesFH) ) {
455 # We need to create the NewFileList output file
458 open(NEW_FILES, ">", "$TopDir/pc/$client/NewFileList")
459 || die("can't open $TopDir/pc/$client/NewFileList");
460 $newFilesFH = *NEW_FILES;
464 # Run the transport program
470 shareName => $shareName,
474 newFilesFH => $newFilesFH,
477 lastFull => $lastFull,
478 lastBkupNum => $lastBkupNum,
479 lastFullBkupNum => $lastFullBkupNum,
480 backups => \@Backups,
481 compress => $Conf{CompressLevel},
482 XferMethod => $Conf{XferMethod},
485 if ( !defined($logMsg = $xfer->start()) ) {
486 print(LOG $bpc->timeStamp, "xfer start failed: ", $xfer->errStr, "\n");
487 print("dump failed: ", $xfer->errStr, "\n");
488 print("link $clientURI\n") if ( $needLink );
490 # kill off the tar process, first nicely then forcefully
497 UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
501 $xferPid = $xfer->xferPid;
504 # The parent must close both handles on the pipe since the children
505 # are using these handles now.
509 print(LOG $bpc->timeStamp, $logMsg,
510 " (xferPid=$xferPid, tarPid=$tarPid)\n");
511 } elsif ( $xferPid > 0 ) {
512 print(LOG $bpc->timeStamp, $logMsg, " (xferPid=$xferPid)\n");
514 print(LOG $bpc->timeStamp, $logMsg, "\n");
516 print("started $type dump, pid=$xferPid, tarPid=$tarPid,"
517 . " share=$shareName\n");
519 if ( $useTar || $xferPid > 0 ) {
521 # Parse the output of the transfer program and BackupPC_tarExtract
522 # while they run. Since we might be reading from two or more children
525 my($FDread, $tarOut, $mesg);
526 vec($FDread, fileno(TAR), 1) = 1 if ( $useTar );
527 $xfer->setSelectMask(\$FDread);
531 last if ( $FDread =~ /^\0*$/ );
532 select(my $rout = $FDread, undef, $ein, undef);
534 if ( vec($rout, fileno(TAR), 1) ) {
535 if ( sysread(TAR, $mesg, 8192) <= 0 ) {
536 vec($FDread, fileno(TAR), 1) = 0;
542 while ( $tarOut =~ /(.*?)[\n\r]+(.*)/s ) {
545 $XferLOG->write(\"tarExtract: $_\n");
546 if ( /^Done: (\d+) errors, (\d+) filesExist, (\d+) sizeExist, (\d+) sizeExistComp, (\d+) filesTotal, (\d+) sizeTotal/ ) {
550 $sizeExistComp += $4;
556 last if ( !$xfer->readOutput(\$FDread, $rout) );
557 while ( my $str = $xfer->logMsgGet ) {
558 print(LOG $bpc->timeStamp, "xfer: $str\n");
560 if ( $xfer->getStats->{fileCnt} == 1 ) {
562 # Make sure it is still the machine we expect. We do this while
563 # the transfer is running to avoid a potential race condition if
564 # the ip address was reassigned by dhcp just before we started
567 if ( my $errMsg = CorrectHostCheck($hostIP, $host) ) {
568 $stat{hostError} = $errMsg;
575 # otherwise the xfer module does everything for us
577 my @results = $xfer->run();
578 $tarErrs += $results[0];
579 $nFilesExist += $results[1];
580 $sizeExist += $results[2];
581 $sizeExistComp += $results[3];
582 $nFilesTotal += $results[4];
583 $sizeTotal += $results[5];
587 # Merge the xfer status (need to accumulate counts)
589 my $newStat = $xfer->getStats;
590 foreach my $k ( (keys(%stat), keys(%$newStat)) ) {
591 next if ( !defined($newStat->{$k}) );
592 if ( $k =~ /Cnt$/ ) {
593 $stat{$k} += $newStat->{$k};
594 delete($newStat->{$k});
597 if ( !defined($stat{$k}) ) {
598 $stat{$k} = $newStat->{$k};
599 delete($newStat->{$k});
603 $stat{xferOK} = 0 if ( $stat{hostError} || $stat{hostAbort} );
604 if ( !$stat{xferOK} ) {
606 # kill off the tranfer program, first nicely then forcefully
608 if ( $xferPid > 0 ) {
614 # kill off the tar process, first nicely then forcefully
622 # don't do any more shares on this host
630 # Do one last check to make sure it is still the machine we expect.
632 if ( $stat{xferOK} && (my $errMsg = CorrectHostCheck($hostIP, $host)) ) {
633 $stat{hostError} = $errMsg;
637 UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
639 close($newFilesFH) if ( defined($newFilesFH) );
641 if ( $stat{xferOK} ) {
642 @Backups = $bpc->BackupInfoRead($client);
643 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
644 $lastNum = $Backups[$i]{num} if ( $lastNum < $Backups[$i]{num} );
647 $bpc->RmTreeDefer("$TopDir/trash", "$Dir/$lastNum")
648 if ( -d "$Dir/$lastNum" );
649 if ( !rename("$Dir/new", "$Dir/$lastNum") ) {
650 print(LOG $bpc->timeStamp,
651 "Rename $Dir/new -> $Dir/$lastNum failed\n");
654 rename("$Dir/XferLOG$fileExt", "$Dir/XferLOG.$lastNum$fileExt");
655 rename("$Dir/NewFileList", "$Dir/NewFileList.$lastNum");
657 my $endTime = time();
660 # If the dump failed, clean up
662 if ( !$stat{xferOK} ) {
664 # wait a short while and see if the system is still alive
666 $stat{hostError} = $stat{lastOutputLine} if ( $stat{hostError} eq "" );
667 if ( $stat{hostError} ) {
668 print(LOG $bpc->timeStamp,
669 "Got fatal error during xfer ($stat{hostError})\n");
672 if ( $bpc->CheckHostAlive($hostIP) < 0 ) {
673 $stat{hostAbort} = 1;
675 if ( $stat{hostAbort} ) {
676 $stat{hostError} = "lost network connection during backup";
678 print(LOG $bpc->timeStamp, "Dump aborted ($stat{hostError})\n");
679 unlink("$Dir/timeStamp.level0");
680 unlink("$Dir/SmbLOG.bad");
681 unlink("$Dir/SmbLOG.bad$fileExt");
682 unlink("$Dir/XferLOG.bad");
683 unlink("$Dir/XferLOG.bad$fileExt");
684 unlink("$Dir/NewFileList");
685 rename("$Dir/XferLOG$fileExt", "$Dir/XferLOG.bad$fileExt");
686 $bpc->RmTreeDefer("$TopDir/trash", "$Dir/new") if ( -d "$Dir/new" );
687 print("dump failed: $stat{hostError}\n");
688 print("link $clientURI\n") if ( $needLink );
693 # Add the new backup information to the backup file
695 @Backups = $bpc->BackupInfoRead($client);
697 $Backups[$i]{num} = $lastNum;
698 $Backups[$i]{type} = $type;
699 $Backups[$i]{startTime} = $startTime;
700 $Backups[$i]{endTime} = $endTime;
701 $Backups[$i]{size} = $sizeTotal;
702 $Backups[$i]{nFiles} = $nFilesTotal;
703 $Backups[$i]{xferErrs} = $stat{xferErrCnt} || 0;
704 $Backups[$i]{xferBadFile} = $stat{xferBadFileCnt} || 0;
705 $Backups[$i]{xferBadShare} = $stat{xferBadShareCnt} || 0;
706 $Backups[$i]{nFilesExist} = $nFilesExist;
707 $Backups[$i]{sizeExist} = $sizeExist;
708 $Backups[$i]{sizeExistComp} = $sizeExistComp;
709 $Backups[$i]{tarErrs} = $tarErrs;
710 $Backups[$i]{compress} = $Conf{CompressLevel};
711 $Backups[$i]{noFill} = $type eq "full" ? 0 : 1;
712 $Backups[$i]{mangle} = 1; # name mangling always on for v1.04+
713 $bpc->BackupInfoWrite($client, @Backups);
715 unlink("$Dir/timeStamp.level0");
718 # Now remove the bad files, replacing them if possible with links to
721 foreach my $file ( $xfer->getBadFiles ) {
723 unlink("$Dir/$lastNum/$file");
724 for ( $j = $i - 1 ; $j >= 0 ; $j-- ) {
725 next if ( !-f "$Dir/$Backups[$j]{num}/$file" );
726 if ( !link("$Dir/$Backups[$j]{num}/$file", "$Dir/$lastNum/$file") ) {
727 print(LOG $bpc->timeStamp,
728 "Unable to link $lastNum/$file to"
729 . " $Backups[$j]{num}/$file\n");
731 print(LOG $bpc->timeStamp,
732 "Bad file $lastNum/$file replaced by link to"
733 . " $Backups[$j]{num}/$file\n");
738 print(LOG $bpc->timeStamp,
739 "Removed bad file $lastNum/$file (no older"
740 . " copy to link to)\n");
744 my $otherCount = $stat{xferErrCnt} - $stat{xferBadFileCnt}
745 - $stat{xferBadShareCnt};
746 print(LOG $bpc->timeStamp,
747 "$type backup $lastNum complete, $stat{fileCnt} files,"
748 . " $stat{byteCnt} bytes,"
749 . " $stat{xferErrCnt} xferErrs ($stat{xferBadFileCnt} bad files,"
750 . " $stat{xferBadShareCnt} bad shares, $otherCount other)\n");
752 BackupExpire($client);
754 print("$type backup complete\n");
756 ###########################################################################
758 ###########################################################################
764 print("nothing to do\n");
765 print("link $clientURI\n") if ( $needLink );
772 my $fileExt = $Conf{CompressLevel} > 0 ? ".z" : "";
774 print(LOG $bpc->timeStamp, "cleaning up after signal $signame\n");
775 UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
776 $XferLOG->write(\"exiting after signal $signame\n");
778 if ( $xferPid > 0 ) {
779 if ( kill(2, $xferPid) <= 0 ) {
785 if ( kill(2, $tarPid) <= 0 ) {
790 unlink("$Dir/timeStamp.level0");
791 unlink("$Dir/NewFileList");
792 unlink("$Dir/XferLOG.bad");
793 unlink("$Dir/XferLOG.bad$fileExt");
794 rename("$Dir/XferLOG$fileExt", "$Dir/XferLOG.bad$fileExt");
795 $bpc->RmTreeDefer("$TopDir/trash", "$Dir/new") if ( -d "$Dir/new" );
796 if ( $signame eq "INT" ) {
797 print("dump failed: aborted by user (signal=$signame)\n");
799 print("dump failed: received signal=$signame\n");
801 print("link $clientURI\n") if ( $needLink );
806 # Decide which old backups should be expired by moving them
812 my($Dir) = "$TopDir/pc/$client";
813 my(@Backups) = $bpc->BackupInfoRead($client);
814 my($cntFull, $cntIncr, $firstFull, $firstIncr, $oldestIncr, $oldestFull);
817 $cntFull = $cntIncr = 0;
818 $oldestIncr = $oldestFull = 0;
819 for ( $i = 0 ; $i < @Backups ; $i++ ) {
820 if ( $Backups[$i]{type} eq "full" ) {
821 $firstFull = $i if ( $cntFull == 0 );
824 $firstIncr = $i if ( $cntIncr == 0 );
828 $oldestIncr = (time - $Backups[$firstIncr]{startTime}) / (24 * 3600)
830 $oldestFull = (time - $Backups[$firstFull]{startTime}) / (24 * 3600)
832 if ( $cntIncr > $Conf{IncrKeepCnt}
833 || ($cntIncr > $Conf{IncrKeepCntMin}
834 && $oldestIncr > $Conf{IncrAgeMax})
835 && (@Backups <= $firstIncr + 1
836 || $Backups[$firstIncr]{noFill}
837 || !$Backups[$firstIncr + 1]{noFill}) ) {
839 # Only delete an incr backup if the Conf settings are satisfied.
840 # We also must make sure that either this backup is the most
841 # recent one, or it is not filled, or the next backup is filled.
842 # (We can't deleted a filled incr if the next backup is not
845 print(LOG $bpc->timeStamp,
846 "removing incr backup $Backups[$firstIncr]{num}\n");
847 $bpc->RmTreeDefer("$TopDir/trash",
848 "$Dir/$Backups[$firstIncr]{num}");
849 unlink("$Dir/SmbLOG.$Backups[$firstIncr]{num}")
850 if ( -f "$Dir/SmbLOG.$Backups[$firstIncr]{num}" );
851 unlink("$Dir/SmbLOG.$Backups[$firstIncr]{num}.z")
852 if ( -f "$Dir/SmbLOG.$Backups[$firstIncr]{num}.z" );
853 unlink("$Dir/XferLOG.$Backups[$firstIncr]{num}")
854 if ( -f "$Dir/XferLOG.$Backups[$firstIncr]{num}" );
855 unlink("$Dir/XferLOG.$Backups[$firstIncr]{num}.z")
856 if ( -f "$Dir/XferLOG.$Backups[$firstIncr]{num}.z" );
857 splice(@Backups, $firstIncr, 1);
858 } elsif ( ($cntFull > $Conf{FullKeepCnt}
859 || ($cntFull > $Conf{FullKeepCntMin}
860 && $oldestFull > $Conf{FullAgeMax}))
861 && (@Backups <= $firstFull + 1
862 || !$Backups[$firstFull + 1]{noFill}) ) {
864 # Only delete a full backup if the Conf settings are satisfied.
865 # We also must make sure that either this backup is the most
866 # recent one, or the next backup is filled.
867 # (We can't deleted a full backup if the next backup is not
870 print(LOG $bpc->timeStamp,
871 "removing full backup $Backups[$firstFull]{num}\n");
872 $bpc->RmTreeDefer("$TopDir/trash",
873 "$Dir/$Backups[$firstFull]{num}");
874 unlink("$Dir/SmbLOG.$Backups[$firstFull]{num}")
875 if ( -f "$Dir/SmbLOG.$Backups[$firstFull]{num}" );
876 unlink("$Dir/SmbLOG.$Backups[$firstFull]{num}.z")
877 if ( -f "$Dir/SmbLOG.$Backups[$firstFull]{num}.z" );
878 unlink("$Dir/XferLOG.$Backups[$firstFull]{num}")
879 if ( -f "$Dir/XferLOG.$Backups[$firstFull]{num}" );
880 unlink("$Dir/XferLOG.$Backups[$firstFull]{num}.z")
881 if ( -f "$Dir/XferLOG.$Backups[$firstFull]{num}.z" );
882 splice(@Backups, $firstFull, 1);
887 $bpc->BackupInfoWrite($client, @Backups);
892 my($hostIP, $host) = @_;
893 return if ( $hostIP eq $host && !$Conf{FixedIPNetBiosNameCheck} );
894 my($netBiosHost, $netBiosUser) = $bpc->NetBiosInfoGet($hostIP);
895 return "host $host has mismatching netbios name $netBiosHost"
896 if ( $netBiosHost ne $host );
901 # Run an optional pre- or post-dump command
907 return if ( !defined($Conf{$type}) );
913 share => $ShareNames->[0],
914 shares => $ShareNames,
915 XferMethod => $Conf{XferMethod},
919 xferOK => $stat{xferOK},
922 my $cmd = $bpc->cmdVarSubstitute($Conf{$type}, $vars);
923 $XferLOG->write(\"Executing $type: @$cmd\n");
925 # Run the user's command, dumping the stdout/stderr into the
926 # Xfer log file. Also supply the optional $vars and %Conf in
927 # case the command is really perl code instead of a shell
930 $bpc->cmdSystemOrEval($cmd,
932 $XferLOG->write(\$_[0]);