Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: Sync Two Apache Web Servers  (Read 2752 times)

0 Members and 1 Guest are viewing this topic.

Vinil

  • Guest
Sync Two Apache Web Servers
« on: January 21, 2014, 07:11:22 am »
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.


Advantages of Syncing Web Servers


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

*   Rsync syncs only those bytes and blocks of data that have changed.
*   Rsync has the ability to check and delete those files and directories at backup server that have been deleted from the main web server.
*   It takes care of permissions, ownerships and special attributes while copying data remotely.
*   It also supports SSH protocol to transfer data in an encrypted manner so that you will be assured that all data is safe.
*   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

Quote
    IP Address: 192.168.0.100
    Hostname: webserver.example.com

Backup Server

Quote
    IP Address: 192.168.0.101
    Hostname: backup.example.com


Install Rsync Tool

We need to install Rsync on both the server with the help of following command.

# yum install rsync


Create a User to run Rsync

Code: [Select]
useradd user1
passwd user1


Test Rsync Setup

Quote
[root@backup www]# rsync -avzhe ssh user1@webserver.example.com:/var/www/ /var/www

Sample Output

Code: [Select]
user1@webserver.example.com'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

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.

First we’ll generate a public and private key with following commands on backups server "backup.example.com".

[root@backup]# ssh-keygen -t rsa -b 2048

Now, our Public and Private key has been generated and we will have to share it with main server so that main web server will recognize this backup machine and will allow it to login without asking any password while syncing data.

Quote
[root@backup html]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@webserver.example.com


Schedule Cron To Automate Sync

[root@backup ~]# crontab –e

Here In this example, write a cron to run it every 5 minutes to sync the data.

Code: [Select]
*/5        *        *        *        *   rsync -avzhe ssh root@webserver.example.com:/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.