How to migrate a Linux server by copying its content/copy folders and files recursively to another server

One of benefits of using a folder to house all your applications (e.g. I use /system and put my Tomcat, MySQL, MariaDB and application code in it, when compiling from source I use –prefix to put the resulting code under /system), is that I can upgrade to a later version of Linux (in my case its Ubuntu) with ease.

Scenario:

Source server is an Ubuntu 20 named serverSource.

Destination server is an Ubuntu 22 named serverDestination.

In serverSource, make sure you can SSH to serverDestination, and you want to copy the /system folder.Do this

tar cf - /system | ssh -i /root/serverDestination.pem root@serverDestinationIP "cd / && tar xvf -"

This will copy serverSource’s /system to serverDestination’s /system.

Sometimes you want to copy to another folder in serverDestination. Then you do this

tar cf - /system | ssh -i /root/serverDestination.pem root@serverDestinationIP "cd /differentFolder && tar xvf -"

If you are copying lots of compressible files (e.g. text files) or you are on a metered connection (e.g. transferring between regions in AWS). You can use the z flag for compression, like so

tar czf - /system | ssh -i /root/serverDestination.pem root@serverDestinationIP "cd / && tar xvzf -"

Leave a Reply

Your email address will not be published. Required fields are marked *