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")
相关推荐
冷雨夜中漫步5 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
郝学胜-神的一滴5 小时前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
百锦再6 小时前
Reactive编程入门:Project Reactor 深度指南
前端·javascript·python·react.js·django·前端框架·reactjs
喵手7 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_944934737 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy7 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
肖永威9 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos
TechWJ9 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
枷锁—sha9 小时前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全
abluckyboy9 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法