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
, hostnamewebserver.example.com
. - Backup Server: IP address
192.168.0.101
, hostnamebackup.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.
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.
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 .;