From 15ff2f1c3ccd8c23f6c5ac6ddbd85e674dbaa2c3 Mon Sep 17 00:00:00 2001 From: hailin Date: Sat, 24 Jan 2026 07:17:56 -0800 Subject: [PATCH] feat(deploy): add full-reset command for complete system reset - Add do_full_reset function that deletes all data and rebuilds system - Preserves SSL certificates (stored in /etc/letsencrypt/) - Requires explicit YES confirmation before proceeding - Includes: stop services, clean Docker, rebuild, migrate, restart Co-Authored-By: Claude Opus 4.5 --- deploy.sh | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/deploy.sh b/deploy.sh index d5dffdc..c9d4af5 100755 --- a/deploy.sh +++ b/deploy.sh @@ -906,6 +906,87 @@ do_deploy_full() { echo "" } +#=============================================================================== +# 完整重置 (危险操作) +#=============================================================================== +do_full_reset() { + echo -e "${RED}╔═══════════════════════════════════════════════════════════════════════════════╗${NC}" + echo -e "${RED}║ ⚠️ 警告 ⚠️ ║${NC}" + echo -e "${RED}║ ║${NC}" + echo -e "${RED}║ 这将删除所有数据并重置系统,包括: ║${NC}" + echo -e "${RED}║ - PostgreSQL 数据库中的所有数据 ║${NC}" + echo -e "${RED}║ - Redis 缓存数据 ║${NC}" + echo -e "${RED}║ - Neo4j 图数据库中的所有数据 ║${NC}" + echo -e "${RED}║ - 所有 Docker 镜像和容器 ║${NC}" + echo -e "${RED}║ - 所有构建产物 ║${NC}" + echo -e "${RED}║ ║${NC}" + echo -e "${RED}║ SSL 证书将被保留(存储在 /etc/letsencrypt/) ║${NC}" + echo -e "${RED}║ 此操作不可撤销! ║${NC}" + echo -e "${RED}╚═══════════════════════════════════════════════════════════════════════════════╝${NC}" + echo "" + read -p "确认要完全重置系统吗?请输入 YES 确认: " confirm + + if [ "$confirm" != "YES" ]; then + log_info "操作已取消" + exit 0 + fi + + log_warning "开始完整重置..." + + # 1. 停止所有服务 + log_step "停止所有服务..." + $DOCKER_COMPOSE down -v --remove-orphans 2>/dev/null || true + log_success "服务已停止" + + # 2. 清理 Docker 资源 + log_step "清理 Docker 资源..." + docker system prune -af --volumes 2>/dev/null || true + docker volume prune -f 2>/dev/null || true + log_success "Docker 资源已清理" + + # 3. 清理构建产物和依赖 + log_step "清理构建产物..." + rm -rf node_modules 2>/dev/null || true + for dir in "${SERVICE_DIRS[@]}"; do + rm -rf "$PROJECT_ROOT/$dir/dist" 2>/dev/null || true + rm -rf "$PROJECT_ROOT/$dir/node_modules" 2>/dev/null || true + done + log_success "构建产物已清理" + + # 4. 重新构建所有服务 + log_step "重新构建所有服务..." + do_build all + + # 5. 启动基础设施 + log_step "启动基础设施..." + do_start infra docker + + # 6. 等待数据库就绪 + wait_for_service localhost 5432 "PostgreSQL" + wait_for_service localhost 6379 "Redis" + wait_for_service localhost 7474 "Neo4j" + + # 7. 执行数据库迁移 + log_step "执行数据库迁移..." + sleep 5 # 等待数据库完全初始化 + do_db migrate + + # 8. 启动所有后端服务 + log_step "启动后端服务..." + do_start backend docker + + # 9. 配置 Kong (如果需要) + if docker ps | grep -q kong; then + log_step "配置 Kong API 网关..." + sleep 5 + do_kong setup 2>/dev/null || log_warning "Kong 配置跳过" + fi + + log_success "完整重置完成!" + echo "" + do_status +} + #=============================================================================== # SSL 证书管理 (Let's Encrypt) #=============================================================================== @@ -1327,6 +1408,8 @@ show_help() { deploy-full 完整部署 (含 SSL 证书自动申请) + full-reset ⚠️ 完整重置 (删除所有数据,重建系统,保留SSL证书) + ssl SSL 证书管理 (Let's Encrypt) action: install - 安装 certbot obtain - 申请证书 @@ -1356,6 +1439,7 @@ show_help() { ./deploy.sh restart user docker # 重启用户服务 (Docker) ./deploy.sh logs conversation 200 # 查看对话服务最近200行日志 ./deploy.sh clean all # 清理所有构建产物和依赖 + ./deploy.sh full-reset # ⚠️ 完整重置系统 (危险操作) ./deploy.sh db init # 初始化数据库 (首次部署) ./deploy.sh db backup # 备份数据库 ./deploy.sh db migrate # 执行数据库迁移 @@ -1413,6 +1497,9 @@ main() { deploy-full) do_deploy_full ;; + full-reset) + do_full_reset + ;; ssl) do_ssl "$@" ;;