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

#!/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 \<\

相关推荐
Java后端的Ai之路11 分钟前
【AI编程工具】-关闭Trae IDE的Git自动Pull开关
ide·git·ai编程·trae
梅梅绵绵冰12 小时前
Git分支管理
git
ys~~14 小时前
git学习
git·vscode·python·深度学习·学习·nlp·github
Rysxt_17 小时前
IDEA中Git隐藏更改(Stash)功能详解教程
数据库·git·intellij-idea·stash
Mr-Apple19 小时前
记录一次git commit --amend的误操作
大数据·git·elasticsearch
UVM_ERROR20 小时前
Git仓库损坏(Segmentation fault)修复实战:虚拟机环境下UVM项目救援指南
笔记·git·vscode·github·芯片
笨蛋少年派21 小时前
Shell脚本编程(bash)简述
开发语言·bash
j_xxx404_21 小时前
Linux:版本控制器Git(第一章)|历史|理解Git|相关git操作|提交冲突解决
linux·运维·git·ai
牛奔21 小时前
git本地提交后,解决push被拒绝 error: failed to push some refs to
大数据·git·elasticsearch·搜索引擎·全文检索
少年姜太公1 天前
什么?还不知道git cherry pick?
前端·javascript·git