在 VPS 服务器之间迁移数据,是许多管理员觉得压力山大的工作——担心长时间停机或数据在传输过程中丢失。借助 Linux 标准文件同步工具 rsync,您可以安全、高效地迁移数 GB 的数据。由于 rsync 只传输变更的部分(增量同步),您可以多次运行以逐步完成同步,最终切换时只需极短的维护窗口。

本指南涵盖每一个步骤:配置 SSH 密钥以实现免密登录、使用 rsync 传输网站文件、迁移 MySQL 数据库、转移 Nginx 或 Apache 配置,以及以最短停机时间完成 DNS 切换。本文的操作说明同样适用于 Ubuntu、Debian 以及 CentOS/Rocky Linux。

开始之前了解 rsync

rsync(Remote Sync,远程同步)是 Linux 上的标准命令行工具,可通过 SSH 在两台机器之间同步文件和目录。其核心优势在于"增量传输算法"——它分析源端与目标端之间的差异,仅传输发生变化的部分,而非完整文件。这使得重复同步的速度越来越快,非常适合服务器迁移场景。

与 scp 相比,rsync 在迁移工作中的主要优势:

准备目标 VPS 与 SSH 密钥认证

在运行 rsync 之前,需要在旧 VPS(源端)和新 VPS(目标端)之间配置 SSH 密钥认证,使 rsync 可以无需密码登录。如果您计划通过 cron 定时任务自动同步,此步骤必不可少。

从旧 VPS 向新 VPS 配置 SSH 密钥

# On the old VPS (source) — generate a key pair if you don't have one
ssh-keygen -t ed25519 -C "migration-key" -f ~/.ssh/migration_key -N ""

# Copy the public key to the new VPS
ssh-copy-id -i ~/.ssh/migration_key.pub root@NEW_VPS_IP

# Test that login works without a password prompt
ssh -i ~/.ssh/migration_key root@NEW_VPS_IP "echo OK"

完成上述配置后,所有 rsync 命令均通过 -e "ssh -i ~/.ssh/migration_key" 指定密钥。同时请确保新 VPS 上已安装 rsync:

# Ubuntu / Debian
apt update && apt install -y rsync

# CentOS / Rocky Linux
yum install -y rsync

rsync 常用参数说明

选择正确的参数对迁移结果至关重要。下表列出了最常用的选项:

参数 含义 使用场景
-a 归档模式 — 包含 -rlptgoD 所有迁移操作 — 保留元数据
-v 详细输出 — 列出每个同步的文件 确认正在传输的内容
--progress 显示传输速度及每个文件的进度 传输大文件时
--delete 删除目标端中源端没有的文件 仅用于最终同步 — 谨慎使用
--exclude 跳过指定文件或目录 过滤日志、缓存、临时文件、.git
-z 传输时压缩数据 网络较慢或文本文件较多时
-n 模拟运行 — 显示将要同步的内容 每次正式运行前的测试验证
--partial 保留部分传输的文件以便续传 文件很大或网络不稳定时

第一步 — 同步网站文件(初次同步)

首先将旧 VPS 上的网站文件同步到新 VPS。在旧服务器的服务仍在运行时执行此次初始同步——此阶段您是在复制大量数据,无需停机。暂时不要使用 --delete 参数。

# Initial file sync (run on the old VPS)
rsync -avz --progress \
  --exclude='*.log' \
  --exclude='*.tmp' \
  --exclude='cache/' \
  --exclude='.git/' \
  -e "ssh -i ~/.ssh/migration_key" \
  /var/www/html/ \
  root@NEW_VPS_IP:/var/www/html/

初次同步完成后,对比两端的数据大小,确认所有文件均已传输:

# Check data size on the source
du -sh /var/www/html/

# Check data size on the destination
ssh -i ~/.ssh/migration_key root@NEW_VPS_IP "du -sh /var/www/html/"

第二步 — 迁移 MySQL 数据库

MySQL 数据必须与 rsync 分开迁移。在服务运行时直接复制 MySQL 数据文件存在损坏风险。正确的做法是在源端使用 mysqldump 导出,传输导出文件,再在新服务器上导入。

从旧 VPS 导出所有数据库

# Dump all databases to a single file
mysqldump --all-databases --single-transaction \
  --routines --events --triggers \
  -u root -p > /root/all_databases_backup.sql

# Or dump individual databases (recommended for many databases)
mysqldump --single-transaction --routines \
  -u root -p mywebsite_db > /root/mywebsite_db.sql

将导出文件传输到新 VPS

# Transfer via rsync
rsync -avz --progress \
  -e "ssh -i ~/.ssh/migration_key" \
  /root/all_databases_backup.sql \
  root@NEW_VPS_IP:/root/

# Alternative: pipe directly via SSH (saves local disk space)
mysqldump --all-databases --single-transaction \
  --routines --events --triggers \
  -u root -p | \
  ssh -i ~/.ssh/migration_key root@NEW_VPS_IP \
  "mysql -u root -p"

在新 VPS 上导入数据

# On the new VPS — import the dump
mysql -u root -p < /root/all_databases_backup.sql

# Verify all databases are present
mysql -u root -p -e "SHOW DATABASES;"

重要提示:在新 VPS 上导入前,请确认 MySQL 版本不低于旧服务器的版本。将较新版本 MySQL 创建的导出文件导入较旧版本可能会失败。在两台服务器上分别运行 mysql --version 对比版本后再继续操作。

第三步 — 迁移 Web 服务器配置

迁移 Web 服务器配置文件时需格外谨慎。请注意 SSL 证书路径,以及两台服务器之间可能不同的硬编码 IP 地址或目录路径。

迁移 Nginx 配置

# Transfer Nginx virtual host configs
rsync -avz --progress \
  -e "ssh -i ~/.ssh/migration_key" \
  /etc/nginx/sites-available/ \
  root@NEW_VPS_IP:/etc/nginx/sites-available/

rsync -avz --progress \
  -e "ssh -i ~/.ssh/migration_key" \
  /etc/nginx/snippets/ \
  root@NEW_VPS_IP:/etc/nginx/snippets/

# On the new VPS — enable the site and test config
ssh -i ~/.ssh/migration_key root@NEW_VPS_IP \
  "ln -s /etc/nginx/sites-available/mysite.conf \
  /etc/nginx/sites-enabled/ && nginx -t"

迁移 Apache 配置

# Transfer VirtualHost configs
rsync -avz --progress \
  -e "ssh -i ~/.ssh/migration_key" \
  /etc/apache2/sites-available/ \
  root@NEW_VPS_IP:/etc/apache2/sites-available/

# On the new VPS — enable site and check syntax
ssh -i ~/.ssh/migration_key root@NEW_VPS_IP \
  "a2ensite mysite.conf && apache2ctl configtest"

迁移 Let's Encrypt SSL 证书

# Transfer all Certbot certificate data
rsync -avz --progress \
  -e "ssh -i ~/.ssh/migration_key" \
  /etc/letsencrypt/ \
  root@NEW_VPS_IP:/etc/letsencrypt/

# On the new VPS — verify certificates
ssh -i ~/.ssh/migration_key root@NEW_VPS_IP \
  "certbot certificates"

DNS 切换完成后,在新 VPS 上运行 certbot renew --dry-run,确认自动续期功能正常。Let's Encrypt 的 HTTP 验证需要 DNS 已指向新 IP 才能成功完成验证。

第四步 — 最终同步与正式切换

最终同步确保切换前两台服务器的数据完全一致。这一步包含实际的停机时间——但由于 rsync 只传输变更数据,如果初次同步是最近完成的,通常几分钟内即可结束。

# Final cutover sequence (run in order)

# 1. Stop the web server on the old VPS
systemctl stop nginx
# or: systemctl stop apache2

# 2. Flush and take a final MySQL dump
mysql -u root -p -e "FLUSH TABLES WITH READ LOCK;"
mysqldump --all-databases --single-transaction \
  --routines --events --triggers \
  -u root -p > /root/final_dump.sql
mysql -u root -p -e "UNLOCK TABLES;"

# 3. Final rsync with --delete to achieve exact mirror
rsync -avz --progress --delete \
  --exclude='*.log' \
  --exclude='*.tmp' \
  --exclude='cache/' \
  -e "ssh -i ~/.ssh/migration_key" \
  /var/www/html/ \
  root@NEW_VPS_IP:/var/www/html/

# 4. Import the final dump on the new VPS
ssh -i ~/.ssh/migration_key root@NEW_VPS_IP \
  "mysql -u root -p < /root/final_dump.sql"

# 5. Start services on the new VPS
ssh -i ~/.ssh/migration_key root@NEW_VPS_IP \
  "systemctl start nginx && systemctl start php8.2-fpm"

切换前验证清单

在修改 DNS 之前,通过临时编辑本地机器的 /etc/hosts 文件,将域名解析到新 VPS 的 IP,先在新 VPS 上测试网站:

# Add to /etc/hosts on your local machine (not on the VPS)
# Replace NEW_VPS_IP with the actual IP of the new server
NEW_VPS_IP  mydomain.com  www.mydomain.com

DNS 切换前需验证的事项:

DNS 切换与迁移后验证

所有检查项通过后,建议提前 24–48 小时降低 DNS 记录的 TTL 值以加速解析生效。然后更新 A 记录,将其指向新 VPS 的 IP 地址。通过 dig 命令监控解析生效情况:

# Verify DNS propagation after the change
dig +short mydomain.com A
nslookup mydomain.com 8.8.8.8

# Check from multiple resolvers
dig mydomain.com @1.1.1.1 A +short
dig mydomain.com @8.8.8.8 A +short

切换完成后,请至少保留旧 VPS 运行 7 天,作为回滚的保障,同时留有时间找回可能遗漏的数据。确认新服务器完全正常且稳定运行后,再将旧服务器下线。

常见问题解答

迁移 VPS 时,rsync 和 scp 有什么区别?

rsync 只传输变更数据(增量同步),非常适合重复运行——每次后续同步只发送新增或修改的文件,速度更快。它还支持 --exclude 排除模式,并通过 -a 参数完整保留权限、所有者和时间戳。scp 每次都复制完整文件,没有增量检测,只适合小规模的一次性传输。对于涉及数 GB 数据的 VPS 迁移,rsync 是更好的选择,因为您可以在简短的维护窗口期内运行最终同步,将停机时间降到最低。

运行 rsync 前需要停止旧 VPS 上的服务吗?

初次同步不需要停止服务。在服务运行的情况下执行第一次 rsync,可以在不中断服务的前提下传输大量数据。只有在执行最终切换同步时,才需要停止 Nginx/Apache 和 MySQL,然后再运行一次 rsync,同步自初次同步以来的变更内容。这种方式将总停机时间控制在最终同步和 DNS 传播期间的短短几分钟内。

rsync 迁移时会保留文件权限和所有者吗?

是的,使用 -a(归档)参数时,rsync 会保留权限(-p)、用户组(-g)、所有者(-o)和时间戳(-t)。但保留所有者信息需要在目标服务器上以 root 身份运行 rsync。如果以非 root 用户运行,rsync 将使用当前用户的身份。同步完成后,通过在两台服务器上执行 ls -la 对比,确认权限和所有者信息一致。

切换 DNS 之前需要在新 VPS 上检查哪些内容?

DNS 切换前至少需要验证五项内容:(1) 通过临时修改本地机器的 /etc/hosts 文件,将域名指向新 IP,测试网站是否正常;(2) 确认 MySQL 数据库和用户已存在且可以正常连接;(3) 对所有服务运行 systemctl status,确认均处于 active 状态;(4) 检查 Nginx 或 Apache 错误日志,确认无 500 错误;(5) 确认新 VPS 上已配置 SSL 证书自动续期脚本(cron)。所有检查项通过后再执行 DNS 变更。

AsiaGB 高性能 KVM VPS

AsiaGB VPS 提供完整 root 权限——Docker、MySQL、Python、Node.js 开箱即用。月付低至 500 泰铢起。

查看 VPS 套餐

查看泰国VPS主机全部套餐 →