From: Stefan Huber Date: Sun, 13 May 2012 17:16:42 +0000 (+0200) Subject: restructuring code X-Git-Tag: v1.0~17 X-Git-Url: https://git.sthu.org/?p=sitarba.git;a=commitdiff_plain;h=212160362993410e7ee742a7f6546ef3d6d171a3 restructuring code --- diff --git a/shbackup.py b/shbackup.py index f9f7007..e1f038d 100755 --- a/shbackup.py +++ b/shbackup.py @@ -9,6 +9,41 @@ import subprocess import random, re +Mode = ["full", "incr", "diff"] + +Epoch = { "hour" : datetime.timedelta(0, 3600), \ + "day" : datetime.timedelta(1), \ + "week" : datetime.timedelta(7), \ + "month" : datetime.timedelta(30), \ + "year" : datetime.timedelta(365) } + +class Backup: + """A single backup has a date, an epoch and a mode.""" + + def __init__(self, date, epoch, mode): + self.date = date + self.epoch = epoch + self.mode = mode + + def __str__(self): + return "[date: " + self.date.ctime() + \ + ", epoch: " + self.epoch + \ + ", mode: " + self.mode + "]" + + @staticmethod + def getDirName(date, epoch, mode): + """Get directory name of backup by given properties.""" + return date.strftime("%Y%m%d-%H%M") + "-" + epoch + "-" + mode + + @staticmethod + def isBackupDir(dirname): + """Is directory a backup directory?""" + p = re.compile(r'^\d\d\d\d\d\d\d\d-\d\d\d\d-\w+-\w+$') + return p.match(dirname) + + + + class Config: """Encapsules the configuration for the backup program.""" @@ -125,39 +160,6 @@ class Config: self.lastchecksum = None -Mode = ["full", "incr", "diff"] - -Epoch = { "hour" : datetime.timedelta(0, 3600), \ - "day" : datetime.timedelta(1), \ - "week" : datetime.timedelta(7), \ - "month" : datetime.timedelta(30), \ - "year" : datetime.timedelta(365) } - -class Backup: - """A single backup has a date, an epoch and a mode.""" - - def __init__(self, date, epoch, mode): - self.date = date - self.epoch = epoch - self.mode = mode - - def __str__(self): - return "[date: " + self.date.ctime() + \ - ", epoch: " + self.epoch + \ - ", mode: " + self.mode + "]" - - @staticmethod - def getDirName(date, epoch, mode): - """Get directory name of backup by given properties.""" - return date.strftime("%Y%m%d-%H%M") + "-" + epoch + "-" + mode - - @staticmethod - def isBackupDir(dirname): - """Is directory a backup directory?""" - p = re.compile(r'^\d\d\d\d\d\d\d\d-\d\d\d\d-\w+-\w+$') - return p.match(dirname) - - class BackupManager: """List and create backups""" @@ -280,7 +282,7 @@ class BackupManager: mode = self.conf.epochmodes[epoch] print("Making a backup. Epoch: " + epoch + ", mode: " + mode) - oldfullbackups = [ b for b in oldbackups if b.mode=="full" ] + oldfullbackups = [ b for b in oldbackups if b.mode == "full" ] # No old full backups existing if mode != "full" and len(oldfullbackups)==0: @@ -370,7 +372,7 @@ if __name__ == "__main__": conffn = "/etc/shbackup.conf" - i=0 + i = 0 while i < len(sys.argv)-1: i += 1 opt = sys.argv[i]