python3实现gitlab备份文件上传腾讯云COS

gitlab备份文件上传腾讯云COS

  • 脚本说明

    shell 复制代码
    脚本名称:upload.py
    假设gitlab备份文件目录:/opt/gitlab/backups
    gitlab备份文件格式:1706922037_2024_02_06_14.2.1_gitlab_backup.tar
    
    1.脚本需和gitlab备份文件同级目录
    2.根据备份文件中的日期判断是否上传,如今天的日期存在于备份文件名列表中,则上传今天备份文件,反之不上传。
    3.如需上传此目录下所有文件,则去掉日期判断逻辑即可
      upload_cos(file) # ===> 上传单个文件
      upload_cos(file_list) # ===> 上传目录下所有文件
  • 源码如下

    python3 复制代码
    # -*- 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, CosServiceError
    
    logging.basicConfig(level=logging.INFO, stream=sys.stdout)
    
    secret_id = 'SecretId'
    secret_key = 'SecretKey'
    region = 'ap-guangzhou'
    token = None
    
    config = 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,
                )
                break
            except 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_time
        print(f"上传耗时:{allCostTime}ms")
相关推荐
默_笙4 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
qq_426003964 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫4 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技4 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读4 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时4 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
奇思妙想聪明勤奋的小羊4 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1234 天前
2026年第38周GitHub趋势周报
python·科技·github
IZero074 天前
Jev 与 Laya
python·语言模型