add TODO
[sitarba.git] / shbackup.py
index 0aedc6f623589bdc79b872245ec09f744bb19dec..f9f700719f4bc8c950fea2099062c57dbd0ce68a 100755 (executable)
@@ -46,11 +46,13 @@ class Config:
                                  ", keeps: " + str(self.epochkeeps) + \
                                  ", modes: " + str(self.epochmodes) + \
                                  ", exclpatterns: " + str(self.exclpatterns) + \
-                                 ", sets: " + str([str(s) for s in self.sets]) + "]";
+                                 ", sets: " + str([str(s) for s in self.sets]) + "]"
 
     def read(self, filename):
         """Read configuration from file"""
 
+        if not os.path.isfile(filename):
+            raise Config.ReadException("No file '" + filename + "'.")
 
         config = configparser.RawConfigParser()
         config.read(filename)
@@ -171,14 +173,14 @@ class BackupManager:
         basedir = self.conf.directory
         dirs = os.listdir(basedir)
         # Filter directories
-        return filter( lambda d: os.path.isdir(os.path.join(basedir, d)), dirs)
+        return [ d for d in dirs if os.path.isdir(os.path.join(basedir, d)) ]
 
     def listOldBackups(self):
         """Returns a list of old backups."""
 
         backups = []
 
-        for entry in filter(Backup.isBackupDir, self.listAllDirs()):
+        for entry in [ b for b in self.listAllDirs() if Backup.isBackupDir(b) ]:
             [strdate, strtime, epoch, mode] = entry.split("-")
 
             if not epoch in Epoch.keys():
@@ -206,7 +208,7 @@ class BackupManager:
                 continue
 
             # Get backups of that epoch
-            byepoch = list(sorted(filter( lambda b: b.epoch==e, backups), \
+            byepoch = list(sorted( [ b for b in backups if b.epoch==e], \
                 key=lambda b: b.date))
 
             # If there are any, determine the latest
@@ -278,7 +280,7 @@ class BackupManager:
         mode = self.conf.epochmodes[epoch]
         print("Making a backup. Epoch: " + epoch + ", mode: " + mode)
 
-        oldfullbackups = list(filter(lambda b: b.mode=="full", oldbackups))
+        oldfullbackups = [ b for b in oldbackups if b.mode=="full" ]
 
         # No old full backups existing
         if mode != "full" and len(oldfullbackups)==0:
@@ -324,12 +326,13 @@ class BackupManager:
     def prune(self):
         """Prune old backup files"""
 
-        noBackupDir = lambda d: not Backup.isBackupDir(d)        
-        dirs = list(filter(noBackupDir, self.listAllDirs()))
+        # Collect all directories not matching backup name
+        dirs = [ d for d in self.listAllDirs() if not Backup.isBackupDir(d) ]
 
+        # Get all directories which are outdated
         backups = self.listOldBackups()
-        byepoch = { e : list(reversed(sorted(filter(lambda b: b.epoch==e, backups), \
-                key=lambda b : b.date))) for e in Epoch }
+        byepoch = { e : list(sorted( [ b for b in backups if b.epoch == e ], \
+                key=lambda b : b.date, reverse=True)) for e in Epoch }
         for e in byepoch:
             keep = self.conf.epochkeeps[e]
             old = byepoch[e][keep:]
@@ -344,22 +347,57 @@ class BackupManager:
             print("  " + d)
 
         basedir = self.conf.directory
-        yesno = input("Remove listed entries? [y,N] ")
+        yesno = input("Remove listed entries? [y, N] ")
         if yesno == "y":
             for d in dirs:
-                shutil.rmtree(os.path.join(basedir,d))
+                shutil.rmtree(os.path.join(basedir, d))
+
+
+def printUsage():
+    """Print --help text"""
+
+    print("shbackup - a simple backup solution.")
+    print("")
+    print("Usage:")
+    print("  " + sys.argv[0] + " [-C <configfile>")
+    print("  " + sys.argv[0] + " --help")
+    print("")
+    print("Options:")
+    print("  -C <configfile>        default: /etc/shbackup.conf")
 
 
 if __name__ == "__main__":
 
-    conffn = "shbackup.conf"
+    conffn = "/etc/shbackup.conf"
+
+    i=0
+    while i < len(sys.argv)-1:
+        i += 1
+        opt = sys.argv[i]
+
+        if opt in ["-h", "--help"]:
+            printUsage()
+            exit(0)
+
+        elif opt in ["-C", "--config"]:
+            i += 1
+            conffn = sys.argv[i]
+            continue
+
+        else:
+            print("Unknown option: " + opt)
+            exit(1)
 
-    if len(sys.argv) > 1:
-        conffn = sys.argv[1]
+    try:
+        man = BackupManager(conffn)
+        man.backup()
+        man.prune()
 
-    man = BackupManager(conffn)
-    man.backup()
-    man.prune()
+    except Config.ReadException as e:
+        print("Error reading config file: ", end="")
+        for a in e.args:
+            print(a, end=" ")
+        print()