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 a 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-sized 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
Hostname: webserver.example123.com
Backup Server –
IP Address: xxx.xxx.x.xxx
Hostname: backup.example123.com
- Rsync Installation
In this tutorial, the web server data of webserver.example123.com mirrored on backup.example123.com. Firstly, install Rsync on both servers by running the following command[root@tecmint]# yum install rsync [On Red Hat based systems] [root@tecmint]# apt-get install rsync [On Debian based systems]
- Creating a User to run Rsync
Rsync set up with a root user but due to security issues you have to create an unprivileged user on the main server – webserver.example123.com to run rsync.[root@tecmint]# useradd tecgeek [root@tecmint]# passwd tecgeek
Here the user created as an example is a tech geek and also assigned a password.
- 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[root@backup www]# rsync -avzhe ssh [email protected]:/var/www/ /var/www
You will get the output 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 changed as per your needs.
- Automating Sync with SSH Passwordless Login
Let’s set up a cron for rsync. Since rsync used with SSH protocol, SSH will ask for authentication and if the password isn’t provided to cron it won’t work. For cron to make work smooth, setting up passwordless SSH logins for rsync is a must.In this example, it’s done as root to prevent file ownership too and it is also done for alternative users too.
Firstly, generate a private and public key with the commands below on the backup server (backup.example123.com)
[root@backup]# ssh-keygen -t rsa -b 2048
Don’t provide a passphrase after entering this command and click enter for an Empty passphrase so that rsync cron will sync data without any password.
The 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 result proves the generation of private and public keys and now it’s time to share it with the main server so that it will recognize this backup machine and permit it to log in without a password when synching data.
[root@backup 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.
[root@backup html]# [email protected]
With this sharing keys is completed.
- Scheduling Cron for Automatic Synching
Start with setting up cron by opening the crontab file with the following command.[root@backup ~]# crontab –e
This command will open /etc/ crontab file to edit with the default browser. Here a corn 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 changed as per your requirements.
Hope this tutorial will help you to sync two Apache web servers using rsync.