-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathbackup_ql.py
128 lines (103 loc) · 4.02 KB
/
backup_ql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import json
import os
import smtplib
import time
from email.header import Header
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import requests
url = '127.0.0.1:5700'
def getToken():
if os.path.isfile('/ql/config/auth.json'):
with open('/ql/config/auth.json', 'r') as f:
config = json.load(f)
return 'Bearer ' + config['token']
def 保存文件(file, data):
with open(file, 'w') as f:
json.dump(data, f)
def 备份订阅信息():
newUrl = f"http://{url}/open/subscriptions"
headers = {
'Authorization': getToken(),
'Content-Type': 'application/json'
}
sec = requests.session()
sec.verify = False
sec.trust_env = False
rj = sec.get(newUrl, headers=headers).json()
if rj['code'] == 200:
保存文件('subscription.json', rj)
def 备份环境变量():
newUrl = f"http://{url}/open/envs"
headers = {
'Authorization': getToken(),
'Content-Type': 'application/json'
}
sec = requests.session()
sec.verify = False
sec.trust_env = False
rj = sec.get(newUrl, headers=headers).json()
if rj['code'] == 200:
保存文件('envs.json', rj)
def 备份依赖():
newUrl = f"http://{url}/open/dependencies"
headers = {
'Authorization': getToken(),
'Content-Type': 'application/json'
}
sec = requests.session()
sec.verify = False
sec.trust_env = False
rj = sec.get(newUrl, headers=headers, params={'type': 'python3'}).json()
if rj['code'] == 200:
保存文件('dependencies-python3.json', rj)
rj = sec.get(newUrl, headers=headers, params={'type': 'nodejs'}).json()
if rj['code'] == 200:
保存文件('dependencies-nodejs.json', rj)
def 邮箱发送文件():
# 邮件服务器的信息
smtp_server = os.getenv("SMTP_SERVER")
smtp_username = os.getenv('SMTP_USERNAME')
smtp_password = os.getenv('SMTP_PASSWORD')
# 发件人、收件人和主题
from_email = smtp_username
to_email = smtp_username
subject = f"青龙备份-{time.strftime('%Y-%m-%d', time.localtime())}"
# 创建一个带附件的邮件
message = MIMEMultipart()
message["From"] = Header(from_email)
message["To"] = Header(to_email)
message["Subject"] = Header(subject, 'utf-8')
# 邮件正文
with open('subscription.json', "rb") as attachment_file:
attachment = MIMEApplication(attachment_file.read(), Name="subscription.json")
attachment.add_header("Content-Disposition", "subscription.json", filename="subscription.json")
message.attach(attachment)
with open('dependencies-nodejs.json', "rb") as attachment_file:
attachment = MIMEApplication(attachment_file.read(), Name="dependencies-nodejs.json")
attachment.add_header("Content-Disposition", "dependencies-nodejs.json", filename="dependencies-nodejs.json")
message.attach(attachment)
with open('dependencies-python3.json', "rb") as attachment_file:
attachment = MIMEApplication(attachment_file.read(), Name="dependencies-python3.json")
attachment.add_header("Content-Disposition", "dependencies-python3.json", filename="dependencies-python3.json")
message.attach(attachment)
with open('envs.json', "rb") as attachment_file:
attachment = MIMEApplication(attachment_file.read(), Name="envs.json")
attachment.add_header("Content-Disposition", "envs.json", filename="envs.json")
message.attach(attachment)
# 连接到SMTP服务器并发送邮件
try:
smtpObj = smtplib.SMTP(smtp_server)
# smtpObj.set_debuglevel(1)
smtpObj.login(smtp_username, smtp_password)
smtpObj.sendmail(from_email, [to_email], message.as_string())
print("SEND EMAIL SUCCESS")
except smtplib.SMTPException:
print("SEND EMAIL FAIL")
if __name__ == '__main__':
# print(getToken())
备份订阅信息()
备份环境变量()
备份依赖()
邮箱发送文件()