Backup 程序如果寫的不好,希望大家能夠教導我,,[Python]代码#!


[Python]代码

#!/usr/bin/python3.2muimport timeimport shutilimport fnmatch import tarfile import sysimport os import rebackupname = 'backup'+time.strftime('%Y-%m-%d-%H')def ignore_patterns(*patterns):     def _ignore_patterns(path, names):         ignore_names = [ ]        for pattern in patterns:             print(pattern)             ignore_names.extend(fnmatch.filter(names, pattern))        return set(ignore_names)    return _ignore_patternsdef TarFiles(dirfrom, dirto=None, args=None):    global tar    keyword = ['-n', '--not', 'gz', 'bz2', '-d', '--decompress', '-l', '--list']    if [s for s in args if s not in ['-d', '--decompress', '-l', '--list']] :        if '-n' in args or '--not' in args:            tar_ext = ''             tar_mod = ''         elif 'bz2' in args or 'gz' in args:            if 'bz2' in args: t_mod = 'bz2'    # because args is tuple so use if to judge            else: t_mod = 'gz'            tar_ext = '.' + t_mod            tar_mod = ':' + t_mod        else:             tar_ext = '.' + 'bz2'              tar_mod = ':' + 'bz2'        tar_name = '%s.tar%s' % (backupname, tar_ext)        path_to   = os.path.join(dirto, tar_name)        if not os.path.exists(path_to):            tar = tarfile.TarFile.open(path_to, 'w'+tar_mod)         names = os.listdir(dirfrom)        ig_names = [x for x in args if x not in keyword]         if ig_names:              print(ig_names)            ignore = ignore_patterns(*ig_names)            ignore_name = ignore(dirfrom, names)         else:             ignore_name = set( )         for name in names:             if name in ignore_name:                 continue             srcname = os.path.join(dirfrom, name)             dstname = os.path.join(dirto,   name)            if os.path.isdir(srcname):                 TarFiles(srcname, dirto, args)                 if not os.listdir(srcname):                     tar.add(srcname)             else:                 tar.add(srcname)     if '-d' in args or '--decompress' in args:         try:             de_tar = tarfile.TarFile.open(dirfrom, 'r')            names  = de_tar.getnames( )            for name in names:                de_tar.extract(name, dirto)                de_tar.close( )         except:            print(sys.exc_info()[0], '\n%s' % sys.exc_info()[1])    if '-l' in args or '--list' in args:        tar = tarfile.TarFile.open(dirfrom)         print(tar.list( ))        tar.close( ) def Backup(dirfrom, dirto, *args):    for oldbackup in os.listdir(dirto):         find = re.search(r'\bbackup\d+-\d+-\d+-\d+-', oldbackup)        if find:            rmpath = os.path.join(dirto, oldbackup)            if os.path.isdir(rmpath): os.remove(rmpath)             else: shutil.rmtree(rmpath)    path_to = os.path.join(dirto, backupname)     TarFiles(dirfrom, dirto, args)def AutoBackup(dirfrom, dirto, *args):    timer   = [x for x in args if '-t' and '--time' in x][0]      if timer:         timer   = timer[-5:]    flags   = 1    retimer = 0      while True:        if timer == time.strftime('%H:%M') and flags:            TarFiles(dirfrom, dirto, args)            if '-o' in args or '--one' in args: break               flags = 0             h = timer[:2]             if timer[3] == '0':                 t = int(timer[3]) + 1                 m = int(timer[4]) + 1                 m = str(m)                retimer = h + ':' + str(t) + m[-1]             else:                 m = int(timer[3:]) + 1                   if m == 60: m = '00'                 retimer = h + ':' + str(m)        if retimer == time.strftime('%H:%M'):             flags = 1         time.sleep(1)   if __name__ == '__main__':    Backup('/home/jack/python', '/home/jack/desktop', '*.py', '*.txt')     tar.close( )  '''    print(J-Backup 1.01. Compress Files/Directorys') 2. Backup directorys 3. Auto-backup  4. Helps 5. Exit )     def Main():         try:             print(Option)             opt = input('>')         except:             print(Error includes only intege option!)         if opt == 1:             TarFiles('''

评论关闭