要求:
1.需要备份的文件和目录由一个列表指定。
2.备份应该保存在主备份目录中。
3.文件备份成一个zip文件。
4.zip存档的名称是当前的日期和时间。
初始实现版本:
1 # Filename:backup_v1.py
2
3 import os
4 import time
5
6 # 1.需要备份的源文件列表
7 # 问题:暂时无法解决源文件名中带有空格的情况(如C:\test 1目录)
8 # Linux
9 #source = ['/root/test1','/root/test2']
10 # Windows
11 #source = ['C:\\test1','C:\\test2']
12 source = [r'C:\test1',r'C:\test2']
13
14 # 2.备份的目标文件目录
15 # Linux
16 #target_dir = '/root/backup'
17 # Windows
18 target_dir = r'D:\backup'
19
20 # 3.压缩文件名字
21 target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'
22
23 # 4.zip命令
24 # Linux
25 #zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))
26 # Windows(压缩软件为7-Zip)
27 #zip_command = r"D:\Progra~1\7-Zip\7z.exe a {0} {1}".format(target, ' '.join(source))
28 zip_command = r"D:\Progra~1\7-Zip\7z.exe a %s %s" %(target, ' '.join(source))
29
30 # 5.运行备份命令
31 if os.system(zip_command) == 0:
32 print('Successful backup to', target)
33 else:
34 print('Backup FAILED')
改进版1:
更好的文件命名机制--当前日期作为子目录名,当前时间作为压缩文件名
优点1:备份以分层形式存储,便于管理
优点2:文件名变短
优点3:方便检查每日备份
1 # Filename:backup_v2.py
2
3 import os
4 import time
5
6 # 1.需要备份的源文件列表
7 # 问题:暂时无法解决源文件名中带有空格的情况(如C:\test 1目录)
8 # Linux
9 #source = ['/root/test1','/root/test2']
10 # Windows
11 #source = ['C:\\test1','C:\\test2']
12 source = [r'C:\test1',r'C:\test2']
13
14 # 2.备份的目标文件目录
15 # Linux
16 #target_dir = '/root/backup'
17 # Windows
18 target_dir = r'D:\backup'
19
20 # 3.当前日期作为在主备份目录下的子目录
21 today = target_dir + os.sep +time.strftime('%Y%m%d')
22
23 # 4.当前时间作为压缩文件名
24 now = time.strftime('%H%M%S')
25
26 # 5.创建子目录若其不存在
27 if not os.path.exists(today):
28 os.mkdir(today) # 创建目录
29 print('Successfully created directory', today)
30
31 # 6.压缩文件的完整名字
32 target = today + os.sep + now + '.zip'
33
34 # 7.zip命令
35 # Linux
36 #zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))
37 # Windows(压缩软件为7-Zip)
38 #zip_command = r"D:\Progra~1\7-Zip\7z.exe a {0} {1}".format(target, ' '.join(source))
39 zip_command = r"D:\Progra~1\7-Zip\7z.exe a %s %s" %(target, ' '.join(source))
40
41 # 8.运行备份命令
42 if os.system(zip_command) == 0:
43 print('Successful backup to', target)
44 else:
45 print('Backup FAILED')
改进版2:
在备份文件名中新增用户备注信息:
1 # Filename:backup_v3.py
2
3 import os
4 import time
5
6 # 1.需要备份的源文件列表
7 # 问题:暂时无法解决源文件名中带有空格的情况(如C:\test 1目录)
8 # Linux
9 #source = ['/root/test1','/root/test2']
10 # Windows
11 #source = ['C:\\test1','C:\\test2']
12 source = [r'C:\test1',r'C:\test2']
13
14 # 2.备份的目标文件目录
15 # Linux
16 #target_dir = '/root/backup'
17 # Windows
18 target_dir = r'D:\backup'
19
20 # 3.当前日期作为在主备份目录下的子目录
21 today = target_dir + os.sep +time.strftime('%Y%m%d')
22
23 # 4.当前时间作为压缩文件名
24 now = time.strftime('%H%M%S')
25
26 # 5.创建子目录若其不存在
27 if not os.path.exists(today):
28 os.mkdir(today) # 创建目录
29 print('Successfully created directory', today)
30
31 # 6.新增部分:备份文件完整名字+[用户备注信息]
32 comment = input('Enter a comment --> ')
33 if len(comment) == 0:
34 target = today + os.sep + now + '.zip'
35 else:
36 target = today + os.sep + now + '_' + comment.replace(' ', '_') + '.zip'
37
38 # 7.zip命令
39 # Linux
40 #zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))
41 # Windows(压缩软件为7-Zip)
42 #zip_command = r"D:\Progra~1\7-Zip\7z.exe a {0} {1}".format(target, ' '.join(source))
43 zip_command = r"D:\Progra~1\7-Zip\7z.exe a %s %s" %(target, ' '.join(source))
44
45 # 8.运行备份命令
46 if os.system(zip_command) == 0:
47 print('Successful backup to', target)
48 else:
49 print('Backup FAILED')
改进版2对大多数用户来说,已经是一个令人满意的可运行脚本了。
还可以使用的压缩命令:
tar = 'tar -cvzf %s %s -X /home/swaroop/excludes.txt' % (target, ' '.join(srcdir))
选项解释如下:
-c
表示创建一个归档。-v
表示交互,即命令更具交互性。-z
表示使用gzip滤波器。-f
表示强迫创建归档,即如果已经有一个同名文件,它会被替换。-X
表示含在指定文件名列表中的文件会被排除在备份之外。例如,你可以在文件中指定*~
,从而不让备份包括所有以~
结尾的文件。
注意:
最重要的改进应该是不要使用os.system的方式创建压缩文档,取而代之应该使用zipfile或tarfile内建模块去创建这些压缩文档。它们是标准库的一部分,可供你使用,且不需要外部依赖于zip程序在你的计算机上可用。
zipfile和tarfile模块信息见:http://docs.python.org/3/library/index.html