From: Stefan Huber Date: Tue, 15 May 2012 10:04:13 +0000 (+0200) Subject: Using select.select on tarp's output X-Git-Tag: v1.0~5 X-Git-Url: https://git.sthu.org/?a=commitdiff_plain;h=8bc0914cc0e9ebe9e42c97ce6d85f399e4a2f4da;p=sitarba.git Using select.select on tarp's output --- diff --git a/shbackup b/shbackup index d5b8b81..977fb5c 100755 --- 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) + ".") diff --git a/shbackup.conf b/shbackup.conf index fb293cf..287a02e 100644 --- a/shbackup.conf +++ b/shbackup.conf @@ -1,6 +1,6 @@ [destination] # Where shbackup stores the backups and other stuff -directory = ./backup +directory = /media/backup # Which format should be used by tar? # One of tar, tar.gz, tar.bz2, tar.xz format = tar.gz @@ -8,8 +8,9 @@ format = tar.gz [history] # keepEPOCH contains the number of backups that are kept for that particular -# epoch, where EPOCH can be year, month, week, day, or hour. modeEPOCH defines -# the backup mode for that particular epoch. Mode can be full, diff or incr. +# epoch, where EPOCH can be year, month, week, day, or hour. If the number is 0 +# then no backup iscreated for that epoch. modeEPOCH defines the backup mode +# for that particular epoch. Mode can be full, diff or incr. keephour = 1 keepday = 14 keepweek = 2 @@ -23,17 +24,20 @@ modeyear = full [input] -# exclude* contains --exclude patterns for tar. Files matching the pattern are -# not included into the backup. +# Each option starting with "exclude" contains a single --exclude pattern for +# tar. Files matching the pattern are not included into the backup. Tar is +# invoked with "-C /" and, thus, exclude patterns refering to absolute paths +# need to start with "./" instead of "/". exclude1 = */tmp/* exclude2 = */[Cc]cache/* exclude3 = */log* -exclude5 = /home/*/.wine/* -exclude7 = /lib/layman/* +exclude5 = ./home/*/.wine/* +exclude7 = ./lib/layman/* # A file set called 'home'. Each file set is packed into one archive using tar. [set home] -# dir* is one directory which belongs to this file set. +# Each option starting with "dir" contains a single directory which belongs to +# this file set. dir1 = /home/username/ dir2 = /media/books/