现在的位置: 首页 > 综合 > 正文

Python 简单数据备份脚本

2018年09月21日 ⁄ 综合 ⁄ 共 680字 ⁄ 字号 评论关闭

手动备份 version 1

#!/usr/bin/python
#Filename: backup_v1.py

import os
import time

source = ['/root/slp/NetWrokProgram/client','/root/slp/NetWrokProgram/server']

dist = '/root/slp/NetWrokProgram/backup'

now = time.strftime("%Y%m%d")

today_dir = dist + "/" + now 

if not os.path.exists(today_dir):
    if 0 == os.mkdir(today_dir):
        print 'mkdir %s success' % today_dir
    else:
        print 'mkdir %s failed' % today_dir

comment = raw_input("Please enter a comment:")
if len(comment) == 0:
    target = today_dir + os.sep + time.strftime("%H%M%S")+'.tar.gz'else:
    target = today_dir + os.sep  + time.strftime("%H%M%S")+comment.replace(' ','_')+'.tar.gz'

tar_cmd = "tar -czvf %s %s" % (target,' '.join(source))

if os.system(tar_cmd) == 0:
    print 'success backup to',target
else:
    print 'backup failed'

抱歉!评论已关闭.