From: Stefan Huber Date: Sun, 13 May 2012 19:43:26 +0000 (+0200) Subject: add many cmdline options X-Git-Tag: v1.0~16 X-Git-Url: https://git.sthu.org/?a=commitdiff_plain;h=5edbf71fccf18182670b8d991b8bbb3ee014b0c9;p=sitarba.git add many cmdline options --- diff --git a/shbackup.py b/shbackup similarity index 85% rename from shbackup.py rename to shbackup index e1f038d..0137b9d 100755 --- a/shbackup.py +++ 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 ") + print(" " + sys.argv[0] + " [-C ] [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 default: /etc/shbackup.conf") + print(" -C use given configuration file") + print(" default: /etc/shbackup.conf") + print(" -m, --mode override mode: full, diff, or incr") + print(" -e, --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="")