# coding=utf-8 import os import time # 1. 需要备份的文件与目录将被 # 指定在一个列表中。 # 例如在 Windows 下: source = ['"C:\\My Documents"', 'C:\\Code'] # 又例如在 Mac OS X 与 Linux 下: # source = ['/Users/swa/notes'] # 在这里要注意到我们必须在字符串中使用双引号 # 用以括起其中包含空格的名称。 # 2. 备份文件必须存储在一个 # 主备份目录中 # 例如在 Windows 下: target_dir = 'E:\\Backup' # 又例如在 Mac OS X 和 Linux 下: # target_dir = '/Users/swa/backup' # 要记得将这里的目录地址修改至你将使用的路径 # 如果目标目录还不存在, 则进行创建 if not os.path.exists(target_dir): os.mkdir(target_dir) # 创建目录 # 3. 备份文件将打包压缩成 zip 文件。 # 4. 将当前日期作为主备份目录下的 # 子目录名称 today = target_dir + os.sep + time.strftime('%Y%m%d') # 将当前时间作为 zip 文件的文件名 now = time.strftime('%H%M%S') # 添加一条来自用户的注释以创建 # zip 文件的文件名 comment = input('Enter a comment --> ') # 检查是否有评论键入 if len(comment) == 0: target = today + os.sep + now + '.zip' else: target = today + os.sep + now + '_' + \ comment.replace(' ', '_') + '.zip' # 如果子目录尚不存在则创建一个 if not os.path.exists(today): os.mkdir(today) print('Successfully created directory', today) # 5. 我们使用 zip 命令将文件打包成 zip 格式 zip_command = "zip -r {0} {1}".format(target, ' '.join(source)) # 运行备份 print('Zip command is:') print(zip_command) print('Running:') if os.system(zip_command) == 0: print('Successful backup to', target) else: print('Backup FAILED')
运行结果:E:\python_work\python36\venv\Scripts\python.exe E:/python_work/python36/backup/backup_ver3.py
Enter a comment --> ver3
Zip command is:
zip -r E:\Backup\20180612\133837_ver3.zip "C:\My Documents" C:\Code
Running:
adding: My Documents/ (224 bytes security) (stored 0%)
adding: My Documents/byte-of-python-chinese-edition.pdf (160 bytes security) (deflated 17%)
adding: Code/ (224 bytes security) (stored 0%)
adding: Code/新建文本文档.txt (160 bytes security) (stored 0%)
Successful backup to E:\Backup\20180612\133837_ver3.zip
Process finished with exit code 0