定期备份和清理gitlab文件

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019-12-05 14:39
# @Author  : Anthony
# @Email   : ianghont7@163.com
# @File    : check_gitlab.py


# import requests
# import re
# import time
#
# url = "http://gitlab.test.cn/api/v3/projects?private_token=xxxxx"
#
# response = requests.get(url)
# print(response.json())
#


import os
import time
import datetime
import threading
import subprocess

# 注意点,千万要注意不要和内置方法重名啊!!!!


# 文件所在路径
source_path = "/home/xxx/xxx/"

# 当前时间
now_time = datetime.datetime.now()

# 获取全部文件名称
source_path_lists = os.listdir(source_path)

# 清理过期文件,只保留7天内
def remove_dated_files():
    # 选择要提前的天数
    change_time = now_time + datetime.timedelta(days=-2)
    # 格式化处理时间戳
    change_time_format = change_time.strftime('%Y%m%d')
    for line in source_path_lists:
        file_ctime = int(line.split('_')[0].strip())
        file_local_time = time.localtime(file_ctime)
        # 文件名中的时间戳
        file_local_time_end = time.strftime("%Y%m%d", file_local_time)
        # 清理2天前的过期文件
        if file_local_time_end <= change_time_format:
            all_file_path = source_path + line
            # 清理过期文件
            os.remove(all_file_path)
            print('2天前过期文件 %s 清理完成,该文件创建时间:%s' % (all_file_path, file_local_time_end))


def get_gitlab_backup_fils():
    for line in source_path_lists:
        all_file_path = source_path + line
        print(all_file_path)
        args = "scp -P xxxx  %s  root@192.168.xx.xx:/home/data/gitlabbackup"%all_file_path
        shell_runnings = subprocess.Popen(args,
                                          shell=True,
                                          stdin=subprocess.PIPE,
                                          stdout=subprocess.PIPE,
                                          stderr=subprocess.PIPE,)
        out, err = shell_runnings.communicate()
        for line in out.splitlines():
            print('开始传输:%s'%line)


if __name__ == "__main__":
    threads_lists = [threading.Thread(target=remove_dated_files),
                    threading.Thread(target=get_gitlab_backup_fils)]
    for i in threads_lists:
        i.start()
 
原文地址:https://www.cnblogs.com/ipyanthony/p/11994117.html