2 #============================================================= -*-perl-*-
4 # BackupPC_dump: Dump a single client.
8 # Usage: BackupPC_dump [-i] [-f] [-d] [-e] [-v] <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 # -v verbose. for manual usage: prints failure reasons in more detail.
29 # BackupPC_dump is run periodically by BackupPC to backup $client.
30 # The file $TopDir/pc/$client/backups is read to decide whether a
31 # full or incremental backup needs to be run. If no backup is
32 # scheduled, or a ping to $client fails, then BackupPC_dump quits.
34 # The backup is done using the selected XferMethod (smb, tar, rsync etc),
35 # extracting the dump into $TopDir/pc/$client/new. The xfer output is
36 # put into $TopDir/pc/$client/XferLOG.
38 # If the dump succeeds (based on parsing the output of the XferMethod):
39 # - $TopDir/pc/$client/new is renamed to $TopDir/pc/$client/nnn, where
40 # nnn is the next sequential dump number.
41 # - $TopDir/pc/$client/XferLOG is renamed to $TopDir/pc/$client/XferLOG.nnn.
42 # - $TopDir/pc/$client/backups is updated.
45 # - $TopDir/pc/$client/new is moved to $TopDir/trash for later removal.
46 # - $TopDir/pc/$client/XferLOG is renamed to $TopDir/pc/$client/XferLOG.bad
49 # BackupPC_dump communicates to BackupPC via printing to STDOUT.
52 # Craig Barratt <cbarratt@users.sourceforge.net>
55 # Copyright (C) 2001 Craig Barratt
57 # This program is free software; you can redistribute it and/or modify
58 # it under the terms of the GNU General Public License as published by
59 # the Free Software Foundation; either version 2 of the License, or
60 # (at your option) any later version.
62 # This program is distributed in the hope that it will be useful,
63 # but WITHOUT ANY WARRANTY; without even the implied warranty of
64 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
65 # GNU General Public License for more details.
67 # You should have received a copy of the GNU General Public License
68 # along with this program; if not, write to the Free Software
69 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
71 #========================================================================
73 # Version 2.0.0, released 14 Jun 2003.
75 # See http://backuppc.sourceforge.net.
77 #========================================================================
81 use lib "/usr/local/BackupPC/lib";
83 use BackupPC::FileZIO;
84 use BackupPC::Xfer::Smb;
85 use BackupPC::Xfer::Tar;
86 use BackupPC::Xfer::Rsync;
91 ###########################################################################
93 ###########################################################################
95 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
96 my $TopDir = $bpc->TopDir();
97 my $BinDir = $bpc->BinDir();
98 my %Conf = $bpc->Conf();
105 if ( !getopts("defiv", \%opts) || @ARGV != 1 ) {
106 print("usage: $0 [-d] [-e] [-f] [-i] [-v] <client>\n");
109 if ( $ARGV[0] !~ /^([\w\.\s-]+)$/ ) {
110 print("$0: bad client name '$ARGV[0]'\n");
113 my $client = $1; # BackupPC's client name (might not be real host name)
114 my $hostIP; # this is the IP address
115 my $host; # this is the real host name
117 my($clientURI, $user);
119 $bpc->verbose(1) if ( $opts{v} );
123 # The client name $client is simply a DHCP address. We need to check
124 # if there is any machine at this address, and if so, get the actual
125 # host name via NetBios using nmblookup.
128 if ( $bpc->CheckHostAlive($hostIP) < 0 ) {
129 print("Exiting because CheckHostAlive($hostIP) failed\n")
133 if ( $Conf{NmbLookupCmd} eq "" ) {
134 print("Exiting because \$Conf{NmbLookupCmd} is empty\n")
138 ($client, $user) = $bpc->NetBiosInfoGet($hostIP);
139 if ( $client !~ /^([\w\.\s-]+)$/ ) {
140 print("Exiting because NetBiosInfoGet($hostIP) returned '$client',"
141 . " an invalid host name\n")
145 $Hosts = $bpc->HostInfoRead($client);
148 $Hosts = $bpc->HostInfoRead($client);
150 if ( !defined($Hosts->{$client}) ) {
151 print("Exiting because host $client does not exist in the hosts file\n")
156 my $Dir = "$TopDir/pc/$client";
161 # Re-read config file, so we can include the PC-specific config
163 $clientURI = $bpc->uriEsc($client);
164 if ( defined(my $error = $bpc->ConfigRead($client)) ) {
165 print("dump failed: Can't read PC's config file: $error\n");
168 %Conf = $bpc->Conf();
171 # Catch various signals
173 $SIG{INT} = \&catch_signal;
174 $SIG{ALRM} = \&catch_signal;
175 $SIG{TERM} = \&catch_signal;
176 $SIG{PIPE} = \&catch_signal;
177 $SIG{STOP} = \&catch_signal;
178 $SIG{TSTP} = \&catch_signal;
179 $SIG{TTIN} = \&catch_signal;
183 # Make sure we eventually timeout if there is no activity from
184 # the data transport program.
186 alarm($Conf{ClientTimeout});
188 mkpath($Dir, 0, 0777) if ( !-d $Dir );
189 if ( !-f "$Dir/LOCK" ) {
190 open(LOCK, ">", "$Dir/LOCK") && close(LOCK);
192 open(LOG, ">>", "$Dir/LOG");
193 select(LOG); $| = 1; select(STDOUT);
196 # For the -e option we just expire backups and quit
199 BackupExpire($client);
205 # In the non-DHCP case, make sure the host can be looked up
206 # via NS, or otherwise find the IP address via NetBios.
208 if ( $Conf{ClientNameAlias} ne "" ) {
209 $host = $Conf{ClientNameAlias};
213 if ( !defined(gethostbyname($host)) ) {
215 # Ok, NS doesn't know about it. Maybe it is a NetBios name
218 print("Name server doesn't know about $host; trying NetBios\n")
220 if ( !defined($hostIP = $bpc->NetBiosHostIPFind($host)) ) {
221 print(LOG $bpc->timeStamp, "Can't find host $host via netbios\n");
222 print("host not found\n");
230 ###########################################################################
231 # Figure out what to do and do it
232 ###########################################################################
235 # See if we should skip this host during a certain range
238 my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort});
240 print("Can't connect to server ($err)\n");
241 print(LOG $bpc->timeStamp, "Can't connect to server ($err)\n");
244 my $reply = $bpc->ServerMesg("status host($clientURI)");
245 $reply = $1 if ( $reply =~ /(.*)/s );
248 $bpc->ServerDisconnect();
251 # For DHCP tell BackupPC which host this is
254 if ( $StatusHost{activeJob} ) {
255 # oops, something is already running for this host
256 print("Exiting because backup is already running for $client\n")
260 print("DHCP $hostIP $clientURI\n");
263 my($needLink, @Backups, $type, $lastBkupNum, $lastFullBkupNum);
267 if ( $Conf{FullPeriod} == -1 && !$opts{f} && !$opts{i}
268 || $Conf{FullPeriod} == -2 ) {
269 NothingToDo($needLink);
272 if ( !$opts{i} && !$opts{f} && $Conf{BlackoutGoodCnt} >= 0
273 && $StatusHost{aliveCnt} >= $Conf{BlackoutGoodCnt} ) {
274 my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
275 my($currHours) = $hour + $min / 60 + $sec / 3600;
276 if ( $Conf{BlackoutHourBegin} <= $currHours
277 && $currHours <= $Conf{BlackoutHourEnd}
278 && grep($_ == $wday, @{$Conf{BlackoutWeekDays}}) ) {
279 # print(LOG $bpc->timeStamp, "skipping because of blackout"
280 # . " (alive $StatusHost{aliveCnt} times)\n");
281 NothingToDo($needLink);
285 if ( !$opts{i} && !$opts{f} && $StatusHost{backoffTime} > time ) {
286 printf(LOG "%sskipping because of user requested delay (%.1f hours left)",
287 $bpc->timeStamp, ($StatusHost{backoffTime} - time) / 3600);
288 NothingToDo($needLink);
292 # Now see if there are any old backups we should delete
294 BackupExpire($client);
297 # Read Backup information, and find times of the most recent full and
298 # incremental backups
300 @Backups = $bpc->BackupInfoRead($client);
301 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
302 $needLink = 1 if ( $Backups[$i]{nFilesNew} eq ""
303 || -f "$Dir/NewFileList.$Backups[$i]{num}" );
304 $lastBkupNum = $Backups[$i]{num};
305 if ( $Backups[$i]{type} eq "full" ) {
306 if ( $lastFull < $Backups[$i]{startTime} ) {
307 $lastFull = $Backups[$i]{startTime};
308 $lastFullBkupNum = $Backups[$i]{num};
311 $lastIncr = $Backups[$i]{startTime}
312 if ( $lastIncr < $Backups[$i]{startTime} );
317 # Decide whether we do nothing, or a full or incremental backup.
321 || (!$opts{i} && (time - $lastFull > $Conf{FullPeriod} * 24*3600
322 && time - $lastIncr > $Conf{IncrPeriod} * 24*3600)) ) {
324 } elsif ( $opts{i} || (time - $lastIncr > $Conf{IncrPeriod} * 24*3600
325 && time - $lastFull > $Conf{IncrPeriod} * 24*3600) ) {
328 NothingToDo($needLink);
332 # Check if $host is alive
334 my $delay = $bpc->CheckHostAlive($hostIP);
336 print(LOG $bpc->timeStamp, "no ping response\n");
337 print("no ping response\n");
338 print("link $clientURI\n") if ( $needLink );
340 } elsif ( $delay > $Conf{PingMaxMsec} ) {
341 printf(LOG "%sping too slow: %.4gmsec\n", $bpc->timeStamp, $delay);
342 printf("ping too slow: %.4gmsec (threshold is %gmsec)\n",
343 $delay, $Conf{PingMaxMsec});
344 print("link $clientURI\n") if ( $needLink );
349 # Make sure it is really the machine we expect (only for fixed addresses,
350 # since we got the DHCP address above).
352 if ( !$opts{d} && (my $errMsg = CorrectHostCheck($hostIP, $host)) ) {
353 print(LOG $bpc->timeStamp, "dump failed: $errMsg\n");
354 print("dump failed: $errMsg\n");
356 } elsif ( $opts{d} ) {
357 print(LOG $bpc->timeStamp, "$host is dhcp $hostIP, user is $user\n");
361 # Get a clean directory $Dir/new
363 $bpc->RmTreeDefer("$TopDir/trash", "$Dir/new") if ( -d "$Dir/new" );
366 # Setup file extension for compression and open XferLOG output file
368 $Conf{CompressLevel} = 0 if ( !BackupPC::FileZIO->compOk );
369 my $fileExt = $Conf{CompressLevel} > 0 ? ".z" : "";
370 my $XferLOG = BackupPC::FileZIO->open("$Dir/XferLOG$fileExt", 1,
371 $Conf{CompressLevel});
372 if ( !defined($XferLOG) ) {
373 print(LOG $bpc->timeStamp, "dump failed: unable to open/create"
374 . " $Dir/XferLOG$fileExt\n");
375 print("dump failed: unable to open/create $Dir/XferLOG$fileExt\n");
378 $XferLOG->writeTeeStdout(1) if ( $opts{v} );
379 unlink("$Dir/NewFileList");
380 my $startTime = time();
385 my $sizeExistComp = 0;
388 my($logMsg, %stat, $xfer, $ShareNames);
391 if ( $Conf{XferMethod} eq "tar" ) {
392 $ShareNames = $Conf{TarShareName};
393 } elsif ( $Conf{XferMethod} eq "rsync" || $Conf{XferMethod} eq "rsyncd" ) {
394 $ShareNames = $Conf{RsyncShareName};
396 $ShareNames = $Conf{SmbShareName};
399 $ShareNames = [ $ShareNames ] unless ref($ShareNames) eq "ARRAY";
402 # Run an optional pre-dump command
404 UserCommandRun("DumpPreUserCmd");
408 # Now backup each of the shares
410 for my $shareName ( @$ShareNames ) {
413 $stat{xferOK} = $stat{hostAbort} = undef;
414 $stat{hostError} = $stat{lastOutputLine} = undef;
415 if ( -d "$Dir/new/$shareName" ) {
416 print(LOG $bpc->timeStamp,
417 "unexpected repeated share name $shareName skipped\n");
421 if ( $Conf{XferMethod} eq "tar" ) {
423 # Use tar (eg: tar/ssh) as the transport program.
425 $xfer = BackupPC::Xfer::Tar->new($bpc);
426 } elsif ( $Conf{XferMethod} eq "rsync" || $Conf{XferMethod} eq "rsyncd" ) {
428 # Use rsync as the transport program.
430 if ( !defined($xfer = BackupPC::Xfer::Rsync->new($bpc)) ) {
431 my $errStr = BackupPC::Xfer::Rsync::errStr;
432 print(LOG $bpc->timeStamp, "dump failed: $errStr\n");
433 print("dump failed: $errStr\n");
434 UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
439 # Default is to use smbclient (smb) as the transport program.
441 $xfer = BackupPC::Xfer::Smb->new($bpc);
444 my $useTar = $xfer->useTar;
448 # This xfer method outputs a tar format file, so we start a
449 # BackupPC_tarExtract to extract the data.
451 # Create a socketpair to connect the Xfer method to BackupPC_tarExtract
452 # WH is the write handle for writing, provided to the transport
453 # program, and RH is the other end of the socket for reading,
454 # provided to BackupPC_tarExtract.
456 if ( socketpair(RH, WH, AF_UNIX, SOCK_STREAM, PF_UNSPEC) ) {
457 shutdown(RH, 1); # no writing to this socket
458 shutdown(WH, 0); # no reading from this socket
459 setsockopt(RH, SOL_SOCKET, SO_RCVBUF, 8 * 65536);
460 setsockopt(WH, SOL_SOCKET, SO_SNDBUF, 8 * 65536);
463 # Default to pipe() if socketpair() doesn't work.
469 # fork a child for BackupPC_tarExtract. TAR is a file handle
470 # on which we (the parent) read the stdout & stderr from
471 # BackupPC_tarExtract.
473 if ( !defined($tarPid = open(TAR, "-|")) ) {
474 print(LOG $bpc->timeStamp, "can't fork to run tar\n");
475 print("can't fork to run tar\n");
483 # This is the tar child. Close the write end of the pipe,
484 # clone STDERR to STDOUT, clone STDIN from RH, and then
485 # exec BackupPC_tarExtract.
490 open(STDERR, ">&STDOUT");
493 exec("$BinDir/BackupPC_tarExtract", $client, $shareName,
494 $Conf{CompressLevel});
495 print(LOG $bpc->timeStamp,
496 "can't exec $BinDir/BackupPC_tarExtract\n");
499 } elsif ( !defined($newFilesFH) ) {
501 # We need to create the NewFileList output file
504 open(NEW_FILES, ">", "$TopDir/pc/$client/NewFileList")
505 || die("can't open $TopDir/pc/$client/NewFileList");
506 $newFilesFH = *NEW_FILES;
511 # Run the transport program
517 shareName => $shareName,
521 newFilesFH => $newFilesFH,
524 lastFull => $lastFull,
525 lastBkupNum => $lastBkupNum,
526 lastFullBkupNum => $lastFullBkupNum,
527 backups => \@Backups,
528 compress => $Conf{CompressLevel},
529 XferMethod => $Conf{XferMethod},
530 pidHandler => \&pidHandler,
533 if ( !defined($logMsg = $xfer->start()) ) {
534 print(LOG $bpc->timeStamp, "xfer start failed: ", $xfer->errStr, "\n");
535 print("dump failed: ", $xfer->errStr, "\n");
536 print("link $clientURI\n") if ( $needLink );
538 # kill off the tar process, first nicely then forcefully
550 UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
554 @xferPid = $xfer->xferPid;
558 # The parent must close both handles on the pipe since the children
559 # are using these handles now.
564 print(LOG $bpc->timeStamp, $logMsg, "\n");
565 print("started $type dump, share=$shareName\n");
567 pidHandler(@xferPid);
571 # Parse the output of the transfer program and BackupPC_tarExtract
572 # while they run. Since we might be reading from two or more children
575 my($FDread, $tarOut, $mesg);
576 vec($FDread, fileno(TAR), 1) = 1 if ( $useTar );
577 $xfer->setSelectMask(\$FDread);
581 last if ( $FDread =~ /^\0*$/ );
582 select(my $rout = $FDread, undef, $ein, undef);
584 if ( vec($rout, fileno(TAR), 1) ) {
585 if ( sysread(TAR, $mesg, 8192) <= 0 ) {
586 vec($FDread, fileno(TAR), 1) = 0;
592 while ( $tarOut =~ /(.*?)[\n\r]+(.*)/s ) {
595 $XferLOG->write(\"tarExtract: $_\n");
596 if ( /^Done: (\d+) errors, (\d+) filesExist, (\d+) sizeExist, (\d+) sizeExistComp, (\d+) filesTotal, (\d+) sizeTotal/ ) {
600 $sizeExistComp += $4;
606 last if ( !$xfer->readOutput(\$FDread, $rout) );
607 while ( my $str = $xfer->logMsgGet ) {
608 print(LOG $bpc->timeStamp, "xfer: $str\n");
610 if ( $xfer->getStats->{fileCnt} == 1 ) {
612 # Make sure it is still the machine we expect. We do this while
613 # the transfer is running to avoid a potential race condition if
614 # the ip address was reassigned by dhcp just before we started
617 if ( my $errMsg = CorrectHostCheck($hostIP, $host) ) {
618 $stat{hostError} = $errMsg if ( $stat{hostError} eq "" );
625 # otherwise the xfer module does everything for us
627 my @results = $xfer->run();
628 $tarErrs += $results[0];
629 $nFilesExist += $results[1];
630 $sizeExist += $results[2];
631 $sizeExistComp += $results[3];
632 $nFilesTotal += $results[4];
633 $sizeTotal += $results[5];
637 # Merge the xfer status (need to accumulate counts)
639 my $newStat = $xfer->getStats;
640 foreach my $k ( (keys(%stat), keys(%$newStat)) ) {
641 next if ( !defined($newStat->{$k}) );
642 if ( $k =~ /Cnt$/ ) {
643 $stat{$k} += $newStat->{$k};
644 delete($newStat->{$k});
647 if ( !defined($stat{$k}) ) {
648 $stat{$k} = $newStat->{$k};
649 delete($newStat->{$k});
653 $stat{xferOK} = 0 if ( $stat{hostError} || $stat{hostAbort} );
654 if ( !$stat{xferOK} ) {
656 # kill off the tranfer program, first nicely then forcefully
664 # kill off the tar process, first nicely then forcefully
672 # don't do any more shares on this host
680 # Do one last check to make sure it is still the machine we expect.
682 if ( $stat{xferOK} && (my $errMsg = CorrectHostCheck($hostIP, $host)) ) {
683 $stat{hostError} = $errMsg;
687 UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
689 close($newFilesFH) if ( defined($newFilesFH) );
691 if ( $stat{xferOK} ) {
692 @Backups = $bpc->BackupInfoRead($client);
693 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
694 $lastNum = $Backups[$i]{num} if ( $lastNum < $Backups[$i]{num} );
697 $bpc->RmTreeDefer("$TopDir/trash", "$Dir/$lastNum")
698 if ( -d "$Dir/$lastNum" );
699 if ( !rename("$Dir/new", "$Dir/$lastNum") ) {
700 print(LOG $bpc->timeStamp,
701 "Rename $Dir/new -> $Dir/$lastNum failed\n");
704 rename("$Dir/XferLOG$fileExt", "$Dir/XferLOG.$lastNum$fileExt");
705 rename("$Dir/NewFileList", "$Dir/NewFileList.$lastNum");
707 my $endTime = time();
710 # If the dump failed, clean up
712 if ( !$stat{xferOK} ) {
714 # wait a short while and see if the system is still alive
716 $stat{hostError} = $stat{lastOutputLine} if ( $stat{hostError} eq "" );
717 if ( $stat{hostError} ) {
718 print(LOG $bpc->timeStamp,
719 "Got fatal error during xfer ($stat{hostError})\n");
722 if ( $bpc->CheckHostAlive($hostIP) < 0 ) {
723 $stat{hostAbort} = 1;
725 if ( $stat{hostAbort} ) {
726 $stat{hostError} = "lost network connection during backup";
728 print(LOG $bpc->timeStamp, "Dump aborted ($stat{hostError})\n");
729 unlink("$Dir/timeStamp.level0");
730 unlink("$Dir/SmbLOG.bad");
731 unlink("$Dir/SmbLOG.bad$fileExt");
732 unlink("$Dir/XferLOG.bad");
733 unlink("$Dir/XferLOG.bad$fileExt");
734 unlink("$Dir/NewFileList");
735 rename("$Dir/XferLOG$fileExt", "$Dir/XferLOG.bad$fileExt");
736 $bpc->RmTreeDefer("$TopDir/trash", "$Dir/new") if ( -d "$Dir/new" );
737 print("dump failed: $stat{hostError}\n");
738 print("link $clientURI\n") if ( $needLink );
743 # Add the new backup information to the backup file
745 @Backups = $bpc->BackupInfoRead($client);
747 $Backups[$i]{num} = $lastNum;
748 $Backups[$i]{type} = $type;
749 $Backups[$i]{startTime} = $startTime;
750 $Backups[$i]{endTime} = $endTime;
751 $Backups[$i]{size} = $sizeTotal;
752 $Backups[$i]{nFiles} = $nFilesTotal;
753 $Backups[$i]{xferErrs} = $stat{xferErrCnt} || 0;
754 $Backups[$i]{xferBadFile} = $stat{xferBadFileCnt} || 0;
755 $Backups[$i]{xferBadShare} = $stat{xferBadShareCnt} || 0;
756 $Backups[$i]{nFilesExist} = $nFilesExist;
757 $Backups[$i]{sizeExist} = $sizeExist;
758 $Backups[$i]{sizeExistComp} = $sizeExistComp;
759 $Backups[$i]{tarErrs} = $tarErrs;
760 $Backups[$i]{compress} = $Conf{CompressLevel};
761 $Backups[$i]{noFill} = $type eq "full" ? 0 : 1;
762 $Backups[$i]{mangle} = 1; # name mangling always on for v1.04+
763 $bpc->BackupInfoWrite($client, @Backups);
765 unlink("$Dir/timeStamp.level0");
768 # Now remove the bad files, replacing them if possible with links to
771 foreach my $file ( $xfer->getBadFiles ) {
773 unlink("$Dir/$lastNum/$file");
774 for ( $j = $i - 1 ; $j >= 0 ; $j-- ) {
775 next if ( !-f "$Dir/$Backups[$j]{num}/$file" );
776 if ( !link("$Dir/$Backups[$j]{num}/$file", "$Dir/$lastNum/$file") ) {
777 print(LOG $bpc->timeStamp,
778 "Unable to link $lastNum/$file to"
779 . " $Backups[$j]{num}/$file\n");
781 print(LOG $bpc->timeStamp,
782 "Bad file $lastNum/$file replaced by link to"
783 . " $Backups[$j]{num}/$file\n");
788 print(LOG $bpc->timeStamp,
789 "Removed bad file $lastNum/$file (no older"
790 . " copy to link to)\n");
794 my $otherCount = $stat{xferErrCnt} - $stat{xferBadFileCnt}
795 - $stat{xferBadShareCnt};
796 print(LOG $bpc->timeStamp,
797 "$type backup $lastNum complete, $stat{fileCnt} files,"
798 . " $stat{byteCnt} bytes,"
799 . " $stat{xferErrCnt} xferErrs ($stat{xferBadFileCnt} bad files,"
800 . " $stat{xferBadShareCnt} bad shares, $otherCount other)\n");
802 BackupExpire($client);
804 print("$type backup complete\n");
806 ###########################################################################
808 ###########################################################################
814 print("nothing to do\n");
815 print("link $clientURI\n") if ( $needLink );
822 my $fileExt = $Conf{CompressLevel} > 0 ? ".z" : "";
825 # Children quit quietly on ALRM
827 exit(1) if ( $Pid != $$ && $signame eq "ALRM" );
830 # Ignore other signals in children
832 return if ( $Pid != $$ );
834 print(LOG $bpc->timeStamp, "cleaning up after signal $signame\n");
835 $SIG{$signame} = 'IGNORE';
836 UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
837 $XferLOG->write(\"exiting after signal $signame\n");
849 unlink("$Dir/timeStamp.level0");
850 unlink("$Dir/NewFileList");
851 unlink("$Dir/XferLOG.bad");
852 unlink("$Dir/XferLOG.bad$fileExt");
853 rename("$Dir/XferLOG$fileExt", "$Dir/XferLOG.bad$fileExt");
854 $bpc->RmTreeDefer("$TopDir/trash", "$Dir/new") if ( -d "$Dir/new" );
855 if ( $signame eq "INT" ) {
856 print("dump failed: aborted by user (signal=$signame)\n");
858 print("dump failed: received signal=$signame\n");
860 print("link $clientURI\n") if ( $needLink );
865 # Decide which old backups should be expired by moving them
871 my($Dir) = "$TopDir/pc/$client";
872 my(@Backups) = $bpc->BackupInfoRead($client);
873 my($cntFull, $cntIncr, $firstFull, $firstIncr, $oldestIncr, $oldestFull);
876 $cntFull = $cntIncr = 0;
877 $oldestIncr = $oldestFull = 0;
878 for ( $i = 0 ; $i < @Backups ; $i++ ) {
879 if ( $Backups[$i]{type} eq "full" ) {
880 $firstFull = $i if ( $cntFull == 0 );
883 $firstIncr = $i if ( $cntIncr == 0 );
887 $oldestIncr = (time - $Backups[$firstIncr]{startTime}) / (24 * 3600)
889 $oldestFull = (time - $Backups[$firstFull]{startTime}) / (24 * 3600)
891 if ( $cntIncr > $Conf{IncrKeepCnt}
892 || ($cntIncr > $Conf{IncrKeepCntMin}
893 && $oldestIncr > $Conf{IncrAgeMax})
894 && (@Backups <= $firstIncr + 1
895 || $Backups[$firstIncr]{noFill}
896 || !$Backups[$firstIncr + 1]{noFill}) ) {
898 # Only delete an incr backup if the Conf settings are satisfied.
899 # We also must make sure that either this backup is the most
900 # recent one, or it is not filled, or the next backup is filled.
901 # (We can't deleted a filled incr if the next backup is not
904 print(LOG $bpc->timeStamp,
905 "removing incr backup $Backups[$firstIncr]{num}\n");
906 $bpc->RmTreeDefer("$TopDir/trash",
907 "$Dir/$Backups[$firstIncr]{num}");
908 unlink("$Dir/SmbLOG.$Backups[$firstIncr]{num}")
909 if ( -f "$Dir/SmbLOG.$Backups[$firstIncr]{num}" );
910 unlink("$Dir/SmbLOG.$Backups[$firstIncr]{num}.z")
911 if ( -f "$Dir/SmbLOG.$Backups[$firstIncr]{num}.z" );
912 unlink("$Dir/XferLOG.$Backups[$firstIncr]{num}")
913 if ( -f "$Dir/XferLOG.$Backups[$firstIncr]{num}" );
914 unlink("$Dir/XferLOG.$Backups[$firstIncr]{num}.z")
915 if ( -f "$Dir/XferLOG.$Backups[$firstIncr]{num}.z" );
916 splice(@Backups, $firstIncr, 1);
917 } elsif ( ($cntFull > $Conf{FullKeepCnt}
918 || ($cntFull > $Conf{FullKeepCntMin}
919 && $oldestFull > $Conf{FullAgeMax}))
920 && (@Backups <= $firstFull + 1
921 || !$Backups[$firstFull + 1]{noFill}) ) {
923 # Only delete a full backup if the Conf settings are satisfied.
924 # We also must make sure that either this backup is the most
925 # recent one, or the next backup is filled.
926 # (We can't deleted a full backup if the next backup is not
929 print(LOG $bpc->timeStamp,
930 "removing full backup $Backups[$firstFull]{num}\n");
931 $bpc->RmTreeDefer("$TopDir/trash",
932 "$Dir/$Backups[$firstFull]{num}");
933 unlink("$Dir/SmbLOG.$Backups[$firstFull]{num}")
934 if ( -f "$Dir/SmbLOG.$Backups[$firstFull]{num}" );
935 unlink("$Dir/SmbLOG.$Backups[$firstFull]{num}.z")
936 if ( -f "$Dir/SmbLOG.$Backups[$firstFull]{num}.z" );
937 unlink("$Dir/XferLOG.$Backups[$firstFull]{num}")
938 if ( -f "$Dir/XferLOG.$Backups[$firstFull]{num}" );
939 unlink("$Dir/XferLOG.$Backups[$firstFull]{num}.z")
940 if ( -f "$Dir/XferLOG.$Backups[$firstFull]{num}.z" );
941 splice(@Backups, $firstFull, 1);
946 $bpc->BackupInfoWrite($client, @Backups);
951 my($hostIP, $host) = @_;
952 return if ( $hostIP eq $host && !$Conf{FixedIPNetBiosNameCheck}
953 || $Conf{NmbLookupCmd} eq "" );
954 my($netBiosHost, $netBiosUser) = $bpc->NetBiosInfoGet($hostIP);
955 return "host $host has mismatching netbios name $netBiosHost"
956 if ( $netBiosHost ne $host );
961 # The Xfer method might tell us from time to time about processes
962 # it forks. We tell BackupPC about this (for status displays) and
963 # keep track of the pids in case we cancel the backup
968 @xferPid = grep(/./, @xferPid);
969 return if ( !@xferPid && $tarPid < 0 );
971 push(@pids, $tarPid) if ( $tarPid > 0 );
972 my $str = join(",", @pids);
973 $XferLOG->write(\"Xfer PIDs are now $str\n") if ( defined($XferLOG) );
974 print("xferPids $str\n");
978 # Run an optional pre- or post-dump command
984 return if ( !defined($Conf{$type}) );
990 user => $Hosts->{$client}{user},
991 moreUsers => $Hosts->{$client}{moreUsers},
992 share => $ShareNames->[0],
993 shares => $ShareNames,
994 XferMethod => $Conf{XferMethod},
995 sshPath => $Conf{SshPath},
999 xferOK => $stat{xferOK} || 0,
1002 my $cmd = $bpc->cmdVarSubstitute($Conf{$type}, $vars);
1003 $XferLOG->write(\"Executing $type: @$cmd\n");
1005 # Run the user's command, dumping the stdout/stderr into the
1006 # Xfer log file. Also supply the optional $vars and %Conf in
1007 # case the command is really perl code instead of a shell
1010 $bpc->cmdSystemOrEval($cmd,
1012 $XferLOG->write(\$_[0]);