import os, shutil, sys
import configparser
import hashlib
-import subprocess, fcntl
+import subprocess, fcntl, select
import random, re
import logging
", mode: " + self.mode + "]"
def colAlignedString(self):
- return "%16s %8s %4s" % ( \
- self.date.strftime("%Y-%m-%d %H:%M"), self.epoch, self.mode)
+ age = datetime.datetime.now() - self.date
+ total_hours = age.total_seconds()/3600
+ if total_hours <= 48:
+ agestr = "(%s h)" % int(total_hours)
+ else:
+ agestr = "(%s d)" % age.days
+ return "%16s %7s %8s %4s" % ( \
+ self.date.strftime("%Y-%m-%d %H:%M"), agestr, self.epoch, self.mode)
@staticmethod
def getDirName(date, epoch, mode):
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 )
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
- def readlineNonBlocking(stream):
- """Read a line nonblocking. Returns b'' if nothing read."""
- try:
- return stream.readline()
- except:
- return b''
- pass
-
-
# 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:
+ logging.debug( tarp.stdout.readline()[:-1].decode() )
+ if tarp.stderr in rd:
+ errmsg += tarp.stderr.read()
# Get the remainging output of tarp
for l in tarp.stdout.readlines():
# 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) + ".")