Python备份文件目录脚本

1.备份到某一目录,以当前时间命名:

#!/usr/bin/python
import os
import time
source = ['/home/utrace/python/training/','/home/utrace/python/Day1/']
dest = '/home/utrace/python/backup/'
backup_format = dest + time.strftime('%Y%m%d-%H%M%S') + '.zip'
zip_command = "zip -qr %s %s" % (backup_format, ' '.join(source))
if os.system(zip_command) == 0:
        print "It is successfully backup!"
else:
        print "backup failed"

2.备份到当前日期的目录,以时间命名:

#!/usr/bin/python
import os
import time
source = ['/home/utrace/python/training/','/home/utrace/python/Day1/']
dest = '/home/utrace/python/backup/'
backup_date = dest + time.strftime('%Y%m%d')
backup_now = time.strftime('%H%M%S')
if not os.path.exists(backup_date):
        os.mkdir(backup_date)
        print "Successfully created directory!"
backup_format = backup_date +os.sep + backup_now + '.zip'
zip_command = "zip -qr %s %s" % (backup_format, ' '.join(source))
if os.system(zip_command) == 0:
        print "It is successfully backup!"
else:
        print "backup failed"


你可能感兴趣的:(Python备份文件目录脚本)