byte_of_python中的备份脚本
2013-04-17 00:18
441 查看
要求:
1.需要备份的文件和目录由一个列表指定。
2.备份应该保存在主备份目录中。
3.文件备份成一个zip文件。
4.zip存档的名称是当前的日期和时间。
初始实现版本:
改进版1:
更好的文件命名机制--当前日期作为子目录名,当前时间作为压缩文件名
优点1:备份以分层形式存储,便于管理
优点2:文件名变短
优点3:方便检查每日备份
改进版2:
在备份文件名中新增用户备注信息:
改进版2对大多数用户来说,已经是一个令人满意的可运行脚本了。
还可以使用的压缩命令:
选项解释如下:
注意:
最重要的改进应该是不要使用os.system的方式创建压缩文档,取而代之应该使用zipfile或tarfile内建模块去创建这些压缩文档。它们是标准库的一部分,可供你使用,且不需要外部依赖于zip程序在你的计算机上可用。
zipfile和tarfile模块信息见:http://docs.python.org/3/library/index.html
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
相关文章推荐
- A Byte of Python习题:用于备份文件的Python脚本
- 《a byte of python》学习笔记:windows下实现备份的python脚本
- A BYTE OF PYTHON 文件备份的WINRAR版本
- 【A ByteOf Python2.7】第10章 解决问题——编写一个Python脚本
- <<A Byte Of Python>>第10章的例子——备份文件和目录
- python备份脚本
- A Byte of Python 笔记(10)输入/输出:文件和储存器
- Python入门——A Byte of Python
- Linux自动定时备份文件夹Python脚本
- 《A Byte of Python》 notes
- python入门之初体验-----A Byte of Python3(观后感)
- python构建一个简单的备份脚本
- 简明Python3教程(A Byte of Python 3)
- 用python编写脚本实现备份文件
- 使用python在windows 下 编写 自动备份脚本
- 简明 Python 教程-win10下备份脚本
- 编写第一个python 脚本(重要文件创建备份)
- 使用python脚本备份crontab定时任务
- python 备份mysql脚本
- Openstack 数据库自动化备份python脚本