How to Migrate Linux Servers

Migrating data between Linux servers is an uncomplicated task if the setup is simple. However, it becomes complex if there are many interdependent services or highly customized setups. Learning how to migrate Linux servers is a crucial task for Linux administrators.

There are many scenarios where you might have to transfer data to another server. You might be upgrading to a larger machine, transitioning to new hardware, or setting up a new data center. Modern server deployments require deployment management tools. 

However, in this article, you will learn how to migrate simple Linux servers. The article also covers the full migration process and tools required to make the migration process easier.

Backup the Current Server to Migrate Linux Servers

To migrate Linux servers, the first step is to back up the server. You do not want to be left with a situation where the migration does not complete successfully, and you are left with no data. Backup in a separate flash drive is important. Clone the disk image on any external flash drive or Hard disk.

Gather Source System Information to Migrate Linux Server

Secondly, try to match the system configurations of source and destination servers as much as you can. For this step, you will need source system information to have similar configurations to the destination system. Run the command given below to get the complete information on the system:

uname -r

The output would look something like this:

Migrate Linux Servers
cat /etc/issue

Output:

Migrate Linux Servers

From the output, you can see the kernel and distribution version. Try to match that in the destination system as well. 

Set Up SSH Keys Between Source and Destination System

After configuring the destination system similar to the source system, the next step is to create an SSH connection between the two. This step is crucial for transferring files between the servers. At this point, we assume that you have already installed SSH on your system.

To start the configuration process, you will first check if the SSH key already exists in the source system. For this step, execute the command given below:

ls ~/.ssh

If you get a similar output, then the keys do not exist. 

Migrate Linux Servers

Now, you can create SSH keys using the ssh-keygen command. For example:

ssh-keygen -t rsa
SSH keygen

Alternatively, if the keys exist, you just have to transfer them to the source system. 

Secondly, create a pipeline to transfer the SSH keys from the source system to the destination system. Specifically, type:

cat ~/.ssh/id_rsa.pub | ssh other_server_ip "cat >> ~/.ssh/authorized_keys"

Now, you can easily SSH to your destination server using the SSH command. Specifically, type:

SSH <Destination IP Address>

Gather Package Versions

The next step is to gather package and service information to make the migration process easier. It is not necessary to view every package and its version. However, you should check for the components crucial to your need. To check the version of a package, use the dpkg command in combination with the grep command. For instance:

dpkg -l | grep <package_name>

For example:

dpkg -l | grep ssh

You will get a similar output:

grep packages

For Fedora-based Linux systems, use the rpm command. For instance:

rpm -qa | grep package_name

This command will give you an overview of the package versions that you should match in the destination system.

In addition, you can also use the service command to check the services running on your system. Type:

service --status-all

Install Programs on the Destination Linux Server

On the destination server, run the following command to ensure that it matches the source system configurations:

sudo apt update
sudo apt upgrade
Migrate Linux Servers

Alternatively, you can also install the packages by specifying the exact version you want to install. For this step, use the command given below:

sudo apt install package_name=version_number

For Fedora-based servers, use the dnf package. For instance:

Sudo dnf install package_name-version_number

Migrate Linux Servers

Migration is not laborers but is time-intensive. For the data transfer, you will use the rsync tool on the source server. Rsync enables users to replicate files and directories across different Linux environments. The syntax for the rsync command is:

rsync -azvP --progress source_server:/path/to/directory/to/transfer /path/to/local/directory

Type:

sudo rsync -avz /var/www/ 10.130.59.210:/var/www

Similarly, do this step for all the packages identified in step 4. For instance:

sudo rsync -avz /etc/nginx/sites-available/ 10.130.59.210:/etc/nginx/sites-available
sudo rsync -avz /etc/nginx/sites-enabled/ 10.130.59.210:/etc/nginx/sites-enabled
sudo rsync -avz /etc/nginx/snippets/ 10.130.59.210:/etc/nginx/snippets
sudo rsync -avz /etc/nginx/nginx.conf 10.130.59.210:/etc/nginx/nginx.conf
sudo rsync -avz /etc/ssl/certs/dhparam.pem 10.130.59.210:/etc/ssl/certs/dhparam.pem
sudo rsync -avz /etc/apt/apt.conf.d/50unattended-upgrades 
sudo rsync -avz /etc/apt/apt.conf.d/10periodic 10.130.59.210:/etc/apt/apt.conf.d/10periodic
sudo rsync -avz /etc/apt/apt.conf.d/10periodic 10.130.59.210:/etc/apt/apt.conf.d/10periodic
sudo rsync -avz /usr/bin/letsencrypt 10.130.59.210:/usr/bin/letsencrypt
sudo rsync -avz /etc/letsencrypt/ 10.130.59.210:/etc/letsencrypt

In addition, you can also transfer users, databases and jobs to the destination system. For database dump, type:

sudo rsync -avz ~/data-dump.sql 10.130.59.210:/home/user

To transfer jobs and mails, type:

rsync -azvP --progress source_server:/var/spool/mail/* /var/spool/mail/

Similarly, to transfer cron files, use the rsync as shown below:

rsync -azvP --progress source_server:/etc/crontab /etc/crontab

Change DNS Settings

The last step is to configure DNS to point to your new server. Before this step, make sure to configure IP Tables and firewall settings. Also, register your HTTP certificates and check all web endpoints. DNS changes usually take a few minutes to an hour to become visible on the network.

Finally, you have migrated the Linux servers successfully. Your new server should be up and running now and handling all the data. The best way to get a smooth migration is to understand and prepare the data thoroughly. We hope the article was useful to you. 

If this guide helped you, please share it.

Leave a Reply
Related Posts