Files
chengshishouce/city-manual/scripts/deploy-via-rsync.sh
maoshen 89e8589e87 feat: 支持从环境变量配置数据库
- 添加 os 模块导入
- DEBUG 和 ALLOWED_HOSTS 从环境变量读取
- DATABASE_URL 支持 PostgreSQL 和 SQLite
- 默认使用 SQLite 便于部署
2026-04-12 22:06:04 +00:00

160 lines
4.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.
#!/bin/bash
# 城市手册 - 通过 rsync 部署到云服务器
# 用法:./deploy-via-rsync.sh
set -e
SERVER_HOST="cssc.datalibstar.com"
SERVER_USER="ubuntu"
SERVER_PASS="825670@MashenClaw"
LOCAL_PATH="/root/.openclaw/workspace/city-manual"
REMOTE_PATH="/home/ubuntu/city-manual"
echo "🚀 开始部署城市手册到云服务器..."
echo "服务器:$SERVER_USER@$SERVER_HOST"
echo "目标路径:$REMOTE_PATH"
echo ""
# 检查 sshpass
if ! command -v sshpass &> /dev/null; then
echo "❌ 错误sshpass 未安装"
echo "请运行sudo apt install sshpass"
exit 1
fi
# 1. 创建远程目录
echo "📁 1. 创建远程目录..."
sshpass -p "$SERVER_PASS" ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_HOST << 'EOF'
mkdir -p /home/ubuntu/city-manual
EOF
# 2. 同步代码(排除不需要的项目)
echo "📦 2. 同步代码..."
sshpass -p "$SERVER_PASS" rsync -avz --delete \
--exclude '.git' \
--exclude '__pycache__' \
--exclude '*.pyc' \
--exclude 'venv' \
--exclude 'node_modules' \
--exclude '.env' \
--exclude 'db.sqlite3' \
"$LOCAL_PATH/" \
"$SERVER_USER@$SERVER_HOST:$REMOTE_PATH/"
# 3. 远程部署
echo "🔧 3. 远程配置..."
sshpass -p "$SERVER_PASS" ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_HOST << 'ENDSSH'
set -e
cd /home/ubuntu/city-manual
echo "🐍 创建虚拟环境..."
cd backend
python3 -m venv venv
source venv/bin/activate
echo "📦 安装依赖..."
pip install -r requirements.txt -q
pip install gunicorn -q
echo "🗄️ 配置环境变量..."
cd /home/ubuntu/city-manual/backend
cat > .env << EOF
DJANGO_SETTINGS_MODULE=config.settings.production
DJANGO_SECRET_KEY=cssc-secret-key-$(date +%s)
DEBUG=False
ALLOWED_HOSTS=cssc.datalibstar.com,127.0.0.1,localhost
# 临时使用 SQLite后续配置可访问的 PostgreSQL
DATABASE_URL=sqlite:///db.sqlite3
MEDIA_ROOT=/home/ubuntu/city-manual/backend/media
STATIC_ROOT=/home/ubuntu/city-manual/backend/static
EOF
echo "📦 数据库迁移..."
cd /home/ubuntu/city-manual/backend
source venv/bin/activate
python manage.py migrate --noinput
python manage.py collectstatic --noinput
echo "⚙️ 创建 Gunicorn 服务..."
sudo cat > /etc/systemd/system/city-manual.service << 'EOF'
[Unit]
Description=City Manual Gunicorn Service
After=network.target
[Service]
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/city-manual/backend
ExecStart=/home/ubuntu/city-manual/backend/venv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/city-manual.sock \
city_manual.wsgi:application
Restart=always
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable city-manual
sudo systemctl start city-manual
echo "🌐 配置 Nginx..."
sudo cat > /etc/nginx/sites-available/city-manual << 'EOF'
server {
listen 80;
server_name cssc.datalibstar.com;
access_log /var/log/nginx/city-manual-access.log;
error_log /var/log/nginx/city-manual-error.log;
location /static/ {
alias /home/ubuntu/city-manual/backend/static/;
expires 30d;
add_header Cache-Control "public, immutable";
}
location /media/ {
alias /home/ubuntu/city-manual/backend/media/;
expires 7d;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/city-manual.sock;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
client_max_body_size 10M;
}
EOF
sudo ln -sf /etc/nginx/sites-available/city-manual /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
echo ""
echo "✅ 部署完成!"
echo ""
echo "📍 访问地址http://cssc.datalibstar.com"
echo "📍 Admin: http://cssc.datalibstar.com/admin"
echo ""
echo "📊 服务状态:"
sudo systemctl status city-manual --no-pager | head -5
sudo systemctl status nginx --no-pager | head -5
ENDSSH
if [ $? -eq 0 ]; then
echo ""
echo "🎉 部署成功!"
else
echo ""
echo "❌ 部署失败,请检查错误信息"
exit 1
fi