[简明python教程]学习笔记之编写简单备份脚本

[root@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'
[root@reed 0503]# ./backup_ver3.py
enter commonet-->hello reed by 51cto
successful backup to /tmp/20140503/191316_hello_reed_by_51cto.zip

获取日期和时间函数:os.strftime

获取路径分隔符:os.sep

刚学,脚本逻辑好像有点问题。。。。

本文出自 “[reed@卢伟开~]#rm -rf /” 博客,谢绝转载!

你可能感兴趣的:(python)