首页 > 学院 > 开发设计 > 正文

文件备份脚本

2019-11-14 17:50:37
字体:
来源:转载
供稿:网友

要求安装:

1.Python:http://www.python.org/download/

2.7z:http://www.7-zip.org/


backup.py
# Filename: backup.py'''Backup files.    Version: V2, based on Python 3.3    Usage: backup.py -s:"dir1|dir2|..." -t:"target_dir" [-c:"comment"]        -s: The source directories.        -t: The target directory.        -c: Optional, any comment.    Examples:         backup.py -s:"c://src//F1|c://src//F2|c://src//F 3" -t:"c://backup"        backup.py -s:"c://src//F 3" -t:"c://backup" -c:"For sample"'''import osimport sysimport time# Read sys.argvPRint(sys.argv)if len(sys.argv) < 2:    print(__doc__)    sys.exit()source=[]target_dir=''comment=''for arg in sys.argv:    if arg.startswith('-s:'):        source=arg[3:].split('|')        print(source)    elif arg.startswith('-t:'):        target_dir=arg[3:]+os.sep        print(target_dir)    elif arg.startswith('-c:'):        comment=arg[3:]        print(comment)for i in range(0, len(source)):    source[i] = "/"" + source[i] + "/""    print(source[i])# Make the file name with the time and commenttoday=target_dir+time.strftime('%Y%m%d')now=time.strftime('%H%M%S')if len(comment)==0: # check if a comment was entered    target=today+os.sep+now+'.7z'else:    target=today+os.sep+now+'_'+/            comment.replace(' ','_')+'.7z'# Create the subdirectory by dayif not os.path.exists(today):    os.mkdir(today) # make directory    print('Successfully created directory',today)# zip commandzip_command="7z a %s %s" %(target,' '.join(source))print(zip_command)# Run the backupif os.system(zip_command)==0:    print('Successful backup to',target)else:    print('Backup FAILED')

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表