2 #============================================================= -*-perl-*-
4 # BackupPC_restore: Restore files to a client.
8 # Usage: BackupPC_restore <hostIP> <host> <reqFileName>
11 # Craig Barratt <cbarratt@users.sourceforge.net>
14 # Copyright (C) 2001 Craig Barratt
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #========================================================================
32 # Version 1.5.0, released 2 Aug 2002.
34 # See http://backuppc.sourceforge.net.
36 #========================================================================
39 use lib "__INSTALLDIR__/lib";
41 use BackupPC::FileZIO;
42 use BackupPC::Xfer::Smb;
43 use BackupPC::Xfer::Tar;
48 use vars qw( %RestoreReq );
50 ###########################################################################
52 ###########################################################################
54 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
55 my $TopDir = $bpc->TopDir();
56 my $BinDir = $bpc->BinDir();
57 my %Conf = $bpc->Conf();
59 my($hostIP, $host, $reqFileName);
64 print("usage: $0 <hostIP> <host> <reqFileName>\n");
67 $hostIP = $1 if ( $ARGV[0] =~ /(.+)/ );
68 $host = $1 if ( $ARGV[1] =~ /(.+)/ );
69 if ( $ARGV[2] !~ /^([\w.]+)$/ ) {
70 print("$0: bad reqFileName (arg #3): $ARGV[2]\n");
75 my $Hosts = $bpc->HostInfoRead();
78 # Re-read config file, so we can include the PC-specific config
80 $bpc->ConfigRead($host);
83 my $Dir = "$TopDir/pc/$host";
88 # Catch various signals
90 $SIG{INT} = \&catch_signal;
91 $SIG{ALRM} = \&catch_signal;
92 $SIG{TERM} = \&catch_signal;
95 # Read the request file
97 if ( !(my $ret = do "$Dir/$reqFileName") ) {
98 die "couldn't parse $Dir/$reqFileName: $@" if $@;
99 die "couldn't do $Dir/$reqFileName: $!" unless defined $ret;
100 die "couldn't run $Dir/$reqFileName";
104 # Make sure we eventually timeout if there is no activity from
105 # the data transport program.
107 alarm($Conf{SmbClientTimeout});
109 mkpath($Dir, 0, 0777) if ( !-d $Dir );
110 if ( !-f "$Dir/LOCK" ) {
111 open(LOCK, ">$Dir/LOCK") && close(LOCK);
113 open(LOG, ">>$Dir/LOG");
114 select(LOG); $| = 1; select(STDOUT);
117 # Check if $host is alive
119 my $delay = $bpc->CheckHostAlive($hostIP);
121 print(LOG $bpc->timeStamp, "no ping response\n");
122 print("no ping response\n");
124 } elsif ( $delay > $Conf{PingMaxMsec} ) {
125 printf(LOG "%sping too slow: %.4gmsec\n", $bpc->timeStamp, $delay);
126 printf("ping too slow: %.4gmsec (threshold is %gmsec)\n",
127 $delay, $Conf{PingMaxMsec});
132 # Make sure it is really the machine we expect
134 if ( (my $errMsg = CorrectHostCheck($hostIP, $host)) ) {
135 print(LOG $bpc->timeStamp, "restore failed: $errMsg\n");
136 print("restore failed: $errMsg\n");
141 # Setup file extension for compression and open RestoreLOG output file
143 $Conf{CompressLevel} = 0 if ( !BackupPC::FileZIO->compOk );
144 my $fileExt = $Conf{CompressLevel} > 0 ? ".z" : "";
145 my $RestoreLOG = BackupPC::FileZIO->open("$Dir/RestoreLOG$fileExt", 1,
146 $Conf{CompressLevel});
147 my $startTime = time();
149 my $tarCreateFileCnt = 0;
150 my $tarCreateByteCnt = 0;
151 my $tarCreateErrCnt = 1; # assume not ok until we learn otherwise
153 my($logMsg, %stat, $xfer);
160 $stat{xferOK} = $stat{hostAbort} = undef;
161 $stat{hostError} = $stat{lastOutputLine} = undef;
164 # Create a pipe to connect BackupPC_tarCreate to the transport program
165 # (smbclient, tar, etc).
166 # WH is the write handle for writing, provided to BackupPC_tarCreate
167 # and RH is the other end of the pipe for reading provided to the
173 # Run the transport program, which reads from RH and extracts the data.
179 shareName => $RestoreReq{shareDest},
182 XferLOG => $RestoreLOG,
184 if ( $Conf{XferMethod} eq "tar" ) {
186 # Use tar (eg: tar/ssh) as the transport program.
188 $xfer = BackupPC::Xfer::Tar->new($bpc, $xferArgs);
191 # Default is to use smbclient (smb) as the transport program.
193 $xfer = BackupPC::Xfer::Smb->new($bpc, $xferArgs);
195 if ( !defined($logMsg = $xfer->start()) ) {
196 print(LOG $bpc->timeStamp, $xfer->errStr, "\n");
197 print($xfer->errStr, "\n");
201 # The parent must close the read handle since the transport program
207 # fork a child for BackupPC_tarCreate. TAR is a file handle
208 # on which we (the parent) read the stderr from BackupPC_tarCreate.
211 if ( defined($RestoreReq{pathHdrDest})
212 && $RestoreReq{pathHdrDest} ne $RestoreReq{pathHdrSrc} ) {
213 @tarPathOpts = ("-r", $RestoreReq{pathHdrSrc},
214 "-p", $RestoreReq{pathHdrDest}
218 "-h", $RestoreReq{hostSrc},
219 "-n", $RestoreReq{num},
220 "-s", $RestoreReq{shareSrc},
223 @{$RestoreReq{fileList}},
225 my $logMsg = "Running: $BinDir/BackupPC_tarCreate "
226 . join(" ", @tarArgs) . "\n";
227 $RestoreLOG->write(\$logMsg);
228 if ( !defined($tarPid = open(TAR, "-|")) ) {
229 print(LOG $bpc->timeStamp, "can't fork to run tar\n");
230 print("can't fork to run tar\n");
232 # FIX: need to cleanup xfer
237 # This is the tarCreate child. Clone STDERR to STDOUT,
238 # STDOUT to WH, and then exec BackupPC_tarCreate.
242 open(STDERR, ">&STDOUT");
244 open(STDOUT, ">&WH");
245 exec("$BinDir/BackupPC_tarCreate", @tarArgs);
246 print(LOG $bpc->timeStamp, "can't exec $BinDir/BackupPC_tarCreate\n");
247 # FIX: need to cleanup xfer
251 # The parent must close the write handle since BackupPC_tarCreate
256 $xferPid = $xfer->xferPid;
257 print(LOG $bpc->timeStamp, $logMsg, " (tarPid=$tarPid, xferPid=$xferPid)\n");
258 print("started restore, tarPid=$tarPid, xferPid=$xferPid\n");
261 # Parse the output of the transfer program and BackupPC_tarCreate
262 # while they run. Since we are reading from two or more children
265 my($FDread, $tarOut, $mesg);
266 vec($FDread, fileno(TAR), 1) = 1;
267 $xfer->setSelectMask(\$FDread);
271 last if ( $FDread =~ /^\0*$/ );
272 select(my $rout = $FDread, undef, $ein, undef);
273 if ( vec($rout, fileno(TAR), 1) ) {
274 if ( sysread(TAR, $mesg, 8192) <= 0 ) {
275 vec($FDread, fileno(TAR), 1) = 0;
277 $tarCreateErrCnt = 1;
278 $tarCreateErr = "BackupPC_tarCreate failed";
284 while ( $tarOut =~ /(.*?)[\n\r]+(.*)/s ) {
287 $RestoreLOG->write(\"tarCreate: $_\n");
288 if ( /^Done: (\d+) files, (\d+) bytes, (\d+) dirs, (\d+) specials, (\d+) errors/ ) {
289 $tarCreateFileCnt = $1;
290 $tarCreateByteCnt = $2;
291 $tarCreateErrCnt = $5;
294 last if ( !$xfer->readOutput(\$FDread, $rout) );
295 while ( my $str = $xfer->logMsgGet ) {
296 print(LOG $bpc->timeStamp, "xfer: $str\n");
298 if ( $xfer->getStats->{fileCnt} == 1 ) {
300 # Make sure it is still the machine we expect. We do this while
301 # the transfer is running to avoid a potential race condition if
302 # the ip address was reassigned by dhcp just before we started
305 if ( my $errMsg = CorrectHostCheck($hostIP, $host) ) {
306 $stat{hostError} = $errMsg;
313 # Merge the xfer status (need to accumulate counts)
315 my $newStat = $xfer->getStats;
316 foreach my $k ( (keys(%stat), keys(%$newStat)) ) {
317 next if ( !defined($newStat->{$k}) );
318 if ( $k =~ /Cnt$/ ) {
319 $stat{$k} += $newStat->{$k};
320 delete($newStat->{$k});
323 if ( !defined($stat{$k}) ) {
324 $stat{$k} = $newStat->{$k};
325 delete($newStat->{$k});
329 $RestoreLOG->close();
330 $stat{xferOK} = 0 if ( $stat{hostError} || $stat{hostAbort} || $tarCreateErr );
332 if ( !$stat{xferOK} ) {
334 # kill off the tranfer program, first nicely then forcefully
340 # kill off the tar process, first nicely then forcefully
351 # Do one last check to make sure it is still the machine we expect.
353 if ( $stat{xferOK} && (my $errMsg = CorrectHostCheck($hostIP, $host)) ) {
354 $stat{hostError} = $errMsg;
357 @Restores = $bpc->RestoreInfoRead($host);
358 for ( my $i = 0 ; $i < @Restores ; $i++ ) {
359 $lastNum = $Restores[$i]{num} if ( $lastNum < $Restores[$i]{num} );
362 rename("$Dir/RestoreLOG$fileExt", "$Dir/RestoreLOG.$lastNum$fileExt");
363 rename("$Dir/$reqFileName", "$Dir/RestoreInfo.$lastNum");
364 my $endTime = time();
367 # If the restore failed, clean up
369 if ( !$stat{xferOK} ) {
371 # wait a short while and see if the system is still alive
373 $stat{hostError} ||= $tarCreateErr if ( $tarCreateErr ne "" );
374 $stat{hostError} = $stat{lastOutputLine} if ( $stat{hostError} eq "" );
375 if ( $stat{hostError} ) {
376 print(LOG $bpc->timeStamp,
377 "Got fatal error during xfer ($stat{hostError})\n");
380 if ( $bpc->CheckHostAlive($hostIP) < 0 ) {
381 $stat{hostAbort} = 1;
383 if ( $stat{hostAbort} ) {
384 $stat{hostError} = "lost network connection during restore";
389 # Add the new restore information to the restore file
391 @Restores = $bpc->RestoreInfoRead($host);
393 $Restores[$i]{num} = $lastNum;
394 $Restores[$i]{startTime} = $startTime;
395 $Restores[$i]{endTime} = $endTime;
396 $Restores[$i]{result} = $stat{xferOK} ? "ok" : "failed";
397 $Restores[$i]{errorMsg} = $stat{hostError};
398 $Restores[$i]{nFiles} = $tarCreateFileCnt;
399 $Restores[$i]{size} = $tarCreateByteCnt;
400 $Restores[$i]{tarCreateErrs} = $tarCreateErrCnt;
401 $Restores[$i]{xferErrs} = $stat{xferErrCnt} || 0;
403 while ( @Restores > $Conf{RestoreInfoKeepCnt} ) {
404 my $num = $Restores[0]{num};
405 unlink("$Dir/RestoreLOG.$num.z");
406 unlink("$Dir/RestoreLOG.$num");
407 unlink("$Dir/RestoreInfo.$num");
410 $bpc->RestoreInfoWrite($host, @Restores);
412 if ( !$stat{xferOK} ) {
413 print(LOG $bpc->timeStamp, "Restore aborted ($stat{hostError})\n");
414 print("restore failed: $stat{hostError}\n");
416 print("restore complete\n");
419 ###########################################################################
421 ###########################################################################
425 my($hostIP, $host) = @_;
426 return if ( $hostIP eq $host && !$Conf{FixedIPNetBiosNameCheck} );
427 my($netBiosHost, $netBiosUser) = $bpc->NetBiosInfoGet($hostIP);
428 return "host $host has mismatching netbios name $netBiosHost"
429 if ( $netBiosHost ne $host );