Skip to main content

Migrate Linux server data to a Selectel cloud server using rsync

You can migrate data from a loaded Linux server to a Selectel cloud server using the rsync utility. Migrating data using the rsync utility is suitable when the disk sizes or partitioning on the old and new servers differ.

rsync copies files rather than the whole disk. This helps avoid transferring hidden corruption that can occur during byte-by-byte copying of a disk with active writes. Learn more about migrating data via disk copying in the Migrate Linux server data to a Selectel cloud server using dd instructions.

The bootloader, fstab, and network settings of the cloud server can be used as on the source server. Service downtime occurs only during the final synchronization and switching between servers. Running rsync again transfers only changed files, so the final synchronization before switching between servers takes little time.

Migrate data

  1. Create a cloud server.

  2. Prepare data for migration.

  3. Migrate data.

  4. Check and switch services.

1. Create a cloud server

Use the Create a cloud server instructions. When creating the server:

  • as the source, select the same OS version as on the source server;
  • select a disk size sufficient for the amount of data to be migrated.

2. Prepare data for transfer

  1. Check the OS version and disk layout of the source server:

    cat /etc/os-release
    lsblk -o NAME,SIZE,FSTYPE,MOUNTPOINT
    df -h
  2. Back up data and databases on the source server.

  3. Save the list of services and open ports to compare them with the new server later:

    systemctl list-units --type=service --state=running > services-before.txt
    ss -tulpn > ports-before.txt
  4. 24 hours before the switchover, reduce the TTL of DNS records to 300 seconds.

  5. Verify that the cloud server has SSH access to the source server with root privileges.

3. Transfer data using rsync

warning

If there is a DBMS on the source server, we do not recommend migrating data while it is running. During data migration, the DBMS will continue write operations to its files, which will cause some data on the new server to become inconsistent — the database will not start or will be corrupted. Before running rsync, stop the DBMS for the duration of the copy or migrate databases separately using a dump or replication.

  1. Boot the cloud server in Rescue mode.

  2. Connect to the server in Rescue mode.

  3. If the /boot directories or other directories are located on separate partitions, mount them to the /mnt partition.

  4. Enter the installed OS environment — chroot:

mount /dev/sda2 /mnt
for d in dev proc sys run; do mount --bind /$d /mnt/$d; done
chroot /mnt /bin/bash
  1. Copy data from the source server using rsync:

    rsync -aAXHxvzh --numeric-ids --progress --log-file=/root/rsync.log \
    --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found","/etc/fstab","/boot/*"} \
    root@SOURCE_IP:/ /

    Where:

    • -a — preserves permissions, ownership, and dates;

    • -A and -X — ACLs and extended attributes;

    • -H — hard links;

    • -x — restriction from crossing filesystem boundaries;

    • --numeric-ids — preserves numeric UIDs and GIDs;

    • --excludeВ — exclusions containing system directories, as well as the bootloader and fstab of the new server. To keep the network settings of the new server, add network files to the exclusions:

      • for Debian and Ubuntu — /etc/network/interfaces and /etc/netplan/*;
      • for RHEL-like OSs (CentOS, AlmaLinux, Rocky) — /etc/sysconfig/* or only /etc/sysconfig/network-scripts/*.
  2. Exit chroot and reboot the server in normal mode:

    exit
    reboot
  3. Compare services and ports with those saved at the data preparation stage in step 3.

  4. Reconfigure services bound to the old IP to the new IP:

    grep -rl "OLD_IP" /etc

4. Check and switch services

For your information

During the final data synchronization and switching between servers, service downtime occurs. We recommend synchronizing data and switching services during off-peak hours.

  1. Check service operation on the new server. To do this, add an entry with the new IP to the hosts file on your computer. This will allow you to connect to services on the new server, while user requests continue to be routed to the source server:

    NEW_IP example.com www.example.com

    In Linux and macOS, the hosts file is located at /etc/hosts; in Windows, at C:\Windows\System32\drivers\etc\hosts.

  2. If services access their own domain, add the same entry to the hosts file on the new server.

  3. If you find errors in service operation, fix them and repeat step 1.

  4. Monitor the services for 3 business days and resolve issues if necessary.

  5. To synchronize modified data, stop services that modify data on the source server.

  6. Copy data from the source server again using rsync:

    rsync -aAXHvz --numeric-ids --delete root@SOURCE_IP:/var/www/ /var/www/

    Here, --delete is a flag for deleting files on the new server that no longer exist on the source server.

    Only modified files will be copied.

  7. Transfer databases using a fresh dump or wait until the replica synchronizes with the source server.

  8. Switch DNS records to the new IP.

  9. Check service operation for another 3 business days.

  10. If everything works properly, the source server can be decommissioned.