Using select.select on tarp's output
[sitarba.git] / shbackup
index d5b8b812073f75208576f3b80e2465d0d3757ff1..977fb5cd35403ccdf4fc41cc172369fa598d0b2d 100755 (executable)
--- a/shbackup
+++ b/shbackup
@@ -8,7 +8,7 @@ import datetime
 import os, shutil, sys
 import configparser
 import hashlib
-import subprocess, fcntl
+import subprocess, fcntl, select
 import random, re
 import logging
 
@@ -261,16 +261,21 @@ class BackupManager:
         tarpath = "/bin/tar"
         fsfn = os.path.join(targetdir, fileset.name) + "." + self.conf.format
 
-        taropts = ["-cpva"]
+        taropts = []
 
+        # Add the since date, if given
         if since != None:
             taropts += ["-N", since.strftime("%Y-%m-%d %H:%M:%S")]
 
+        # Add the exclude patterns
         for pat in self.conf.exclpatterns:
             taropts += ["--exclude", pat]
 
+        # Adding directories to backup
+        taropts += ["-C", "/"] + [ "./" + d.lstrip("/") for d in fileset.dirs]
+
         # Launch the tar process
-        tarargs = [tarpath] + taropts + ["-f", fsfn] + fileset.dirs
+        tarargs = [tarpath] + ["-cpvaf", fsfn] + taropts
         logfile.debug("tar call: " + " ".join(tarargs))
         tarp = subprocess.Popen( tarargs, bufsize=-1, \
                 stdout=subprocess.PIPE, stderr=subprocess.PIPE )
@@ -293,10 +298,15 @@ class BackupManager:
         # Read stdout and stderr of tarp
         errmsg = b""
         while tarp.poll() == None:
-            l = readlineNonBlocking(tarp.stdout)
-            if l != b"":
-                logging.debug(l[:-1].decode())
-            errmsg += readlineNonBlocking(tarp.stderr)
+            rd,wr,ex = select.select([tarp.stdout, tarp.stderr], [], [], 0.05)
+
+            if tarp.stdout in rd:
+                l = readlineNonBlocking(tarp.stdout)
+                if l != b"":
+                    logging.debug(l[:-1].decode())
+
+            if tarp.stderr in rd:
+                errmsg += readlineNonBlocking(tarp.stderr)
 
 
         # Get the remainging output of tarp
@@ -307,8 +317,8 @@ class BackupManager:
         # Get return code of tarp
         rett = tarp.wait()
         if rett != 0:
-            for l in errmsg.split("\n"):
-                logfile.error( l.decode().strip().rstrip() )
+            for l in errmsg.decode().split("\n"):
+                logfile.error(l)
             logfile.error(tarpath + " returned with exit status " + str(rett) + ".")