Chinaunix首页 | 论坛 | 博客
  • 博客访问: 165446
  • 博文数量: 37
  • 博客积分: 1132
  • 博客等级: 准尉
  • 技术积分: 380
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-18 16:56
个人简介

吆嘻!

文章分类

全部博文(37)

文章存档

2013年(1)

2012年(19)

2011年(17)

分类: Python/Ruby

2012-05-07 10:53:44

我提出的问题是: 我想要一个可以为我的所有重要文件创建备份的程序。

尽管这是一个简单的问题,但是问题本身并没有给我们足够的信息来解决它。进一步的分析是必需的。例如,我们如何确定该备份哪些文件?备份保存在哪里?我们怎么样存储备份?

在恰当地分析了这个问题之后,我们开始设计我们的程序。我们列了一张表,表示我们的程序应该如何工作。对于这个问题,我已经创建了下面这个列表以说明 我 如何让它工作。如果是你设计的话,你可能不会这样来解决问题——每个人都有其做事的方法,这很正常。

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

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

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

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

  5. 我们使用标准的zip命令,它通常默认地随Linux/Unix发行版提供。Windows用户可以使用Info-Zip程序。注意你可以使用任何地存档命令,只要它有命令行界面就可以了,那样的话我们可以从我们的脚本中传递参数给它。 


 

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. # Filename: backup_ver4.py

  3. import os
  4. import time

  5. # 1. The files and directories to be backed up are specified in a list.
  6. source = ['/home/swaroop/byte', '/home/swaroop/bin']
  7. # If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or something like that

  8. # 2. The backup must be stored in a main backup directory
  9. target_dir = '/mnt/e/backup/' # Remember to change this to what you will be using

  10. # 3. The files are backed up into a zip file.
  11. # 4. The current day is the name of the subdirectory in the main directory
  12. today = target_dir + time.strftime('%Y%m%d')
  13. # The current time is the name of the zip archive
  14. now = time.strftime('%H%M%S')

  15. # Take a comment from the user to create the name of the zip file
  16. comment = raw_input('Enter a comment --> ')
  17. if len(comment) == 0: # check if a comment was entered
  18.     target = today + os.sep + now + '.zip'
  19. else:
  20.     target = today + os.sep + now + '_' + \
  21.         comment.replace(' ', '_') + '.zip'
  22.     # Notice the

  23. # Create the subdirectory if it isn't already there
  24. if not os.path.exists(today):
  25.     os.mkdir(today) # make directory
  26.     print 'Successfully created directory', today

  27. # 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
  28. zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))

  29. # Run the backup
  30. if os.system(zip_command) == 0:
  31.     print 'Successful backup to', target
  32. else:
  33.     print 'Backup FAILED'


输出:


 

点击(此处)折叠或打开

  1. $ python backup_ver4.py
  2. Enter a comment --> added new examples
  3. Successful backup to /mnt/e/backup/20041208/082156_added_new_examples.zip

  4. $ python backup_ver4.py
  5. Enter a comment -->
  6. Successful backup to /mnt/e/backup/20041208/082316.zip


 

对于大多数用户来说,第四个版本是一个满意的工作脚本了,但是它仍然有进一步改进的空间。比如,你可以在程序中包含 交互 程度——你可以用-v选项来使你的程序更具交互性。

另一个可能的改进是使文件和目录能够通过命令行直接传递给脚本。我们可以通过sys.argv列表来获取它们,然后我们可以使用list类提供的extend方法把它们加到source列表中去。

我还希望有的一个优化是使用tar命令替代zip命令。这样做的一个优势是在你结合使用targzip命令的时候,备份会更快更小。如果你想要在Windows中使用这些归档,WinZip也能方便地处理这些.tar.gz文件。tar命令在大多数Linux/Unix系统中都是默认可用的。Windows用户也可以下载安装它。

命令字符串现在将称为:

tar = 'tar -cvzf %s %s -X /home/swaroop/excludes.txt' % (target, ' '.join(srcdir))

选项解释如下:

  • -c表示创建一个归档。

  • -v表示交互,即命令更具交互性。

  • -z表示使用gzip滤波器。

  • -f表示强迫创建归档,即如果已经有一个同名文件,它会被替换。

  • -X表示含在指定文件名列表中的文件会被排除在备份之外。例如,你可以在文件中指定*~,从而不让备份包括所有以~结尾的文件。

重要
最理想的创建这些归档的方法是分别使用zipfile和tarfile。它们是Python标准库的一部分,可以供你使用。使用这些库就避免了使用os.system这个不推荐使用的函数,它容易引发严重的错误。
然而,我在本节中使用os.system的方法来创建备份,这纯粹是为了教学的需要。这样的话,例子就可以简单到让每个人都能够理解,同时也已经足够用了。

简明Python 教程:

阅读(1025) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~