* Moved call to NmbLookupFindHostCmd in BackupPC_dump to after the
[BackupPC.git] / bin / BackupPC_restore
index ff9b413..1a5e487 100755 (executable)
@@ -11,7 +11,7 @@
 #   Craig Barratt  <cbarratt@users.sourceforge.net>
 #
 # COPYRIGHT
-#   Copyright (C) 2001-2003  Craig Barratt
+#   Copyright (C) 2001-2007  Craig Barratt
 #
 #   This program is free software; you can redistribute it and/or modify
 #   it under the terms of the GNU General Public License as published by
@@ -29,7 +29,7 @@
 #
 #========================================================================
 #
-# Version 3.0.0alpha, released 23 Jan 2006.
+# Version 3.2.0beta0, released 17 Jan 2009.
 #
 # See http://backuppc.sourceforge.net.
 #
@@ -40,10 +40,7 @@ no  utf8;
 use lib "/usr/local/BackupPC/lib";
 use BackupPC::Lib;
 use BackupPC::FileZIO;
-use BackupPC::Xfer::Smb;
-use BackupPC::Xfer::Tar;
-use BackupPC::Xfer::Rsync;
-use BackupPC::Xfer::BackupPCd;
+use BackupPC::Xfer;
 use Socket;
 
 use File::Path;
@@ -101,9 +98,30 @@ mkpath($Dir, 0, 0777) if ( !-d $Dir );
 if ( !-f "$Dir/LOCK" ) {
     open(LOCK, ">", "$Dir/LOCK") && close(LOCK);
 }
-open(LOG, ">>", "$Dir/LOG");
-select(LOG); $| = 1; select(STDOUT);
 
+my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
+my $logPath = sprintf("$Dir/LOG.%02d%04d", $mon + 1, $year + 1900);
+
+if ( !-f $logPath ) {
+    #
+    # Compress and prune old log files
+    #
+    my $lastLog = $Conf{MaxOldPerPCLogFiles} - 1;
+    foreach my $file ( $bpc->sortedPCLogFiles($client) ) {
+        if ( $lastLog <= 0 ) {
+            unlink($file);
+            next;
+        }
+        $lastLog--;
+        next if ( $file =~ /\.z$/ || !$Conf{CompressLevel} );
+        BackupPC::FileZIO->compressCopy($file,
+                                        "$file.z",
+                                        undef,
+                                        $Conf{CompressLevel}, 1);
+    }
+}
+open(LOG, ">>", $logPath);
+select(LOG); $| = 1; select(STDOUT);
 
 #
 # Read the request file
@@ -196,6 +214,7 @@ my $RestoreLOG = BackupPC::FileZIO->open("$Dir/RestoreLOG$fileExt", 1,
                                      $Conf{CompressLevel});
 my $tarCreateFileCnt = 0;
 my $tarCreateByteCnt = 0;
+my $tarCreateDirCnt  = 0;
 my $tarCreateErrCnt  = 1;      # assume not ok until we learn otherwise
 my $tarCreateErr;
 my($logMsg, $xfer);
@@ -214,37 +233,14 @@ if ( $? && $Conf{UserCmdCheckStatus} ) {
 }
 $NeedPostCmd = 1;
 
-if ( $Conf{XferMethod} eq "tar" ) {
-    #
-    # Use tar (eg: tar/ssh) as the transport program.
-    #
-    $xfer = BackupPC::Xfer::Tar->new($bpc);
-} elsif ( $Conf{XferMethod} eq "rsync" || $Conf{XferMethod} eq "rsyncd" ) {
-    #
-    # Use rsync as the transport program.
-    #
-    if ( !defined($xfer = BackupPC::Xfer::Rsync->new($bpc)) ) {
-        my $errStr = BackupPC::Xfer::Rsync->errStr;
-       UserCommandRun("RestorePostUserCmd") if ( $NeedPostCmd );
-       $stat{hostError} = $errStr;
-       exit(RestoreCleanup($client));
-    }
-} elsif ( $Conf{XferMethod} eq "backuppcd" ) {
-    #
-    # Use backuppcd as the transport program.
-    #
-    if ( !defined($xfer = BackupPC::Xfer::BackupPCd->new($bpc)) ) {
-        my $errStr = BackupPC::Xfer::BackupPCd->errStr;
-       UserCommandRun("RestorePostUserCmd") if ( $NeedPostCmd );
-       $stat{hostError} = $errStr;
-       exit(RestoreCleanup($client));
-    }
-} else {
-    #
-    # Default is to use smbclient (smb) as the transport program.
-    #
-    $xfer = BackupPC::Xfer::Smb->new($bpc);
+$xfer = BackupPC::Xfer::create($Conf{XferMethod}, $bpc);
+if ( !defined($xfer) ) {
+    my $errStr = BackupPC::Xfer::errStr();
+    UserCommandRun("RestorePostUserCmd") if ( $NeedPostCmd );
+    $stat{hostError} = $errStr;
+    exit(RestoreCleanup($client));
 }
+
 my $useTar = $xfer->useTar;
 
 if ( $useTar ) {
@@ -329,10 +325,10 @@ if ( $useTar ) {
             @tarPathOpts,
             @{$RestoreReq{fileList}},
     );
-    my $logMsg = "Running: "
+    my $runMsg = "Running: "
               . $bpc->execCmd2ShellCmd("$BinDir/BackupPC_tarCreate", @tarArgs)
               . "\n";
-    $RestoreLOG->write(\$logMsg);
+    $RestoreLOG->write(\$runMsg);
     if ( !defined($tarPid = open(TAR, "-|")) ) {
        close(WH);
        # FIX: need to cleanup xfer
@@ -402,6 +398,7 @@ if ( $useTar ) {
            if ( /^Done: (\d+) files, (\d+) bytes, (\d+) dirs, (\d+) specials, (\d+) errors/ ) {
                $tarCreateFileCnt = $1;
                $tarCreateByteCnt = $2;
+               $tarCreateDirCnt  = $3;
                $tarCreateErrCnt  = $5;
            }
        }
@@ -426,7 +423,7 @@ if ( $useTar ) {
     #
     # otherwise the xfer module does everything for us
     #
-    print(LOG $bpc->timeStamp, "Starting restore\n");
+    print(LOG $bpc->timeStamp, $logMsg . "\n");
     print("started_restore\n");
     ($tarCreateFileCnt, $tarCreateByteCnt,
        $tarCreateErrCnt, $tarCreateErr) = $xfer->run();
@@ -464,7 +461,7 @@ sub CorrectHostCheck
                || $Conf{NmbLookupCmd} eq "" );
     my($netBiosHost, $netBiosUser) = $bpc->NetBiosInfoGet($hostIP);
     return "host $host has mismatching netbios name $netBiosHost"
-            if ( $netBiosHost ne $host );
+            if ( lc($netBiosHost) ne lc(substr($host, 0, 15)) );
     return;
 }
 
@@ -573,7 +570,7 @@ sub RestoreCleanup
        if ( $stat{hostAbort} && $stat{hostError} eq "" ) {
            $stat{hostError} = "lost network connection during restore";
        }
-       $RestoreLOG->write(\"Restore failed: $stat{hostError}\n")
+       $RestoreLOG->write(\"restore failed: $stat{hostError}\n")
                                            if ( defined($RestoreLOG) );
     }
 
@@ -604,10 +601,14 @@ sub RestoreCleanup
     $bpc->RestoreInfoWrite($client, @Restores);
 
     if ( !$stat{xferOK} ) {
-       print(LOG $bpc->timeStamp, "Restore failed ($stat{hostError})\n");
+       print(LOG $bpc->timeStamp, "restore failed ($stat{hostError})\n");
        print("restore failed: $stat{hostError}\n");
        return 1;
     } else {
+        $stat{xferErrCnt} ||= 0;
+       print(LOG $bpc->timeStamp, "restore $lastNum complete"
+                . " ($tarCreateFileCnt files, $tarCreateByteCnt bytes,"
+                . " $tarCreateDirCnt dirs, $stat{xferErrCnt} xferErrs)\n");
        print("restore complete\n");
        return;
     }