From df5bbbd956ee3457a86d1dc3fed37c1390e7dedd Mon Sep 17 00:00:00 2001 From: Codex Date: Mon, 16 Mar 2026 20:23:19 +0800 Subject: [PATCH] Move self-check from update to install --- README.md | 9 ++++++++- install.sh | 43 ++++++++++++++++++++++++++++++++++++++++++- update.sh | 5 ++--- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5872c17..0276018 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,8 @@ cd /root/backup_OBS - 修正脚本执行权限 - 安装 `systemd` 更新任务 - 启用 `backup-obs-update.timer` +- 为目标用户写入每天 `03:00` 执行一次的备份 `cron` +- 执行一次 `./backup.sh --self-check` 安装自检 默认使用 system 模式,也就是安装到 `/etc/systemd/system`。 @@ -115,6 +117,7 @@ cd /root/backup_OBS ```bash systemctl status backup-obs-update.timer journalctl -u backup-obs-update.service -n 100 +crontab -l ``` ## 自动更新 @@ -130,7 +133,6 @@ journalctl -u backup-obs-update.service -n 100 - `git fetch --tags origin` - 默认更新 `stable` 分支,或切换到你指定的 tag / 分支 - 修正脚本执行权限 -- 自动执行一次 `./backup.sh --self-check` 自检 也可以手动指定版本: @@ -143,6 +145,11 @@ journalctl -u backup-obs-update.service -n 100 配合 `systemd timer` 后,机器会定时自动执行 `update.sh`。 +当前默认策略是: + +- `systemd timer`:负责定时执行 `./update.sh stable` +- `cron`:负责每天 `03:00` 执行一次 `./backup.sh` + ## GitHub 多机发布流程 推荐流程: diff --git a/install.sh b/install.sh index f517ac3..51fd533 100755 --- a/install.sh +++ b/install.sh @@ -17,8 +17,10 @@ usage() { - 若未检测到仓库内 obsutil,会自动调用 lib/obsutil.sh 安装 - system 模式会安装到 /etc/systemd/system - user 模式会安装到 ~/.config/systemd/user + - 会为目标用户安装每天 03:00 执行一次的备份 cron + - 安装完成后会执行一次 ./backup.sh --self-check 自检 - 不会覆盖已有 env.conf 和 exclude.user.list - - dry-run 只打印将执行的动作,不实际写入 systemd + - dry-run 只打印将执行的动作,不实际写入 systemd 或 crontab USAGE } @@ -42,6 +44,7 @@ need_cmd() { need_cmd systemctl need_cmd install need_cmd sed +need_cmd crontab if [[ "$INSTALL_MODE" != "system" && "$INSTALL_MODE" != "user" ]]; then echo "无效 --mode:$INSTALL_MODE" >&2 @@ -132,6 +135,36 @@ render_unit() { render_unit "${SCRIPT_DIR}/systemd/backup-obs-update.service" "${SYSTEMD_DIR}/backup-obs-update.service" render_unit "${SCRIPT_DIR}/systemd/backup-obs-update.timer" "${SYSTEMD_DIR}/backup-obs-update.timer" +install_backup_cron() { + local cron_line current_cron filtered_cron target_user current_user + cron_line="0 3 * * * ${SCRIPT_DIR}/backup.sh" + current_user="$(id -un)" + target_user="$INSTALL_USER" + + if $DRY_RUN; then + echo "[dry-run] 将为用户 ${target_user} 写入 cron:${cron_line}" + return 0 + fi + + if [[ "$(id -u)" -eq 0 ]]; then + current_cron="$(crontab -u "$target_user" -l 2>/dev/null || true)" + filtered_cron="$(printf '%s\n' "$current_cron" | grep -v -F "${SCRIPT_DIR}/backup.sh" || true)" + printf '%s\n%s\n' "$filtered_cron" "$cron_line" | sed '/^$/N;/^\n$/D' | crontab -u "$target_user" - + echo "已为用户 ${target_user} 安装 cron:${cron_line}" + return 0 + fi + + if [[ "$target_user" != "$current_user" ]]; then + echo "非 root 模式下只能为当前用户 ${current_user} 安装 cron" >&2 + exit 2 + fi + + current_cron="$(crontab -l 2>/dev/null || true)" + filtered_cron="$(printf '%s\n' "$current_cron" | grep -v -F "${SCRIPT_DIR}/backup.sh" || true)" + printf '%s\n%s\n' "$filtered_cron" "$cron_line" | sed '/^$/N;/^\n$/D' | crontab - + echo "已为当前用户安装 cron:${cron_line}" +} + if $DRY_RUN; then echo "[dry-run] 将执行 systemctl daemon-reload" echo "[dry-run] 将启用 backup-obs-update.timer" @@ -148,3 +181,11 @@ else echo "查看状态:systemctl --user status backup-obs-update.timer" echo "查看日志:journalctl --user -u backup-obs-update.service -n 100" fi + +install_backup_cron + +if $DRY_RUN; then + echo "[dry-run] 将执行 ./backup.sh --self-check" +else + bash "${SCRIPT_DIR}/backup.sh" --self-check +fi diff --git a/update.sh b/update.sh index 092a0e3..96def82 100755 --- a/update.sh +++ b/update.sh @@ -13,7 +13,6 @@ usage() { 说明: - 不带参数时,默认更新 stable 分支 - 带参数时,可以切换到指定分支或 tag - - 更新后会自动执行一次 ./backup.sh --self-check 自检 USAGE } @@ -49,8 +48,8 @@ fi chmod 755 \ "${SCRIPT_DIR}/backup.sh" \ "${SCRIPT_DIR}/clean_backups.sh" \ + "${SCRIPT_DIR}/obsutil" \ + "${SCRIPT_DIR}/lib/obsutil.sh" \ "${SCRIPT_DIR}/lib/telegram.sh" \ "${SCRIPT_DIR}/install.sh" \ "${SCRIPT_DIR}/update.sh" - -bash "${SCRIPT_DIR}/backup.sh" --self-check