Files

189 lines
5.4 KiB
Bash
Raw Permalink Normal View History

2026-07-03 16:01:08 +08:00
#!/bin/bash
# ============================================
2026-07-10 17:33:33 +08:00
# 文件管理系统 - 本地构建脚本
# 作用:只构建部署产物,不依赖服务器环境
2026-07-03 16:01:08 +08:00
# ============================================
set -e
DEPLOY_DIR="./deploy"
echo "============================================"
2026-07-10 17:33:33 +08:00
echo " 本地构建部署产物"
2026-07-03 16:01:08 +08:00
echo "============================================"
2026-07-10 17:33:33 +08:00
# 清理旧包
2026-07-03 16:01:08 +08:00
[ -d "$DEPLOY_DIR" ] && rm -rf "$DEPLOY_DIR"
2026-07-10 17:33:33 +08:00
# 创建目录结构
echo "[1/4] 创建目录结构..."
mkdir -p "$DEPLOY_DIR/data/storage/{hot,cold,public,slave}"
2026-07-03 16:01:08 +08:00
2026-07-10 17:33:33 +08:00
# 编译(本地编译,去掉调试信息)
echo "[2/4] 编译后端..."
2026-07-03 16:01:08 +08:00
export GOPROXY=https://goproxy.cn,direct
2026-07-10 17:33:33 +08:00
go build -ldflags="-s -w" -o "$DEPLOY_DIR/server" main.go
2026-07-03 16:01:08 +08:00
2026-07-10 17:33:33 +08:00
# 复制配置与SQL
echo "[3/4] 复制配置文件..."
cp config.yaml "$DEPLOY_DIR/config.yaml.example"
cp -r sql "$DEPLOY_DIR/"
2026-07-03 16:01:08 +08:00
2026-07-10 17:33:33 +08:00
# 生成启动脚本、数据库初始化脚本、systemd服务
echo "[4/4] 生成启动/服务脚本..."
2026-07-03 16:01:08 +08:00
2026-07-10 17:33:33 +08:00
# 启动脚本里面会自动安装Go
cat > "$DEPLOY_DIR/start.sh" <<'STARTEOF'
2026-07-03 16:01:08 +08:00
#!/bin/bash
2026-07-10 17:33:33 +08:00
set -e
cd "$(dirname "$0")"
RUN_DIR=$(pwd)
echo "============================================"
echo " 服务启动脚本自动安装Go环境"
echo "============================================"
# ========== Go 版本配置 ==========
GO_VERSION="1.23.6"
INSTALL_DIR="/usr/local"
GO_TAR_DIR="${INSTALL_DIR}/go"
GO_BIN="${GO_TAR_DIR}/bin/go"
# ========== 安装/加载 Go ==========
install_go() {
echo "[1/3] 准备 Go ${GO_VERSION} 环境..."
ARCH=$(uname -m)
case $ARCH in
x86_64) GO_ARCH="amd64" ;;
aarch64) GO_ARCH="arm64" ;;
*) echo "不支持的架构: $ARCH"; exit 1 ;;
esac
GO_TAR="go${GO_VERSION}.linux-${GO_ARCH}.tar.gz"
GO_URL_MIRROR="https://golang.google.cn/dl/${GO_TAR}"
GO_URL_OFFICIAL="https://go.dev/dl/${GO_TAR}"
echo "下载 Go ${GO_VERSION}..."
DOWNLOAD_OK=0
for URL in "$GO_URL_MIRROR" "$GO_URL_OFFICIAL"; do
echo "尝试: $URL"
if command -v curl &>/dev/null; then
curl -L --connect-timeout 15 --retry 2 -o "/tmp/${GO_TAR}" "$URL" && DOWNLOAD_OK=1 && break
else
wget --timeout=15 -O "/tmp/${GO_TAR}" "$URL" && DOWNLOAD_OK=1 && break
fi
done
if [ "$DOWNLOAD_OK" -eq 0 ] || [ ! -s "/tmp/${GO_TAR}" ]; then
echo "下载失败,请手动安装 Go"
exit 1
fi
echo "解压..."
rm -rf "$GO_TAR_DIR"
tar -C "$INSTALL_DIR" -xzf "/tmp/${GO_TAR}"
rm -f "/tmp/${GO_TAR}"
}
# 检查是否已有Go
if ! command -v go &>/dev/null; then
if [ -f "$GO_BIN" ]; then
export PATH=$PATH:$GO_TAR_DIR/bin
else
install_go
export PATH=$PATH:$GO_TAR_DIR/bin
fi
fi
echo "[2/3] Go 版本: $(go version)"
# 检查配置文件
echo "[3/3] 检查配置..."
if [ ! -f "config.yaml" ]; then
echo "未找到 config.yaml从模板复制..."
cp config.yaml.example config.yaml
echo "请编辑 config.yaml 后重新启动"
exit 1
fi
echo "启动服务..."
echo "工作目录: $RUN_DIR"
2026-07-03 16:01:08 +08:00
./server
2026-07-10 17:33:33 +08:00
STARTEOF
2026-07-03 16:01:08 +08:00
chmod +x "$DEPLOY_DIR/start.sh"
2026-07-10 17:33:33 +08:00
# 数据库初始化脚本
cat > "$DEPLOY_DIR/init-db.sh" <<'DBEOF'
2026-07-03 16:01:08 +08:00
#!/bin/bash
2026-07-10 17:33:33 +08:00
cd "$(dirname "$0")"
echo "============================================"
echo " 数据库初始化"
echo "============================================"
echo "1) MySQL"
echo "2) PostgreSQL"
echo "3) SQL Server"
read -p "请选择数据库类型 [1/2/3]: " DB_TYPE
case $DB_TYPE in
1)
read -sp "MySQL root 密码: " DB_PWD; echo
read -p "数据库名 [seeyon_fs]: " DB_NAME; DB_NAME=${DB_NAME:-seeyon_fs}
mysql -u root -p"$DB_PWD" -e "CREATE DATABASE IF NOT EXISTS ${DB_NAME} DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
mysql -u root -p"$DB_PWD" "$DB_NAME" < sql/mysql/init.sql
echo "MySQL 初始化完成: $DB_NAME"
;;
2)
read -sp "PostgreSQL 密码: " DB_PWD; echo
read -p "数据库名 [seeyon_fs]: " DB_NAME; DB_NAME=${DB_NAME:-seeyon_fs}
export PGPASSWORD="$DB_PWD"
psql -U postgres -c "CREATE DATABASE $DB_NAME;" 2>/dev/null || true
psql -U postgres -d "$DB_NAME" -f sql/postgres/init.sql
unset PGPASSWORD
echo "PostgreSQL 初始化完成: $DB_NAME"
;;
3)
read -sp "SQL Server 密码: " DB_PWD; echo
read -p "数据库名 [seeyon_fs]: " DB_NAME; DB_NAME=${DB_NAME:-seeyon_fs}
sqlcmd -S localhost -U sa -P "$DB_PWD" -Q "CREATE DATABASE [$DB_NAME]" 2>/dev/null || true
sqlcmd -S localhost -U sa -P "$DB_PWD" -d "$DB_NAME" -i sql/sqlserver/init.sql
echo "SQL Server 初始化完成: $DB_NAME"
;;
*)
echo "无效选择"
exit 1
;;
esac
DBEOF
2026-07-03 16:01:08 +08:00
chmod +x "$DEPLOY_DIR/init-db.sh"
2026-07-10 17:33:33 +08:00
# systemd服务
cat > "$DEPLOY_DIR/filesystem.service" <<SVCEOF
2026-07-03 16:01:08 +08:00
[Unit]
2026-07-10 17:33:33 +08:00
Description=Seeyon File Management System
After=network.target
2026-07-03 16:01:08 +08:00
[Service]
Type=simple
User=root
2026-07-10 17:33:33 +08:00
WorkingDirectory=%d
ExecStart=%d/start.sh
2026-07-03 16:01:08 +08:00
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
2026-07-10 17:33:33 +08:00
SVCEOF
2026-07-03 16:01:08 +08:00
echo ""
echo "============================================"
2026-07-10 17:33:33 +08:00
echo " 构建完成!产物目录:$DEPLOY_DIR"
2026-07-03 16:01:08 +08:00
echo "============================================"
2026-07-10 17:33:33 +08:00
echo "只需上传 deploy 目录到 Linux 服务器即可"
2026-07-03 16:01:08 +08:00
echo ""
2026-07-10 17:33:33 +08:00
echo "服务器上操作:"
echo " cd deploy"
echo " chmod +x start.sh init-db.sh"
echo " ./init-db.sh # 初始化数据库"
echo " ./start.sh # 自动装Go并启动"
echo ""