#!/bin/bash
# ═══════════════════════════════════════════════════════════
# 光湖智库数据备份脚本 · 定时备份/opt/zhiku/data到COS桶
# ═══════════════════════════════════════════════════════════
# 项目编号: ZY-PROJ-006
# 守护:     铸渊 · ICE-GL-ZY001
# 版权:     国作登字-2026-A-00037559
#
# 执行频率: 每天 03:00 (cron)
# 备份策略:
#   - 每周一: 全量备份
#   - 其他日: 增量备份(只备份变化文件)
# 存储位置: COS桶/{ZY_COS_BUCKET}/backups/zhiku-data/
# ═══════════════════════════════════════════════════════════

set -e

# ─── 环境变量 ───
COS_SECRET_ID=${ZY_COS_SECRET_ID}
COS_SECRET_KEY=${ZY_COS_SECRET_KEY}
COS_BUCKET=${ZY_COS_BUCKET:-"guanghu-1250000000"}
COS_REGION="ap-singapore"
DATA_DIR="/opt/zhiku/data"
BACKUP_DIR="/opt/zhiku/backups"
LOG_FILE="/var/log/zhiku/backup.log"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
WEEKDAY=$(date +%u) # 1-7 (1=周一)

# ─── 确保目录存在 ───
mkdir -p "${BACKUP_DIR}"
mkdir -p "$(dirname "${LOG_FILE}")"

# ─── 记录开始时间 ───
echo "[$(date '+%Y-%m-%d %H:%M:%S')] 开始备份" >> "${LOG_FILE}"

# ─── 根据星期决定备份类型 ───
if [ "${WEEKDAY}" -eq 1 ]; then
  # 周一全量备份
  BACKUP_NAME="zhiku-data-full-${TIMESTAMP}.tar.gz"
  tar -czf "${BACKUP_DIR}/${BACKUP_NAME}" -C "${DATA_DIR}" .
  echo "[$(date '+%Y-%m-%d %H:%M:%S')] 创建全量备份: ${BACKUP_NAME}" >> "${LOG_FILE}"
else
  # 其他日增量备份
  BACKUP_NAME="zhiku-data-incr-${TIMESTAMP}.tar.gz"
  FIND_CMD="find ${DATA_DIR} -type f -newermt '1 day ago'"
  if [ "${WEEKDAY}" -eq 2 ]; then
    # 周二对比周一的全量备份
    LAST_FULL=$(ls -t "${BACKUP_DIR}"/zhiku-data-full-*.tar.gz | head -1)
    if [ -n "${LAST_FULL}" ]; then
      FIND_CMD="${FIND_CMD} -newer "${LAST_FULL}""
    fi
  fi
  
  # 创建增量备份包
  eval "${FIND_CMD}" | tar -czf "${BACKUP_DIR}/${BACKUP_NAME}" -T - 
  echo "[$(date '+%Y-%m-%d %H:%M:%S')] 创建增量备份: ${BACKUP_NAME}" >> "${LOG_FILE}"
fi

# ─── 上传到COS ───
COS_CLI="/usr/local/bin/coscli"
if [ ! -f "${COS_CLI}" ]; then
  # 安装COS CLI (如果还没有)
  wget -q "https://github.com/tencentyun/coscli/releases/download/v0.11.0-beta/coscli-linux" -O "${COS_CLI}"
  chmod +x "${COS_CLI}"
fi

"${COS_CLI}" cp "${BACKUP_DIR}/${BACKUP_NAME}" "cos://${COS_BUCKET}/backups/zhiku-data/" \
  --secret-id "${COS_SECRET_ID}" \
  --secret-key "${COS_SECRET_KEY}" \
  --region "${COS_REGION}" \
  >> "${LOG_FILE}" 2>&1

# ─── 清理旧备份(本地保留最近3份) ───
ls -t "${BACKUP_DIR}"/zhiku-data-*.tar.gz | tail -n +4 | xargs rm -f

echo "[$(date '+%Y-%m-%d %H:%M:%S')] 备份完成: ${BACKUP_NAME}" >> "${LOG_FILE}"

exit 0