Mirror synchronization, especially mirroring of open source software, relies on accurate and efficient file transfer processes. Reasonable configuration of Rsync on CentOS can not only ensure the timeliness and integrity of the mirror, but also reduce network and disk overhead, and achieve the goal of operation and maintenance automation and platform high availability. This article will explain in depth how to configure the Rsync tool in the CentOS system to complete the whole process of mirror synchronization.
Introduction to Rsync and core advantages
Rsyn is a lightweight tool for remote file synchronization in Linux system, which is often used for file mirroring and updating between local and remote, server and server. It uses the "delta encoding" algorithm to synchronize only the changed part of the file content, which greatly improves the network transmission efficiency.
The core advantage lies in incremental synchronization, bandwidth saving, support for compression and encrypted transmission, can maintain the original permissions, owner, timestamp, support soft links, device files, sparse files, breakpoint continuation and partial failure recovery capabilities. These features make Rsync one of the core tools for building operating system mirror sites, cloud backup servers, and remote synchronization nodes.
Rsync installation and basic configuration on CentOS
1. Install Rsync
Most CentOS systems have Rsync installed by default. If not, you can use YUM to install it:
sudo yum install -y rsync
Confirm the version:
rsync --version
It is recommended to keep it above version 3.x to ensure compatibility with new parameters and transmission optimization.
2. Create a synchronization directory
Suppose we want to synchronize the CentOS official image to the local:
sudo mkdir -p /data/mirror/centos
sudo chown -R root:root /data/mirror
Configure the Rsync client to pull mirror data
Rsync can be run as a server or as a client to initiate synchronization requests. The most common way is to actively pull remote mirror data as a client.
1. Find the source mirror site
Take TUNA open source mirror site as an example, it provides a public rsync interface:
rsync://mirrors.tuna.tsinghua.edu.cn/centos/
We need to synchronize the contents of this site to the local /data/mirror/centos directory.
2. Execute the synchronization command
rsync -avz --delete rsync://mirrors.tuna.tsinghua.edu.cn/centos/ /data/mirror/centos/
Command parameter analysis:
-a: archive mode, retain permissions, soft links, timestamps, etc.
-v: output detailed process
-z: enable compression to speed up transmission
--delete: delete the deleted files on the source side in the target directory to keep the image consistent
The last slash / is used to synchronize the directory content, not the directory itself
The first synchronization time may be longer, depending on the image size and bandwidth.
3. Accelerated synchronization suggestions
If the source station supports rsync acceleration, it is recommended to select a closer or faster mirror node, and the resource priority can be controlled through ionice and nice:
ionice -c2 -n7 nice -n 19 rsync -azvP --delete rsync://mirrors.ustc.edu.cn/centos/ /data/mirror/centos/
The -P parameter is used to display real-time progress, which is especially suitable for large file transfers.
Configure Rsync as a server to provide mirroring
In some scenarios, enterprises or organizations need to further provide local mirroring to downstream nodes for use. In this case, the Rsync server needs to be configured.
1. Edit the configuration file /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 10
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
read only = yes
list = yes
[centos]
path = /data/mirror/centos
comment = CentOS Local Mirror
2. Start the Rsync daemon
rsync --daemon
Confirm listening port 873:
netstat -tunlp | grep rsync
You can set it to start automatically at boot time by customizing the systemd service or adding /etc/rc.d/rc.local.
3. Test the connection
Execute on the remote host:
rsync rsync:///centos/
The directory list should be returned to confirm that the service is available.
Write an automatic synchronization script
Long-term maintenance of the image requires scheduled synchronization, which can be achieved using cron scheduled tasks and Bash scripts.
1. Create a synchronization script /opt/rsync-centos.sh
#!/bin/bash
RSYNC_LOG="/var/log/rsync-centos.log"
MIRROR_DIR="/data/mirror/centos"
RSYNC_URL="rsync://mirrors.tuna.tsinghua.edu.cn/centos/"
rsync -azv --delete $RSYNC_URL $MIRROR_DIR >> $RSYNC_LOG 2>&1
Grant execution permissions:
chmod +x /opt/rsync-centos.sh
2. Add a scheduled task
Edit a scheduled task:
crontab -e
Add entry (executed at 2 am every day):
0 2 * * * /opt/rsync-centos.sh
You can check the execution status through the log file /var/log/rsync-centos.log.
Common problems and troubleshooting methods
Q1. Synchronization is too slow
A1: Check whether the domestic source is used? It is recommended to use -z compression acceleration and use rsync --bwlimit=xxx to limit the speed to avoid affecting the bandwidth.
Q2. Synchronization failure caused by permission issues
A2: Confirm the local directory permissions. The Rsync server path must ensure that the read and write permissions are correct.
Q3. --delete accidentally deletes local files
A3: It is recommended not to use --delete for the first time. You can execute dry-run first:
rsync -azvn --delete ...
After confirmation, execute the official command.
Q4. The server refuses the connection
A4: Confirm that port 873 is not blocked by the firewall, and the release rules are:
firewall-cmd --permanent --add-port=873/tcp
firewall-cmd --reload
Rsync is the backbone of building reliable mirror stations and data synchronization systems. Under the CentOS system, remote mirror pulling, verification, and local deployment can be completed through simple configuration. Whether it is building a local warehouse for the team or building an intranet source synchronization service within the enterprise, Rsync can play the advantages of high efficiency, security, and low load.
By combining timing scripts, permission management, server-side monitoring and other mechanisms, a multi-layer mirror system can also be built to support distributed deployment, disaster recovery switching, and multi-region accelerated access. For operation and maintenance engineers and system architects, proficiency in Rsync can not only improve operation and maintenance efficiency, but also be a basic ability to ensure data consistency and business continuity.