add many cmdline options
[sitarba.git] / shbackup
similarity index 85%
rename from shbackup.py
rename to shbackup
index e1f038d559708119baa96f8b2522ee4397ea68b0..0137b9d25356f71ae9b3165f80c66457487bafc3 100755 (executable)
+++ b/shbackup
@@ -11,7 +11,8 @@ import random, re
 
 Mode = ["full", "incr", "diff"]
 
-Epoch = { "hour" : datetime.timedelta(0, 3600), \
+Epoch = { \
+        "hour" : datetime.timedelta(0, 3600), \
         "day" : datetime.timedelta(1), \
         "week" : datetime.timedelta(7), \
         "month" : datetime.timedelta(30), \
@@ -43,7 +44,6 @@ class Backup:
 
 
 
-
 class Config:
     """Encapsules the configuration for the backup program."""
 
@@ -266,20 +266,24 @@ class BackupManager:
             print( tarp.stderr.read().decode() )
 
 
-    def backup(self):
-        """Make a new backup, if necessary"""
+    def backup(self, epoch=None, mode=None):
+        """Make a new backup, if necessary. If epoch is None then determine
+        desired epoch automatically. Use given epoch otherwise. If mode is None
+        then use mode for given epoch. Use given mode otherwise."""
 
         now = datetime.datetime.now()
         oldbackups = self.listOldBackups()
-        epoch = self.getDesiredEpoch(oldbackups, now)
 
+        # Get epoch of backup
+        if epoch == None:
+            epoch = self.getDesiredEpoch(oldbackups, now)
         if epoch == None:
             print("No backup planned.")
             return
 
-
         # Get mode of backup
-        mode = self.conf.epochmodes[epoch]
+        if mode == None:
+            mode = self.conf.epochmodes[epoch]
         print("Making a backup. Epoch: " + epoch + ", mode: " + mode)
 
         oldfullbackups = [ b for b in oldbackups if b.mode == "full" ]
@@ -361,16 +365,29 @@ def printUsage():
     print("shbackup - a simple backup solution.")
     print("")
     print("Usage:")
-    print("  " + sys.argv[0] + " [-C <configfile>")
+    print("  " + sys.argv[0] + " [-C <configfile>] [cmd]")
     print("  " + sys.argv[0] + " --help")
     print("")
+    print("Commands:")
+    print("  backup                 make a new backup, if necessary")
+    print("  list                   list all backups")
+    print("  prune                  prune outdated/old backups")
+    print("")
     print("Options:")
-    print("  -C <configfile>        default: /etc/shbackup.conf")
+    print("  -C <configfile>        use given configuration file")
+    print("                         default: /etc/shbackup.conf")
+    print("  -m, --mode <mode>      override mode: full, diff, or incr")
+    print("  -e, --epoch <epoch>    create backup for given epoch:")
+    print("                         year, month, week, day, hour")
+    print("  -h, --help             print this usage text")
 
 
 if __name__ == "__main__":
 
     conffn = "/etc/shbackup.conf"
+    cmd = "list"
+    mode = None
+    epoch = None
 
     i = 0
     while i < len(sys.argv)-1:
@@ -384,7 +401,24 @@ if __name__ == "__main__":
         elif opt in ["-C", "--config"]:
             i += 1
             conffn = sys.argv[i]
-            continue
+
+        elif opt in ["-m", "--mode"]:
+            i += 1
+            mode = sys.argv[i]
+            if not mode in Mode:
+                print("Unknown mode '" + mode + "'.")
+                exit(1)
+
+        elif opt in ["-e", "--epoch"]:
+            i += 1
+            epoch = sys.argv[i]
+            if not epoch in Epoch:
+                print("Unknown epoch '" + epoch + "'.")
+                exit(1)
+
+
+        elif opt in ["backup", "list", "prune"]:
+            cmd = opt
 
         else:
             print("Unknown option: " + opt)
@@ -392,8 +426,17 @@ if __name__ == "__main__":
 
     try:
         man = BackupManager(conffn)
-        man.backup()
-        man.prune()
+
+        if cmd == "backup":
+            man.backup(epoch, mode)
+
+        if cmd == "list":
+            for b in sorted(man.listOldBackups(), key=lambda b: b.date):
+                print(b.date.strftime("%Y-%m-%d %H:%M") + \
+                        "\t" + b.epoch + "\t" + b.mode)
+
+        if cmd == "prune":
+            man.prune()
 
     except Config.ReadException as e:
         print("Error reading config file: ", end="")