您的位置:首页 > 编程语言 > Python开发

byte_of_python中的备份脚本

2013-04-17 00:18 441 查看
要求:

1.需要备份的文件和目录由一个列表指定。

2.备份应该保存在主备份目录中。

3.文件备份成一个zip文件。

4.zip存档的名称是当前的日期和时间。

初始实现版本:

# Filename:backup_v1.py

import os
import time

# 1.需要备份的源文件列表
# 问题:暂时无法解决源文件名中带有空格的情况(如C:\test 1目录)
# Linux
#source = ['/root/test1','/root/test2']
# Windows
#source = ['C:\\test1','C:\\test2']
source = [r'C:\test1',r'C:\test2']

# 2.备份的目标文件目录
# Linux
#target_dir = '/root/backup'
# Windows
target_dir = r'D:\backup'

# 3.压缩文件名字
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'

# 4.zip命令
# Linux
#zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))
# Windows(压缩软件为7-Zip)
#zip_command = r"D:\Progra~1\7-Zip\7z.exe a {0} {1}".format(target, ' '.join(source))
zip_command = r"D:\Progra~1\7-Zip\7z.exe a %s %s" %(target, ' '.join(source))

# 5.运行备份命令
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup FAILED')


改进版1:

更好的文件命名机制--当前日期作为子目录名,当前时间作为压缩文件名

优点1:备份以分层形式存储,便于管理

优点2:文件名变短

优点3:方便检查每日备份

# Filename:backup_v2.py

import os
import time

# 1.需要备份的源文件列表
# 问题:暂时无法解决源文件名中带有空格的情况(如C:\test 1目录)
# Linux
#source = ['/root/test1','/root/test2']
# Windows
#source = ['C:\\test1','C:\\test2']
source = [r'C:\test1',r'C:\test2']

# 2.备份的目标文件目录
# Linux
#target_dir = '/root/backup'
# Windows
target_dir = r'D:\backup'

# 3.当前日期作为在主备份目录下的子目录
today = target_dir + os.sep +time.strftime('%Y%m%d')

# 4.当前时间作为压缩文件名
now = time.strftime('%H%M%S')

# 5.创建子目录若其不存在
if not os.path.exists(today):
os.mkdir(today) # 创建目录
print('Successfully created directory', today)

# 6.压缩文件的完整名字
target = today + os.sep + now + '.zip'

# 7.zip命令
# Linux
#zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))
# Windows(压缩软件为7-Zip)
#zip_command = r"D:\Progra~1\7-Zip\7z.exe a {0} {1}".format(target, ' '.join(source))
zip_command = r"D:\Progra~1\7-Zip\7z.exe a %s %s" %(target, ' '.join(source))

# 8.运行备份命令
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup FAILED')


改进版2:

在备份文件名中新增用户备注信息:

# Filename:backup_v3.py

import os
import time

# 1.需要备份的源文件列表
# 问题:暂时无法解决源文件名中带有空格的情况(如C:\test 1目录)
# Linux
#source = ['/root/test1','/root/test2']
# Windows
#source = ['C:\\test1','C:\\test2']
source = [r'C:\test1',r'C:\test2']

# 2.备份的目标文件目录
# Linux
#target_dir = '/root/backup'
# Windows
target_dir = r'D:\backup'

# 3.当前日期作为在主备份目录下的子目录
today = target_dir + os.sep +time.strftime('%Y%m%d')

# 4.当前时间作为压缩文件名
now = time.strftime('%H%M%S')

# 5.创建子目录若其不存在
if not os.path.exists(today):
os.mkdir(today) # 创建目录
print('Successfully created directory', today)

# 6.新增部分:备份文件完整名字+[用户备注信息]
comment = input('Enter a comment --> ')
if len(comment) == 0:
target = today + os.sep + now + '.zip'
else:
target = today + os.sep + now + '_' + comment.replace(' ', '_') + '.zip'

# 7.zip命令
# Linux
#zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))
# Windows(压缩软件为7-Zip)
#zip_command = r"D:\Progra~1\7-Zip\7z.exe a {0} {1}".format(target, ' '.join(source))
zip_command = r"D:\Progra~1\7-Zip\7z.exe a %s %s" %(target, ' '.join(source))

# 8.运行备份命令
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: