Backup 程序如果寫的不好,希望大家能夠教導我,,[Python]代码#!
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('''
相关内容
- 哥&寂寞,,[Python]代码fr
- 一个简单的类封装,提供简单的web操作,类封装web操作
- 使用gevent多线程下载豆瓣音乐,gevent豆瓣,使用gevent多线
- 找出一组数据的峰值,找出一组数据峰值,#-*- coding=
- Python清理文件及文件夹,python清理文件夹,#!c:\\python
- 挑选文件夹里面宽大于300图片,挑选文件夹300,唉,老是
- 把金额小写转换成大写的Python代码,小写python,def IIf(
- 用Python解压缩文件,Python解压缩文件,#rardir.pyim
- 使用python进行adsl拨号,pythonadsl拨号,[Python]代码im
- 统计文件中每行的字符数、总行数、及总共的字符数,
评论关闭