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.

Similar Posts