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
[[email protected]]# yum install rsync [On Red Hat based systems] [[email protected]]# apt-get install rsync [On Debian based systems]
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.
[[email protected]]# useradd tecgeek [[email protected]]# passwd tecgeek
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
[[email protected] www]# rsync -avzhe ssh [email protected]:/var/www/ /var/www
You will get the output as below –
[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
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)
[[email protected]]# ssh-keygen -t rsa -b 2048
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 –
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: 9a:33:a9:5d:f4:e1:41:26:57:d0:9a:68:5b:37:9c:23 [email protected] The key's randomart image is: +--[ RSA 2048]----+ | .o. | | .. | | ..++ . | | o=E * | | .Sooo o | | =.o o | | * . o | | o + | | . . | +-----------------+
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.
[[email protected] html]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
Try logging into the machine with “ssh’[email protected]'” and check in .ssh/authorized_keys.
[[email protected] html]# [email protected]
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.
[[email protected] ~]# crontab –e
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.
*/5 * * * * rsync -avzhe ssh [email protected]:/var/www/ /var/www/
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.