# 05 · 部署与运维 ## 1. 部署脚本 ### 1.1 deploy.sh ```bash # 后端部署 bash deploy.sh server # → rsync 同步代码到服务器 # → npm install # → pm2 restart sbrain-server # 前端部署 bash deploy.sh front # → npm run build # → scp dist/* 到服务器 /var/www/dm.all8ai.top ``` ### 1.2 服务器环境 | 配置项 | 值 | |--------|-----| | 服务器IP | 152.136.182.184 | | SSH用户 | ubuntu | | 后端目录 | /opt/sbrain-server | | 前端目录 | /var/www/dm.all8ai.top | | 后端端口 | 9333 | | PM2进程名 | sbrain-server | | Node版本 | 20+ | ## 2. 数据库连接 ### 2.1 连接配置 | 用途 | 地址 | 端口 | 数据库 | 用户 | 密码 | |------|------|------|--------|------|------| | 业务数据 | 127.0.0.1 (FRP) | 15432 | bill_query | freedak | (空) | | 平台管理 | 127.0.0.1 | 5432 | sbrain_admin | sbrain_admin | sbrain2026 | ### 2.2 FRP隧道 ```toml [[proxies]] name = "dm-db" type = "tcp" localIP = "127.0.0.1" localPort = 5432 remotePort = 15432 ``` - macOS launchd 系统级服务,开机自启 - 管理命令: - 启动:`launchctl load ~/Library/LaunchAgents/com.frp.client.plist` - 停止:`launchctl bootout gui/$(id -u)/com.frp.client` - 日志:`tail -f ~/Library/Logs/frpc.log` ### 2.3 远程数据库操作 ```bash # 通过FRP隧道查询 ssh ubuntu@152.136.182.184 "PGPASSWORD= psql -h 127.0.0.1 -p 15432 -U freedak -d bill_query -c \"SQL\"" # 本地直接查询 psql -h 127.0.0.1 -p 5432 -U freedak -d bill_query -c "SQL" ``` ## 3. 数据库备份 ```bash # 本地备份 pg_dump -h 127.0.0.1 -p 5432 -U freedak bill_query > backups/bill_query_$(date +%Y%m%d).sql # 恢复 psql -h 127.0.0.1 -p 5432 -U freedak -d bill_query < backups/bill_query_YYYYMMDD.sql ``` ## 4. PM2 进程管理 ```bash # 查看状态 ssh ubuntu@152.136.182.184 "pm2 status" # 查看日志 ssh ubuntu@152.136.182.184 "pm2 logs sbrain-server --lines 50" # 重启 ssh ubuntu@152.136.182.184 "pm2 restart sbrain-server" # 保存进程列表 ssh ubuntu@152.136.182.184 "pm2 save" ``` ## 5. 部署前检查清单 - [ ] 数据库已备份 - [ ] 代码已 commit + push - [ ] FRP隧道正常运行(`pgrep -l frpc`) - [ ] 后端本地可正常启动 - [ ] 前端本地可正常构建 - [ ] 部署后API健康检查(`curl -s https://dm.all8ai.top/api/health`) ## 6. 常用运维命令 ```bash # API健康检查 curl -s https://dm.all8ai.top/api/health # 登录获取Token TOKEN=$(curl -s 'https://dm.all8ai.top/api/auth/login' \ -H 'Content-Type: application/json' \ -d '{"username":"总部管理员","password":"123"}' \ | python3 -c "import sys,json;print(json.load(sys.stdin)['data']['token'])") # 测试API curl -s "https://dm.all8ai.top/api/xxx?month=2026-04" \ -H "Authorization: Bearer $TOKEN" | python3 -m json.tool # 查看物化视图列表 ssh ubuntu@152.136.182.184 "PGPASSWORD= psql -h 127.0.0.1 -p 15432 -U freedak -d bill_query -c \"SELECT schemaname, matviewname FROM pg_matviews ORDER BY 1,2\"" # 查看表结构 ssh ubuntu@152.136.182.184 "PGPASSWORD= psql -h 127.0.0.1 -p 15432 -U freedak -d bill_query -c \"SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'xxx' ORDER BY ordinal_position\"" ```