Discover Steps to Sync Two Apache Web Servers Using Rsync

By | November 18, 2015

Sync Server

Do you have a mirrored server of your current server to take the backup when it fails? No, then you need to have a mirror of your web server which can be created with Rsync. This will help to take backup of your failed server without any issues of downtime. Creating a web server backup is a good as well as effective way for small and medium size web businesses.
Let’s learn to setup rsync to create a mirror of your web server –
You will require two servers –
Main Server –
IP Address: xxx.xxx.x.xxx
Host name: webserver.example123.com

Backup Server –
IP Address: xxx.xxx.x.xxx
Host name: backup.example123.com

Step 1 – Rsync Installation
In this tutorial, web server data of webserver.example123.com will be mirrored on backup.example123.com. Firstly, install Rsync on both the servers by running the following command

Step 2 – Creating User to run Rsync
Rsync can be setup with root user but due to security issues you have to create an unprivileged user on the main server – webserver.example123.com to run rsync.

Here the user created as an example is techgeek and also assigned a password.

Step 3 – Testing Rsync Setup
Now, you need to test your rsync setup on your backup server (backup.example123.com) and to do so run the following command

You will get the output as below –

This shows that your rsync is working smoothly and is synching data. Here “/var/www” is used to transfer. The folder location can be changed as per your needs.

Step 4 – Automating Sync with SSH Passwordless Login
Let’s setup a cron for rsync. Since rsync will be used with SSH protocol, SSH will ask for authentication and if password isn’t provided to cron it won’t work. For cron to make work smoothly, setting up passwordless SSH logins for rsync is a must.
In this example, it’s been done as root to prevent file ownerships too and it can also be done for alternative users too.
Firstly, generate a private and public key with the commands below on backup server (backup.example123.com)

Don’t provide passphrase after entering this command and click enter for Empty passphrase so that rsync cron will sync data without any password.
Output will be as below for example –

This results proves generation of private and public keys and now it’s time to share it with main server so that it will recognize this backup machine and permit it to login without password when synching data.

Try logging into the machine with “ssh’[email protected]'” and check in .ssh/authorized_keys.

With this sharing keys is completed.

Step 5 – Scheduling Cron for Automatic Synching
Start with setting up cron by opening crontab file with the following command.

This command will open /etc/ crontab file to edit with the default browser. Here a cron is written to run it every 5 minutes for synching data.

The time and folder location configuration can be changed as per your requirements.

Hope this tutorial will help you to sync two apache web servers using rsync.