There are so many tutorials available on web to mirror or take a backup of your web files with different methods, here I am creating this article for my future reference and here I’ll be using a very simple and versatile command of Linux to create a backup of your website. This tutorial will help you to sync data between your two web servers with “Rsync“.
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. This way of creating a web server backup is very good and effective for small and medium size web businesses.
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
- IP Address: 192.168.0.100
- Hostname: webserver.example.com
Backup Server
- IP Address: 192.168.0.101
- Hostname: backup.example.com
Step 1: Install Rsync Tool
Here in this case web server data of webserver.example.com will be mirrored on backup.example.com. And to do so first, we need to install Rsync on both the server with the help of following command.
[root@tecmint]# yum install rsync [On Red Hat based systems] [root@tecmint]# apt-get install rsync [On Debian based systems]
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 webserver.example.com to run rsync.
[root@tecmint]# useradd tecmint [root@tecmint]# passwd tecmint
Here I have created a user “tecmint” and assigned a password to user.
Step 3: Test Rsync Setup
It’s time to test your rsync setup on your backup server (i.e. backup.example.com) and to do so, please type following command.
[root@backup www]# rsync -avzhe ssh [email protected]:/var/www/ /var/www
Sample Output
[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
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
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.
Here in this example, I am doing it as root to preserve file ownerships as well, you can do it for alternative users too.
First, we’ll generate a public and private key with following commands on backups server (i.e. backup.example.com).
[root@backup]# 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.
Sample Output
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 + | | . . | +-----------------+
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.
[root@backup html]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
Now try logging into the machine, with “ssh ‘[email protected]‘”, and check in .ssh/authorized_keys.
[root@backup html]# [email protected]
Now, we are done with sharing keys. To know more in-depth about SSH password less login, you can read our article on it.
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.
[root@backup ~]# 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 5 minutes to sync the data.
*/5 * * * * rsync -avzhe ssh [email protected]:/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. To be more creative and customize with Rsync and Cron command, you can check out our more detailed articles at:
My thought was to actually sync the two computers so you can do work on either one, and the directories stay the same. Not at the same time, because Linux is not a real-time system. But say,
at the press of a button you can sync them. That way both computers would have the up-to-date files regardless of which one was worked on. Might be dangerous with two people at it.
When I read the title to this article, I thought “This is it~” Alas, it is just another Rsync to the backup article.
JARSTB.
Can you setup a crontab for realtime? Instead of every 5 minutes?
@Jeff,
What you mean by real-time? we can set a cron to run every second, but not in real-time. For more information read this – How to Create and Manage Cron Jobs on Linux.
I do webdesign & Development, This article helps me every time when I setup a site for them. Very useful resource.
I also need after rsync completes the job – the result should be emailed to website owner ?
@Dhaal,
Better add the rsync command to cron and define a MAILTO option in cron to get job completion emails..
Dear Ravi,
I have a website on Old server CentOS release 5.11 which is running a local webportal on Apache Server version: Apache/2.2.3.
I want to move this webportal to the new OS CentOS Linux release 7.2 on Apache Server version: Apache/2.4.6.
can you help how is this possible …
@Umesh,
Simple, install CentOS 7.2 with LAMP stack and move the whole application and database (copy db from old machine and create a database and dump it on new machine) do a required MySQL settings in config file of application.
Thanks Ravi..
I will try ..I am not sure that sql is being used in this portal.
Just FYI, when generating SSH key, defining
-b 2048
is a bit redundant since 2048 is the default bits for type RSA.ssh-keygen -t rsa
is sufficient, one less thing to remember..Hi Ravi,
Nice tutorial, thanks a lot.
The only manual operation in that case would be the dns change of the main server to the backup webserver.
Is there a way or a solution to do this automatically? And how to switch back from backup to main when this one comes back up ?
@Djez,
Unfortunately, you have to manually make the IP switch in DNS when the master goes down or vice-versa, no automation options for this.
Hi ravi, thanks for the answer, this is what I was thinking.
again thank you for the tutorial.
There is a solution. I use keepalived to keep servers highly available. Once configured on both servers, point your DNS to the floating IP. If one dies, the other takes over.
One question, what should I do with the user that I already created. In your crond its root who connect to the other server… :/
@Gustavo,
You can use those newly created users to use for syncing files between two servers, as in my example I used root users to sync..
That’s what I mean, for example if I want to sync Apache conf dir for bk, I need root privileges on the other server am right?
-PS.
Sorry for my bad english…
@Gustavo,
That’s right, you need root privileges to sync core system files, normal users don’t have such privileges to copy/sync system configuration files..
Thank you. I use Rsync to replicate between one web server and two slaves for balancing and works perfect. thank you for share. greetings from Argentina.
Hello,
How backup server will handle traffic if first server will down? rsync command only sync to data from one to another. but how it will handle the traffic just need to know
@Ranjan,
Yes rsync command only syncs data between two servers, it doesn’t programmed to handle traffic. If first server down in case, you need to manually point to slave server (backup server)
Use a floating IP.
What about Data Base replication? Nowadays most web sites uses databases.
@ndres,
Here is the link the MySQL database replication..
https://www.tecmint.com/how-to-setup-mysql-master-slave-replication-in-rhel-centos-fedora/
use KrojamSoft SyncFile it’s the best…
I’m new to this. So apologize in advance for basic, dumb questions. Where do I enter these commands? Please give me specific steps. Do I have to login to my cpanel or WHM? Or is it something I can pull up from the FTP? Thanks in advance for your patience with my ignorant questions.
The commands can be run on server level with direct root access. It will won’t work uner cpanel or WHM.
Please submit your question in http://linuxsay.com forum with your specific needs and we will be more than glad to hep you with that. I am asking to submit the question in there as this is the official TecMint support forum and we will be able to provide more detailed instructions in there.
An other hint that I have noticed: why do you share the ssh authentication file for root instead of your alternative user in your tutorial?
Hey Tarunika, first thanks for your great tutorial.
Anyways does your solution only permanently synchronize the two folders? I think that’s not what I need. What I need are daily and weekly backups.
Maybe I can use your solution and modify it so that before synchronization the latest backup is copied to an other directory.
But in this case I were resulting in duplicated disc usage.
Great Post …
Main Server
IP Address: 192.168.0.100
Hostname: webserver.example.com
Backup Server
IP Address: 192.168.0.101
Hostname: backup.example.com
if i have made some changes index.html file in main server… it work …. Backup server get updated..
But i have change some contain index.html file in backup server….. it is not work ….
in backup server & main server
Hi, you must add some tutorial when one server down, it will automatically redirect to backup server , it will contain some dns setup
This is from around 1999 but still the very best rsync tutorial I’ve ever read.
How to backup on real time ?? I mean a file is created on folder and it needs to be moved in to another folder. Is that possible using rsync. I tried adding cron job with –remove-source-files but to no avail.
every time i run the sync he copy everything or just what changed ?
and he delete wuat is not there tooo ?
A couple of things.
What are the benefits in terms of security of running rsync as root when all you are doing is replicating apache/httpd user files?
Also, if your backup up web server is compromised, then the attacker now has complete access to your main webserver as root.
Hi Tarunika,
Can I use rsync to entirely backup for LAMP(including data and program) ?
Thanks,
Not if you want to replicate database
Thank you ! Great Post .;