#!/usr/bin/python
#Filename: backup.py
import os
import time
source_dir='/usr/local/'
source=['nagios','src']
target_dir='/home/zhangjin/'
today=target_dir+time.strftime('%Y%m%d')
now=time.strftime('%H%M%S')
comment=raw_input('Enter a comment-->')
if len(comment)==0:
target=today+os.sep+now+'.zip'
else:
target=today+os.sep+now+'_'+comment.replace(' ','_')+'.zip'
if not os.path.exists(today):
os.mkdir(today)
print 'Successfully created diretory',today
command='cd %s && tar cf %s %s' %(source_dir,target,' '.join(source))
if os.system(command)==0:
print 'Successful backup to',target
else:
print 'Backup failed!'
学习Python的第一个小程序。