To implement sync lets consider two server.
IP Address: 184.108.40.206
IP Address: 220.127.116.11
Step 1: Install Rsync Tool
At first we need to install Rsync on both the server with the help of following command.
[email@example.com]# yum install rsync
[firstname.lastname@example.org]# yum install rsync
Step 2: Create a User to run Rsync
We can setup rsync with root user, but for security reasons, you can create an unprivileged user on main webserver i.e 18.104.22.168 to run rsync.
[email@example.com]# useradd shamim
[firstname.lastname@example.org]# passwd shamim
Here I have created a user “shamim” and assigned a password(shamim) to user.
Step 3: Test Rsync Setup
It’s time to test your rsync setup on your backup server (i.e. 22.214.171.124) and to do so, please type following command.
[email@example.com]# rsync -avzhe ssh firstname.lastname@example.org:/var/www/ /var/www
sample output should be like
receiving incremental file list
sent 128 bytes received 32.67K bytes 5.96K bytes/sec
total size is 12.78M speedup is 389.70
You can see that your rsync is now working absolutely fine and syncing data. I have used “/var/www” to transfer; you can change the folder location according to your needs
Step 4: Automate Sync with SSH Passwordless Login
First, we’ll generate a public and private key with following commands on backups server (i.e. 126.96.36.199).
[email@example.com]# ssh-keygen -t rsa -b 2048
When you enter this command, please don’t provide passphrase and click enter for Empty passphrase so that rsync cron will not need any password for syncing data.
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
The key's randomart image is:
+--[ RSA 2048]----+
| .o. |
| .. |
| ..++ . |
| o=E * |
| .Sooo o |
| =.o o |
| * . o |
| o + |
| . . |
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
[firstname.lastname@example.org]# ssh-copy-id -i /root/.ssh/id_rsa.pub email@example.com
Now try logging into the machine, with “ssh ‘firstname.lastname@example.org‘”, and check in .ssh/authorized_keys.
[email@example.com]# ssh firstname.lastname@example.org
Now, we are done with sharing keys
Step 5: Schedule Cron To Automate Sync
Let’s setup a cron for this. To setup a cron, please open crontab file with the following command.
[email@example.com ~]# 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 2 minutes to sync the data.
*/2 * * * * rsync -avzhe ssh firstname.lastname@example.org:/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 2 minutes. You can change the time and folder location configuration according to your needs.