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 ServersThe 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 ServersLet’s proceed with setting up rsync to create a mirror of your web server. Here, I’ll be using two servers.
Main Server IP Address: 192.168.0.100
Hostname: webserver.example.com
Backup Server IP Address: 192.168.0.101
Hostname: backup.example.com
Install Rsync ToolWe need to install Rsync on both the server with the help of following command.
# yum install rsync Create a User to run Rsyncuseradd user1
passwd user1
Test Rsync Setup
[root@backup www]# rsync -avzhe ssh user1@webserver.example.com:/var/www/ /var/www
Sample Outputuser1@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 LoginNow, 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 2048Now, 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.
[root@backup html]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@webserver.example.com
Schedule Cron To Automate Sync[root@backup ~]# crontab –eHere In this example, write a cron to run it every 5 minutes to sync the data.
*/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.