]> git.sthu.org Git - sitarba.git/commitdiff
make tarbin configurable, move [destination] to [global]
authorStefan Huber <shuber2@gmx.at>
Mon, 21 May 2012 06:36:06 +0000 (08:36 +0200)
committerStefan Huber <shuber2@gmx.at>
Mon, 21 May 2012 06:36:06 +0000 (08:36 +0200)
shbackup

index e97dbf5182790944bfd2eb1c26987d6e8503ae2e..cd2443727f6ddf851e25c12ded9f4cd9f4f85795 100755 (executable)
--- a/shbackup
+++ b/shbackup
@@ -171,8 +171,9 @@ class Config:
     checksumfn = "checksum"
 
     def __init__(self):
-        self.directory = None
+        self.backupdir = None
         self.format = self.formats[0]
+        self.tarbin = "/bin/tar"
         self.excludes = []
         self.sets = []
         self.checksum = None
@@ -181,8 +182,9 @@ class Config:
 
 
     def __repr__(self):
-        return "[directory: " + self.directory + \
+        return "[backupdir: " + self.backupdir + \
                                  ", format: " + self.format + \
+                                 ", tarbin: " + self.tarbin + \
                                  ", excludes: " + repr(self.excludes) + \
                   ", epochs: " + repr(self.epochs) + \
                                  ", sets: " + repr(self.sets) + "]"
@@ -197,23 +199,21 @@ class Config:
         return realepochs
 
 
-    def _read_destination(self, config, sec):
+    def _read_global(self, config, sec):
         for opt in config.options(sec):
-            if opt=="directory":
-                self.directory = config.get(sec, opt)
-                if not os.path.isdir(self.directory):
-                    raise Config.ReadError("Directory '{0}' does not exist.".format(self.directory))
+            if opt=="backupdir":
+                self.backupdir = config.get(sec, opt)
+                if not os.path.isdir(self.backupdir):
+                    raise Config.ReadError("Backupdir '{0}' does not exist.".format(self.backupdir))
             elif opt=="format":
                 self.format = config.get(sec, opt)
                 if not self.format in Config.formats:
                     raise Config.ReadError("Invalid 'format' given.")
-            else:
-                raise Config.ReadError("Unknown option '{0}'.".format(opt))
-
-
-    def _read_global(self, config, sec):
-        for opt in config.options(sec):
-            if opt.startswith("exclude"):
+            elif opt=="tarbin":
+                self.tarbin = config.get(sec, opt)
+                if not os.path.isfile(self.tarbin):
+                    raise Config.ReadError("Tar binary '{0}' does not exist.".format(self.tarbin))
+            elif opt.startswith("exclude"):
                 self.excludes += [ config.get(sec, opt) ]
             else:
                 raise Config.ReadError("Unknown option '{0}'.".format(opt))
@@ -290,16 +290,13 @@ class Config:
         config = configparser.RawConfigParser()
         config.read(filename)
 
-        for reqsec in ["destination"]:
+        for reqsec in ["global"]:
             if not config.has_section(reqsec):
                 raise Config.ReadError("Mandatory section '" + reqsec + "' is missing.")
 
         for sec in config.sections():
 
-            if sec=="destination":
-                self._read_destination(config, sec)
-
-            elif sec=="global":
+            if sec=="global":
                 self._read_global(config, sec)
 
             elif sec.startswith("epoch "):
@@ -311,8 +308,8 @@ class Config:
             else:
                 raise Config.ReadError("Unknown section '" + sec + "'.")
 
-        if self.directory == None:
-            raise Config.ReadError("No destination directory set.")
+        if self.backupdir == None:
+            raise Config.ReadError("No backup directory set.")
 
 
         # Compute checksum of config file
@@ -325,7 +322,7 @@ class Config:
             f.close()
 
         try:
-            f = open(os.path.join(self.directory, self.checksumfn), 'r')
+            f = open(os.path.join(self.backupdir, self.checksumfn), 'r')
             self.lastchecksum = f.read().strip()
             f.close()
         except IOError:
@@ -341,10 +338,10 @@ class BackupManager:
 
 
     def listAllDirs(self):
-        """List all dirs in destination directory"""
+        """List all dirs in backupdir"""
 
         # Get all entries
-        basedir = self.conf.directory
+        basedir = self.conf.backupdir
         dirs = os.listdir(basedir)
         # Filter directories
         return [ d for d in dirs if os.path.isdir(os.path.join(basedir, d)) ]
@@ -393,9 +390,7 @@ class BackupManager:
         logfile = logging.getLogger('backuplog')
         logfile.info("Running file set: " + fileset.name)
 
-        tarpath = "/bin/tar"
         fsfn = os.path.join(targetdir, fileset.name) + "." + self.conf.format
-
         taropts = []
 
         # Add the since date, if given
@@ -414,7 +409,7 @@ class BackupManager:
         taropts += ["-C", "/"] + [ "./" + d.lstrip("/") for d in fileset.dirs]
 
         # Launch the tar process
-        tarargs = [tarpath] + ["-cpvaf", fsfn] + taropts
+        tarargs = [self.conf.tarbin] + ["-cpvaf", fsfn] + taropts
         logfile.debug("tar call: " + " ".join(tarargs))
         tarp = subprocess.Popen( tarargs, bufsize=-1, \
                 stdout=subprocess.PIPE, stderr=subprocess.PIPE )
@@ -492,8 +487,8 @@ class BackupManager:
         if yesno == "n":
             return
 
-        # Create new target directory
-        basedir = self.conf.directory
+        # Create new backup directory
+        basedir = self.conf.backupdir
         dirname = Backup.getDirName(now, epoch, mode)
         tmpdirname = dirname + ("-%x" % (random.random()*2e16) )
         targetdir = os.path.join(basedir, tmpdirname)
@@ -567,7 +562,7 @@ class BackupManager:
             logging.info("No stale/outdated entries to remove.")
             return
 
-        basedir = self.conf.directory
+        basedir = self.conf.backupdir
         yesno = self.ask_user_yesno("Remove entries marked by '*'? [y, N] ")
         if yesno == "y":
             for d in removeDirs: