Skip to content

Commit c8ad710

Browse files
committed
feat: feishu notify
1 parent 9849f12 commit c8ad710

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,17 @@ S3 支持使用虚拟域名作为 endpoint,即可以将 region 或者 bucket
144144

145145
- S3_ENABLE: 选填,是否启用 S3 存储备份,true 表示启用
146146
- S3_EP: 选填,S3 url,例如 https://minio-api.36node.com
147-
- S3_EP_VIRTUAL: 选填,是否启用虚拟 host url,true 表示启用,使用阿里云 oss 时,需要设置为true,minio 无需处理
147+
- S3_EP_VIRTUAL: 选填,是否启用虚拟 host url,true 表示启用,使用阿里云 oss 时,需要设置为 true,minio 无需处理
148148
- S3_ACCESS_KEY: 选填,S3 access key
149149
- S3_ACCESS_SECRET: 选填,S3 access secret
150150
- S3_REGION: 选填,地区名
151151
- S3_BUCKET: 选填,要存储的桶名
152152
- S3_PREFIX: 选填,要存储的前缀
153153
- S3_SIGNATURE_VERSION: 选填,签名版本,使用阿里云 oss 时,需要设置为"s3",minio 无需处理
154154

155+
- FEISHU_NOTIFY_TOKEN: 选填,飞书推送 token,为空时不推送
156+
- FEISHU_NOTIFY_TITLE: 选填,飞书推送标题,FEISHU_NOTIFY_TOKEN 为空时无效,例如 haiviv cn,此时飞书通知的标题为 `haivivi cn mongodb 备份出错`
157+
155158
### restore
156159

157160
- S3_ENABLE: 选填,是否从 S3 中进行恢复,true 表示启用

docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ RUN apt-get update && \
55
apt-get install -y zip psmisc python3 python3-pip
66

77
RUN pip3 install --upgrade pip
8-
RUN pip3 install boto3
8+
RUN pip3 install boto3 requests
99

1010
WORKDIR /app
1111
ADD . /app

docker/backup.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from urllib.parse import urlparse
77
import boto3
88
from botocore.client import Config
9+
import requests
910

1011
# 数据库清理备份脚本
1112
# 1. 按要求备份数据,并保存到指定路径
@@ -35,6 +36,7 @@
3536
"S3_REGION",
3637
"S3_BUCKET",
3738
"S3_PREFIX",
39+
"FEISHU_TOKEN",
3840
]
3941

4042
for key in must_inputs:
@@ -93,6 +95,13 @@ def check_bool(key):
9395
os.environ["S3_SIGNATURE_VERSION"] if check_var("S3_SIGNATURE_VERSION") else None
9496
)
9597

98+
feishu_notify_token = (
99+
os.environ["FEISHU_NOTIFY_TOKEN"] if check_var("FEISHU_NOTIFY_TOKEN") else None
100+
)
101+
feishu_notify_title = (
102+
os.environ["FEISHU_NOTIFY_TITLE"] if check_var("FEISHU_NOTIFY_TITLE") else None
103+
)
104+
96105
# 计算当前日期,按照 年月日时分 格式
97106
date = (datetime.now() + timedelta(hours=8)).strftime("%Y%m%d%H%M%S")
98107

@@ -236,6 +245,34 @@ def upload_s3(prefix):
236245
print(f"Deleted s3 old backup files: {keys_to_remove}")
237246

238247

248+
def send_feishu_notify(msg):
249+
data = {
250+
"msg_type": "post",
251+
"content": {
252+
"post": {
253+
"zh_cn": {
254+
"title": f"{feishu_notify_title} mongodb 备份出错",
255+
"content": [
256+
[
257+
{
258+
"tag": "text",
259+
"text": msg,
260+
}
261+
]
262+
],
263+
}
264+
}
265+
},
266+
}
267+
try:
268+
requests.post(
269+
f"https://open.feishu.cn/open-apis/bot/v2/hook/{feishu_notify_token}",
270+
json=data,
271+
)
272+
except Exception as e:
273+
print("Feishu notify error:", e)
274+
275+
239276
try:
240277
if not os.path.exists(backup_path):
241278
os.makedirs(backup_path)
@@ -263,4 +300,8 @@ def upload_s3(prefix):
263300

264301
except Exception as e:
265302
print("backup error:", e)
303+
304+
if feishu_notify_token:
305+
send_feishu_notify(f"错误信息: {e}")
306+
266307
sys.exit()

0 commit comments

Comments
 (0)