- Posted on
- • Linux
Efficient Web Server Backup and Mirroring with Rsync
- Author
-
-
- User
- edan
- Posts by this author
- Posts by this author
-

Maintaining the integrity and availability of data on a web server is vital for any organization. Utilizing the rsync tool facilitates effective synchronization of files and directories between servers, making it a robust solution for data backup and mirroring. This guide provides a comprehensive approach to setting up rsync for coordinating data between a primary web server and a backup server while incorporating automated processes for seamless operation.
Prerequisites for Implementation
To successfully implement a backup and mirroring strategy using rsync, ensure that you have:
Two Servers:
- Primary Web Server: IP address
192.168.0.100
, accessible via the hostnamewebserver.example.com
. - Backup Server: IP address
192.168.0.101
, accessible via the hostnamebackup.example.com
.
Required Software:
Both servers should have the rsync utility installed. If it's not already present, install it using the appropriate package management command for your operating system:
# Debian, Ubuntu, and derivatives
sudo apt install rsync
# RHEL, CentOS, Fedora, and derivatives
sudo yum install rsync
# Gentoo
sudo emerge -a sys-apps/rsync
# Alpine
sudo apk add rsync
# Arch Linux
sudo pacman -S rsync
# OpenSUSE
sudo zypper install rsync
# FreeBSD
sudo pkg install rsync
SSH Access:
Ensure that SSH is enabled on both servers to facilitate secure connections. This will also allow us to set up passwordless SSH login, streamlining the automation of data transfers.
Setting Up Passwordless SSH Login
Automating rsync processes requires passwordless SSH to help run synchronizations without needing to input credentials each time. Here’s how to set it up:
- Log in to the Primary Web Server (
webserver.example.com
). Generate SSH keys; accept the default file location and leave the passphrase blank by executing:
ssh-keygen -t rsa -b 2048
Copy your public key to the Backup Server:
ssh-copy-id [email protected]
Replace
user
with your actual username on the Backup Server. You will need to input your password one last time.Test the connection:
ssh [email protected]
If you access the backup server without a password prompt, the configuration is successful.
Synchronizing Data Using Rsync
With passwordless SSH set, you're ready to utilize rsync for data synchronization. The data on your web server is typically stored in /var/www/html/
.
Basic Rsync Command
To execute a manual backup from the Primary Web Server to the Backup Server, run:
rsync -avz /var/www/html/ [email protected]:/path/to/backup/directory
Explanation of Command Options:
-a
: Archive mode to preserve file permissions, timestamps, etc.-v
: Verbose output to show files being transferred.-z
: Compress file data during transfer for efficient use of bandwidth.
Syncing Files to Backup Storage
Ensure to update /path/to/backup/directory
with the actual backup location on the Backup Server. A full command example is:
rsync -avz /var/www/html/ [email protected]:/backup/webserver
Mirroring Source Directory
To mirror the web server directory, including deleting files from the backup that no longer exist on the primary server:
rsync -avz --delete /var/www/html/ [email protected]:/backup/webserver
This effectively ensures that the backup directory is an exact replica of the original.
Automating Backups with Cron
Scheduling regular backups using cron jobs ensures consistent and timely data protection. To automate your syncing process, follow these steps:
Open the crontab for editing on your Primary Web Server:
crontab -e
Schedule the rsync command to run daily at midnight by adding the following line:
0 0 * * * rsync -avz --delete /var/www/html/ [email protected]:/backup/webserver
Once saved, this setting will run every day at midnight, keeping your backup current without manual intervention.
Verifying Backup Operation
After setting up the cron job, confirming that everything works correctly is essential:
Check the cron logs to ensure jobs are executing as scheduled:
grep CRON /var/log/syslog
Log in to the Backup Server and verify that the files in
/backup/webserver
match those from the primary web server's/var/www/html/
directory.As a practical test, delete a file from the Primary Web Server and confirm it is removed from the backup directory after the subsequent rsync operation.
Conclusion
By leveraging the power of rsync, you can establish a reliable process for backing up and mirroring your web server data. The integration of passwordless SSH and cron automation eliminates the need for manual efforts, thereby enhancing efficiency and reliability in safeguarding your data. Implementing this setup ensures that your web content stays protected and readily available, even in the face of unexpected server issues.