本文主要是介绍python3实现gitlab备份文件上传腾讯云COS,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
gitlab备份文件上传腾讯云COS
- 脚本说明
脚本名称:upload.py 假设gitlab备份文件目录:/opt/gitlab/backups gitlab备份文件格式:1706922037_2024_02_06_14.2.1_gitlab_backup.tar1.脚本需和gitlab备份文件同级目录 2.根据备份文件中的日期判断是否上传,如今天的日期存在于备份文件名列表中,则上传今天备份文件,反之不上传。 3.如需上传此目录下所有文件,则去掉日期判断逻辑即可upload_cos(file) # ===> 上传单个文件upload_cos(file_list) # ===> 上传目录下所有文件
- 源码如下
# -*- coding: utf-8 -*- # /usr/bin/env python3 # file_name : upload.py # 依赖安装:pip3 install -U cos-python-sdk-v5 import os import sys import time import datetime import logging from qcloud_cos import CosConfig from qcloud_cos import CosS3Client from qcloud_cos.cos_exception import CosClientError, CosServiceErrorlogging.basicConfig(level=logging.INFO, stream=sys.stdout)secret_id = 'SecretId' secret_key = 'SecretKey' region = 'ap-guangzhou' token = Noneconfig = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) client = CosS3Client(config)def upload_cos(file):current = os.getcwd()# 线程上传for i in range(0, 10):try:client.upload_file(Bucket='backup-1145114',Key=f'gitlab/{file}',LocalFilePath=current + '/' + file,)breakexcept CosClientError or CosServiceError as e:print(e)def get_files():# 切换到gitlab备份目录os.chdir('/opt/gitlab/backups')current_dir = os.getcwd()file_list = os.listdir(current_dir)# 脚本文件排除上传if 'upload.py' in file_list:file_list.remove('upload.py')# print(file_list)current_day = datetime.datetime.now().strftime("%Y_%m_%d")for file in file_list:if current_day in file:print('file exist ===>', file)# window下用 \\ ,linux下用 /print(current_dir + "/" + file)upload_cos(file)if __name__ == '__main__':start_time = int(time.time() * 1000)get_files()end_Time = int(time.time() * 1000)allCostTime = end_Time - start_timeprint(f"上传耗时:{allCostTime}ms")
这篇关于python3实现gitlab备份文件上传腾讯云COS的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!