auto-sync: 2026-04-05 17:32:06

This commit is contained in:
cfdaily
2026-04-05 17:32:06 +08:00
parent f2fe17a075
commit 5031c36ef0
82 changed files with 2238 additions and 57 deletions
+99 -55
View File
@@ -1,73 +1,117 @@
#!/bin/bash
# 自动双向同步脚本
# 每分钟运行一次,双向同步本地和远程Gitee
# 监控 sanguo_projects 目录下所有子项目,每个子项目是独立 git 仓库
# 双向同步本地和远程Gitee
# 错误处理:失败了记录日志不继续错误扩散
PROJECT_DIR="/Users/chufeng/.openclaw/sanguo_projects/sanguo_quant_live"
PROJECT_DIR="/Users/chufeng/.openclaw/sanguo_projects"
LOG_FILE="$PROJECT_DIR/auto-sync.log"
MAX_RETRIES=2
# 需要同步的项目列表(每个都是独立 git 仓库)
PROJECTS=(
"sanguo_quant_live"
"sanguo_mail"
"sanguo_vnpy"
"edict"
)
# 确保目录存在
cd "$PROJECT_DIR" || {
echo "[$(date)] ERROR: Failed to cd into $PROJECT_DIR" >> "$LOG_FILE"
exit 1
}
echo "[$(date)] Starting auto sync..." >> "$LOG_FILE"
echo "[$(date)] ========================================" >> "$LOG_FILE"
echo "[$(date)] Starting auto sync for all projects..." >> "$LOG_FILE"
# 第一步:git pull 拉取远程变更
echo "[$(date)] Step 1: git pull origin main" >> "$LOG_FILE"
git pull origin main
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "[$(date)] WARNING: git pull failed with exit code $exit_code" >> "$LOG_FILE"
# pull失败不推送,避免冲突
exit 1
fi
echo "[$(date)] git pull success" >> "$LOG_FILE"
# 第步:添加所有变更(包括未跟踪文件)
echo "[$(date)] Step 2: Adding all changes..." >> "$LOG_FILE"
git add .
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "[$(date)] ERROR: git add failed with exit code $exit_code" >> "$LOG_FILE"
exit 1
fi
# 第三步:检查是否有内容需要提交
if git diff --cached --quiet; then
# 没有变更需要提交,正常退出
echo "[$(date)] No changes to commit, exiting." >> "$LOG_FILE"
exit 0
fi
# 有变更,进行提交
echo "[$(date)] Step 3: Found changes to commit, committing..." >> "$LOG_FILE"
git commit -m "auto-sync: $(date '+%Y-%m-%d %H:%M:%S')"
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "[$(date)] ERROR: git commit failed with exit code $exit_code" >> "$LOG_FILE"
exit 1
fi
# 推送到远程
echo "[$(date)] Step 3: Pushing to origin/main..." >> "$LOG_FILE"
for i in $(seq 1 $MAX_RETRIES); do
git push origin main
exit_code=$?
if [ $exit_code -eq 0 ]; then
echo "[$(date)] Push success! Sync complete." >> "$LOG_FILE"
exit 0
# 同步单个项目函数
sync_project() {
local project=$1
local dir="$PROJECT_DIR/$project"
if [[ ! -d "$dir/.git" ]]; then
echo "[$(date)] [$project] SKIP: not a git repository, skipping" >> "$LOG_FILE"
return 0
fi
echo "[$(date)] [$project] Starting sync..." >> "$LOG_FILE"
cd "$dir"
# 第步:git pull 拉取远程变更
echo "[$(date)] [$project] Step 1: git pull origin main" >> "$LOG_FILE"
git pull origin main
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "[$(date)] [$project] WARNING: git pull failed with exit code $exit_code" >> "$LOG_FILE"
# pull失败不推送,避免冲突
return 1
fi
echo "[$(date)] [$project] git pull success" >> "$LOG_FILE"
# 第二步:添加所有变更(包括未跟踪文件)
echo "[$(date)] [$project] Step 2: Adding all changes..." >> "$LOG_FILE"
git add .
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "[$(date)] [$project] ERROR: git add failed with exit code $exit_code" >> "$LOG_FILE"
return 1
fi
# 第三步:检查是否有内容需要提交
if git diff --cached --quiet; then
# 没有变更需要提交,正常退出
echo "[$(date)] [$project] No changes to commit, done." >> "$LOG_FILE"
return 0
fi
# 有变更,进行提交
echo "[$(date)] [$project] Step 3: Found changes to commit, committing..." >> "$LOG_FILE"
git commit -m "auto-sync: $(date '+%Y-%m-%d %H:%M:%S')"
exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "[$(date)] [$project] ERROR: git commit failed with exit code $exit_code" >> "$LOG_FILE"
return 1
fi
# 推送到远程
echo "[$(date)] [$project] Step 4: Pushing to origin/main..." >> "$LOG_FILE"
for i in $(seq 1 $MAX_RETRIES); do
git push origin main
exit_code=$?
if [ $exit_code -eq 0 ]; then
echo "[$(date)] [$project] Push success! Sync complete." >> "$LOG_FILE"
return 0
fi
echo "[$(date)] [$project] Push attempt $i failed, retrying..." >> "$LOG_FILE"
sleep 2
done
echo "[$(date)] [$project] ERROR: Push failed after $MAX_RETRIES attempts" >> "$LOG_FILE"
return 1
}
# 遍历所有项目进行同步
FAILED_PROJECTS=""
for project in "${PROJECTS[@]}"; do
echo "" >> "$LOG_FILE"
sync_project "$project"
if [ $? -ne 0 ]; then
FAILED_PROJECTS="$FAILED_PROJECTS $project"
fi
echo "[$(date)] Push attempt $i failed, retrying..." >> "$LOG_FILE"
sleep 2
done
echo "[$(date)] ERROR: Push failed after $MAX_RETRIES attempts" >> "$LOG_FILE"
exit 1
echo "" >> "$LOG_FILE"
echo "[$(date)] Sync finished." >> "$LOG_FILE"
if [[ -n "$FAILED_PROJECTS" ]]; then
echo "[$(date)] WARNING: Some projects failed to sync:$FAILED_PROJECTS" >> "$LOG_FILE"
exit 1
else
echo "[$(date)] All projects synced successfully!" >> "$LOG_FILE"
exit 0
fi
+1 -1
View File
@@ -3,7 +3,7 @@
# 文件监控脚本
# 实时监控目录变化,触发同步
PROJECT_DIR="/Users/chufeng/.openclaw/sanguo_projects/sanguo_quant_live"
PROJECT_DIR="/Users/chufeng/.openclaw/sanguo_projects"
LOG_FILE="$PROJECT_DIR/file-watcher.log"
SYNC_SCRIPT="$PROJECT_DIR/management/sanguo_auto_sync/auto-sync.sh"
LOCK_FILE="/tmp/sanguo_sync.lock"
+1 -1
View File
@@ -1 +1 @@
38582
38830