#!/usr/bin/env bash
# =============================================================================
# Meta              : Gitea 备份执行脚本
# Version           : 2.0.0
# Author            : Bash Shell Senior Development Engineer
# License           : MIT
# Description       : 自动化执行Gitea远程备份、同步、加密、上传及清理任务。
# =============================================================================

source "$(dirname "$0")/common.sh" || { echo "FATAL: common.sh not found." >&2; exit 1; }

#------------------------------------------------------------------------------
# 脚本配置区
#------------------------------------------------------------------------------
readonly APP_NAME="Gitea"
readonly REMOTE_USER="root"
readonly REMOTE_HOST="t0"
readonly MAX_ENCRYPTED_REPLICAS=4

# > 远程配置
readonly REMOTE_CONTAINER="gitea-gitea-1"
readonly REMOTE_GITEA_CONF="/bitnami/gitea/custom/conf/app.ini"
readonly REMOTE_TMP_DIR="/opt/bitnami/gitea/data/tmp"
readonly REMOTE_RSYNC_SOURCE_DIR="/data/gitea/gitea_data/data/tmp/" # 注意末尾的斜杠

# > 本地路径
readonly LOCAL_BACKUP_DIR="/data/t0_150_230_198_103/gitea"

# =============================================================================
# 主执行流程
# =============================================================================
main() {
  trap 'log_message "ERROR" "${APP_NAME}的备份任务出现错误! 终止"' ERR
  log_message "INFO" "====== 开始 ${APP_NAME} 备份任务 ======"

  # > 步骤 1: 执行Gitea备份命令
  log_message "INFO" "[Step 1/8] 远程执行${APP_NAME} 备份 DUMP..."
  local dump_cmd="docker exec ${REMOTE_CONTAINER} /opt/bitnami/gitea/bin/gitea dump -c ${REMOTE_GITEA_CONF}"
  execute_remote_command "${REMOTE_USER}" "${REMOTE_HOST}" "${dump_cmd}"

  # > 步骤 2: 移动并重命名备份文件
  log_message "INFO" "[Step 2/8] 移动并重命名备份文件..."
  local new_filename="gitea-dump-$(date +%Y%m%d-%H%M%S).zip"
  local move_cmd="docker exec ${REMOTE_CONTAINER} /bin/sh -c 'mv /opt/bitnami/gitea/gitea-dump-*.zip ${REMOTE_TMP_DIR}/${new_filename}'"
  execute_remote_command "${REMOTE_USER}" "${REMOTE_HOST}" "${move_cmd}"

  # > 步骤 3: rsync复制备份文件到本地
  log_message "INFO" "[Step 3/8] rsync复制备份文件到本地..."
  mkdir -p "${LOCAL_BACKUP_DIR}"
  rsync -avz --progress -e "ssh -p ${REMOTE_SSH_PORT}" \
    "${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_RSYNC_SOURCE_DIR}${new_filename}" \
    "${LOCAL_BACKUP_DIR}/"

  # > 步骤 4: 远程清理备份文件
  log_message "INFO" "[Step 4/8] Cleaning up remote dump file..."
  execute_remote_command "${REMOTE_USER}" "${REMOTE_HOST}" "rm -f ${REMOTE_RSYNC_SOURCE_DIR}gitea-dump-*.zip"

  # > 步骤 5: 7zip加密
  local archive_file="${SCRIPT_RUN_DIR}/${APP_NAME}-backup-$(date +%Y%m%d-%H%M%S).7z"
  log_message "INFO" "[Step 5/8] 开始加密本地备份目录..."
  encrypt_with_7zip "${LOCAL_BACKUP_DIR}" "${archive_file}"

  # > 步骤 6: rclone上传
  log_message "INFO" "[Step 6/8] 上传加密压缩包至冷存储 => ${RCLONE_REMOTE_REPO}..."
  rclone_copy "${archive_file}" "${RCLONE_REMOTE_REPO}"

  # > 步骤 7: 控制远程副本数
  log_message "INFO" "[Step 7/8] 控制冷备份的副本数量 => ${MAX_ENCRYPTED_REPLICAS}..."
  rclone_control_replicas "${RCLONE_REMOTE_REPO}" "${APP_NAME}-backup-" "${MAX_ENCRYPTED_REPLICAS}"

  # > 步骤 8: 清理本地
  log_message "INFO" "[Step 8/8] 清理本地压缩包..."
  cleanup_local_encrypted_files "${SCRIPT_RUN_DIR}"
  # rm -rf "${LOCAL_BACKUP_DIR}"

  log_message "INFO" "====== ${APP_NAME} 备份任务已全部完成! ======"
}

# =============================================================================
# 脚本入口点
# =============================================================================
# 函数调用关系图
# main
# ├─ execute_remote_command (4)
# ├─ encrypt_with_7zip
# ├─ rclone_copy
# ├─ rclone_control_replicas
# └─ cleanup_local_encrypted_files
# =============================================================================
main "$@"
