python教程在例10.4 备份脚本的代码的介绍

开发 后端
以下文章主要是通过代码的相关介绍的方式来引出python教程的实际应用方案的相关介绍,以下是文章的详细内容,望你会有所收获。

python教程在例10.4 备份脚本——版本四的代码是相当长的,所以很多人对其有束手无策之感,但是你通过以下的代码对其,有更好的了解,以下就是相关内容的详细介绍。希望你会有所收获。

#!/usr/bin/python  
# Filename: backup_ver4.py  
import os  
import time  
# 1. The files and directories to be backed up are 
specified in a list.  
source = [‘/home/swaroop/byte’, ‘/home/swaroop/bin’]   # If you are using Windows, use source =
 [r'C:\Documents'
, r'D:\Work'] or something like that  
# 2. The backup must be stored in a main backup
 directory  
target_dir = ‘/mnt/e/backup/’ # Remember to change 
this to what you will be using  
# 3. The files are backed up into a zip file.   # 4. The current day is the name of the 
subdirectory in the main directory  
today = target_dir + time.strftime(‘%Y%m%d’)   # The current time is the name of the zip archive   now = time.strftime(‘%H%M%S’)   # Take a comment from the user to create the name 
of the zip file  
comment = raw_input(‘Enter a comment –> ‘)   if len(comment) == 0: # check if a comment was entered   target = today + os.sep + now + ‘.zip’   else:   target = today + os.sep + now + ‘_’ + \   comment.replace(‘ ‘, ‘_’) + ‘.zip’   # Notice the backslash!   # Create the subdirectory if it isn’t already there   if not os.path.exists(today):   os.mkdir(today) # make directory   print ‘Successfully created directory’, today   # 5. We use the zip command (in Unix/Linux) to put the 
files in a zip archive  
zip_command = "zip -qr ‘%s’ %s" % (target, ‘ 
‘.join(source))  
# Run the backup   if os.system(zip_command) == 0:   print ‘Successful backup to’, target   else:   print ‘Backup FAILED’ 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.

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

tar = ‘tar -cvzf %s %s -X /home/swaroop/
excludes.txt’ % (target, ‘ ‘.join(srcdir)) 
  • 1.
  • 2.

 

需要补充的是Windows用户安装完LibArchive后命令为bsdtar,而且若想使用-z这个选项,还需安装gzip软件。代码中的

target = today + os.sep + now + ‘.zip’ 
  • 1.

改为

target = today + os.sep + now + ‘.tar.gz’ 
  • 1.

使用

bsdtar -cvzf %s %s -X /home/swaroop/
excludes.txt’ % (target, ‘ ‘.join(source)) 
  • 1.
  • 2.

以上的内容就是对简明python教程在例10.4 备份脚本——版本四的代码中有些需要注意的地方的详细介绍。

【编辑推荐】

  1. Python字符串与转义序列的相关内容的介绍
  2. python随机数生成的代码的详细解析
  3. Python字符和字符串的相关代码示例解析
  4. Python对象以C语言实现其相关功能的介绍
  5. Python VIM 注释掉代码的具体方案的描述
责任编辑:佚名 来源: 博客园
相关推荐

2010-03-25 17:53:39

Python教程

2010-03-10 13:42:44

Python脚本

2010-05-31 10:55:08

Nagios mysq

2010-04-08 09:27:44

Oracle 安装脚本

2010-03-26 16:35:29

Python open

2010-03-19 14:59:00

python Stri

2010-03-25 12:33:14

Python脚本文件

2010-03-17 17:06:10

python脚本语言

2010-03-25 10:13:03

Python代码

2010-03-19 09:26:52

Python编程

2023-10-25 18:18:10

Python脚本代码

2010-03-26 15:28:05

Python编写

2010-02-02 16:40:34

Python 脚本

2011-08-09 09:54:02

Exchange 20备份

2021-07-07 05:53:23

PythonPython 语法加密源代码

2010-03-26 15:14:26

Python编写

2010-03-30 11:29:24

Oracle备份

2010-02-23 10:32:20

Python 脚本

2010-09-10 13:25:22

2011-05-16 09:32:33

mysql数据库备份
点赞
收藏

51CTO技术栈公众号