#!/bin/bash # 每日回顾整理脚本 # 流程:NAS pull → 整理任务 → Git commit/push → NAS sync set -e WORK_DIR="/home/starmen1265/work/zq" TODO_FILE="$WORK_DIR/01.待办/TODO.md" REVIEW_DIR="$WORK_DIR/40.归档/每日回顾" LOG_FILE="$HOME/work/backup/daily-review.log" # 获取昨天的日期(格式:YYYY-MM-DD) YESTERDAY=$(date -d "yesterday" +%Y-%m-%d) REVIEW_FILE="$REVIEW_DIR/$YESTERDAY.md" # 确保日志目录存在 mkdir -p "$(dirname "$LOG_FILE")" mkdir -p "$REVIEW_DIR" log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE" } log "=== 每日回顾整理开始 ===" log "处理日期: $YESTERDAY" # 切换到工作目录 cd "$WORK_DIR" # 1. 先从 NAS 拉取最新内容 log "1. 从 NAS 拉取更新..." rclone sync "nas-webdav:/home/Drive/工作区/张青/工作区/" "$WORK_DIR/" \ --exclude ".git/" \ --exclude ".gitignore" \ --log-level INFO >> "$LOG_FILE" 2>&1 # 如果没有已完成的任务,直接退出 if ! grep -q "\[x\]" "$TODO_FILE"; then log "没有已完成的任务,跳过" exit 0 fi log "2. 发现已完成的任务,开始整理..." # 提取已完成的任务(保留标题结构) # 读取TODO.md,提取所有包含[x]的行及其所属标题 COMPLETED_TASKS=$(awk ' /^#/ { current_section = $0 } /\[x\]/ { if (current_section != last_section) { print current_section last_section = current_section } print $0 } ' "$TODO_FILE") # 如果每日回顾文件不存在,创建它 if [ ! -f "$REVIEW_FILE" ]; then touch "$REVIEW_FILE" log "创建每日回顾文件: $REVIEW_FILE" fi # 将完成的任务追加到每日回顾文件(去掉✅及后面的日期) echo "$COMPLETED_TASKS" | sed 's/ ✅ [0-9-]*$//' >> "$REVIEW_FILE" log "已添加任务到每日回顾" # 从TODO.md中删除已完成的任务行 sed -i '/\[x\]/d' "$TODO_FILE" # 清理TODO.md中的空标题(标题下没有任务的情况) awk ' /^#/ { if (section_content != "") { print section_header print section_content } section_header = $0 section_content = "" next } !/^#/ && $0 != "" { section_content = section_content "\n" $0 } END { if (section_content != "") { print section_header print section_content } } ' "$TODO_FILE" > "$TODO_FILE.tmp" && mv "$TODO_FILE.tmp" "$TODO_FILE" # 3. 提交并推送到 Gitea log "3. 提交并推送到 Gitea..." git add "$TODO_FILE" "$REVIEW_FILE" git commit -m "每日回顾整理:$YESTERDAY" >> "$LOG_FILE" 2>&1 || log "Git提交无变更" git push origin main >> "$LOG_FILE" 2>&1 || log "警告:Git推送失败" # 4. 全量同步回 NAS log "4. 同步回 NAS..." rclone sync "$WORK_DIR/" "nas-webdav:/home/Drive/工作区/张青/工作区/" \ --exclude ".git/" \ --exclude ".gitignore" \ --log-level INFO >> "$LOG_FILE" 2>&1 || log "警告:rclone 推送失败" log "=== 每日回顾整理完成 ===" echo "Daily review completed for $YESTERDAY"