[iyunv@reed 0503]# cat backup_ver3.py
#!/usr/bin/python
#filename:backup_ver3.py
import os
import time
#source
source=['/root/a.sh','/root/b.sh','/root/c.sh']
#source='/root/c.sh'
#backup dir
target_dir='/tmp/'
today=target_dir+time.strftime('%Y%m%d')
now=time.strftime('%H%M%S')
comment=raw_input('enter commonet-->')
if len(comment)==0:
target=today+os.sep+now+'.zip'
else:
target=today+os.sep+now+'_'+comment.replace(' ','_')+'.zip'
#target name
if not os.path.exists(today):
os.mkdir(today)
print 'successful mkdir',today
#command
for i in source:
zip_command="zip -qr '%s' %s"%(target,i)
#run
if os.system(zip_command)==0:
print 'successful backup to',target
else:
print 'backup failed'
[iyunv@reed 0503]# ./backup_ver3.py
enter commonet-->hello reed by iyunv
successful backup to /tmp/20140503/191316_hello_reed_by_iyunv.zip
获取日期和时间函数:os.strftime
获取路径分隔符:os.sep