Python/CMD 文件备份
1.使用Python压缩文件并另存
1 import zipfile, os 2 3 #备份文件ZIP格式: folder 目标文件夹 ; Targetfolder:另存地址 4 def backuptozip(folder, Targetfolder): 5 if Targetfolder=="": 6 Targetfolder=folder 7 folder = os.path.abspath(folder) 8 number = 1 9 while True: 10 zipFilename = os.path.basename(folder) + "_" + str(number) + '.zip' 11 newzip=Targetfolder+zipFilename 12 if not os.path.exists(newzip): 13 break 14 number = number + 1 15 16 print('creating...' + (newzip)) 17 backupzip = zipfile.ZipFile(newzip, 'w') 18 newfolder = os.path.basename(folder) 19 for foldername, subfolders, filenames in os.walk(folder): 20 print("adding file in ..." + foldername) 21 cc = len(folder) 22 cnewfolder = newfolder + foldername[cc:] 23 backupzip.write(foldername, cnewfolder) 24 for filename in filenames: 25 newBase = newfolder + '_' 26 if filename.startswith(newBase) and filename.endswith('.zip'): 27 continue 28 cnewfile = cnewfolder +"\\"+ filename 29 backupzip.write(os.path.join(foldername, filename), cnewfile) 30 backupzip.close() 31 print('Done.') 32 33 backuptozip("C:\\Users\\xxx\\OneDrive\\","")
2.使用CMD脚本 - robocopy 命令备份并存储日志
robocopy Z:\ E:\ShareBackup /z /is /e >> E:\ShareBackuplog\%date:~0,4%%date:~5,2%%date:~8,2%_log.txt
(更多robocopy 参数使用可参考 http://www.cnblogs.com/xinyuxin912/archive/2013/07/10/3181564.html)
3.通过window自带任务计划程序定时调度