Files
jinniu-daikuan/manage.sh
T
梁铭洋andCursor e90756f7a3 Add Ubuntu Docker manage.sh menu for start/stop/port.
Provide an interactive script and .env.example so operators can change APP_PORT and run common compose actions without memorizing commands.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-29 15:17:26 +08:00

225 lines
5.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 金牛集团贷款档案管理系统 - Docker 管理脚本(Ubuntu
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$ROOT_DIR"
ENV_FILE="$ROOT_DIR/.env"
COMPOSE=(docker compose)
CONTAINER_NAME="baota-archive"
# ---------- 基础工具 ----------
need_cmd() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "缺少命令:$1"
echo "Ubuntu 可先安装:sudo apt update && sudo apt install -y docker.io docker-compose-v2"
exit 1
fi
}
ensure_env() {
if [[ ! -f "$ENV_FILE" ]]; then
cat > "$ENV_FILE" <<'EOF'
# 网站对外端口(宿主机)
APP_PORT=8080
EOF
fi
}
get_port() {
ensure_env
# shellcheck disable=SC1090
source "$ENV_FILE"
echo "${APP_PORT:-8080}"
}
set_port() {
local port="$1"
if ! [[ "$port" =~ ^[0-9]+$ ]] || (( port < 1 || port > 65535 )); then
echo "端口无效,请输入 1-65535 之间的数字。"
return 1
fi
ensure_env
if grep -qE '^APP_PORT=' "$ENV_FILE"; then
sed -i.bak "s/^APP_PORT=.*/APP_PORT=${port}/" "$ENV_FILE"
rm -f "${ENV_FILE}.bak"
else
echo "APP_PORT=${port}" >> "$ENV_FILE"
fi
echo "已保存端口:${port}(写入 .env"
}
show_status() {
local port
port="$(get_port)"
echo "----------------------------------------"
echo "项目目录:$ROOT_DIR"
echo "当前端口:$port"
echo "访问地址:http://服务器IP:${port}"
echo "容器名称:$CONTAINER_NAME"
echo "----------------------------------------"
"${COMPOSE[@]}" ps || true
}
pause() {
echo
read -r -p "按回车返回菜单..." _
}
# ---------- 功能 ----------
do_start() {
local port
port="$(get_port)"
echo "正在构建并启动(端口 ${port}..."
APP_PORT="$port" "${COMPOSE[@]}" up -d --build
echo
echo "启动完成。访问:http://服务器IP:${port}"
echo "默认账号:admin / jinniu123(登录后请改密)"
}
do_stop() {
echo "正在停止并移除容器..."
"${COMPOSE[@]}" down
echo "已停止。"
}
do_restart() {
local port
port="$(get_port)"
echo "正在重启(端口 ${port}..."
APP_PORT="$port" "${COMPOSE[@]}" up -d --build
echo "重启完成。"
}
do_logs() {
echo "查看日志(Ctrl+C 退出)..."
"${COMPOSE[@]}" logs -f --tail=200
}
do_change_port() {
local current new_port
current="$(get_port)"
echo "当前端口:${current}"
read -r -p "请输入新端口(例如 80 或 8080):" new_port
set_port "$new_port" || return 0
echo
read -r -p "是否立即按新端口重启服务?[Y/n] " ans
ans="${ans:-Y}"
if [[ "$ans" =~ ^[Yy]$ ]]; then
do_restart
else
echo "已改端口,但未重启。下次启动会使用新端口。"
fi
}
do_pull() {
if [[ ! -d "$ROOT_DIR/.git" ]]; then
echo "当前目录不是 git 仓库,无法 pull。"
return 0
fi
echo "正在拉取代码更新..."
echo "提示:远程地址请用不带 .git 后缀的 URL(反代会拦截 .git)。"
git pull
echo
read -r -p "代码已更新,是否重新构建并启动?[Y/n] " ans
ans="${ans:-Y}"
if [[ "$ans" =~ ^[Yy]$ ]]; then
do_restart
fi
}
do_help_cmds() {
local port
port="$(get_port)"
cat <<EOF
======== 本站 Docker 常用命令(也可直接在终端敲)========
进入项目目录:
cd $ROOT_DIR
启动 / 构建:
docker compose up -d --build
停止:
docker compose down
重启(改端口后常用):
APP_PORT=${port} docker compose up -d --build
看状态:
docker compose ps
看日志:
docker compose logs -f
进容器(排查用):
docker exec -it ${CONTAINER_NAME} bash
改端口(写入 .env 后重启):
echo 'APP_PORT=8080' > .env
docker compose up -d --build
拉代码更新:
git pull
docker compose up -d --build
数据目录(勿删):
${ROOT_DIR}/data # SQLite + JSON
${ROOT_DIR}/uploads # 上传附件
======================================================
EOF
}
# ---------- 主菜单 ----------
main() {
need_cmd docker
if ! docker compose version >/dev/null 2>&1; then
echo "未检测到 docker composev2)。"
echo "Ubuntu 可安装:sudo apt install -y docker-compose-v2"
exit 1
fi
ensure_env
while true; do
clear 2>/dev/null || true
local port
port="$(get_port)"
cat <<EOF
========================================
金牛贷款档案系统 · Docker 管理
========================================
当前端口:${port}
访问示例:http://服务器IP:${port}
----------------------------------------
1) 一键启动(构建并后台运行)
2) 停止服务
3) 重启服务
4) 查看运行状态
5) 查看日志
6) 修改启动端口
7) 拉取代码更新(git pull
8) 显示常用命令说明
0) 退出
========================================
EOF
read -r -p "请选择 [0-8]" choice
echo
case "$choice" in
1) do_start; pause ;;
2) do_stop; pause ;;
3) do_restart; pause ;;
4) show_status; pause ;;
5) do_logs; pause ;;
6) do_change_port; pause ;;
7) do_pull; pause ;;
8) do_help_cmds; pause ;;
0) echo "已退出。"; exit 0 ;;
*) echo "无效选项。"; pause ;;
esac
done
}
main "$@"