Adding a '-n, --dry-run' option
[sitarba.git] / sitarba
diff --git a/sitarba b/sitarba
index d941c7048e5571099cf83ccfccb4b752b96329a5..160f09b7f165bed39f915b78c00cd6d3ca7d65ee 100755 (executable)
--- a/sitarba
+++ b/sitarba
@@ -15,6 +15,11 @@ import logging
 
 Modes = ["full", "incr", "diff"]
 
+
+class Options:
+    dryrun = False
+
+
 class Epoch:
 
     units = {
@@ -356,7 +361,7 @@ class BackupManager:
         return [ d for d in dirs if os.path.isdir(os.path.join(basedir, d)) ]
 
 
-    def listOldBackups(self):
+    def listExistingBackups(self):
         """Returns a list of old backups."""
 
         backups = []
@@ -463,7 +468,7 @@ class BackupManager:
         then use mode for given epoch. Use given mode otherwise."""
 
         now = datetime.datetime.now()
-        oldbackups = self.listOldBackups()
+        oldbackups = self.listExistingBackups()
 
         # Get epoch of backup
         if epoch == None:
@@ -498,6 +503,9 @@ class BackupManager:
         if since != None:
             logging.debug("Making backup relative to " + since.ctime())
 
+        if Options.dryrun:
+            return
+
         yesno = self.ask_user_yesno("Proceed? [Y, n] ")
         if yesno == "n":
             return
@@ -507,12 +515,12 @@ class BackupManager:
         dirname = Backup.getDirName(now, epoch, mode)
         tmpdirname = dirname + ("-%x" % (random.random()*2e16) )
         targetdir = os.path.join(basedir, tmpdirname)
-        os.mkdir( targetdir )
+        os.mkdir(targetdir)
 
 
         # Add file logger
         logfile = logging.getLogger("backuplog")
-        fil = logging.FileHandler( os.path.join(targetdir, "log") )
+        fil = logging.FileHandler(os.path.join(targetdir, "log"))
         fil.setLevel(logging.DEBUG)
         logfile.addHandler(fil)
 
@@ -540,14 +548,16 @@ class BackupManager:
         """Prune old backup files"""
 
         allDirs = sorted(self.listAllDirs())
-        # Collect all directories not matching backup name
+        # Collect all directories that are removed
         removeDirs = [ d for d in allDirs if not Backup.isBackupDir(d) ]
 
-        # Get all directories which are kept
-        backups = self.listOldBackups()
-        keepdirs = []
+        # Get all backups
+        backups = self.listExistingBackups()
+        # Group backups by epoch and sort them by age
         byepoch = { e : list(sorted( [ b for b in backups if b.epoch == e ], \
-                key=lambda b : b.date, reverse=True)) for e in self.conf.getRealEpochsSorted() }
+                key=lambda b : b.date, reverse=True)) \
+                for e in self.conf.getRealEpochsSorted() }
+        # If we have too many backups of a specific epoch --> add them to remove list
         for e in byepoch:
             epoch = self.conf.epochs[e]
             old = byepoch[e][epoch.numkeeps:]
@@ -577,6 +587,9 @@ class BackupManager:
             logging.info("No stale/outdated entries to remove.")
             return
 
+        if Options.dryrun:
+            return
+
         basedir = self.conf.backupdir
         yesno = self.ask_user_yesno("Remove entries marked by '*'? [y, N] ")
         if yesno == "y":
@@ -615,6 +628,7 @@ def printUsage():
     print("  -e, --epoch EPOCH          force to create backup for given epoch, which")
     print("                             can be 'sporadic' or one of the configured epochs")
     print("  -m, --mode MODE            override mode: full, diff, or incr")
+    print("  -n, --dry-run              don't do anything, just tell what would be done")
     print("  -v, --verbose              be more verbose and interact with user")
     print("  --verbosity LEVEL          set verbosity to LEVEL, which can be")
     print("                             error, warning, info, debug")
@@ -684,6 +698,9 @@ if __name__ == "__main__":
                 logging.error("Unknown mode '" + mode + "'.")
                 exit(1)
 
+        elif opt in ["-n", "--dry-run"]:
+            Options.dryrun = True
+
         elif opt in ["-e", "--epoch"]:
             i += 1
             epoch = sys.argv[i]
@@ -708,7 +725,7 @@ if __name__ == "__main__":
             man.backup(epoch, mode)
 
         if cmd == "list":
-            for b in sorted(man.listOldBackups(), key=lambda b: b.date):
+            for b in sorted(man.listExistingBackups(), key=lambda b: b.date):
                 print(b.colAlignedString())
 
         if cmd == "prune":