]> acesimba.cloud Git - studybase.git/commitdiff
自动同步:代码/数据/Media 更新(2026-09-19 01:21:33)
authorroot <root@localhost.localdomain>
Fri, 18 Sep 2026 17:21:33 +0000 (01:21 +0800)
committerroot <root@localhost.localdomain>
Fri, 18 Sep 2026 17:21:33 +0000 (01:21 +0800)
auto_sync.sh [new file with mode: 0644]

diff --git a/auto_sync.sh b/auto_sync.sh
new file mode 100644 (file)
index 0000000..e204e67
--- /dev/null
@@ -0,0 +1,57 @@
+#!/bin/sh
+# StudyBase 每日自动同步:检查 代码/数据库(s/studybase.db)/Media 是否有变更,
+# 有变更则 git 提交并推送至 origin(本机 /var/lib/git/studybase.git) 与 github。
+# 由 /etc/cron.d/studybase-sync 于每日 02:00 调用。
+# 安全约定:本脚本只执行 add/commit/push,绝不执行 reset/checkout/clean 等覆盖工作区的命令。
+# 日志:/var/log/studybase-auto-sync.log(置于仓库外,避免日志本身进入提交)
+
+REPO=/home/StudyBase
+LOG=/var/log/studybase-auto-sync.log
+
+cd "$REPO" || { echo "$(date '+%F %T') cd $REPO 失败" >> "$LOG"; exit 1; }
+
+{
+echo "===== $(date '+%F %T') 同步开始 ====="
+
+# 有其他 git 操作进行中时跳过本次
+if [ -e .git/index.lock ]; then
+  echo "检测到 .git/index.lock,疑似存在其他 git 操作,本次跳过"
+  exit 0
+fi
+
+# 无任何变更(含未跟踪文件)则结束
+if [ -z "$(git status --porcelain)" ]; then
+  echo "无变更,无需提交"
+  exit 0
+fi
+
+git add -A || { echo "git add 失败,本次终止"; exit 1; }
+if [ -z "$(git status --porcelain)" ]; then
+  echo "无实质变更,无需提交"
+  exit 0
+fi
+
+git commit -m "自动同步:代码/数据/Media 更新($(date '+%F %T'))" || { echo "git commit 失败,本次终止"; exit 1; }
+
+echo "--- push origin ---"
+if git push origin main; then
+  echo "origin push OK"
+else
+  echo "origin push FAILED"
+fi
+
+echo "--- push github ---"
+gh_ok=0
+for i in 1 2 3 4 5; do
+  if git push github main; then gh_ok=1; break; fi
+  echo "github push 第 $i 次失败,30 秒后重试"
+  sleep 30
+done
+if [ "$gh_ok" -eq 1 ]; then
+  echo "github push OK"
+else
+  echo "github push FAILED(5 次重试均失败,待下次任务或人工重试)"
+fi
+
+echo "===== $(date '+%F %T') 同步结束 ====="
+} >> "$LOG" 2>&1