您的位置:首页 > 编程语言 > Python开发

python备份脚本

2011-09-18 20:18 204 查看
<bk1>引用os,time,sys模块,join()函数

#!/usr/bin/python
#2011/09/17 by larry
import time,os,sys
start_data=['/home/tech1/lianghq','/home/tech1/bak1/']
source_data=' '.join(start_data)

T=time.strftime('%F-%H%M')
bak_data='/tmp/liang/%s' % (T)

bak_command='tar czvf %s.tgz %s' % (bak_data,source_data)
print bak_command

if os.system(bak_command)==0:
print 'The bak data,is successfull',bak_data
else:
print "Backup failed."

<bk2>判断目录是否存在

#!/usr/bin/python
#2011/09/18 by larry
import time,os,sys

Tdir=time.strftime('%Y%m%d')
dir='/tmp/lianghq/%s' % (Tdir)
if not os.path.exists(dir):
os.mkdir(dir)
print "It's successful to created directory",dir

start_data=['/home/tech1/lianghq','/home/tech1/bak1/']
source_data=' '.join(start_data)
T=time.strftime('%F-%H%M')
bak_data='/tmp/lianghq/%s/%s' % (Tdir,T)
bak_command='tar czvf %s.tgz %s' % (bak_data,source_data)

if os.system(bak_command)==0:
print 'The bak data,is successfull',bak_data
else:
print "Backup failed."

<bk3>os.sep的应用

#!/usr/bin/python
import time,os,sys

dir1='/tmp/lianghq'
dir=dir1+os.sep+time.strftime('%Y%m%d')
if not os.path.exists(dir):
os.mkdir(dir)
print "It's successful to created directory",dir

start_data=['/home/tech1/lianghq','/home/tech1/bak1/']
source_data=' '.join(start_data)

bak_data=dir+os.sep+time.strftime('%Y%m%d-%H%M%S')
bak_command='tar czvf %s.tgz %s' % (bak_data,source_data)

if os.system(bak_command)==0:
print 'The bak data,is successfull',bak_data
else:
print "Backup failed."


本文出自 “Larry学习之路” 博客,请务必保留此出处http://5iqiong.blog.51cto.com/2999926/667898
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: