Management Guides

How to Migrate From One VPS to Another Without Losing Data

A provider-neutral runbook for moving a site to a new VPS: inventory, DNS TTL, two-pass rsync, database dumps, private testing, a short cutover and a rollback plan.

Moving a site to a new VPS goes wrong in predictable ways: data written to the old server after the copy is lost, visitors reach the old machine for a day because of DNS caching, or something small such as a cron job or a mail setting is forgotten. This runbook avoids all three. The idea is to build and test the new server while the old one keeps serving, then switch in a short, planned window, with a way back.

Commands are given for Ubuntu 26.04/24.04 and Debian 13 (apt) and for AlmaLinux and Rocky Linux 9 and 10 (dnf). They are standard tools from each distribution; this guide is a reference and was not run on a test server for this article.

Overview

PhaseWhenSite status
1. Inventory and DNS preparationTwo or more days beforeLive on old server
2. Build the new serverAny timeLive on old server
3. First copyAny timeLive on old server
4. Test the new server privatelyAny timeLive on old server
5. CutoverA quiet hourRead-only for a few minutes
6. Watch, then retire the old serverThe following weekLive on new server

1. Inventory and DNS preparation

Write down everything the old server does. The items people forget:

sudo ss -tulpn                              # every service that listens
systemctl list-unit-files --state=enabled   # what starts at boot
sudo crontab -l; crontab -l; ls /etc/cron.d # scheduled jobs
systemctl list-timers
php -m; php -v                              # runtime version and extensions
mysql -e "SHOW DATABASES; SELECT user, host FROM mysql.user;"
ls /etc/letsencrypt/live 2>/dev/null        # certificates

Also note: mail handling, firewall rules, outgoing connections that other services allow only from the old IP address (payment gateways, APIs, remote databases), and any place where the old IP is written into configuration.

Lower the DNS TTL now. Set the TTL of the records you will change to 300 seconds. Resolvers keep the old value until the previous TTL has passed, so do this at least a day before the move. It is what turns a day of split traffic into five minutes.

2. Build the new server

Set it up as you would any new machine: updates, user, SSH keys, firewall (see initial server setup). Install the same major versions of the web server, runtime and database as the old server. A migration is a poor moment to discover that the application does not run on a newer PHP or MySQL; upgrade versions as a separate step, before or after.

Create the same system users and, where possible, the same user and group IDs for the accounts that own website files.

3. First copy

Run these on the new server, pulling from the old one. Give the new server's SSH key access to the old server first.

rsync -aHAX --numeric-ids --info=progress2 root@OLD_IP:/var/www/ /var/www/

The trailing slashes matter: they copy the contents of the folder into the folder. -a keeps permissions, owners and times; -H, -A and -X keep hard links, ACLs and extended attributes.

Databases travel as dumps, not as copied files:

# on the old server
mysqldump --single-transaction --routines --triggers --events --databases shop blog | gzip > /root/db.sql.gz

# on the new server
scp root@OLD_IP:/root/db.sql.gz /root/
gunzip -c /root/db.sql.gz | mysql

Recreate the database users and their privileges on the new server; a dump made with --databases does not include them. On the old server, SHOW GRANTS FOR 'user'@'localhost'; prints what to recreate. For PostgreSQL use pg_dump -Fc and pg_restore, and pg_dumpall --roles-only for the roles.

Copy configuration selectively. Take your virtual host files, PHP pool settings and application config; do not overwrite the whole of /etc, which contains machine-specific files.

4. Test the new server privately

You can request the site from the new server without touching DNS:

curl -sI --resolve example.com:443:NEW_IP https://example.com/

For a browser test, add a line to the hosts file on your own computer (/etc/hosts, or C:\Windows\System32\drivers\etc\hosts):

NEW_IP  example.com www.example.com

HTTPS needs a certificate on the new server before DNS points to it. Either copy /etc/letsencrypt from the old server, or issue a new certificate with a DNS challenge.

Click through everything that matters: log in, submit a form, upload a file, place a test order, open the admin area. Check the web server and application error logs on the new machine while you do. Remove the hosts line afterwards.

5. Cutover

Choose a quiet hour and work through this list in order.

  1. Stop writes on the old server. Put the application in maintenance mode, or stop the services that write data (the application, cron jobs, queue workers). Leave the database running for the dump.
  2. Final database dump and import, exactly as in step 3. Drop and recreate the databases on the new server first so that nothing stale remains.
  3. Final file sync. The same rsync with --delete added, so files removed on the old server since the first copy are removed on the new one too:
    rsync -aHAX --numeric-ids --delete root@OLD_IP:/var/www/ /var/www/
    Check the paths twice before running any rsync with --delete.
  4. Start services on the new server and repeat the curl --resolve test.
  5. Change DNS to the new IP address (A and AAAA records).
  6. Keep the old server in maintenance mode, not live. Visitors still arriving there see a maintenance page instead of writing data that will be lost. If you can, proxy the old server's traffic to the new one for the transition.

6. Watch, then retire

  • Tail the access log on the old server. When requests stop arriving, DNS has moved.
  • Check on the new server: certificate renewal (sudo certbot renew --dry-run), cron jobs and timers, outgoing mail, backups pointing at the new machine, monitoring updated to the new IP.
  • Tell third parties that restrict access by IP address about the new one.
  • Raise the DNS TTL back to its normal value after a day or two.
  • Keep the old server, switched off or in maintenance mode, for at least a week. Take a final snapshot or backup before cancelling it.

Rollback

If something is badly wrong after the switch, point DNS back to the old IP and take the old server out of maintenance mode. With a 300-second TTL the return takes minutes. Any data written on the new server in between has to be carried back by hand, which is why the decision should be made quickly: test thoroughly in the first half hour, then commit.

Email needs extra care

If the server sends mail, the new IP address needs a matching reverse DNS (PTR) record, set in the provider's panel, and your SPF record must include it. Many providers block outgoing port 25 on new accounts until you ask. If the server stores mailboxes, sync them with a tool made for it; this site has a guide to imapsync.

Frequently asked questions

How long does it take to migrate a VPS?

The preparation takes a few hours spread over several days. The cutover itself, done this way, usually means minutes of read-only time. Large databases and file sets lengthen the final sync.

Can I migrate without downtime?

For a site that only reads data, yes. For a site that accepts writes, a short read-only window is the safe way to guarantee nothing is lost. Zero-downtime moves need database replication between the two servers.

Can I just copy the whole disk?

Between different providers this rarely works cleanly, because network settings, drivers and boot configuration differ. Copying data onto a freshly built server is more reliable and leaves you with a clean system.

Should I upgrade the operating system at the same time?

Moving to a new server is a good opportunity to start on a current release, and that is safer than an in-place upgrade. Keep the application runtime (PHP, database) at the versions the application is known to work with, and upgrade those separately.

Before you start, make sure you have a current backup: see VPS backup strategy.

Share story:

Continue reading other guides on How VPS.