How to Sync Two Apache Web Servers/Websites Using Rsync

There are so many tutorials available on web to mirror or take a backup of your web files with different methods, here I am creating this article for my future reference and here I’ll be using a very simple and versatile command of Linux to create a backup of your website. This tutorial will help you to sync data between your two web servers with “Rsync“.

Sync Apache Web Server

Sync Two Apache Web Server

The purpose of creating a mirror of your Web Server with Rsync is if your main web server fails, your backup server can take over to reduce downtime of your website. This way of creating a web server backup is very good and effective for small and medium size web businesses.

Advantages of Syncing Web Servers

The main advantages of creating a web server backup with rsync are as follows:

  1. Rsync syncs only those bytes and blocks of data that have changed.
  2. Rsync has the ability to check and delete those files and directories at backup server that have been deleted from the main web server.
  3. It takes care of permissions, ownerships and special attributes while copying data remotely.
  4. It also supports SSH protocol to transfer data in an encrypted manner so that you will be assured that all data is safe.
  5. Rsync uses compression and decompression method while transferring data which consumes less bandwidth.

How To Sync Two Apache Web Servers

Let’s proceed with setting up rsync to create a mirror of your web server. Here, I’ll be using two servers.

Main Server
  1. IP Address:
  2. Hostname:
Backup Server
  1. IP Address:
  2. Hostname:

Step 1: Install Rsync Tool

Here in this case web server data of will be mirrored on And to do so first, we need to install Rsync on both the server with the help of following command.

[[email protected]]# yum install rsync        [On Red Hat based systems]
[[email protected]]# apt-get install rsync    [On Debian based systems]

Step 2: Create a User to run Rsync


[[email protected]]# useradd tecmint
[[email protected]]# passwd tecmint

Here I have created a user “tecmint” and assigned a password to user.

Step 3: Test Rsync Setup


[[email protected] www]# rsync -avzhe ssh [email protected]:/var/www/ /var/www
Sample Output
[email protected]'s password:

receiving incremental file list
sent 128 bytes  received 32.67K bytes  5.96K bytes/sec
total size is 12.78M  speedup is 389.70


Step 4: Automate Sync with SSH Passwordless Login

Now, we are done with rsync setups and now its time to setup a cron for rsync. As we are going to use rsync with SSH protocol, ssh will be asking for authentication and if we won’t provide a password to cron it will not work. In order to work cron smoothly, we need to setup passwordless ssh logins for rsync.

Here in this example, I am doing it as root to preserve file ownerships as well, you can do it for alternative users too.


[[email protected]]# ssh-keygen -t rsa -b 2048


Sample Output
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
The key fingerprint is:
The key's randomart image is:
+--[ RSA 2048]----+
|          .o.    |
|           ..    |
|        ..++ .   |
|        o=E *    |
|       .Sooo o   |
|       =.o o     |
|      * . o      |
|     o +         |
|    . .          |


[[email protected] html]# ssh-copy-id -i /root/.ssh/ [email protected]

Now try logging into the machine, with “ssh ‘[email protected]‘”, and check in .ssh/authorized_keys.

[[email protected] html]# [email protected]

Now, we are done with sharing keys. To know more in-depth about SSH password less login, you can read our article on it.

  1. SSH Passwordless Login in in 5 Easy Steps

Step 5: Schedule Cron To Automate Sync


[[email protected] ~]# crontab –e

It will open up /etc/crontab file to edit with your default editor. Here In this example, I am writing a cron to run it every 5 minutes to sync the data.

*/5        *        *        *        *   rsync -avzhe ssh [email protected]:/var/www/ /var/www/

The above cron and rsync command simply syncing “/var/www/” from the main web server to a backup server in every 5 minutes. You can change the time and folder location configuration according to your needs. To be more creative and customize with Rsync and Cron command, you can check out our more detailed articles at:

  1. 10 Rsync Commands to Sync Files/Folders in Linux
  2. 11 Cron Scheduling Examples in Linux




Support Us

We are thankful for your never ending support.

Tarunika Shrivastava



RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

38 Responses

  1. Jeff says:

    Can you setup a crontab for realtime? Instead of every 5 minutes?

  2. says:

    I do webdesign & Development, This article helps me every time when I setup a site for them. Very useful resource.

Leave a Reply to Cancel reply

