| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906 |
- #!/bin/bash
- # ============================================
- # 灵越智报 2.0 - 服务器一键部署脚本
- # 适用于: Ubuntu 22.04 LTS
- # 服务器: lanaipc
- # 版本: 2.1.0
- # 更新日期: 2026-01-19
- # ============================================
- set -e
- # 颜色定义
- RED='\033[0;31m'
- GREEN='\033[0;32m'
- YELLOW='\033[1;33m'
- BLUE='\033[0;34m'
- CYAN='\033[0;36m'
- NC='\033[0m'
- # 配置
- PROJECT_DIR="/mnt/win_home/lingyue-zhibao"
- LOG_DIR="/var/log/lingyue"
- DATA_DIR="/mnt/win_home/lingyue-data"
- BACKUP_DIR="/mnt/win_home/lingyue-backup"
- PYTHON_VENV_DIR="${PROJECT_DIR}/python-services/ner-service/venv"
- # 数据库配置
- DB_NAME="lingyue_zhibao"
- DB_USER="lingyue"
- DB_PASS="123123"
- DB_HOST="localhost"
- DB_PORT="5432"
- # RabbitMQ 配置
- MQ_USER="admin"
- MQ_PASS="admin123"
- # NER 服务配置
- NER_SERVICE_PORT="8001"
- NER_SERVICE_HOST="localhost"
- # 日志函数
- log_info() { echo -e "${GREEN}[INFO]${NC} $(date '+%Y-%m-%d %H:%M:%S') $1"; }
- log_warn() { echo -e "${YELLOW}[WARN]${NC} $(date '+%Y-%m-%d %H:%M:%S') $1"; }
- log_error() { echo -e "${RED}[ERROR]${NC} $(date '+%Y-%m-%d %H:%M:%S') $1"; }
- log_title() { echo -e "\n${BLUE}========== $1 ==========${NC}\n"; }
- log_step() { echo -e "${CYAN}[STEP]${NC} $1"; }
- # 检查 root 权限
- check_root() {
- if [ "$EUID" -ne 0 ]; then
- log_error "请使用 root 用户运行此脚本"
- exit 1
- fi
- }
- # 检查必要的服务是否运行
- check_services() {
- log_title "检查服务状态"
-
- local all_ok=true
-
- if systemctl is-active --quiet postgresql; then
- log_info "PostgreSQL: 运行中"
- else
- log_warn "PostgreSQL: 未运行"
- all_ok=false
- fi
-
- if systemctl is-active --quiet redis-server; then
- log_info "Redis: 运行中"
- else
- log_warn "Redis: 未运行"
- all_ok=false
- fi
-
- if systemctl is-active --quiet rabbitmq-server; then
- log_info "RabbitMQ: 运行中"
- else
- log_warn "RabbitMQ: 未运行"
- all_ok=false
- fi
-
- if [ "$all_ok" = false ]; then
- log_error "部分服务未运行,请先启动依赖服务"
- return 1
- fi
-
- return 0
- }
- # 确保目录存在
- ensure_directories() {
- mkdir -p ${LOG_DIR}
- mkdir -p ${DATA_DIR}
- mkdir -p ${BACKUP_DIR}
- chmod 755 ${LOG_DIR} ${DATA_DIR} ${BACKUP_DIR}
- }
- # 安装基础依赖
- install_dependencies() {
- log_title "安装基础依赖"
-
- apt update
- apt install -y openjdk-17-jdk maven git curl wget
-
- log_info "Java 版本: $(java -version 2>&1 | head -1)"
- log_info "Maven 版本: $(mvn -version 2>&1 | head -1)"
- }
- # 安装 PostgreSQL + pgvector
- install_postgresql() {
- log_title "安装 PostgreSQL + pgvector"
-
- # 安装 PostgreSQL
- apt install -y postgresql postgresql-contrib postgresql-server-dev-all build-essential
-
- # 编译安装 pgvector
- if [ ! -d "/tmp/pgvector" ]; then
- cd /tmp
- git clone https://github.com/pgvector/pgvector.git
- fi
- cd /tmp/pgvector
- make clean || true
- make
- make install
-
- # 启动服务
- systemctl enable postgresql
- systemctl restart postgresql
-
- log_info "PostgreSQL 已安装并启动"
- }
- # 配置数据库
- setup_database() {
- log_title "配置数据库"
-
- # 创建用户和数据库
- sudo -u postgres psql <<EOF
- DO \$\$
- BEGIN
- IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '${DB_USER}') THEN
- CREATE USER ${DB_USER} WITH PASSWORD '${DB_PASS}';
- END IF;
- END
- \$\$;
- SELECT 'CREATE DATABASE ${DB_NAME} OWNER ${DB_USER}'
- WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '${DB_NAME}')\gexec
- GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} TO ${DB_USER};
- EOF
- # 创建扩展
- sudo -u postgres psql -d ${DB_NAME} <<EOF
- CREATE EXTENSION IF NOT EXISTS vector;
- CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
- EOF
-
- log_info "数据库 ${DB_NAME} 配置完成"
- }
- # 安装 Redis
- install_redis() {
- log_title "安装 Redis"
-
- apt install -y redis-server
- systemctl enable redis-server
- systemctl restart redis-server
-
- log_info "Redis 已安装: $(redis-cli ping)"
- }
- # 安装 RabbitMQ
- install_rabbitmq() {
- log_title "安装 RabbitMQ"
-
- apt install -y rabbitmq-server
- systemctl enable rabbitmq-server
- systemctl restart rabbitmq-server
-
- # 启用管理插件
- rabbitmq-plugins enable rabbitmq_management
-
- # 创建用户
- rabbitmqctl add_user ${MQ_USER} ${MQ_PASS} 2>/dev/null || true
- rabbitmqctl set_user_tags ${MQ_USER} administrator
- rabbitmqctl set_permissions -p / ${MQ_USER} ".*" ".*" ".*"
-
- log_info "RabbitMQ 已安装,管理界面: http://localhost:15672"
- }
- # 安装 Ollama
- install_ollama() {
- log_title "安装 Ollama"
-
- if ! command -v ollama &> /dev/null; then
- curl -fsSL https://ollama.ai/install.sh | sh
- fi
-
- # 创建 Ollama systemd 服务
- if [ ! -f /etc/systemd/system/ollama.service ]; then
- cat > /etc/systemd/system/ollama.service <<EOF
- [Unit]
- Description=Ollama Service
- After=network.target
- [Service]
- Type=simple
- User=root
- ExecStart=/usr/local/bin/ollama serve
- Restart=always
- RestartSec=10
- StandardOutput=append:/var/log/ollama.log
- StandardError=append:/var/log/ollama-error.log
- [Install]
- WantedBy=multi-user.target
- EOF
- systemctl daemon-reload
- fi
-
- systemctl enable ollama
- systemctl restart ollama
- sleep 5
-
- # 下载 Embedding 模型
- if ! ollama list 2>/dev/null | grep -q "nomic-embed-text"; then
- log_info "下载 nomic-embed-text 模型..."
- ollama pull nomic-embed-text
- fi
-
- log_info "Ollama 已安装"
- }
- # 安装 Python 环境(用于 NER 服务)
- install_python_env() {
- log_title "安装 Python 环境"
-
- # 安装 Python 和 pip
- apt install -y python3 python3-pip python3-venv
-
- log_info "Python 版本: $(python3 --version)"
- }
- # 部署 NER Python 服务
- deploy_ner_service() {
- log_title "部署 NER Python 服务"
-
- local ner_dir="${PROJECT_DIR}/python-services/ner-service"
-
- if [ ! -d "${ner_dir}" ]; then
- log_warn "NER 服务目录不存在: ${ner_dir}"
- return 0
- fi
-
- cd ${ner_dir}
-
- # 创建虚拟环境
- if [ ! -d "${PYTHON_VENV_DIR}" ]; then
- log_step "创建 Python 虚拟环境..."
- python3 -m venv ${PYTHON_VENV_DIR}
- fi
-
- # 激活虚拟环境并安装依赖
- log_step "安装 Python 依赖..."
- source ${PYTHON_VENV_DIR}/bin/activate
- pip install --upgrade pip -q
- pip install -r requirements.txt -q
- deactivate
-
- log_info "NER 服务依赖安装完成"
- }
- # 创建 NER 服务的 Systemd 配置
- create_ner_systemd_service() {
- log_title "创建 NER Systemd 服务"
-
- local ner_dir="${PROJECT_DIR}/python-services/ner-service"
-
- if [ ! -d "${ner_dir}" ]; then
- log_warn "NER 服务目录不存在,跳过"
- return 0
- fi
-
- cat > /etc/systemd/system/lingyue-ner.service <<EOF
- [Unit]
- Description=Lingyue NER Service
- After=network.target
- [Service]
- Type=simple
- User=root
- WorkingDirectory=${ner_dir}
- ExecStart=${PYTHON_VENV_DIR}/bin/uvicorn app.main:app --host 0.0.0.0 --port ${NER_SERVICE_PORT}
- Restart=always
- RestartSec=10
- StandardOutput=append:${LOG_DIR}/ner-service.log
- StandardError=append:${LOG_DIR}/ner-service-error.log
- Environment=NER_MODEL=rule
- Environment=LOG_LEVEL=INFO
- Environment=MAX_TEXT_LENGTH=50000
- [Install]
- WantedBy=multi-user.target
- EOF
- systemctl daemon-reload
- log_info "NER Systemd 服务已创建"
- }
- # 启动 NER 服务
- start_ner_service() {
- log_title "启动 NER 服务"
-
- if [ ! -f /etc/systemd/system/lingyue-ner.service ]; then
- log_warn "NER 服务未配置,跳过"
- return 0
- fi
-
- systemctl enable lingyue-ner
- systemctl restart lingyue-ner
-
- sleep 5
-
- if systemctl is-active --quiet lingyue-ner; then
- log_info "NER 服务启动成功: http://localhost:${NER_SERVICE_PORT}"
- else
- log_warn "NER 服务启动失败,查看日志: journalctl -u lingyue-ner -f"
- fi
- }
- # 停止 NER 服务
- stop_ner_service() {
- if systemctl is-active --quiet lingyue-ner; then
- systemctl stop lingyue-ner
- log_info "NER 服务已停止"
- fi
- }
- # 部署项目
- deploy_project() {
- log_title "部署项目"
-
- # 创建目录
- ensure_directories
-
- # 进入项目目录
- if [ ! -d "${PROJECT_DIR}" ]; then
- log_error "项目目录不存在: ${PROJECT_DIR}"
- log_info "请先克隆项目到 ${PROJECT_DIR}"
- exit 1
- fi
-
- cd ${PROJECT_DIR}
-
- # 初始化数据库表
- init_database_tables
-
- # 编译项目
- build_java_project
-
- log_info "项目部署完成"
- }
- # 初始化数据库表
- init_database_tables() {
- log_step "初始化数据库表..."
-
- cd ${PROJECT_DIR}
-
- # 执行初始化脚本
- if [ -f "backend/sql/init.sql" ]; then
- PGPASSWORD=${DB_PASS} psql -U ${DB_USER} -d ${DB_NAME} -h ${DB_HOST} -f backend/sql/init.sql 2>/dev/null || true
- log_info "init.sql 执行完成"
- fi
-
- # 执行 RAG 表脚本
- if [ -f "backend/sql/rag_tables.sql" ]; then
- PGPASSWORD=${DB_PASS} psql -U ${DB_USER} -d ${DB_NAME} -h ${DB_HOST} -f backend/sql/rag_tables.sql 2>/dev/null || true
- log_info "rag_tables.sql 执行完成"
- fi
-
- # 执行补充表脚本
- if [ -f "backend/sql/supplement_tables.sql" ]; then
- PGPASSWORD=${DB_PASS} psql -U ${DB_USER} -d ${DB_NAME} -h ${DB_HOST} -f backend/sql/supplement_tables.sql 2>/dev/null || true
- log_info "supplement_tables.sql 执行完成"
- fi
- }
- # 编译 Java 项目
- build_java_project() {
- log_step "编译 Java 项目..."
-
- cd ${PROJECT_DIR}/backend
-
- # 清理并编译
- mvn clean package -DskipTests -q
-
- if [ -f "lingyue-starter/target/lingyue-starter.jar" ]; then
- log_info "Java 项目编译成功"
- else
- log_error "Java 项目编译失败"
- exit 1
- fi
- }
- # 创建 Systemd 服务
- create_systemd_service() {
- log_title "创建 Systemd 服务"
-
- cat > /etc/systemd/system/lingyue.service <<EOF
- [Unit]
- Description=Lingyue Zhibao Application
- After=network.target postgresql.service redis-server.service rabbitmq-server.service
- [Service]
- Type=simple
- User=root
- WorkingDirectory=${PROJECT_DIR}/backend
- ExecStart=/usr/bin/java -Xms1g -Xmx2g -XX:+UseG1GC -jar lingyue-starter/target/lingyue-starter.jar
- Restart=always
- RestartSec=10
- StandardOutput=append:${LOG_DIR}/lingyue.log
- StandardError=append:${LOG_DIR}/lingyue-error.log
- # Java 环境
- Environment=JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
- Environment=SPRING_PROFILES_ACTIVE=prod
- # 数据库配置
- Environment=DB_HOST=${DB_HOST}
- Environment=DB_PORT=${DB_PORT}
- Environment=DB_NAME=${DB_NAME}
- Environment=DB_USERNAME=${DB_USER}
- Environment=DB_PASSWORD=${DB_PASS}
- # Redis 配置
- Environment=REDIS_HOST=localhost
- Environment=REDIS_PORT=6379
- # RabbitMQ 配置
- Environment=RABBITMQ_HOST=localhost
- Environment=RABBITMQ_PORT=5672
- Environment=RABBITMQ_USERNAME=${MQ_USER}
- Environment=RABBITMQ_PASSWORD=${MQ_PASS}
- # NER 服务配置
- Environment=NER_SERVICE_URL=http://${NER_SERVICE_HOST}:${NER_SERVICE_PORT}
- [Install]
- WantedBy=multi-user.target
- EOF
- systemctl daemon-reload
- log_info "Systemd 服务已创建"
- }
- # 启动应用
- start_app() {
- log_title "启动应用"
-
- systemctl enable lingyue
- systemctl restart lingyue
-
- sleep 10
-
- if systemctl is-active --quiet lingyue; then
- log_info "应用启动成功"
- log_info "访问地址: http://$(hostname -I | awk '{print $1}'):8000"
- else
- log_error "应用启动失败,查看日志: journalctl -u lingyue -f"
- fi
- }
- # 停止应用
- stop_app() {
- log_title "停止应用"
- systemctl stop lingyue
- log_info "应用已停止"
- }
- # 查看状态
- show_status() {
- log_title "服务状态"
-
- printf "%-20s %s\n" "服务" "状态"
- printf "%-20s %s\n" "--------------------" "----------"
- printf "%-20s %s\n" "PostgreSQL" "$(systemctl is-active postgresql 2>/dev/null || echo 'unknown')"
- printf "%-20s %s\n" "Redis" "$(systemctl is-active redis-server 2>/dev/null || echo 'unknown')"
- printf "%-20s %s\n" "RabbitMQ" "$(systemctl is-active rabbitmq-server 2>/dev/null || echo 'unknown')"
- printf "%-20s %s\n" "Lingyue (Java)" "$(systemctl is-active lingyue 2>/dev/null || echo 'unknown')"
- printf "%-20s %s\n" "Lingyue NER" "$(systemctl is-active lingyue-ner 2>/dev/null || echo 'unknown')"
-
- if command -v ollama &> /dev/null && curl -s http://localhost:11434/api/tags > /dev/null 2>&1; then
- printf "%-20s %s\n" "Ollama" "active"
- else
- printf "%-20s %s\n" "Ollama" "inactive"
- fi
-
- echo ""
- local ip=$(hostname -I | awk '{print $1}')
-
- if systemctl is-active --quiet lingyue; then
- log_info "主应用: http://${ip}:8000"
- log_info "Swagger: http://${ip}:8000/swagger-ui.html"
- log_info "健康检查: http://${ip}:8000/actuator/health"
- fi
-
- if systemctl is-active --quiet lingyue-ner; then
- log_info "NER 服务: http://${ip}:${NER_SERVICE_PORT}"
- log_info "NER 健康检查: http://${ip}:${NER_SERVICE_PORT}/health"
- fi
- }
- # 查看日志
- show_logs() {
- journalctl -u lingyue -f
- }
- # 健康检查
- health_check() {
- log_title "健康检查"
-
- echo "=== Java 主应用 ==="
- local health=$(curl -s http://localhost:8000/actuator/health 2>/dev/null)
- if [ -n "$health" ]; then
- echo "$health" | python3 -m json.tool 2>/dev/null || echo "$health"
- else
- log_error "Java 应用未响应"
- fi
-
- echo ""
- echo "=== NER 服务 ==="
- local ner_health=$(curl -s http://localhost:${NER_SERVICE_PORT}/health 2>/dev/null)
- if [ -n "$ner_health" ]; then
- echo "$ner_health" | python3 -m json.tool 2>/dev/null || echo "$ner_health"
- else
- log_warn "NER 服务未响应(可能未部署)"
- fi
- }
- # 备份数据库
- backup_database() {
- log_title "备份数据库"
-
- ensure_directories
-
- local timestamp=$(date '+%Y%m%d_%H%M%S')
- local backup_file="${BACKUP_DIR}/db_backup_${timestamp}.sql"
-
- log_step "备份数据库到 ${backup_file}..."
- PGPASSWORD=${DB_PASS} pg_dump -U ${DB_USER} -h ${DB_HOST} -d ${DB_NAME} > ${backup_file}
-
- # 压缩备份
- gzip ${backup_file}
-
- log_info "数据库备份完成: ${backup_file}.gz"
-
- # 清理 7 天前的备份
- find ${BACKUP_DIR} -name "db_backup_*.sql.gz" -mtime +7 -delete
- log_info "已清理 7 天前的备份"
-
- # 显示备份列表
- echo ""
- echo "现有备份:"
- ls -lh ${BACKUP_DIR}/db_backup_*.sql.gz 2>/dev/null || echo "无备份文件"
- }
- # 恢复数据库
- restore_database() {
- log_title "恢复数据库"
-
- local backup_file=$1
-
- if [ -z "$backup_file" ]; then
- echo "可用的备份文件:"
- ls -1 ${BACKUP_DIR}/db_backup_*.sql.gz 2>/dev/null || echo "无备份文件"
- echo ""
- log_error "请指定备份文件: ./server-deploy.sh restore <backup_file>"
- return 1
- fi
-
- if [ ! -f "$backup_file" ]; then
- log_error "备份文件不存在: $backup_file"
- return 1
- fi
-
- log_warn "警告: 此操作将覆盖现有数据库!"
- read -p "确认恢复? (yes/no): " confirm
-
- if [ "$confirm" != "yes" ]; then
- log_info "操作已取消"
- return 0
- fi
-
- log_step "恢复数据库..."
-
- # 解压并恢复
- if [[ "$backup_file" == *.gz ]]; then
- gunzip -c "$backup_file" | PGPASSWORD=${DB_PASS} psql -U ${DB_USER} -h ${DB_HOST} -d ${DB_NAME}
- else
- PGPASSWORD=${DB_PASS} psql -U ${DB_USER} -h ${DB_HOST} -d ${DB_NAME} < "$backup_file"
- fi
-
- log_info "数据库恢复完成"
- }
- # 清理日志
- clean_logs() {
- log_title "清理日志"
-
- # 清理 30 天前的日志
- find ${LOG_DIR} -name "*.log" -mtime +30 -delete 2>/dev/null || true
-
- # 截断当前日志文件(保留最后 10000 行)
- for logfile in ${LOG_DIR}/*.log; do
- if [ -f "$logfile" ]; then
- tail -n 10000 "$logfile" > "${logfile}.tmp" && mv "${logfile}.tmp" "$logfile"
- fi
- done
-
- log_info "日志清理完成"
-
- # 显示日志目录大小
- echo "日志目录大小:"
- du -sh ${LOG_DIR}
- }
- # 查看所有日志
- show_all_logs() {
- log_title "选择要查看的日志"
-
- echo "1) Java 主应用日志"
- echo "2) Java 错误日志"
- echo "3) NER 服务日志"
- echo "4) NER 错误日志"
- echo "5) Ollama 日志"
- echo "6) 全部日志 (tail)"
-
- read -p "选择 [1-6]: " choice
-
- case $choice in
- 1) tail -f ${LOG_DIR}/lingyue.log ;;
- 2) tail -f ${LOG_DIR}/lingyue-error.log ;;
- 3) tail -f ${LOG_DIR}/ner-service.log ;;
- 4) tail -f ${LOG_DIR}/ner-service-error.log ;;
- 5) tail -f /var/log/ollama.log ;;
- 6) tail -f ${LOG_DIR}/*.log ;;
- *) log_error "无效选择" ;;
- esac
- }
- # 完整安装
- full_install() {
- check_root
- ensure_directories
-
- install_dependencies
- install_python_env
- install_postgresql
- setup_database
- install_redis
- install_rabbitmq
- install_ollama
-
- deploy_project
- deploy_ner_service
-
- create_systemd_service
- create_ner_systemd_service
-
- start_app
- start_ner_service
-
- log_title "部署完成"
- show_status
- }
- # 仅安装基础设施(不部署应用)
- install_infra_only() {
- check_root
- ensure_directories
-
- install_dependencies
- install_python_env
- install_postgresql
- setup_database
- install_redis
- install_rabbitmq
- install_ollama
-
- log_title "基础设施安装完成"
- show_status
- }
- # 仅部署应用(假设基础设施已就绪)
- deploy_app_only() {
- check_root
- check_services || exit 1
-
- deploy_project
- deploy_ner_service
-
- create_systemd_service
- create_ner_systemd_service
-
- start_app
- start_ner_service
-
- log_title "应用部署完成"
- show_status
- }
- # 仅更新代码
- update_only() {
- log_title "更新代码"
-
- cd ${PROJECT_DIR}
-
- # 拉取最新代码
- log_step "拉取最新代码..."
- git fetch --all
- git pull origin dev || git pull origin main || git pull origin master || true
-
- # 编译 Java 项目
- build_java_project
-
- # 更新 NER 服务依赖
- if [ -d "python-services/ner-service" ]; then
- log_step "更新 NER 服务依赖..."
- source ${PYTHON_VENV_DIR}/bin/activate 2>/dev/null || true
- pip install -r python-services/ner-service/requirements.txt -q 2>/dev/null || true
- deactivate 2>/dev/null || true
- fi
-
- # 重启服务
- log_step "重启服务..."
- systemctl restart lingyue
- systemctl restart lingyue-ner 2>/dev/null || true
-
- sleep 10
- health_check
- }
- # 快速重启(不重新编译)
- quick_restart() {
- log_title "快速重启"
-
- systemctl restart lingyue
- systemctl restart lingyue-ner 2>/dev/null || true
-
- sleep 5
- health_check
- }
- # 停止所有服务
- stop_all() {
- log_title "停止所有服务"
-
- systemctl stop lingyue 2>/dev/null || true
- systemctl stop lingyue-ner 2>/dev/null || true
-
- log_info "所有应用服务已停止"
- }
- # 启动所有服务
- start_all() {
- log_title "启动所有服务"
-
- # 先检查基础服务
- check_services || {
- log_warn "部分基础服务未运行,尝试启动..."
- systemctl start postgresql 2>/dev/null || true
- systemctl start redis-server 2>/dev/null || true
- systemctl start rabbitmq-server 2>/dev/null || true
- sleep 3
- }
-
- start_app
- start_ner_service
- }
- # 显示帮助
- show_help() {
- cat <<EOF
- ${BLUE}灵越智报 2.0 - 服务器部署脚本${NC}
- ${CYAN}版本: 2.1.0${NC}
- ${GREEN}用法:${NC} ./server-deploy.sh [命令] [参数]
- ${YELLOW}部署命令:${NC}
- install 完整安装(首次部署,包含所有基础设施)
- install-infra 仅安装基础设施(PostgreSQL, Redis, RabbitMQ, Ollama)
- deploy 仅部署应用(假设基础设施已就绪)
- update 更新代码并重启(git pull + mvn package)
- ${YELLOW}服务控制:${NC}
- start 启动所有应用服务
- stop 停止所有应用服务
- restart 快速重启(不重新编译)
- status 查看所有服务状态
- ${YELLOW}日志与监控:${NC}
- logs 选择查看日志
- health 健康检查(Java + NER)
- ${YELLOW}数据库管理:${NC}
- backup 备份数据库
- restore <file> 恢复数据库(需指定备份文件)
- init-db 重新初始化数据库表
- ${YELLOW}维护命令:${NC}
- clean-logs 清理旧日志文件
- help 显示此帮助
- ${GREEN}示例:${NC}
- # 首次部署
- ./server-deploy.sh install
- # 更新代码并重启
- ./server-deploy.sh update
- # 备份数据库
- ./server-deploy.sh backup
- # 查看服务状态
- ./server-deploy.sh status
- ${YELLOW}目录说明:${NC}
- 项目目录: ${PROJECT_DIR}
- 日志目录: ${LOG_DIR}
- 数据目录: ${DATA_DIR}
- 备份目录: ${BACKUP_DIR}
- ${YELLOW}服务端口:${NC}
- Java 主应用: 8000
- NER 服务: ${NER_SERVICE_PORT}
- PostgreSQL: ${DB_PORT}
- Redis: 6379
- RabbitMQ: 5672 / 15672 (管理界面)
- Ollama: 11434
- EOF
- }
- # 主函数
- main() {
- case "${1:-help}" in
- install)
- full_install
- ;;
- install-infra)
- install_infra_only
- ;;
- deploy)
- deploy_app_only
- ;;
- update)
- update_only
- ;;
- start)
- start_all
- ;;
- stop)
- stop_all
- ;;
- restart)
- quick_restart
- ;;
- status)
- show_status
- ;;
- logs)
- show_all_logs
- ;;
- health)
- health_check
- ;;
- backup)
- backup_database
- ;;
- restore)
- restore_database "$2"
- ;;
- init-db)
- init_database_tables
- ;;
- clean-logs)
- clean_logs
- ;;
- help|--help|-h)
- show_help
- ;;
- *)
- log_error "未知命令: $1"
- show_help
- exit 1
- ;;
- esac
- }
- main "$@"
|