Python实现服务器备份指定目录且上传至oss
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2019-06-12 17:45:28
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
使用计划任务定时执行这个脚本,就可以备份指定目录,可以配置多个目录,使用前先安装oss2模块
import zipfile import os import oss2 import time import sys # 是否删除本地备份,如果上传oss开启后此项生效 isRemoveBak = False # 是否备份到oss isUploadOss = True # oss配置信息 AccessKeyID = 'L*********T' AccessKeySecret = 'aOLFLa*************AuXA1sKGKB' endpoint = 'oss-cn-*************.com' bucketName = 'xia**************ve' ossBakPath = 'webbak/' # 远程oss备份路径 bakpath = 'D:/webbak' # 本地备份路径 # 要备份的目录路径 baklist = ['D:/gitweb/zhaokeli.com', 'D:/gitweb/dflz.org'] # 要忽略的文件或目录 ignoreDirOrFile = ['.git', 'runtime', 'Data', 'aspnet_client'] def percentage(consumed_bytes, total_bytes): if total_bytes: sys.stdout.write('{0:.2f}% {1:.2f}/{2:.2f}M\r'.format(100 * (consumed_bytes / total_bytes), consumed_bytes / 1000000, total_bytes / 1000000)) sys.stdout.flush() def uploadoss(localfile, remotePath): if not os.path.exists(localfile): print('localfile is not exists!') return print("start uploadoss...") auth = oss2.Auth(AccessKeyID, AccessKeySecret) bucket = oss2.Bucket(auth, endpoint, bucketName, enable_crc=False) osspath = remotePath + os.path.basename(localfile) print("uploading to %s" % (osspath)) with open(localfile, 'rb') as fileobj: bucket.put_object(osspath, fileobj, progress_callback=percentage) print('\nuploadoss success!') # 递归目录 def dfs_get_zip_file(input_path, result, ignore=[['.git']]): files = os.listdir(input_path) for file in files: filePath = input_path + '/' + file if file in ignore: print("ignored: " + filePath) continue if os.path.isdir(filePath): dfs_get_zip_file(filePath, result, ignore) else: print("added: " + filePath) result.append(filePath) def zip_path(input_path, output_path, ignore=['.git']): outdir = os.path.dirname(output_path) if not os.path.isdir(outdir): os.makedirs(outdir) f = zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED) filelists = [] dfs_get_zip_file(input_path, filelists, ignore) print('start compress file...') for file in filelists: print('compress: ' + file) file = file.replace('\\', '/') input_path = input_path.replace('\\', '/') f.write(file, file.replace(input_path, '')) f.close() print('compress complete!') return output_path if __name__ == '__main__': for item in baklist: dirpath = item zippath = "%s/%s-%s.zip" % (bakpath, os.path.basename(item), time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime())) zip_path(dirpath, zippath, ignoreDirOrFile) if isUploadOss: uploadoss(zippath, ossBakPath) if isRemoveBak and isUploadOss: os.remove(zippath) # uploadoss('D:/webbak/zhaokeli.com-2019-06-12_16-31-24.zip', 'webbak/') input('...')