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
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
-
Check the OS version and disk layout of the source server:
cat /etc/os-releaselsblk -o NAME,SIZE,FSTYPE,MOUNTPOINTdf -h -
Back up data and databases on the source server.
-
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.txtss -tulpn > ports-before.txt -
24 hours before the switchover, reduce the TTL of DNS records to 300 seconds.
-
Verify that the cloud server has SSH access to the source server with
rootprivileges.
3. Transfer data using rsync
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.
-
If the
/bootdirectories or other directories are located on separate partitions, mount them to the/mntpartition. -
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
-
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; -
-Aand-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 andfstabof the new server. To keep the network settings of the new server, add network files to the exclusions:- for Debian and Ubuntu —
/etc/network/interfacesand/etc/netplan/*; - for RHEL-like OSs (CentOS, AlmaLinux, Rocky) —
/etc/sysconfig/*or only/etc/sysconfig/network-scripts/*.
- for Debian and Ubuntu —
-
-
Exit
chrootand reboot the server in normal mode:exitreboot -
Compare services and ports with those saved at the data preparation stage in step 3.
-
Reconfigure services bound to the old IP to the new IP:
grep -rl "OLD_IP" /etc
4. Check and switch services
During the final data synchronization and switching between servers, service downtime occurs. We recommend synchronizing data and switching services during off-peak hours.
-
Check service operation on the new server. To do this, add an entry with the new IP to the
hostsfile 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.comIn Linux and macOS, the
hostsfile is located at/etc/hosts; in Windows, atC:\Windows\System32\drivers\etc\hosts. -
If services access their own domain, add the same entry to the
hostsfile on the new server. -
If you find errors in service operation, fix them and repeat step 1.
-
Monitor the services for 3 business days and resolve issues if necessary.
-
To synchronize modified data, stop services that modify data on the source server.
-
Copy data from the source server again using
rsync:rsync -aAXHvz --numeric-ids --delete root@SOURCE_IP:/var/www/ /var/www/Here,
--deleteis a flag for deleting files on the new server that no longer exist on the source server.Only modified files will be copied.
-
Transfer databases using a fresh dump or wait until the replica synchronizes with the source server.
-
Switch DNS records to the new IP.
-
Check service operation for another 3 business days.
-
If everything works properly, the source server can be decommissioned.