* Added BackupPC::Xfer::Protocol as a common class for each Xfer
[BackupPC.git] / bin / BackupPC_archive
index c61e5cf..87ed842 100644 (file)
@@ -8,10 +8,10 @@
 #   Usage: BackupPC_archive <user> <archiveclient> <reqFileName>
 #
 # AUTHOR
-#   Craig Barratt  <cbarratt@users.sourceforge.net>
+#   Josh Marshall
 #
 # 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 2.1.0_CVS, released 8 Feb 2004.
+# Version 3.1.0, released 25 Nov 2007.
 #
 # See http://backuppc.sourceforge.net.
 #
@@ -64,7 +64,7 @@ if ( @ARGV != 3 ) {
 }
 $user = $1 if ( $ARGV[0] =~ /(.+)/ );
 $client = $1 if ( $ARGV[1] =~ /(.+)/ );
-if ( $ARGV[2] !~ /^([\w.]+)$/ ) {
+if ( $ARGV[2] !~ /^([\w\.\s-]+)$/ ) {
     print("$0: bad reqFileName (arg #3): $ARGV[2]\n");
     exit(1);
 }
@@ -91,9 +91,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
@@ -139,7 +160,10 @@ if ( $Conf{ClientNameAlias} ne "" ) {
 #
 # Setup file extension for compression and open ArchiveLOG output file
 #
-$Conf{CompressLevel} = 0 if ( !BackupPC::FileZIO->compOk );
+if ( $Conf{CompressLevel} && !BackupPC::FileZIO->compOk ) {
+    $stat{hostError} = "Compress::Zlib not found";
+    exit(ArchiveCleanup($client));
+}
 my $fileExt = $Conf{CompressLevel} > 0 ? ".z" : "";
 my $ArchiveLOG = BackupPC::FileZIO->open("$Dir/ArchiveLOG$fileExt", 1,
                                      $Conf{CompressLevel});
@@ -154,6 +178,10 @@ local(*RH, *WH);
 # Run an optional pre-archive command
 #
 UserCommandRun("ArchivePreUserCmd");
+if ( $? && $Conf{UserCmdCheckStatus} ) {
+    $stat{hostError} = "ArchivePreUserCmd returned error status $?";
+    exit(ArchiveCleanup($client));
+}
 $NeedPostCmd = 1;
 
 $xfer = BackupPC::Xfer::Archive->new($bpc);
@@ -241,12 +269,18 @@ sub ArchiveCleanup
 
     if ( !$stat{xferOK} ) {
        #
-       # kill off the tranfer program, first nicely then forcefully
+       # Kill off the tranfer program, first nicely then forcefully.
+       # We use negative PIDs to make sure all processes in each
+       # group get the signal.
        #
        if ( @xferPid ) {
-           kill(2, @xferPid);
+           foreach my $pid ( @xferPid ) {
+               kill($bpc->sigName2num("INT"), -$pid);
+           }
            sleep(1);
-           kill(9, @xferPid);
+           foreach my $pid ( @xferPid ) {
+               kill($bpc->sigName2num("KILL"), -$pid);
+           }
        }
     }
 
@@ -262,7 +296,13 @@ sub ArchiveCleanup
     #
     # Run an optional post-archive command
     #
-    UserCommandRun("ArchivePostUserCmd") if ( $NeedPostCmd );
+    if ( $NeedPostCmd ) {
+        UserCommandRun("ArchivePostUserCmd");
+        if ( $? && $Conf{UserCmdCheckStatus} ) {
+            $stat{hostError} = "RestorePreUserCmd returned error status $?";
+            $stat{xferOK} = 0;
+        }
+    }
 
     rename("$Dir/ArchiveLOG$fileExt", "$Dir/ArchiveLOG.$lastNum$fileExt");
     rename("$Dir/$reqFileName", "$Dir/ArchiveInfo.$lastNum");
@@ -272,11 +312,7 @@ sub ArchiveCleanup
     # If the archive failed, clean up
     #
     if ( !$stat{xferOK} ) {
-       #
-       # wait a short while and see if the system is still alive
-       #
        $stat{hostError} = $stat{lastOutputLine} if ( $stat{hostError} eq "" );
-       sleep(2);
        $stat{hostAbort} = 1;
        $ArchiveLOG->write(\"Archive failed: $stat{hostError}")
                                            if ( defined($ArchiveLOG) );
@@ -336,9 +372,9 @@ sub pidHandler
 #
 sub UserCommandRun
 {
-    my($type) = @_;
+    my($cmdType) = @_;
 
-    return if ( !defined($Conf{$type}) );
+    return if ( !defined($Conf{$cmdType}) );
     my $vars = {
         xfer         => $xfer,
         client       => $client,
@@ -358,10 +394,11 @@ sub UserCommandRun
         XferLOG      => $ArchiveLOG,
         stat         => \%stat,
         xferOK       => $stat{xferOK} || 0,
-       type         => $type,
+       type         => "archive",
+       cmdType      => $cmdType,
     };
-    my $cmd = $bpc->cmdVarSubstitute($Conf{$type}, $vars);
-    $ArchiveLOG->write(\"Executing $type: @$cmd\n");
+    my $cmd = $bpc->cmdVarSubstitute($Conf{$cmdType}, $vars);
+    $ArchiveLOG->write(\"Executing $cmdType: @$cmd\n");
     #
     # Run the user's command, dumping the stdout/stderr into the
     # Xfer log file.  Also supply the optional $vars and %Conf in