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,
35 # backuppcd etc), extracting the dump into $TopDir/pc/$client/new.
36 # The xfer output is 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-2003 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 3.0.0alpha, released 23 Jan 2006.
75 # See http://backuppc.sourceforge.net.
77 #========================================================================
81 use lib "/usr/local/BackupPC/lib";
83 use BackupPC::FileZIO;
84 use BackupPC::Storage;
85 use BackupPC::Xfer::Smb;
86 use BackupPC::Xfer::Tar;
87 use BackupPC::Xfer::Rsync;
88 use BackupPC::Xfer::BackupPCd;
94 ###########################################################################
96 ###########################################################################
98 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
99 my $TopDir = $bpc->TopDir();
100 my $BinDir = $bpc->BinDir();
101 my %Conf = $bpc->Conf();
110 if ( !getopts("defiv", \%opts) || @ARGV != 1 ) {
111 print("usage: $0 [-d] [-e] [-f] [-i] [-v] <client>\n");
114 if ( $ARGV[0] !~ /^([\w\.\s-]+)$/ ) {
115 print("$0: bad client name '$ARGV[0]'\n");
118 my $client = $1; # BackupPC's client name (might not be real host name)
119 my $hostIP; # this is the IP address
120 my $host; # this is the real host name
122 my($clientURI, $user);
124 $bpc->verbose(1) if ( $opts{v} );
128 # The client name $client is simply a DHCP address. We need to check
129 # if there is any machine at this address, and if so, get the actual
130 # host name via NetBios using nmblookup.
133 if ( $bpc->CheckHostAlive($hostIP) < 0 ) {
134 print(STDERR "Exiting because CheckHostAlive($hostIP) failed\n")
138 if ( $Conf{NmbLookupCmd} eq "" ) {
139 print(STDERR "Exiting because \$Conf{NmbLookupCmd} is empty\n")
143 ($client, $user) = $bpc->NetBiosInfoGet($hostIP);
144 if ( $client !~ /^([\w\.\s-]+)$/ ) {
145 print(STDERR "Exiting because NetBiosInfoGet($hostIP) returned"
146 . " '$client', an invalid host name\n") if ( $opts{v} );
149 $Hosts = $bpc->HostInfoRead($client);
152 $Hosts = $bpc->HostInfoRead($client);
154 if ( !defined($Hosts->{$client}) ) {
155 print(STDERR "Exiting because host $client does not exist in the"
156 . " hosts file\n") if ( $opts{v} );
160 my $Dir = "$TopDir/pc/$client";
165 # Re-read config file, so we can include the PC-specific config
167 $clientURI = $bpc->uriEsc($client);
168 if ( defined(my $error = $bpc->ConfigRead($client)) ) {
169 print("dump failed: Can't read PC's config file: $error\n");
172 %Conf = $bpc->Conf();
175 # Catch various signals
177 $SIG{INT} = \&catch_signal;
178 $SIG{ALRM} = \&catch_signal;
179 $SIG{TERM} = \&catch_signal;
180 $SIG{PIPE} = \&catch_signal;
181 $SIG{STOP} = \&catch_signal;
182 $SIG{TSTP} = \&catch_signal;
183 $SIG{TTIN} = \&catch_signal;
187 # Make sure we eventually timeout if there is no activity from
188 # the data transport program.
190 alarm($Conf{ClientTimeout});
192 mkpath($Dir, 0, 0777) if ( !-d $Dir );
193 if ( !-f "$Dir/LOCK" ) {
194 open(LOCK, ">", "$Dir/LOCK") && close(LOCK);
196 open(LOG, ">>", "$Dir/LOG");
197 select(LOG); $| = 1; select(STDOUT);
200 # For the -e option we just expire backups and quit
203 BackupExpire($client);
208 # For archive hosts we don't bother any further
210 if ($Conf{XferMethod} eq "archive" ) {
211 print(STDERR "Exiting because the XferMethod is set to archive\n")
218 # In the non-DHCP case, make sure the host can be looked up
219 # via NS, or otherwise find the IP address via NetBios.
221 if ( $Conf{ClientNameAlias} ne "" ) {
222 $host = $Conf{ClientNameAlias};
226 if ( !defined(gethostbyname($host)) ) {
228 # Ok, NS doesn't know about it. Maybe it is a NetBios name
231 print(STDERR "Name server doesn't know about $host; trying NetBios\n")
233 if ( !defined($hostIP = $bpc->NetBiosHostIPFind($host)) ) {
234 print(LOG $bpc->timeStamp, "Can't find host $host via netbios\n");
235 print("host not found\n");
243 ###########################################################################
244 # Figure out what to do and do it
245 ###########################################################################
248 # See if we should skip this host during a certain range
251 my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort});
253 print("Can't connect to server ($err)\n");
254 print(LOG $bpc->timeStamp, "Can't connect to server ($err)\n");
257 my $reply = $bpc->ServerMesg("status host($clientURI)");
258 $reply = $1 if ( $reply =~ /(.*)/s );
261 $bpc->ServerDisconnect();
264 # For DHCP tell BackupPC which host this is
267 if ( $StatusHost{activeJob} ) {
268 # oops, something is already running for this host
269 print(STDERR "Exiting because backup is already running for $client\n")
273 print("DHCP $hostIP $clientURI\n");
276 my($needLink, @Backups, $type, $lastBkupNum, $lastFullBkupNum);
283 if ( $Conf{FullPeriod} == -1 && !$opts{f} && !$opts{i}
284 || $Conf{FullPeriod} == -2 ) {
285 print(STDERR "Exiting because backups are disabled with"
286 . " \$Conf{FullPeriod} = $Conf{FullPeriod}\n") if ( $opts{v} );
288 # Tell BackupPC to ignore old failed backups on hosts that
289 # have backups disabled.
291 print("backups disabled\n")
292 if ( defined($StatusHost{errorTime})
293 && $StatusHost{reason} ne "Reason_backup_done"
294 && time - $StatusHost{errorTime} > 4 * 24 * 3600 );
295 NothingToDo($needLink);
298 if ( !$opts{i} && !$opts{f} && $Conf{BlackoutGoodCnt} >= 0
299 && $StatusHost{aliveCnt} >= $Conf{BlackoutGoodCnt} ) {
300 my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
301 my($currHours) = $hour + $min / 60 + $sec / 3600;
305 # Handle backward compatibility with original separate scalar
308 if ( defined($Conf{BlackoutHourBegin}) ) {
309 push(@{$Conf{BlackoutPeriods}},
311 hourBegin => $Conf{BlackoutHourBegin},
312 hourEnd => $Conf{BlackoutHourEnd},
313 weekDays => $Conf{BlackoutWeekDays},
317 foreach my $p ( @{$Conf{BlackoutPeriods}} ) {
319 # Allow blackout to span midnight (specified by BlackoutHourBegin
320 # being greater than BlackoutHourEnd)
322 next if ( ref($p->{weekDays}) ne "ARRAY"
323 || !defined($p->{hourBegin})
324 || !defined($p->{hourEnd})
326 if ( $p->{hourBegin} > $p->{hourEnd} ) {
327 $blackout = $p->{hourBegin} <= $currHours
328 || $currHours <= $p->{hourEnd};
329 if ( $currHours <= $p->{hourEnd} ) {
331 # This is after midnight, so decrement the weekday for the
332 # weekday check (eg: Monday 11pm-1am means Monday 2300 to
333 # Tuesday 0100, not Monday 2300-2400 plus Monday 0000-0100).
336 $wday += 7 if ( $wday < 0 );
339 $blackout = $p->{hourBegin} <= $currHours
340 && $currHours <= $p->{hourEnd};
342 if ( $blackout && grep($_ == $wday, @{$p->{weekDays}}) ) {
343 # print(LOG $bpc->timeStamp, "skipping because of blackout"
344 # . " (alive $StatusHost{aliveCnt} times)\n");
345 print(STDERR "Skipping $client because of blackout\n")
347 NothingToDo($needLink);
352 if ( !$opts{i} && !$opts{f} && $StatusHost{backoffTime} > time ) {
353 printf(LOG "%sskipping because of user requested delay (%.1f hours left)\n",
354 $bpc->timeStamp, ($StatusHost{backoffTime} - time) / 3600);
355 NothingToDo($needLink);
359 # Now see if there are any old backups we should delete
361 BackupExpire($client);
364 # Read Backup information, and find times of the most recent full and
365 # incremental backups
367 @Backups = $bpc->BackupInfoRead($client);
368 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
369 $needLink = 1 if ( $Backups[$i]{nFilesNew} eq ""
370 || -f "$Dir/NewFileList.$Backups[$i]{num}" );
371 $lastBkupNum = $Backups[$i]{num};
372 if ( $Backups[$i]{type} eq "full" ) {
373 if ( $lastFull < $Backups[$i]{startTime} ) {
374 $lastFull = $Backups[$i]{startTime};
375 $lastFullBkupNum = $Backups[$i]{num};
377 } elsif ( $Backups[$i]{type} eq "incr" ) {
378 $lastIncr = $Backups[$i]{startTime}
379 if ( $lastIncr < $Backups[$i]{startTime} );
380 } elsif ( $Backups[$i]{type} eq "partial" ) {
382 $lastPartial = $Backups[$i]{startTime};
383 $partialNum = $Backups[$i]{num};
388 # Decide whether we do nothing, or a full or incremental backup.
392 || (!$opts{i} && (time - $lastFull > $Conf{FullPeriod} * 24*3600
393 && time - $lastIncr > $Conf{IncrPeriod} * 24*3600)) ) {
395 } elsif ( $opts{i} || (time - $lastIncr > $Conf{IncrPeriod} * 24*3600
396 && time - $lastFull > $Conf{IncrPeriod} * 24*3600) ) {
399 NothingToDo($needLink);
403 # Check if $host is alive
405 my $delay = $bpc->CheckHostAlive($hostIP);
407 print(LOG $bpc->timeStamp, "no ping response\n");
408 print("no ping response\n");
409 print("link $clientURI\n") if ( $needLink );
411 } elsif ( $delay > $Conf{PingMaxMsec} ) {
412 printf(LOG "%sping too slow: %.4gmsec\n", $bpc->timeStamp, $delay);
413 printf("ping too slow: %.4gmsec (threshold is %gmsec)\n",
414 $delay, $Conf{PingMaxMsec});
415 print("link $clientURI\n") if ( $needLink );
420 # Make sure it is really the machine we expect (only for fixed addresses,
421 # since we got the DHCP address above).
423 if ( !$opts{d} && (my $errMsg = CorrectHostCheck($hostIP, $host)) ) {
424 print(LOG $bpc->timeStamp, "dump failed: $errMsg\n");
425 print("dump failed: $errMsg\n");
427 } elsif ( $opts{d} ) {
428 print(LOG $bpc->timeStamp, "$host is dhcp $hostIP, user is $user\n");
432 # Get a clean directory $Dir/new
434 $bpc->RmTreeDefer("$TopDir/trash", "$Dir/new") if ( -d "$Dir/new" );
437 # Setup file extension for compression and open XferLOG output file
439 if ( $Conf{CompressLevel} && !BackupPC::FileZIO->compOk ) {
440 print(LOG $bpc->timeStamp, "dump failed: can't find Compress::Zlib\n");
441 print("dump failed: can't find Compress::Zlib\n");
444 my $fileExt = $Conf{CompressLevel} > 0 ? ".z" : "";
445 my $XferLOG = BackupPC::FileZIO->open("$Dir/XferLOG$fileExt", 1,
446 $Conf{CompressLevel});
447 if ( !defined($XferLOG) ) {
448 print(LOG $bpc->timeStamp, "dump failed: unable to open/create"
449 . " $Dir/XferLOG$fileExt\n");
450 print("dump failed: unable to open/create $Dir/XferLOG$fileExt\n");
455 # Ignore the partial dump in the case of an incremental
456 # or when the partial is too old. A partial is a partial full.
458 if ( $type ne "full" || time - $lastPartial > $Conf{PartialAgeMax} * 24*3600 ) {
464 # If this is a partial, copy the old XferLOG file
467 my($compress, $fileName);
468 if ( -f "$Dir/XferLOG.$partialNum.z" ) {
469 $fileName = "$Dir/XferLOG.$partialNum.z";
471 } elsif ( -f "$Dir/XferLOG.$partialNum" ) {
472 $fileName = "$Dir/XferLOG.$partialNum";
475 if ( my $oldLOG = BackupPC::FileZIO->open($fileName, 0, $compress) ) {
477 while ( $oldLOG->read(\$data, 65536) > 0 ) {
478 $XferLOG->write(\$data);
484 $XferLOG->writeTeeStderr(1) if ( $opts{v} );
485 unlink("$Dir/NewFileList") if ( -f "$Dir/NewFileList" );
487 my $startTime = time();
491 my $sizeExistComp = 0;
494 my($logMsg, %stat, $xfer, $ShareNames, $noFilesErr);
497 if ( $Conf{XferMethod} eq "tar" ) {
498 $ShareNames = $Conf{TarShareName};
499 } elsif ( $Conf{XferMethod} eq "rsync" || $Conf{XferMethod} eq "rsyncd" ) {
500 $ShareNames = $Conf{RsyncShareName};
501 } elsif ( $Conf{XferMethod} eq "backuppcd" ) {
502 $ShareNames = $Conf{BackupPCdShareName};
504 $ShareNames = $Conf{SmbShareName};
507 $ShareNames = [ $ShareNames ] unless ref($ShareNames) eq "ARRAY";
510 # Run an optional pre-dump command
512 UserCommandRun("DumpPreUserCmd");
516 # Now backup each of the shares
518 for my $shareName ( @$ShareNames ) {
521 $stat{xferOK} = $stat{hostAbort} = undef;
522 $stat{hostError} = $stat{lastOutputLine} = undef;
523 if ( -d "$Dir/new/$shareName" ) {
524 print(LOG $bpc->timeStamp,
525 "unexpected repeated share name $shareName skipped\n");
529 UserCommandRun("DumpPreShareCmd", $shareName);
531 if ( $Conf{XferMethod} eq "tar" ) {
533 # Use tar (eg: tar/ssh) as the transport program.
535 $xfer = BackupPC::Xfer::Tar->new($bpc);
536 } elsif ( $Conf{XferMethod} eq "rsync" || $Conf{XferMethod} eq "rsyncd" ) {
538 # Use rsync as the transport program.
540 if ( !defined($xfer = BackupPC::Xfer::Rsync->new($bpc)) ) {
541 my $errStr = BackupPC::Xfer::Rsync::errStr;
542 print(LOG $bpc->timeStamp, "dump failed: $errStr\n");
543 print("dump failed: $errStr\n");
544 UserCommandRun("DumpPostShareCmd", $shareName) if ( $NeedPostCmd );
545 UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
548 } elsif ( $Conf{XferMethod} eq "backuppcd" ) {
550 # Use backuppcd as the transport program.
552 if ( !defined($xfer = BackupPC::Xfer::BackupPCd->new($bpc)) ) {
553 my $errStr = BackupPC::Xfer::BackupPCd::errStr;
554 print(LOG $bpc->timeStamp, "dump failed: $errStr\n");
555 print("dump failed: $errStr\n");
556 UserCommandRun("DumpPostShareCmd", $shareName) if ( $NeedPostCmd );
557 UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
562 # Default is to use smbclient (smb) as the transport program.
564 $xfer = BackupPC::Xfer::Smb->new($bpc);
567 my $useTar = $xfer->useTar;
571 # This xfer method outputs a tar format file, so we start a
572 # BackupPC_tarExtract to extract the data.
574 # Create a socketpair to connect the Xfer method to BackupPC_tarExtract
575 # WH is the write handle for writing, provided to the transport
576 # program, and RH is the other end of the socket for reading,
577 # provided to BackupPC_tarExtract.
579 if ( socketpair(RH, WH, AF_UNIX, SOCK_STREAM, PF_UNSPEC) ) {
580 shutdown(RH, 1); # no writing to this socket
581 shutdown(WH, 0); # no reading from this socket
582 setsockopt(RH, SOL_SOCKET, SO_RCVBUF, 8 * 65536);
583 setsockopt(WH, SOL_SOCKET, SO_SNDBUF, 8 * 65536);
586 # Default to pipe() if socketpair() doesn't work.
592 # fork a child for BackupPC_tarExtract. TAR is a file handle
593 # on which we (the parent) read the stdout & stderr from
594 # BackupPC_tarExtract.
596 if ( !defined($tarPid = open(TAR, "-|")) ) {
597 print(LOG $bpc->timeStamp, "can't fork to run tar\n");
598 print("can't fork to run tar\n");
606 # This is the tar child. Close the write end of the pipe,
607 # clone STDERR to STDOUT, clone STDIN from RH, and then
608 # exec BackupPC_tarExtract.
613 open(STDERR, ">&STDOUT");
617 exec("$BinDir/BackupPC_tarExtract", $client, $shareName,
618 $Conf{CompressLevel});
619 print(LOG $bpc->timeStamp,
620 "can't exec $BinDir/BackupPC_tarExtract\n");
623 } elsif ( !defined($newFilesFH) ) {
625 # We need to create the NewFileList output file
628 open(NEW_FILES, ">", "$TopDir/pc/$client/NewFileList")
629 || die("can't open $TopDir/pc/$client/NewFileList");
630 $newFilesFH = *NEW_FILES;
635 # Run the transport program
641 shareName => $shareName,
645 newFilesFH => $newFilesFH,
648 lastFull => $lastFull,
649 lastBkupNum => $lastBkupNum,
650 lastFullBkupNum => $lastFullBkupNum,
651 backups => \@Backups,
652 compress => $Conf{CompressLevel},
653 XferMethod => $Conf{XferMethod},
654 logLevel => $Conf{XferLogLevel},
655 pidHandler => \&pidHandler,
656 partialNum => $partialNum,
659 if ( !defined($logMsg = $xfer->start()) ) {
660 print(LOG $bpc->timeStamp, "xfer start failed: ", $xfer->errStr, "\n");
661 print("dump failed: ", $xfer->errStr, "\n");
662 print("link $clientURI\n") if ( $needLink );
664 # kill off the tar process, first nicely then forcefully
667 kill($bpc->sigName2num("INT"), $tarPid);
669 kill($bpc->sigName2num("KILL"), $tarPid);
672 kill($bpc->sigName2num("INT"), @xferPid);
674 kill($bpc->sigName2num("KILL"), @xferPid);
676 UserCommandRun("DumpPostShareCmd", $shareName) if ( $NeedPostCmd );
677 UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
681 @xferPid = $xfer->xferPid;
685 # The parent must close both handles on the pipe since the children
686 # are using these handles now.
691 print(LOG $bpc->timeStamp, $logMsg, "\n");
692 print("started $type dump, share=$shareName\n");
694 pidHandler(@xferPid);
698 # Parse the output of the transfer program and BackupPC_tarExtract
699 # while they run. Since we might be reading from two or more children
702 my($FDread, $tarOut, $mesg);
703 vec($FDread, fileno(TAR), 1) = 1 if ( $useTar );
704 $xfer->setSelectMask(\$FDread);
708 last if ( $FDread =~ /^\0*$/ );
709 select(my $rout = $FDread, undef, $ein, undef);
711 if ( vec($rout, fileno(TAR), 1) ) {
712 if ( sysread(TAR, $mesg, 8192) <= 0 ) {
713 vec($FDread, fileno(TAR), 1) = 0;
719 while ( $tarOut =~ /(.*?)[\n\r]+(.*)/s ) {
723 $XferLOG->write(\"$_\n");
725 $XferLOG->write(\"tarExtract: $_\n");
727 if ( /^BackupPC_tarExtact aborting \((.*)\)/ ) {
728 $stat{hostError} = $1;
730 if ( /^Done: (\d+) errors, (\d+) filesExist, (\d+) sizeExist, (\d+) sizeExistComp, (\d+) filesTotal, (\d+) sizeTotal/ ) {
734 $sizeExistComp += $4;
740 last if ( !$xfer->readOutput(\$FDread, $rout) );
741 while ( my $str = $xfer->logMsgGet ) {
742 print(LOG $bpc->timeStamp, "xfer: $str\n");
744 if ( $xfer->getStats->{fileCnt} == 1 ) {
746 # Make sure it is still the machine we expect. We do this while
747 # the transfer is running to avoid a potential race condition if
748 # the ip address was reassigned by dhcp just before we started
751 if ( my $errMsg = CorrectHostCheck($hostIP, $host) ) {
752 $stat{hostError} = $errMsg if ( $stat{hostError} eq "" );
759 # otherwise the xfer module does everything for us
761 my @results = $xfer->run();
762 $tarErrs += $results[0];
763 $nFilesExist += $results[1];
764 $sizeExist += $results[2];
765 $sizeExistComp += $results[3];
766 $nFilesTotal += $results[4];
767 $sizeTotal += $results[5];
771 # Merge the xfer status (need to accumulate counts)
773 my $newStat = $xfer->getStats;
774 if ( $newStat->{fileCnt} == 0 ) {
775 $noFilesErr ||= "No files dumped for share $shareName";
777 foreach my $k ( (keys(%stat), keys(%$newStat)) ) {
778 next if ( !defined($newStat->{$k}) );
779 if ( $k =~ /Cnt$/ ) {
780 $stat{$k} += $newStat->{$k};
781 delete($newStat->{$k});
784 if ( !defined($stat{$k}) ) {
785 $stat{$k} = $newStat->{$k};
786 delete($newStat->{$k});
791 UserCommandRun("DumpPostShareCmd", $shareName) if ( $NeedPostCmd );
793 $stat{xferOK} = 0 if ( $stat{hostError} || $stat{hostAbort} );
794 if ( !$stat{xferOK} ) {
796 # kill off the tranfer program, first nicely then forcefully
799 kill($bpc->sigName2num("INT"), @xferPid);
801 kill($bpc->sigName2num("KILL"), @xferPid);
804 # kill off the tar process, first nicely then forcefully
807 kill($bpc->sigName2num("INT"), $tarPid);
809 kill($bpc->sigName2num("KILL"), $tarPid);
812 # don't do any more shares on this host
819 # If this is a full, and any share had zero files then consider the dump bad
821 if ( $type eq "full" && $stat{hostError} eq ""
822 && length($noFilesErr) && $Conf{BackupZeroFilesIsFatal} ) {
823 $stat{hostError} = $noFilesErr;
827 $stat{xferOK} = 0 if ( $Abort );
830 # Do one last check to make sure it is still the machine we expect.
832 if ( $stat{xferOK} && (my $errMsg = CorrectHostCheck($hostIP, $host)) ) {
833 $stat{hostError} = $errMsg;
837 UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
838 close($newFilesFH) if ( defined($newFilesFH) );
840 my $endTime = time();
843 # If the dump failed, clean up
845 if ( !$stat{xferOK} ) {
846 $stat{hostError} = $stat{lastOutputLine} if ( $stat{hostError} eq "" );
847 if ( $stat{hostError} ) {
848 print(LOG $bpc->timeStamp,
849 "Got fatal error during xfer ($stat{hostError})\n");
850 $XferLOG->write(\"Got fatal error during xfer ($stat{hostError})\n");
854 # wait a short while and see if the system is still alive
857 if ( $bpc->CheckHostAlive($hostIP) < 0 ) {
858 $stat{hostAbort} = 1;
860 if ( $stat{hostAbort} ) {
861 $stat{hostError} = "lost network connection during backup";
863 print(LOG $bpc->timeStamp, "Backup aborted ($stat{hostError})\n");
864 $XferLOG->write(\"Backup aborted ($stat{hostError})\n");
866 $XferLOG->write(\"Backup aborted by user signal\n");
870 # Close the log file and call BackupFailCleanup, which exits.
875 my $newNum = BackupSave();
877 my $otherCount = $stat{xferErrCnt} - $stat{xferBadFileCnt}
878 - $stat{xferBadShareCnt};
879 print(LOG $bpc->timeStamp,
880 "$type backup $newNum complete, $stat{fileCnt} files,"
881 . " $stat{byteCnt} bytes,"
882 . " $stat{xferErrCnt} xferErrs ($stat{xferBadFileCnt} bad files,"
883 . " $stat{xferBadShareCnt} bad shares, $otherCount other)\n");
885 BackupExpire($client);
887 print("$type backup complete\n");
889 ###########################################################################
891 ###########################################################################
897 print("nothing to do\n");
898 print("link $clientURI\n") if ( $needLink );
907 # The first time we receive a signal we try to gracefully
908 # abort the backup. This allows us to keep a partial dump
909 # with the in-progress file deleted and attribute caches
910 # flushed to disk etc.
912 if ( !length($SigName) ) {
914 if ( $sigName eq "INT" ) {
915 $reason = "aborted by user (signal=$sigName)";
917 $reason = "aborted by signal=$sigName";
921 # Parent logs a message
923 print(LOG $bpc->timeStamp,
924 "Aborting backup up after signal $sigName\n");
929 $xfer->abort($reason);
932 # Send ALRMs to BackupPC_tarExtract if we are using it
935 kill($bpc->sigName2num("ARLM"), $tarPid);
939 # Schedule a 20 second timer in case the clean
940 # abort doesn't complete
945 # Children ignore anything other than ALRM and INT
947 if ( $sigName ne "ALRM" && $sigName ne "INT" ) {
952 # The child also tells xfer to abort
954 $xfer->abort($reason);
957 # Schedule a 15 second timer in case the clean
958 # abort doesn't complete
968 # This is a second signal: time to clean up.
970 if ( $Pid != $$ && ($sigName eq "ALRM" || $sigName eq "INT") ) {
972 # Children quit quietly on ALRM or INT
978 # Ignore other signals in children
980 return if ( $Pid != $$ );
982 $SIG{$sigName} = 'IGNORE';
983 UserCommandRun("DumpPostUserCmd") if ( $NeedPostCmd );
984 $XferLOG->write(\"exiting after signal $sigName\n");
986 kill($bpc->sigName2num("INT"), @xferPid);
988 kill($bpc->sigName2num("KILL"), @xferPid);
991 kill($bpc->sigName2num("INT"), $tarPid);
993 kill($bpc->sigName2num("KILL"), $tarPid);
995 if ( $sigName eq "INT" ) {
996 $stat{hostError} = "aborted by user (signal=$sigName)";
998 $stat{hostError} = "received signal=$sigName";
1000 BackupFailCleanup();
1003 sub CheckForNewFiles
1005 if ( -f _ && $File::Find::name !~ /\/fattrib$/ ) {
1009 # No need to check entire tree
1011 $File::Find::prune = 1 if ( $nFilesTotal );
1015 sub BackupFailCleanup
1017 my $fileExt = $Conf{CompressLevel} > 0 ? ".z" : "";
1018 my $keepPartial = 0;
1021 # We keep this backup if it is a full and we actually backed
1024 if ( $type eq "full" ) {
1025 if ( $nFilesTotal == 0 && $xfer->getStats->{fileCnt} == 0 ) {
1027 # Xfer didn't report any files, but check in the new
1028 # directory just in case.
1030 find(\&CheckForNewFiles, "$Dir/new");
1031 $keepPartial = 1 if ( $nFilesTotal );
1034 # Xfer reported some files
1041 # Don't keep partials if they are disabled
1043 $keepPartial = 0 if ( $Conf{PartialAgeMax} < 0 );
1045 if ( !$keepPartial ) {
1047 # No point in saving this dump; get rid of eveything.
1050 unlink("$Dir/timeStamp.level0") if ( -f "$Dir/timeStamp.level0" );
1051 unlink("$Dir/SmbLOG.bad") if ( -f "$Dir/SmbLOG.bad" );
1052 unlink("$Dir/SmbLOG.bad$fileExt") if ( -f "$Dir/SmbLOG.bad$fileExt" );
1053 unlink("$Dir/XferLOG.bad") if ( -f "$Dir/XferLOG.bad" );
1054 unlink("$Dir/XferLOG.bad$fileExt") if ( -f "$Dir/XferLOG.bad$fileExt" );
1055 unlink("$Dir/NewFileList") if ( -f "$Dir/NewFileList" );
1056 rename("$Dir/XferLOG$fileExt", "$Dir/XferLOG.bad$fileExt");
1057 $bpc->RmTreeDefer("$TopDir/trash", "$Dir/new") if ( -d "$Dir/new" );
1058 print("dump failed: $stat{hostError}\n");
1060 print("link $clientURI\n") if ( $needLink );
1064 # Ok, now we should save this as a partial dump
1067 my $newNum = BackupSave();
1068 print("dump failed: $stat{hostError}\n");
1069 print("link $clientURI\n") if ( $needLink );
1070 print(LOG $bpc->timeStamp, "Saved partial dump $newNum\n");
1075 # Decide which old backups should be expired by moving them
1081 my($Dir) = "$TopDir/pc/$client";
1082 my(@Backups) = $bpc->BackupInfoRead($client);
1083 my($cntFull, $cntIncr, $firstFull, $firstIncr, $oldestIncr,
1084 $oldestFull, $changes);
1086 if ( $Conf{FullKeepCnt} <= 0 ) {
1087 print(LOG $bpc->timeStamp,
1088 "Invalid value for \$Conf{FullKeepCnt}=$Conf{FullKeepCnt}\n");
1090 "Invalid value for \$Conf{FullKeepCnt}=$Conf{FullKeepCnt}\n")
1095 $cntFull = $cntIncr = 0;
1096 $oldestIncr = $oldestFull = 0;
1097 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
1098 if ( $Backups[$i]{type} eq "full" ) {
1099 $firstFull = $i if ( $cntFull == 0 );
1102 $firstIncr = $i if ( $cntIncr == 0 );
1106 $oldestIncr = (time - $Backups[$firstIncr]{startTime}) / (24 * 3600)
1107 if ( $cntIncr > 0 );
1108 $oldestFull = (time - $Backups[$firstFull]{startTime}) / (24 * 3600)
1109 if ( $cntFull > 0 );
1110 if ( $cntIncr > $Conf{IncrKeepCnt}
1111 || ($cntIncr > $Conf{IncrKeepCntMin}
1112 && $oldestIncr > $Conf{IncrAgeMax})
1113 && (@Backups <= $firstIncr + 1
1114 || $Backups[$firstIncr]{noFill}
1115 || !$Backups[$firstIncr + 1]{noFill}) ) {
1117 # Only delete an incr backup if the Conf settings are satisfied.
1118 # We also must make sure that either this backup is the most
1119 # recent one, or it is not filled, or the next backup is filled.
1120 # (We can't deleted a filled incr if the next backup is not
1123 print(LOG $bpc->timeStamp,
1124 "removing incr backup $Backups[$firstIncr]{num}\n");
1125 BackupRemove($client, \@Backups, $firstIncr);
1131 # Delete any old full backups, according to $Conf{FullKeepCntMin}
1132 # and $Conf{FullAgeMax}.
1134 # First make sure that $Conf{FullAgeMax} is at least bigger
1135 # than $Conf{FullPeriod} * $Conf{FullKeepCnt}, including
1136 # the exponential array case.
1138 my $fullKeepCnt = $Conf{FullKeepCnt};
1139 $fullKeepCnt = [$fullKeepCnt] if ( ref($fullKeepCnt) ne "ARRAY" );
1141 my $fullPeriod = int(0.5 + $Conf{FullPeriod});
1142 $fullPeriod = 7 if ( $fullPeriod <= 0 );
1143 for ( my $i = 0 ; $i < @$fullKeepCnt ; $i++ ) {
1144 $fullAgeMax += $fullKeepCnt->[$i] * $fullPeriod;
1147 $fullAgeMax += $fullPeriod; # add some buffer
1149 if ( $cntFull > $Conf{FullKeepCntMin}
1150 && $oldestFull > $Conf{FullAgeMax}
1151 && $oldestFull > $fullAgeMax
1152 && $Conf{FullKeepCntMin} > 0
1153 && $Conf{FullAgeMax} > 0
1154 && (@Backups <= $firstFull + 1
1155 || !$Backups[$firstFull + 1]{noFill}) ) {
1157 # Only delete a full backup if the Conf settings are satisfied.
1158 # We also must make sure that either this backup is the most
1159 # recent one, or the next backup is filled.
1160 # (We can't deleted a full backup if the next backup is not
1163 print(LOG $bpc->timeStamp,
1164 "removing old full backup $Backups[$firstFull]{num}\n");
1165 BackupRemove($client, \@Backups, $firstFull);
1171 # Do new-style full backup expiry, which includes the the case
1172 # where $Conf{FullKeepCnt} is an array.
1174 last if ( !BackupFullExpire($client, \@Backups) );
1176 $bpc->BackupInfoWrite($client, @Backups) if ( $changes );
1180 # Handle full backup expiry, using exponential periods.
1182 sub BackupFullExpire
1184 my($client, $Backups) = @_;
1186 my $fullPeriod = $Conf{FullPeriod};
1187 my $origFullPeriod = $fullPeriod;
1188 my $fullKeepCnt = $Conf{FullKeepCnt};
1189 my $fullKeepIdx = 0;
1190 my(@delete, @fullList);
1193 # Don't delete anything if $Conf{FullPeriod} or $Conf{FullKeepCnt} are
1194 # not defined - possibly a corrupted config.pl file.
1196 return if ( !defined($Conf{FullPeriod}) || !defined($Conf{FullKeepCnt}) );
1199 # If regular backups are still disabled with $Conf{FullPeriod} < 0,
1200 # we still expire backups based on a typical FullPeriod value - weekly.
1202 $fullPeriod = 7 if ( $fullPeriod <= 0 );
1204 $fullKeepCnt = [$fullKeepCnt] if ( ref($fullKeepCnt) ne "ARRAY" );
1206 for ( my $i = 0 ; $i < @$Backups ; $i++ ) {
1207 next if ( $Backups->[$i]{type} ne "full" );
1208 push(@fullList, $i);
1210 for ( my $k = @fullList - 1 ; $k >= 0 ; $k-- ) {
1211 my $i = $fullList[$k];
1212 my $prevFull = $fullList[$k-1] if ( $k > 0 );
1214 # Don't delete any full that is followed by an unfilled backup,
1215 # since it is needed for restore.
1217 my $noDelete = $i + 1 < @$Backups ? $Backups->[$i+1]{noFill} : 0;
1220 ($fullKeepIdx >= @$fullKeepCnt
1223 && $Backups->[$i]{startTime} - $Backups->[$prevFull]{startTime}
1224 < ($fullPeriod - $origFullPeriod / 2) * 24 * 3600
1228 # Delete the full backup
1230 #print("Deleting backup $i ($prevFull)\n");
1231 unshift(@delete, $i);
1234 while ( $fullKeepIdx < @$fullKeepCnt
1235 && $fullCnt >= $fullKeepCnt->[$fullKeepIdx] ) {
1238 $fullPeriod = 2 * $fullPeriod;
1243 # Now actually delete the backups
1245 for ( my $i = @delete - 1 ; $i >= 0 ; $i-- ) {
1246 print(LOG $bpc->timeStamp,
1247 "removing full backup $Backups->[$delete[$i]]{num}\n");
1248 BackupRemove($client, $Backups, $delete[$i]);
1254 # Removes any partial backups
1256 sub BackupPartialRemove
1258 my($client, $Backups) = @_;
1260 for ( my $i = @$Backups - 1 ; $i >= 0 ; $i-- ) {
1261 next if ( $Backups->[$i]{type} ne "partial" );
1262 BackupRemove($client, $Backups, $i);
1268 my @Backups = $bpc->BackupInfoRead($client);
1273 # Since we got a good backup we should remove any partial dumps
1274 # (the new backup might also be a partial, but that's ok).
1276 BackupPartialRemove($client, \@Backups);
1279 # Number the new backup
1281 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
1282 $num = $Backups[$i]{num} if ( $num < $Backups[$i]{num} );
1285 $bpc->RmTreeDefer("$TopDir/trash", "$Dir/$num") if ( -d "$Dir/$num" );
1286 if ( !rename("$Dir/new", "$Dir/$num") ) {
1287 print(LOG $bpc->timeStamp, "Rename $Dir/new -> $Dir/$num failed\n");
1290 $needLink = 1 if ( -f "$Dir/NewFileList" );
1293 # Add the new backup information to the backup file
1296 $Backups[$i]{num} = $num;
1297 $Backups[$i]{type} = $type;
1298 $Backups[$i]{startTime} = $startTime;
1299 $Backups[$i]{endTime} = $endTime;
1300 $Backups[$i]{size} = $sizeTotal;
1301 $Backups[$i]{nFiles} = $nFilesTotal;
1302 $Backups[$i]{xferErrs} = $stat{xferErrCnt} || 0;
1303 $Backups[$i]{xferBadFile} = $stat{xferBadFileCnt} || 0;
1304 $Backups[$i]{xferBadShare} = $stat{xferBadShareCnt} || 0;
1305 $Backups[$i]{nFilesExist} = $nFilesExist;
1306 $Backups[$i]{sizeExist} = $sizeExist;
1307 $Backups[$i]{sizeExistComp} = $sizeExistComp;
1308 $Backups[$i]{tarErrs} = $tarErrs;
1309 $Backups[$i]{compress} = $Conf{CompressLevel};
1310 $Backups[$i]{noFill} = $type eq "incr" ? 1 : 0;
1311 $Backups[$i]{level} = $type eq "incr" ? 1 : 0;
1312 $Backups[$i]{mangle} = 1; # name mangling always on for v1.04+
1313 $Backups[$i]{xferMethod} = $Conf{XferMethod};
1314 $Backups[$i]{charset} = $Conf{ClientCharset};
1316 # Save the main backups file
1318 $bpc->BackupInfoWrite($client, @Backups);
1320 # Save just this backup's info in case the main backups file
1323 BackupPC::Storage->backupInfoWrite($Dir, $Backups[$i]{num},
1326 unlink("$Dir/timeStamp.level0") if ( -f "$Dir/timeStamp.level0" );
1327 foreach my $ext ( qw(bad bad.z) ) {
1328 next if ( !-f "$Dir/XferLOG.$ext" );
1329 unlink("$Dir/XferLOG.$ext.old") if ( -f "$Dir/XferLOG.$ext" );
1330 rename("$Dir/XferLOG.$ext", "$Dir/XferLOG.$ext.old");
1334 # Now remove the bad files, replacing them if possible with links to
1337 foreach my $f ( $xfer->getBadFiles ) {
1339 my $shareM = $bpc->fileNameEltMangle($f->{share});
1340 my $fileM = $bpc->fileNameMangle($f->{file});
1341 unlink("$Dir/$num/$shareM/$fileM");
1342 for ( $j = $i - 1 ; $j >= 0 ; $j-- ) {
1344 if ( $Backups[$j]{mangle} ) {
1345 $file = "$shareM/$fileM";
1347 $file = "$f->{share}/$f->{file}";
1349 next if ( !-f "$Dir/$Backups[$j]{num}/$file" );
1351 my($exists, $digest, $origSize, $outSize, $errs)
1352 = BackupPC::PoolWrite::LinkOrCopy(
1354 "$Dir/$Backups[$j]{num}/$file",
1355 $Backups[$j]{compress},
1356 "$Dir/$num/$shareM/$fileM",
1357 $Conf{CompressLevel});
1360 # the hard link failed, most likely because the target
1361 # file has too many links. We have copied the file
1362 # instead, so add this to the new file list.
1364 if ( !defined($newFilesFH) ) {
1365 my $str = "Appending to NewFileList for $shareM/$fileM\n";
1366 $XferLOG->write(\$str);
1367 open($newFilesFH, ">>", "$TopDir/pc/$client/NewFileList")
1368 || die("can't open $TopDir/pc/$client/NewFileList");
1369 binmode($newFilesFH);
1371 if ( -f "$Dir/$num/$shareM/$fileM" ) {
1372 print($newFilesFH "$digest $origSize $shareM/$fileM\n");
1374 my $str = "Unable to link/copy $num/$f->{share}/$f->{file}"
1375 . " to $Backups[$j]{num}/$f->{share}/$f->{file}\n";
1376 $XferLOG->write(\$str);
1379 my $str = "Bad file $num/$f->{share}/$f->{file} replaced"
1381 . " $Backups[$j]{num}/$f->{share}/$f->{file}\n";
1382 $XferLOG->write(\$str);
1387 my $str = "Removed bad file $num/$f->{share}/$f->{file}"
1388 . " (no older copy to link to)\n";
1389 $XferLOG->write(\$str);
1392 close($newFilesFH) if ( defined($newFilesFH) );
1394 rename("$Dir/XferLOG$fileExt", "$Dir/XferLOG.$num$fileExt");
1395 rename("$Dir/NewFileList", "$Dir/NewFileList.$num");
1401 # Removes a specific backup
1405 my($client, $Backups, $idx) = @_;
1406 my($Dir) = "$TopDir/pc/$client";
1408 if ( $Backups->[$idx]{num} eq "" ) {
1409 print("BackupRemove: ignoring empty backup number for idx $idx\n");
1413 $bpc->RmTreeDefer("$TopDir/trash",
1414 "$Dir/$Backups->[$idx]{num}");
1415 unlink("$Dir/SmbLOG.$Backups->[$idx]{num}")
1416 if ( -f "$Dir/SmbLOG.$Backups->[$idx]{num}" );
1417 unlink("$Dir/SmbLOG.$Backups->[$idx]{num}.z")
1418 if ( -f "$Dir/SmbLOG.$Backups->[$idx]{num}.z" );
1419 unlink("$Dir/XferLOG.$Backups->[$idx]{num}")
1420 if ( -f "$Dir/XferLOG.$Backups->[$idx]{num}" );
1421 unlink("$Dir/XferLOG.$Backups->[$idx]{num}.z")
1422 if ( -f "$Dir/XferLOG.$Backups->[$idx]{num}.z" );
1423 splice(@{$Backups}, $idx, 1);
1426 sub CorrectHostCheck
1428 my($hostIP, $host) = @_;
1429 return if ( $hostIP eq $host && !$Conf{FixedIPNetBiosNameCheck}
1430 || $Conf{NmbLookupCmd} eq "" );
1431 my($netBiosHost, $netBiosUser) = $bpc->NetBiosInfoGet($hostIP);
1432 return "host $host has mismatching netbios name $netBiosHost"
1433 if ( $netBiosHost ne $host );
1438 # The Xfer method might tell us from time to time about processes
1439 # it forks. We tell BackupPC about this (for status displays) and
1440 # keep track of the pids in case we cancel the backup
1445 @xferPid = grep(/./, @xferPid);
1446 return if ( !@xferPid && $tarPid < 0 );
1447 my @pids = @xferPid;
1448 push(@pids, $tarPid) if ( $tarPid > 0 );
1449 my $str = join(",", @pids);
1450 $XferLOG->write(\"Xfer PIDs are now $str\n") if ( defined($XferLOG) );
1451 print("xferPids $str\n");
1455 # Run an optional pre- or post-dump command
1459 my($cmdType, $sharename) = @_;
1461 return if ( !defined($Conf{$cmdType}) );
1467 user => $Hosts->{$client}{user},
1468 moreUsers => $Hosts->{$client}{moreUsers},
1469 share => $ShareNames->[0],
1470 shares => $ShareNames,
1471 XferMethod => $Conf{XferMethod},
1472 sshPath => $Conf{SshPath},
1474 XferLOG => $XferLOG,
1476 xferOK => $stat{xferOK} || 0,
1477 hostError => $stat{hostError},
1479 cmdType => $cmdType,
1482 if ($cmdType eq 'DumpPreShareCmd' || $cmdType eq 'DumpPostShareCmd') {
1483 $vars->{share} = $sharename;
1486 my $cmd = $bpc->cmdVarSubstitute($Conf{$cmdType}, $vars);
1487 $XferLOG->write(\"Executing $cmdType: @$cmd\n");
1489 # Run the user's command, dumping the stdout/stderr into the
1490 # Xfer log file. Also supply the optional $vars and %Conf in
1491 # case the command is really perl code instead of a shell
1494 $bpc->cmdSystemOrEval($cmd,
1496 $XferLOG->write(\$_[0]);