主页 > Python > Python学习之备份脚本

Python学习之备份脚本

2012 年 08 月 08 日 没有评论

功能是:可以为指定的目录、文件创建备份的程序

'''
Created on 2012-8-8
#!/usr/bin/python
@author: fbbin
@desc bakup
'''

# 1. import some modules
import os,sys,time

# 2. The files and directories to be backed up are specified in a list.
source = ['/home/www','/home/bin','/home/info']
targetDir = '/home/python/'

# The current day is the name of the subdirectory in the main directory
todayPath = targetDir + time.strftime('%Y%m%d')
# The current time is the name of the tarball archive
now = time.strftime('%H%M%S')

if len(sys.argv) >= 2:
	if sys.argv[1] == '-v':
		print 'version:1.0.1'
	elif sys.argv[1] == '-d':
		# check this day path is exists
		if not os.path.exists(todayPath):
			os.mkdir(todayPath)
			print 'Successfully created directory', todayPath

		# Take a comment from the user to create the name of the tarball file
		comment = raw_input('Enter a comment --> ')

		if len(comment) == 0:
			target = todayPath + os.sep + now + '.tar.gz'
		else:
			target = todayPath + os.sep + now + '_' + comment.replace(' ', '_') + '.tar.gz'

		commond = "tar -cvzf %s %s " % (target, ' '.join(source))

		#exec the linux commond
		if os.system(commond) == 0:
			print 'Successful backup to %s' % target
		else:
			print 'Backup Failed'
	else:
		print 'Nothing Done'
else:
	print '''
		This program prints files to the standard output.
		Any number of files can be specified.
		Options include:
		--v : Prints the version number
		--d : go the bakup'''
	sys.exit()

发表评论

电子邮件地址不会被公开。 必填项已用*标注


*

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>