#!/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()