ai-chatbot/backup-old-server.sh
2026-05-23 11:09:03 +08:00

119 lines
3.8 KiB
Bash
Executable File
Raw Permalink 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
# 备份旧服务器数据脚本
# 这个脚本只备份数据,不执行迁移
set -e
OLD_SERVER="156.225.27.52"
OLD_PORT="5204"
OLD_USER="root"
OLD_PASS="${DEPLOY_SSH_PASSWORD:?set DEPLOY_SSH_PASSWORD}"
BACKUP_DIR="./migration-backup-$(date +%Y%m%d_%H%M%S)"
echo "=========================================="
echo "AI Chatbot 旧服务器数据备份"
echo "=========================================="
echo "服务器: $OLD_USER@$OLD_SERVER:$OLD_PORT"
echo "备份目录: $BACKUP_DIR"
echo "=========================================="
echo ""
# 创建备份目录
mkdir -p "$BACKUP_DIR"
# 步骤1: 备份数据库
echo "📦 步骤 1/3: 备份数据库..."
sshpass -p "$OLD_PASS" ssh -p "$OLD_PORT" "$OLD_USER@$OLD_SERVER" << 'EOF'
cd /var/www/ai-chatbot
if [ ! -f .env.local ]; then
echo "❌ 未找到 .env.local 文件"
exit 1
fi
# 读取 POSTGRES_URL
POSTGRES_URL=$(grep "^POSTGRES_URL=" .env.local | cut -d '=' -f2- | tr -d '"' | tr -d "'")
if [ -z "$POSTGRES_URL" ]; then
echo "❌ 无法读取 POSTGRES_URL"
exit 1
fi
# 解析数据库连接信息
DB_NAME=$(echo "$POSTGRES_URL" | sed -n 's|.*/\([^?]*\)$|\1|p')
DB_USER=$(echo "$POSTGRES_URL" | sed -n 's|.*://\([^:]*\):.*|\1|p')
DB_PASS=$(echo "$POSTGRES_URL" | sed -n 's|.*://[^:]*:\([^@]*\)@.*|\1|p')
DB_HOST=$(echo "$POSTGRES_URL" | sed -n 's|.*@\([^:]*\):.*|\1|p')
DB_PORT=$(echo "$POSTGRES_URL" | sed -n 's|.*:\([0-9]*\)/.*|\1|p')
if [ -z "$DB_PORT" ]; then
DB_PORT=5432
fi
echo "数据库信息: $DB_USER@$DB_HOST:$DB_PORT/$DB_NAME"
# 创建备份目录
mkdir -p /tmp/ai-chatbot-backup
# 使用 pg_dump 备份数据库
export PGPASSWORD="$DB_PASS"
BACKUP_FILE="/tmp/ai-chatbot-backup/database_backup_$(date +%Y%m%d_%H%M%S).dump"
pg_dump -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -F c -f "$BACKUP_FILE" 2>&1
if [ $? -eq 0 ]; then
echo "✅ 数据库备份完成: $BACKUP_FILE"
ls -lh "$BACKUP_FILE"
else
echo "❌ 数据库备份失败"
exit 1
fi
EOF
# 下载数据库备份
echo ""
echo "📥 步骤 2/3: 下载数据库备份..."
LATEST_BACKUP=$(sshpass -p "$OLD_PASS" ssh -p "$OLD_PORT" "$OLD_USER@$OLD_SERVER" "ls -t /tmp/ai-chatbot-backup/*.dump 2>/dev/null | head -1")
if [ -z "$LATEST_BACKUP" ]; then
echo "❌ 未找到备份文件"
exit 1
fi
echo "正在下载: $LATEST_BACKUP"
sshpass -p "$OLD_PASS" scp -P "$OLD_PORT" "$OLD_USER@$OLD_SERVER:$LATEST_BACKUP" "$BACKUP_DIR/database.dump"
echo "✅ 数据库备份已下载 ($(du -h "$BACKUP_DIR/database.dump" | cut -f1))"
# 下载项目文件和配置
echo ""
echo "📥 步骤 3/3: 下载项目文件..."
sshpass -p "$OLD_PASS" ssh -p "$OLD_PORT" "$OLD_USER@$OLD_SERVER" << 'EOF'
cd /var/www/ai-chatbot
# 创建项目打包(排除 node_modules 和 .next)
tar --exclude='node_modules' \
--exclude='.next' \
--exclude='.git' \
--exclude='*.log' \
-czf /tmp/ai-chatbot-backup/project.tar.gz .
echo "✅ 项目文件打包完成"
EOF
sshpass -p "$OLD_PASS" scp -P "$OLD_PORT" "$OLD_USER@$OLD_SERVER:/tmp/ai-chatbot-backup/project.tar.gz" "$BACKUP_DIR/"
echo "✅ 项目文件已下载 ($(du -h "$BACKUP_DIR/project.tar.gz" | cut -f1))"
# 下载环境变量文件
sshpass -p "$OLD_PASS" scp -P "$OLD_PORT" "$OLD_USER@$OLD_SERVER:/var/www/ai-chatbot/.env.local" "$BACKUP_DIR/.env.local" 2>/dev/null || echo "⚠️ 未找到 .env.local 文件"
echo "✅ 环境变量文件已下载"
echo ""
echo "=========================================="
echo "✅ 备份完成!"
echo "=========================================="
echo "备份目录: $BACKUP_DIR"
echo "包含文件:"
ls -lh "$BACKUP_DIR"
echo ""
echo "📝 下一步:"
echo "1. 确保新服务器 (114.66.31.51) 可以连接"
echo "2. 运行迁移脚本: ./migrate-server.sh"
echo " 或者使用备份文件手动迁移"
echo "=========================================="