Home Apache How to Sync Two Web Servers in Linux Automatically

How to Sync Two Web Servers in Linux Automatically

As you graduate from a Linux enthusiast to a Linux administrator/superuser in a production/server environment, you start to see the sense in working with more than one web server.

Right now, you might not see the logic in having two similar web servers running at the same time but there will come a time when having an extra server environment creates an ideal backup plan for your primary server.

[ You might also like: Lsyncd – Synchronize Local Directories with Remote Linux ]

To reduce or counter unavoidable website downtime, a secondary web server should be set up to automatically take over from a primary web server. This setup process that enables two web servers to communicate with one another is called server syncing.

Advantages of Web Server Syncing

Let’s discuss some advantages of having a web server syncing operation.

  • When data is copied from the primary server to/from the secondary server, permissions, ownership, and other special attribute settings from the initial server environment are retained.
  • You will be dealing with lesser bandwidth since targeted data transfers adhere to compression and decompression data transfer protocols.
  • If the primary web server already has user-deleted files and directories and the secondary server still has these files and directories in place, the data transfer and synchronization process will verify and delete those files and directories to ensure the two servers’ directory structures are in sync.
  • The two servers’ synchronization action also applies to changed data bytes and blocks.
  • If you are concerned about data encryption during your server-to-server data transfer operations, this web servers syncing mechanism supports SSH; an encrypted network protocol.

Requirements for Syncing Two Web Servers

In this case, you need to have two web servers running and their respective domain names or IP addresses. In my case, I will use the following two servers as my primary and secondary web servers:

Main server IP address: 3.135.207.198
Secondary server IP address: 18.118.208.79

Installing Rsync for Syncing Two Web Servers

For our web servers’ syncing operation to be successful, we are going to need the help of the rsync tool. You can install it on your preferred Linux distribution server from either of the following package installation commands. Rsync installation should take place on both the main server and the secondary server you wish to sync.

$ sudo apt-get install rsync     [On Debian, Ubuntu and Mint]
$ sudo yum install rsync         [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
$ sudo emerge -a sys-apps/rsync  [On Gentoo Linux]
$ sudo pacman -S rsync           [On Arch Linux]
$ sudo zypper install rsync      [On OpenSUSE]    

Syncing Two Web Servers Using Rsync in Linux

Before performing syncing, make sure to identify your rsync user on the main/primary server. You can use the already established root user on your Linux system. On my end, I will use the already existing ubuntu user.

Next, you need to identify the server directories you wish to sync on your active primary and secondary servers. On my end, I will try to sync the webserver directories /var/www that exist on both servers.

On my backup/secondary server, I will run the following rsync command that points and links its /var/www directory with the one on the main/primary server.

$ sudo rsync -avzhe ssh [email protected]:/var/www/ /var/www/
Syncing Two Web Servers in Linux
Syncing Two Web Servers in Linux

As per the executed rsync command from the above screen capture, the files and directories that existed in the main server have been synced and copied to the backup server.

[ You might also like: Rsync Show Progress When Syncing Files in Linux ]

Automatically Syncing Two Web Servers Using Rsync in Linux

You don’t need to every time sync your web servers manually. To automate this syncing operation, consider the following two-step approaches:

Step 1: Configuring SSH Passwordless Login

On your backup/secondary server, you need a public and private key. You can generate one from the following command:

$ sudo ssh-keygen -t rsa -b 2048 

Remember to run the above command on your backup server.

Create SSH Passwordless Login
Create SSH Passwordless Login

Also, remember to skip setting up a passphrase by pressing [Enter] to leave the passphrase field blank.

Next, these generated public and private keys need to be copied to the primary/main server from the backup server. Use a command similar to the following to achieve this objective:

$ sudo ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
Copy SSH Public Key to Main Server
Copy SSH Public Key to Main Server

Now access the main server and confirm the newly added keys on your main server:

$ ssh [email protected]
$ sudo nano .ssh/rsa
Confirm SSH Public Key on Main Server
Confirm SSH Public Key on Main Server

We are now done with sharing the required key files with our two servers (primary and secondary servers).

Step 2: Automatically Syncing Two Web Servers via Cron Job

On the backup/secondary server, configure the crontab file for automatic server syncing:

$ sudo crontab -e

You will be asked to choose an editor for opening the crontab file:

Create Cron Job in Linux
Create Cron Job in Linux

At the bottom of the opened file, add a cron job schedule for syncing your servers:

*/5   *   *   *   *   rsync -avzhe ssh [email protected]:/var/www/ /var/www/
Add Cron Job in Linux
Add Cron Job in Linux

As per the above cron job entry on the crontab file, the main and primary web servers will be automatically syncing after every five minutes.

Once you save your changes and exit the editor, the cron job should install and activate itself.

$ sudo crontab -e
Installing Cron Job in Linux
Installing Cron Job in Linux

Automatically syncing your primary web server with a secondary web server saves you from having to deal with server downtime issues, errors, and also creates the perfect backup plan for a productive and highly trafficked website server that you wish to remain active.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

3 thoughts on “How to Sync Two Web Servers in Linux Automatically”

  1. Will this work with a Plesk server, I’m looking for an easy way to sync my server and backup server, located in two different datacenters.

    Reply

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.