import os import time
#要备份的文件和目录 source = ['/www/flask','/www/danmu-demo']
target_dir = '/back'
if not os.path.exists(target_dir): print("正在创建备份目录:{}".format(target_dir)) os.mkdir(target_dir)
target = target_dir + '/' + time.strftime('%Y%m%d%H%M%s') + '.zip'
zip_command = "zip -qr '%s' %s" %(target,' '.join(source))
if os.system(zip_command) == 0: print("Successful backup to",target) else: print('Backup Failed')
print(target) print(zip_command)
root@alex-virtual-machine:/www# python back.py 正在创建备份目录:/back Successful backup to /back/2018101703071539760032.zip /back/2018101703071539760032.zip zip -qr '/back/2018101703071539760032.zip' /www/flask /www/danmu-demo