LaoBlog

How to hot clone a Linux VM with rsync.


Problem: I have a Debian vm on Proxmox which lately suffers weird problems which cause the block of the OS. The vm is very old. Originally a Virtualbox 32 bit vm, then moved to KVM/qemu and migrated to 64 bit (kernel+packages).Therefore I decided to try to clone it. The clone is "hot" because you don't need to shutdown  the source VM; however, to avoid some trouble, it will be better to STOP the running unnecessary  services.I did this on the same Proxmox host. First I created a new Debian vm with a HD of same size as the original, and SAME version of the OS. On the source (old vm), I stopped all the services which keep writing on the disk (excepted those like logrotate,rlog, syslog etc. which I don't care about): servers for database, email, firewall... I left sshd to allow me to connnect to the machines and check what's happening (btw, in case of failure you can still try to use the console embedded in Proxmox). On the source I created a list of installed packages, copied that list on the new vm and used it to install all  the same packages (check for my previous post on this blog); be sure to have the same rsw repositories on both machines. On the source vm I created a file, called for example "excludedpackages.txt) with the following content:/boot /dev /tmp /sys /proc /etc/fstab /etc/mtab /rootYou can add other paths in case of need.With both vm running and connected to my LAN (so that I can ssh into them from a third PC), on the old vm, as root user, I run in a terminal:$ rsync -vPa -e 'ssh -o StrictHostKeyChecking=no' --exclude-from=/root/excludedpackages.txt / DESTINATIONIP:/Obviously you need to change "DESTINATIONIP" with the login and real IP address of your destination VM. The system will ask you for the password of the user on the destination vm, then the synchronization will begin, and take some time.Once finished (hopefully without errors, otherwise you can try again) you can turn off the old vm. Optionally modify the IP address of the destination vm so that it can have the same of the original, old vm; by doing so, you can avoid to tell all the computers of your LAN that the server which provided a certain service (for example the email) has changed IP and therefore they must reconfigure their clients.Reboot the new VM and check that everything is working.This procedure should work with all the other GNU Linux OSs, probably with BSD too, with some change.Enjoy your new shining virtual machine!
Credit for this solution to: https://linuxadmin.io/hot-clone-linux-server/