微信群机器人-备份文件发送通知

#!/bin/bash

配置文件路径(与企业微信配置分离)

CONFIG_FILE="(dirname "0")/git_pull_config.env"

加载配置文件

if [ -f "$CONFIG_FILE" ]; then

source "$CONFIG_FILE"

else

WX_ROBOT_URL="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxx" # 默认不发送通知

fi

获取脚本所在目录的绝对路径

repos_path="(cd "(dirname "$0")" && pwd)"

初始化统计变量(避免子 Shell 作用域问题)

declare -i total_repos=0 success_count=0 error_count=0

declare -a error_messages=()

关键修复:使用进程替换避免子 Shell 变量丢失

while IFS= read -r gitdir; do

((total_repos++))

project_dir=(dirname "gitdir")

echo -e "\n[ START ] 同步仓库: $project_dir"

执行 git pull 并捕获输出

exec_output=(cd "project_dir" && git pull 2>&1)

exec_status=$?

记录执行结果

if [ $exec_status -eq 0 ]; then

((success_count++))

else

((error_count++))

error_messages+=("【失败】project_dir\\n\{exec_output}</pre>")

fi

done < <(find "$repos_path" -type d -name .git) # 使用进程替换

构建企业微信通知内容

build_notification() {

local status_icon="✅"

$error_count -gt 0 \] \&\& status_icon="⚠️" cat \<\ \*\*同步时间\*\*: $(date "+%Y-%m-%d %H:%M:%S") \> \*\*扫描目录\*\*: \\\`${repos_path}\\\` \> \*\*仓库总数\*\*: ${total_repos} \> \*\*成功数量\*\*: ${success_count} \> \*\*失败数量\*\*: ${error_count} EOF \[ $error_count -gt 0 \] \&\& echo -e "\\n\*\*失败详情\*\*:\\n${error_messages\[@\]//$'\\n'/\\\\n}" } # 发送企业微信通知(支持 Markdown) send_wx_notification() { local content=$(build_notification \| jq -Rs .) local json_payload=$(cat \<\

相关推荐
jayaccc1 天前
Git命令大全:从入门到精通
大数据·git·elasticsearch
论迹1 天前
【Git】-- Git安装 & 卸载(ubuntu)
git·ubuntu·elasticsearch
论迹1 天前
【Git】-- Git基本操作
git·ubuntu
wxr06161 天前
GIT学习
git·学习
猫头虎2 天前
2026最新|GitHub 启用双因素身份验证 2FA 教程:TOTP.app 一键生成动态验证码(新手小白图文实操)
git·开源·gitlab·github·开源软件·开源协议·gitcode
爱学英语的程序员2 天前
让AI 帮我做了个个人博客(附提示词!)
人工智能·git·vue·github·node·个人博客
liu****2 天前
git工具
git·python·算法·机器学习·计算机基础
wxr06162 天前
git无法克隆
git
cooldream20092 天前
Git 拒绝推送(Push Rejected)问题全解析与解决方案实战指南
git
tianyuanwo2 天前
Bash与Sh的诞生背景、底层原理及Linux多Shell解释器兼容性解析
linux·开发语言·bash