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")
相关推荐
nbsaas-boot2 小时前
Java 正则表达式白皮书:语法详解、工程实践与常用表达式库
开发语言·python·mysql
仗剑_走天涯2 小时前
基于pytorch.nn模块实现线性模型
人工智能·pytorch·python·深度学习
chao_7892 小时前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
chao_7896 小时前
二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
数据结构·python·算法·leetcode·二分查找
烛阴7 小时前
Python装饰器解除:如何让被装饰的函数重获自由?
前端·python
noravinsc7 小时前
django 一个表中包括id和parentid,如何通过parentid找到全部父爷id
python·django·sqlite
ajassi20007 小时前
开源 python 应用 开发(三)python语法介绍
linux·python·开源·自动化
沉默媛8 小时前
如何安装python以及jupyter notebook
开发语言·python·jupyter
Deng9452013149 小时前
基于Python的旅游数据可视化应用
python·numpy·pandas·旅游·数据可视化技术
2401_878624799 小时前
pytorch 自动微分
人工智能·pytorch·python·机器学习