From 1203a576412dcecd7afd39fb6fa9838e592c1246 Mon Sep 17 00:00:00 2001 From: songtianlun Date: Tue, 22 Apr 2025 15:11:04 +0800 Subject: [PATCH] feat: add Feishu webhook notification for backups - Include a webhook URL to send notifications after backups. - Capture and log the duration and size of the backups. - Create a new Kubernetes secret for storing the Feishu webhook URL. - Enhance the backup script to notify users of backup success with details. This change improves monitoring and user notification of backup events, allowing for better awareness and response times in case of failure or success of the backup processes. --- backups/daemonset+cronjob/cm-script.yaml | 16 ++++++++++++++++ backups/daemonset+cronjob/daemonset.yaml | 6 ++++++ .../daemonset+cronjob/secret-feishu-webhook.yaml | 9 +++++++++ backups/daemonset+cronjob/secret.yaml | 2 ++ 4 files changed, 33 insertions(+) create mode 100644 backups/daemonset+cronjob/secret-feishu-webhook.yaml diff --git a/backups/daemonset+cronjob/cm-script.yaml b/backups/daemonset+cronjob/cm-script.yaml index db491a8..a778cfb 100644 --- a/backups/daemonset+cronjob/cm-script.yaml +++ b/backups/daemonset+cronjob/cm-script.yaml @@ -20,6 +20,9 @@ data: HOSTNAME=$(hostname) HOSTNAME="${NODE_NAME:-$HOSTNAME}" + FEISHU_WEBHOOK_URL=$(cat /etc/feishu-webhook/url) + + START_TIME=$(date +%s) # 配置 s3cmd cat > ~/.s3cfg << EOF @@ -52,6 +55,7 @@ data: BACKUP_NAME="backup-${DIR_NAME}-${TIMESTAMP}.tar.gz" TEMP_DIR=$(mktemp -d) BACKUP_PATH="/tmp/${BACKUP_NAME}" + SOURCE_SIZE=$(du -sh /data/local-csi | cut -f1) echo "正在备份目录: $dir" @@ -95,8 +99,20 @@ data: fi done + END_TIME=$(date +%s) echo "备份完成" + DURATION=$((END_TIME - START_TIME)) + MSG_TITLE="✅ [K3s Backup] $NODE_NAME Backup Success" + MSG_TEXT="Host: $NODE_NAME\nSource: /data\nSource Size: $SOURCE_SIZE\nDuration: ${DURATION}s" + JSON_PAYLOAD=$(jq -n \ + --arg title "$MSG_TITLE" \ + --arg text "$MSG_TEXT" \ + '{msg_type: "post", content: {post: {zh_cn: {title: $title, content: [[{tag: "text", text: $text}]]}}}}') + echo "Sending notification to Feishu..." + curl -X POST -H "Content-Type: application/json" -d "$JSON_PAYLOAD" "$FEISHU_WEBHOOK_URL" + echo "Notification sent." + s3cmd-trigger.sh: | #!/bin/bash set -e diff --git a/backups/daemonset+cronjob/daemonset.yaml b/backups/daemonset+cronjob/daemonset.yaml index 1ee9cae..7f43287 100644 --- a/backups/daemonset+cronjob/daemonset.yaml +++ b/backups/daemonset+cronjob/daemonset.yaml @@ -98,6 +98,9 @@ spec: mountPath: /data - name: scripts mountPath: /scripts + - name: feishu-webhook-volume + mountPath: /etc/feishu-webhook # 挂载飞书 Webhook Secret + readOnly: true volumes: - name: host-data hostPath: @@ -106,3 +109,6 @@ spec: configMap: name: backup-script defaultMode: 0755 + - name: feishu-webhook-volume + secret: + secretName: feishu-webhook diff --git a/backups/daemonset+cronjob/secret-feishu-webhook.yaml b/backups/daemonset+cronjob/secret-feishu-webhook.yaml new file mode 100644 index 0000000..ea317ff --- /dev/null +++ b/backups/daemonset+cronjob/secret-feishu-webhook.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +data: + # https://open.feishu.cn/open-apis/bot/v2/hook/f2a8d634-6a90-4f86-ac2f-ef6a53dbd680 + url: aHR0cHM6Ly9vcGVuLmZlaXNodS5jbi9vcGVuLWFwaXMvYm90L3YyL2hvb2svZjJhOGQ2MzQtNmE5MC00Zjg2LWFjMmYtZWY2YTUzZGJkNjgw +kind: Secret +metadata: + name: feishu-webhook + namespace: backup-system +type: Opaque diff --git a/backups/daemonset+cronjob/secret.yaml b/backups/daemonset+cronjob/secret.yaml index 5b696bd..367add9 100644 --- a/backups/daemonset+cronjob/secret.yaml +++ b/backups/daemonset+cronjob/secret.yaml @@ -10,4 +10,6 @@ data: access-key: RVZuWFViR2xld2t0dFF0em9XUWs= # EVnXUbGlewkttQtzoWQk secret-key: THNxVFRmc0VEVzBFY3Buc09aOUxyTnhwc21zajdIMGxlR2R0WHBwRg== # LsqTTfsEDW0EcpnsOZ9LrNxpsmsj7H0leGdtXppF bucket: YmFja3Vwcw== # backups + # https://open.feishu.cn/open-apis/bot/v2/hook/f2a8d634-6a90-4f86-ac2f-ef6a53dbd680 + #feishu-webhook: aHR0cHM6Ly9vcGVuLmZlaXNodS5jbi9vcGVuLWFwaXMvYm90L3YyL2hvb2svZjJhOGQ2MzQtNmE5MC00Zjg2LWFjMmYtZWY2YTUzZGJkNjgw