X-Git-Url: https://git.sthu.org/?p=sitarba.git;a=blobdiff_plain;f=shbackup;h=0cfc4c74f3a03f5a0d2c846ef7c191f11600770f;hp=41658696b6097a9272b3e49a0f7e291ec515e004;hb=8a63842725558d8877a933bbd1f5312bb8e6f195;hpb=e98aacb004a6a0e76ebcd32b958de979ee2bca75 diff --git a/shbackup b/shbackup index 4165869..0cfc4c7 100755 --- a/shbackup +++ b/shbackup @@ -8,7 +8,7 @@ import datetime import os, shutil, sys import configparser import hashlib -import subprocess +import subprocess, fcntl, select import random, re import logging @@ -57,8 +57,14 @@ class Backup: ", 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): @@ -261,34 +267,50 @@ 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] - tarargs = [tarpath] + taropts + ["-f", fsfn] + fileset.dirs + # Adding directories to backup + taropts += ["-C", "/"] + [ "./" + d.lstrip("/") for d in fileset.dirs] + + # Launch the tar process + tarargs = [tarpath] + ["-cpvaf", fsfn] + taropts logfile.debug("tar call: " + " ".join(tarargs)) tarp = subprocess.Popen( tarargs, bufsize=-1, \ stdout=subprocess.PIPE, stderr=subprocess.PIPE ) - # Output stdout of tar + # Change tarp's stdout and stderr to non-blocking + for s in [tarp.stdout, tarp.stderr]: + fd = s.fileno() + fl = fcntl.fcntl(fd, fcntl.F_GETFL) + fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK) + + # Read stdout and stderr of tarp + errmsg = b"" while tarp.poll() == None: - l = tarp.stdout.readline() - if l != "": - logging.debug(l.decode().rstrip()) + 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() - # Output remaining output of tar + # Get the remainging output of tarp for l in tarp.stdout.readlines(): logging.debug(l.decode().rstrip()) + errmsg += tarp.stderr.read() + # Get return code of tarp rett = tarp.wait() if rett != 0: - for l in tarp.stderr.readlines(): - logfile.error( l.decode().strip().rstrip() ) - sys.stderr.write( tarp.stderr.read().decode() ) + for l in errmsg.decode().split("\n"): + logfile.error(l) logfile.error(tarpath + " returned with exit status " + str(rett) + ".") @@ -415,7 +437,11 @@ class BackupManager: yesno = self.ask_user_yesno("Remove entries marked by '*'? [y, N] ") if yesno == "y": for d in removeDirs: - shutil.rmtree(os.path.join(basedir, d)) + try: + shutil.rmtree(os.path.join(basedir, d)) + except OSError as e: + logging.error("Error when removing '%s': %s" % (d,e.strerror) ) + def ask_user_yesno(self, question): if LogConf.con.level <= logging.INFO: @@ -447,7 +473,7 @@ def printUsage(): print(" -m, --mode override mode: full, diff, or incr") print(" -v, --verbose be more verbose and interact with user") print(" --verbosity LEVEL set verbosity to LEVEL, which can be") - print(" warning, info, debug") + print(" error, warning, info, debug") print(" -V, --version print version info")