您的位置:首页 > 运维架构 > Linux

Python实现linux下文件备份

2014-02-27 10:03 99 查看
#!/usr/bin/python
#Filename: backup_ver2.py

import os
import time

source = ['/home/user/public_html', '/home/angelo/Pictures']

target_dir = '/home/backup/'

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 directory', today

zip_command = "zip -qr '%s' %s" %(target, ' '.join(source))

if os.system(zip_command) == 0:
print 'Successful backup to', target
else:
print 'Backup Failed!'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: