刚看了简明python教程的备份脚本,手痒了,自己也试着写了一个,呵呵。
#!/usr/bin/python
print ‘version: 0.1’
print ‘author : netcat’
import os
import time
define source
print ‘输入要备份的文件或目录’
print ‘注:要写绝对路径,多个之间以空格分开’
print ‘example: /home/haha /home/hehe’
source=raw_input(‘enter sources:’)
define backuppath
print ‘输入备份路径’
print ‘example: /root/backup’
backuppath=raw_input(‘enter backup:’)
if not os.path.exists(backuppath):
os.mkdir(backuppath)
define time
today=backuppath+os.sep+time.strftime(‘%Y%m%d’)
if not os.path.exists(today):
os.mkdir(today)
now=time.strftime(‘%H%M%S’)
define target filename
comment=raw_input(‘enter comment:’)
if len(comment)!=0:
target=today+os.sep+comment.replace(‘ ‘,’‘)+’‘+now+’.tgz’
else:
target=today+os.sep+now+’.tgz’
#define command
command=’tar zcf %s %s’%(target,source)
#run
if os.system(command)==0:
print ‘successful to backup’,target
else:
print ‘failed to backup’