一个简单的python备份脚本

自娱自乐而已
#Filename : backup.py
import os
import time
#The files and directories to be backed up
source = ['/home/huayd/programs']
#The backup you will put
target_dir = '/mnt/back/'
#The files are backed up intot a zip file
#The current day is the name of the subdirectory int the main directory
today = target_dir + time.strftime('%Y%m%d')
#The current time is the name of the zip archive
now = time.strftime('%H%M%S')
#Create the subdirectory if it isn't exist
if not os.path.exists(today):
        os.mkdir(today)
        print 'Successfully created directory',today
#The name of the zip file
target = today + os.sep + now + '.zip'
#Use the zip command to put the files int a zip archive
zip_command = 'zip -qr %s %s' % (target,''.join(source))
#Run the backup
if os.system(zip_command) == 0:
        print 'Successful backup to',target
else:
        print 'Backup failed!'