How to Sync Two Web Servers Using Rsync

When managing a web server, it’s crucial to ensure that your data is safe and can be quickly restored in case of failure. One of the most reliable ways to back up and mirror web server data is by using rsync.

rsync tool helps synchronize files and directories between two servers, making it ideal for creating backups and mirrors of your web server data.

In this article, we will guide you through the process of syncing your web server with a backup server using rsync. We will also set up passwordless login to automate the synchronization process using cron for scheduled backups.

Prerequisites

Before we begin, ensure that you have the following:

Two Servers

  • Main Web Server: IP address 192.168.0.100, hostname webserver.example.com.
  • Backup Server: IP address 192.168.0.101, hostname backup.example.com.

Installed Software

Both servers should have rsync installed, if not you can install it by running:

sudo apt install rsync         [On Debian, Ubuntu and Mint]
sudo yum install rsync         [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
sudo emerge -a sys-apps/rsync  [On Gentoo Linux]
sudo apk add rsync             [On Alpine Linux]
sudo pacman -S rsync           [On Arch Linux]
sudo zypper install rsync      [On OpenSUSE]    
sudo pkg install rsync         [On FreeBSD]

SSH Access

SSH access should be enabled between the two servers, which will used to set up passwordless login using SSH keys for smooth automation.

Step 1: Setting Up Passwordless SSH Login

To automate the syncing process with cron, we need to set up passwordless SSH login from the web server (Main Server) to the backup server, which will allow rsync to run without needing to enter a password every time.

Log in to the Main Web Server (webserver.example.com) and generate SSH keys and make sure to accept the default file location and leave the passphrase empty.

ssh-keygen -t rsa -b 2048

Next, use the ssh-copy-id command to copy the public key to the Backup Server:

ssh-copy-id [email protected]

Replace user with the username on the Backup Server. You will be prompted for the password once, but after that, the passwordless login will be set up.

Finally, test the connection by logging in from the Main Web Server to the Backup Server:

ssh [email protected]

If you can log in without entering a password, the setup is successful.

Step 2: Syncing Web Server Data Using Rsync

Now that we have set up passwordless SSH, we can use rsync to sync the web server’s data to the backup server. The web server’s data is typically stored in the /var/www/html/ directory, which we will use for backup and mirroring.

Basic Rsync Command

To perform a one-time backup, run the following command on the Main Web Server (webserver.example.com):

rsync -avz /var/www/html/ [email protected]:/path/to/backup/directory

Explanation of the above command:

    • -a stands for “archive mode” which preserves permissions, timestamps, and other file properties.c
    • -v enables verbose output, so you can see what files are being transferred.
    • -z enables compression to reduce data transfer size.

Syncing Files to the Backup Directory

Replace /path/to/backup/directory with the actual path on the Backup Server where you want to store the backup.

For example:

rsync -avz /var/www/html/ [email protected]:/backup/webserver

Mirroring the Web Server Directory

If you want to mirror the directory (i.e., make the backup exactly the same as the source), you can use the --delete option:

rsync -avz --delete /var/www/html/ [email protected]:/backup/webserver

This will delete any files in the backup directory that are no longer present on the web server, ensuring both directories are identical.

Step 3: Automating the Backup with Cron

To ensure regular backups, we can schedule the rsync command to run automatically using cron, this way, the backup process will run at a specified time, such as every day at midnight.

Open the crontab file on the Main Web Server (webserver.example.com) by running:

crontab -e

To schedule the backup to run every day at midnight, add the following line to the crontab:

0 0 * * * rsync -avz --delete /var/www/html/ [email protected]:/backup/webserver

Save the crontab file and exit. The cron job will now run automatically at 12:00 AM every day. You can adjust the timing as needed.

Step 4: Verifying the Backup

Once the cron job is set up, it’s important to verify that the backup is working correctly by doing these checks.

You can check the system log to verify that the cron job is running as expected:

grep CRON /var/log/syslog

Log in to the Backup Server (backup.example.com) and check if the files in the /backup/webserver directory match the files on the Main Web Server (/var/www/html/).

You can also perform a test by deleting a file on the Main Web Server and verifying that it gets removed from the backup directory after the next rsync run.

Conclusion

Syncing web servers with rsync for backup and mirroring is a powerful and efficient way to ensure your data is safe and easily recoverable.

By setting up passwordless SSH login and automating the process with cron, you can run regular backups without any manual intervention.

This setup helps maintain up-to-date backups of your web server data and ensures that your website remains available, even in the event of server failure.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Tarunika
I am a linux server admin and love to play with Linux and all other distributions of it. I am working as System Engineer with a Web Hosting Company.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

40 Comments

Leave a Reply
  1. My batch job for this type of backup does the following:

    1. Dumps the MySQL/PostgreSQL database to a folder outside of the web root.
    2. Performs the backup, capturing all the web pages as well as the SQL backup.
    3. Deletes the SQL backup.

    Reply
  2. 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.

    Reply
  3. 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 …

    Reply
    • @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.

      Reply
  4. 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..

    Reply
  5. 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 ?

    Reply
    • @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.

      Reply
        • 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.

          Reply
  6. One question, what should I do with the user that I already created. In your crond its root who connect to the other server… :/

    Reply
    • @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..

      Reply
      • 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…

        Reply
        • @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..

          Reply
  7. 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.

    Reply
  8. 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

    Reply
  9. 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.

    Reply
    • 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.

      Reply
  10. 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?

    Reply
  11. 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.

    Reply
  12. 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

    Reply
  13. Hi, you must add some tutorial when one server down, it will automatically redirect to backup server , it will contain some dns setup

    Reply
  14. 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.

    Reply
  15. 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.

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.