`

python 入门(3)基本语法综合应用值linux文件系统备份

阅读更多
#!/usr/bin/python
#coding=utf-8

#备份系统目录,删除历史备份包
#备份时 修改 source 的目录结构


import os,sys,time
source = ['/boot','/etc']


date = time.strftime("%Y%m%d")

b_dir = "/root/back"

b_name = b_dir+'/'+date+'.tar.gz'

if(os.path.exists(b_name)):
	chooice = raw_input("存在路径 %s\n请选择1:覆盖,2 重命名\n" %b_name)
	if(chooice == '1'):
		rm_cmd = 'rm -rf %s' %(b_name)
		if(os.system(rm_cmd) != 0):
			print "删除命令为正确执行,请检查,已退出,命令:\n%s" %rm_cmd
			sys.exit()
		print '删除命令执行成功,命令:\n %s' %rm_cmd
	elif chooice=='2':
		hms = time.strftime("%H%M%S")
		b_name = b_dir+'/'+date+'-'+hms+'.tar.gz'
		print '重命名文件 %s' %b_name
	else:
		print "选择错误 请重试!"
		sys.exit()


tar_cmd="tar -czvf %s %s" %(b_name,' '.join(source) )


print  '待执行命令 %s'  %tar_cmd

if(raw_input("请选择  1 继续执行,0 退出:\n") == '1' ):
	if os.system(tar_cmd) ==0:
		print '''\033[32m 执行成功\033[0m'''
else:
	print("命令未执行")

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics