#!/usr/local/python251/bin/python
import sys
import os
import time
#如果沒有輸入參數,則會提示并且中斷。
if len(sys.argv[1:]) == 0:
print 'please input source file or directory.'
sys.exit()
else:
source = ' '.join(sys.argv[1:])#按照固定的格式組成一個字元串
target_dir = '/home/handaoliang/backup/'+time.strftime('%Y%m%d')#目标檔案夾
#如果目标檔案夾不存在,則進行建立,并且列印結果
if not os.path.exists(target_dir):
os.mkdir(target_dir)#create directory.
print target_dir,'is successfully created...'
#要求輸入注釋
comment = raw_input('please input a comment:')
#如果注釋為家,則預設檔案名為時分秒格式。
if len(comment) == 0:
target_file_name = target_dir+os.sep+time.strftime('%H%M%S')+'.tar.gz'
target_file_name = target_dir+os.sep+time.strftime('%H%M%S')+'_'+\
comment.replace(' ','_')+'.tar.gz'
#組建tar指令串
tar_command = 'tar czvf %s %s'%(target_file_name,source)
#通過os.system把此指令跑一遍,并根據傳回結果看是否備份成功。
if os.system(tar_command) == 0:
print 'sucessfully backup:',target_file_name
print 'backup FAILED'
本文轉自 ibelieveme 51CTO部落格,原文連結:http://blog.51cto.com/skyson/516020,如需轉載請自行聯系原作者