Skip to content

Migrating Data to a New Server

In this article

When You Need to Migrate

You will need to migrate data yourself in the following cases:

  • Changing the location. A VPS cannot be relocated to another location: back up your data, order a new server in the desired location, and transfer the data (more details);
  • Moving to a different plan or hardware that is not covered by the available VPS upgrade options, as well as moving from a VPS to a dedicated server and vice versa;
  • Changing the OS version via a clean installation instead of an in-place upgrade.

Tip

Migrate data and application configuration rather than the entire operating system. Install a fresh OS on the new server via reinstallation in Invapi, install the required software, and then copy the data. This way you avoid problems with drivers, network settings, and disk UUIDs.

Preparation

  1. Order a new server and install the OS and required software (web server, DBMS, Docker, etc.) of the same or compatible versions.
  2. One or two days before the migration, lower the TTL of your domain's DNS records to 300 seconds so that the switch to the new IP address propagates quickly.
  3. Make a list of what needs to be transferred: website directories (/var/www), configuration (/etc/nginx, /etc/apache2, /etc/php), databases, cron jobs, SSL certificates, user home directories.
  4. Set up SSH key access from the old server to the new one:

    ssh-keygen -t ed25519
    ssh-copy-id root@NEW_SERVER_IP
    
  5. Make sure the new server has enough free space: df -h.

Transferring Files with rsync

rsync copies only changed files, so it is convenient to migrate in two stages: an initial copy while services are running, and a final synchronization after they are stopped.

Run on the old server:

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

Options:

  • -a — preserves permissions, ownership, timestamps, and symbolic links;
  • -H, -A, -X — preserve hard links, ACLs, and extended attributes;
  • --numeric-ids — keeps numeric UIDs/GIDs instead of mapping them by name;
  • --delete — deletes files on the destination that no longer exist on the source (use it for the final synchronization).

Note

A trailing slash on the source path (/var/www/) means "copy the contents of the directory". Without it, a nested /var/www/www directory will be created on the destination.

If you still need to copy the whole system, exclude virtual and system-specific directories:

rsync -aHAX --numeric-ids --info=progress2 \
  --exclude={"/proc/*","/sys/*","/dev/*","/run/*","/tmp/*","/mnt/*","/media/*","/lost+found"} \
  --exclude={"/etc/fstab","/etc/network/*","/etc/netplan/*","/etc/sysconfig/network-scripts/*"} \
  --exclude={"/boot/*","/etc/default/grub"} \
  / root@NEW_SERVER_IP:/

Attention

Copying the entire system over a running OS can render the new server unusable if the OS versions differ. Use this method only when the OS versions are identical, and always exclude network settings and /etc/fstab.

Migrating Databases

Do not copy the files of a running DBMS (/var/lib/mysql, /var/lib/postgresql) with rsync — the copy may be corrupted. Use a dump instead:

  • MySQL/MariaDB — see Creating a Database Backup and Restoration;
  • PostgreSQL:

    pg_dumpall -U postgres > all.sql
    rsync -av all.sql root@NEW_SERVER_IP:/root/
    # on the new server:
    psql -U postgres -f /root/all.sql
    

MySQL/MariaDB can also be transferred directly, without an intermediate file:

mysqldump --single-transaction --routines --triggers --all-databases | ssh root@NEW_SERVER_IP "mysql"

Migrating Docker Volumes

  1. Copy the directory containing your docker-compose.yml and .env files to the new server.
  2. Stop the containers on the old server: docker compose down.
  3. Transfer a named volume as an archive:

    docker run --rm -v myvolume:/data -v $(pwd):/backup alpine \
      tar czf /backup/myvolume.tar.gz -C /data .
    rsync -av myvolume.tar.gz root@NEW_SERVER_IP:/root/
    
  4. On the new server, create the volume and extract the archive:

    docker volume create myvolume
    docker run --rm -v myvolume:/data -v /root:/backup alpine \
      tar xzf /backup/myvolume.tar.gz -C /data
    docker compose up -d
    

Bind mounts (host directories) can be transferred with regular rsync.

Full Disk Copy with dd

This method suits dedicated servers when you need an exact copy of a disk. Both servers are booted into Rescue mode so that the disks are not in use. The disk on the new server must be the same size or larger.

Check the disk names on both servers with lsblk, then run on the old server:

dd if=/dev/sda bs=4M status=progress | gzip -1 | ssh root@NEW_SERVER_IP "gunzip | dd of=/dev/sda bs=4M"

Attention

The dd command completely overwrites the target disk. A mistake in the device name will result in irreversible data loss.

After copying, while the new server is still in Rescue mode:

  • mount the root partition and fix the network settings — the IP address, gateway, and network interface name on the new server will be different;
  • check /etc/fstab: dd preserves partition UUIDs, but if you changed the partitioning or RAID layout, update the UUIDs according to the blkid output;
  • if the disk is larger than the original, expand the partition and file system — see How to expand the file system.

Migrating Data on Windows

Use robocopy to copy files. Connect to the new server's network share and run in PowerShell as Administrator:

robocopy D:\Data \\NEW_SERVER_IP\D$\Data /MIR /COPYALL /R:2 /W:5 /MT:16 /LOG:C:\robocopy.log
  • /MIR — mirrors the directory (deletes extra files on the destination);
  • /COPYALL — copies all attributes, including NTFS permissions;
  • /MT:16 — multithreaded copying.

To migrate the entire system, use Windows Server Backup: create a backup on a network share and restore it on the new server after booting from the installation ISO (Mounting an ISO Image on a VPS, Mounting ISO using IPMI):

wbadmin start backup -backupTarget:\\STORAGE_IP\backup -allCritical -include:C: -quiet

Note

Moving to another server may require reactivating the Windows license. Migrate MS SQL databases using SQL Server's own backup and restore tools.

Switching Over and Canceling the Old Server

  1. Stop the services on the old server (web server, applications) so that the data no longer changes.
  2. Run the final rsync synchronization with the --delete option and transfer fresh database dumps.
  3. Check that the website works on the new server, for example by editing the hosts file on your computer.
  4. Point the domain's A records to the new IP address. If your DNS is hosted in Invapi, see DNS hosting.
  5. Restore the original TTL value.
  6. Keep the old server until you are sure everything works correctly and no more requests arrive at the old IP address.
  7. Cancel the old server — see Service (Server) Cancellation.

Attention

After the service is canceled, it may be impossible to recover data from the old server. Before canceling, make sure all data has been transferred and you have a backup.

question_mark
Is there anything I can help you with?
question_mark
AI Assistant ×