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), \
-
class Config:
"""Encapsules the configuration for the backup program."""
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" ]
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:
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)
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="")