1. Rsync简介与版本说明
Rsync是一款开源的快速增量备份工具,支持本地复制和远程同步。它采用”rsync算法”实现增量传输,只传输文件的变化部分,大大提高了同步效率。更多学习教程www.fgedu.net.cn
Rsync最新版本:
Rsync 3.3.0(最新稳定版)- 发布于2024年,提供增强的安全性和性能优化
Rsync 3.2.7 – 长期支持版本,适合生产环境
Rsync 3.2.5 – 旧版稳定版本,维护支持中
Rsync 3.1.3 – 传统稳定版本
Rsync主要特性:
增量传输:只传输文件变化的部分,节省带宽和时间
保持属性:支持保持文件权限、时间戳、软硬链接等属性
压缩传输:支持数据压缩,减少网络传输量
安全传输:支持SSH加密通道,保证数据安全
排除规则:支持灵活的文件排除规则
2. Rsync下载方式
Rsync提供源码编译和预编译包两种下载方式。学习交流加群风哥微信: itpux-com
方式一:官方源码下载
# cd /fgedudb/rsync
# wget https://download.samba.org/pub/rsync/src/rsync-3.3.0.tar.gz
# 下载输出案例如下:
–2026-04-04 12:00:15– https://download.samba.org/pub/rsync/src/rsync-3.3.0.tar.gz
Resolving download.samba.org… 176.9.154.24
Connecting to download.samba.org|176.9.154.24|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 1123456 (1.1M) [application/gzip]
Saving to: ‘rsync-3.3.0.tar.gz’
rsync-3.3.0.tar.gz 100%[===============================================>] 1.07M 2.5MB/s in 0.4s
2026-04-04 12:00:16 URL:https://download.samba.org/pub/rsync/src/rsync-3.3.0.tar.gz [1123456/1123456] -> “rsync-3.3.0.tar.gz” [1]
# 验证下载文件
# ls -lh rsync-3.3.0.tar.gz
-rw-r–r– 1 root root 1.1M Apr 4 12:00 rsync-3.3.0.tar.gz
# 验证MD5校验和
# md5sum rsync-3.3.0.tar.gz
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6 rsync-3.3.0.tar.gz
# 验证GPG签名
# wget https://download.samba.org/pub/rsync/src/rsync-3.3.0.tar.gz.asc
# gpg –verify rsync-3.3.0.tar.gz.asc rsync-3.3.0.tar.gz
gpg: Signature made Fri Jan 15 10:30:45 2024 CST
gpg: using RSA key A1B2C3D4E5F6G7H8
gpg: Good signature from “Rsync Release Signing Key”
方式二:系统包管理器安装
# yum install -y rsync
# 安装输出案例如下:
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
–> Running transaction check
—> Package rsync.x86_64 0:3.2.7-1.el7 will be installed
–> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
rsync x86_64 3.2.7-1.el7 base 315 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 315 k
Installed size: 645 k
Downloading packages:
rsync-3.2.7-1.el7.x86_64.rpm | 315 kB 00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : rsync-3.2.7-1.el7.x86_64 1/1
Verifying : rsync-3.2.7-1.el7.x86_64 1/1
Installed:
rsync.x86_64 0:3.2.7-1.el7
Complete!
# Ubuntu/Debian安装
# apt-get install -y rsync
# 查看已安装版本
# rsync –version
rsync version 3.2.7 protocol version 31
Copyright (C) 1996-2024 by Andrew Tridgell, Wayne Davison, and others.
Web site: https://rsync.samba.org/
Capabilities:
64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
socketpairs, hardlinks, hardlink-specials, symlinks, IPv6, batchfiles,
inplace, append, ACLs, xattrs, optional protect-args, iconv,
prealloc, stop-at, no crtimes
Optimizations:
SIMD, asm, openssl-crypto
Checksum list:
md5 md4 none
Compress list:
zstd lz4 zlibx zlib none
方式三:下载Windows版本
# cd /fgedudb/rsync
# wget https://itefix.net/download/free/cwrsync_6.3.0_x64_free.zip
# 下载输出案例如下:
–2026-04-04 12:05:30– https://itefix.net/download/free/cwrsync_6.3.0_x64_free.zip
Resolving itefix.net… 91.121.87.18
Connecting to itefix.net|91.121.87.18|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 5678901 (5.4M) [application/zip]
Saving to: ‘cwrsync_6.3.0_x64_free.zip’
cwrsync_6.3.0_x64_free.zip 100%[===============================================>] 5.42M 3.2MB/s in 1.7s
2026-04-04 12:05:32 URL:https://itefix.net/download/free/cwrsync_6.3.0_x64_free.zip [5678901/5678901] -> “cwrsync_6.3.0_x64_free.zip” [1]
# 验证文件
# ls -lh cwrsync_6.3.0_x64_free.zip
-rw-r–r– 1 root root 5.4M Apr 4 12:05 cwrsync_6.3.0_x64_free.zip
3. Rsync安装实战
完成下载后,进行编译安装。学习交流加群风哥QQ113257174
步骤1:安装编译依赖
# yum install -y gcc gcc-c++ make autoconf
# yum install -y popt-devel openssl-devel lz4-devel zstd-devel
# yum install -y acl-devel attr-devel
# 或使用apt-get(Ubuntu/Debian)
# apt-get install -y build-essential autoconf
# apt-get install -y libpopt-dev libssl-dev liblz4-dev libzstd-dev
# apt-get install -y libacl1-dev libattr1-dev
步骤2:编译安装Rsync
# cd /fgedudb/rsync
# tar -xzf rsync-3.3.0.tar.gz
# cd rsync-3.3.0
# 配置编译选项
# ./configure \
–prefix=/usr/local/rsync \
–enable-acl-support \
–enable-xattr-support \
–enable-zstd \
–enable-lz4 \
–with-included-popt=no \
–with-included-zlib=no
# 配置输出案例如下:
checking for gcc… gcc
checking whether the C compiler works… yes
checking for a BSD-compatible install… /usr/bin/install -c
checking for poptGetContext in -lpopt… yes
checking for OpenSSL… yes
checking for lz4… yes
checking for zstd… yes
checking for ACL support… yes
checking for xattr support… yes
rsync 3.3.0 configuration successful
# 编译安装
# make
# make install
# 编译输出案例如下:
gcc -I. -I. -O2 -DHAVE_CONFIG_H -Wall -W -c rsync.c -o rsync.o
gcc -I. -I. -O2 -DHAVE_CONFIG_H -Wall -W -c io.c -o io.o
gcc -I. -I. -O2 -DHAVE_CONFIG_H -Wall -W -c generator.c -o generator.o
…
gcc -o rsync rsync.o io.o generator.o -lpopt -lzstd -llz4 -lcrypto -lacl -lattr
# 验证安装
# /usr/local/rsync/bin/rsync –version
rsync version 3.3.0 protocol version 31
Copyright (C) 1996-2024 by Andrew Tridgell, Wayne Davison, and others.
Web site: https://rsync.samba.org/
4. Rsync服务端配置实战
Rsync可以以守护进程模式运行,提供高效的文件同步服务。更多学习教程公众号风哥教程itpux_com
步骤1:创建配置文件
# mkdir -p /etc/rsyncd
# 创建主配置文件
# vi /etc/rsyncd/rsyncd.conf
# 全局配置
uid = root
gid = root
use chroot = yes
max connections = 200
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
transfer logging = yes
log format = %t %a %m %f %b
timeout = 300
reverse lookup = no
motd file = /etc/rsyncd/rsyncd.motd
# 定义备份模块
[backup]
path = /backup/rsync
comment = Backup Directory
read only = no
list = yes
auth users = backup
secrets file = /etc/rsyncd/rsyncd.secrets
hosts allow = 192.168.1.0/24
hosts deny = *
exclude = lost+found/
# 定义数据库备份模块
[database]
path = /backup/database
comment = Database Backup Directory
read only = no
list = yes
auth users = dbbackup
secrets file = /etc/rsyncd/rsyncd.secrets
hosts allow = 192.168.1.0/24
hosts deny = *
exclude = *.tmp *.log
# 定义应用备份模块
[application]
path = /backup/application
comment = Application Backup Directory
read only = no
list = yes
auth users = appbackup
secrets file = /etc/rsyncd/rsyncd.secrets
hosts allow = 192.168.1.0/24
hosts deny = *
步骤2:创建认证文件
# vi /etc/rsyncd/rsyncd.secrets
backup:backup_password_123
dbbackup:dbbackup_password_456
appbackup:appbackup_password_789
# 设置文件权限(必须为600)
# chmod 600 /etc/rsyncd/rsyncd.secrets
# 创建欢迎信息文件
# vi /etc/rsyncd/rsyncd.motd
++++++++++++++++++++++++++++++++++++++++
+ Welcome to Rsync Server +
+ fgedu.net.cn +
++++++++++++++++++++++++++++++++++++++++
# 创建备份目录
# mkdir -p /backup/rsync
# mkdir -p /backup/database
# mkdir -p /backup/application
# 设置目录权限
# chmod 755 /backup/rsync
# chmod 755 /backup/database
# chmod 755 /backup/application
步骤3:配置systemd服务
# vi /etc/systemd/system/rsyncd.service
[Unit]
Description=Rsync Server Daemon
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/rsync –daemon –config=/etc/rsyncd/rsyncd.conf
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
# 重载systemd配置
# systemctl daemon-reload
# 启动rsync服务
# systemctl start rsyncd
# systemctl enable rsyncd
# 验证服务状态
# systemctl status rsyncd
● rsyncd.service – Rsync Server Daemon
Loaded: loaded (/etc/systemd/system/rsyncd.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2026-04-04 12:15:00 CST; 10s ago
Main PID: 28123 (rsync)
Tasks: 1
Memory: 1.2M
CGroup: /system.slice/rsyncd.service
└─28123 /usr/bin/rsync –daemon –config=/etc/rsyncd/rsyncd.conf
Apr 04 12:15:00 fgedu.net.cn systemd[1]: Started Rsync Server Daemon.
# 验证端口监听
# netstat -tlnp | grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 28123/rsync
5. Rsync客户端同步实战
配置完成后,客户端可以使用rsync命令进行数据同步。from:www.itpux.com
步骤1:基本同步命令
# rsync -avz /source/data/ /dest/data/
# 输出案例如下:
sending incremental file list
./
file1.txt
file2.txt
dir1/
dir1/file3.txt
sent 1,234,567 bytes received 123 bytes 2,469,380.00 bytes/sec
total size is 10,000,000 speedup is 8.10
# 远程同步(SSH方式)
# rsync -avz -e ssh /source/data/ root@192.168.1.51:/dest/data/
# 输出案例如下:
root@192.168.1.51’s password:
sending incremental file list
./
file1.txt
file2.txt
sent 1,234,567 bytes received 123 bytes 246,938.00 bytes/sec
total size is 10,000,000 speedup is 8.10
# 远程同步(rsync守护进程方式)
# rsync -avz –password-file=/etc/rsyncd.pass backup@192.168.1.51::backup /dest/backup/
# 输出案例如下:
receiving incremental file list
./
file1.txt
file2.txt
dir1/
dir1/file3.txt
sent 123 bytes received 1,234,567 bytes 493,876.00 bytes/sec
total size is 10,000,000 speedup is 8.10
步骤2:常用参数说明
-a, –archive 归档模式,等同于-rlptgoD
-v, –verbose 详细输出模式
-z, –compress 传输时压缩数据
-h, –human-readable 以人类可读格式输出
–progress 显示传输进度
–partial 保留部分传输的文件
–delete 删除目标端多余文件
–exclude 排除指定文件
–include 包含指定文件
–bwlimit 限制带宽(KB/s)
–timeout 设置超时时间
# 完整同步示例
# rsync -avz –progress –delete –exclude=’*.log’ –exclude=’*.tmp’ \
/source/data/ backup@192.168.1.51::backup/
# 输出案例如下:
receiving incremental file list
./
file1.txt
1,234,567 100% 1.18MB/s 0:00:01 (xfr#1, to-chk=2/5)
file2.txt
2,345,678 100% 2.24MB/s 0:00:01 (xfr#2, to-chk=1/5)
dir1/
dir1/file3.txt
3,456,789 100% 3.30MB/s 0:00:01 (xfr#3, to-chk=0/5)
sent 123 bytes received 7,037,034 bytes 2,814,862.80 bytes/sec
total size is 7,037,034 speedup is 1.00
步骤3:创建客户端密码文件
# echo “backup_password_123” > /etc/rsyncd.pass
# 设置权限
# chmod 600 /etc/rsyncd.pass
# 使用密码文件同步
# rsync -avz –password-file=/etc/rsyncd.pass /source/data/ backup@192.168.1.51::backup/
# 输出案例如下:
sending incremental file list
./
file1.txt
file2.txt
sent 1,234,567 bytes received 123 bytes 493,876.00 bytes/sec
total size is 10,000,000 speedup is 8.10
6. Rsync高级应用实战
Rsync支持多种高级功能,满足复杂场景需求。
步骤1:增量备份脚本
# vi /usr/local/bin/rsync_backup.sh
#!/bin/bash
# 配置变量
SOURCE_DIR=”/source/data/”
REMOTE_HOST=”192.168.1.51″
REMOTE_MODULE=”backup”
REMOTE_USER=”backup”
PASSWORD_FILE=”/etc/rsyncd.pass”
LOG_FILE=”/var/log/rsync_backup.log”
DATE=$(date +%Y%m%d_%H%M%S)
# 执行同步
rsync -avz –progress –delete \
–exclude=’*.log’ \
–exclude=’*.tmp’ \
–exclude=’lost+found/’ \
–password-file=$PASSWORD_FILE \
–log-file=$LOG_FILE \
$SOURCE_DIR $REMOTE_USER@$REMOTE_HOST::$REMOTE_MODULE/
# 检查执行结果
if [ $? -eq 0 ]; then
echo “[$DATE] Backup completed successfully” >> $LOG_FILE
else
echo “[$DATE] Backup failed with error code $?” >> $LOG_FILE
# 发送告警邮件
echo “Rsync backup failed at $DATE” | mail -s “Rsync Backup Alert” admin@fgedu.net.cn
fi
# 设置执行权限
# chmod +x /usr/local/bin/rsync_backup.sh
# 执行备份
# /usr/local/bin/rsync_backup.sh
# 输出案例如下:
sending incremental file list
./
file1.txt
file2.txt
sent 1,234,567 bytes received 123 bytes 493,876.00 bytes/sec
total size is 10,000,000 speedup is 8.10
[20260404_121500] Backup completed successfully
步骤2:定时同步任务
# crontab -e
# 每天凌晨2点执行全量同步
0 2 * * * /usr/local/bin/rsync_backup.sh >> /var/log/rsync_cron.log 2>&1
# 每4小时执行增量同步
0 */4 * * * rsync -avz –password-file=/etc/rsyncd.pass /source/data/ backup@192.168.1.51::backup/ >> /var/log/rsync_incremental.log 2>&1
# 每周日凌晨3点执行清理
0 3 * * 0 rsync -avz –delete –password-file=/etc/rsyncd.pass /source/data/ backup@192.168.1.51::backup/ >> /var/log/rsync_weekly.log 2>&1
# 查看定时任务
# crontab -l
0 2 * * * /usr/local/bin/rsync_backup.sh >> /var/log/rsync_cron.log 2>&1
0 */4 * * * rsync -avz –password-file=/etc/rsyncd.pass /source/data/ backup@192.168.1.51::backup/ >> /var/log/rsync_incremental.log 2>&1
0 3 * * 0 rsync -avz –delete –password-file=/etc/rsyncd.pass /source/data/ backup@192.168.1.51::backup/ >> /var/log/rsync_weekly.log 2>&1
步骤3:双向同步配置
# vi /usr/local/bin/rsync_bidirectional.sh
#!/bin/bash
LOCAL_DIR=”/data/sync/”
REMOTE_HOST=”192.168.1.52″
REMOTE_DIR=”/data/sync/”
PASSWORD_FILE=”/etc/rsyncd.pass”
# 本地到远程同步
echo “Syncing local to remote…”
rsync -avz –delete –password-file=$PASSWORD_FILE \
$LOCAL_DIR backup@$REMOTE_HOST::sync/
# 远程到本地同步
echo “Syncing remote to local…”
rsync -avz –delete –password-file=$PASSWORD_FILE \
backup@$REMOTE_HOST::sync/ $LOCAL_DIR
echo “Bidirectional sync completed at $(date)”
# 设置执行权限
# chmod +x /usr/local/bin/rsync_bidirectional.sh
7. 生产环境最佳实践
在生产环境中使用Rsync需要考虑性能优化、安全加固和监控告警。
性能优化配置
# vi /etc/rsyncd/rsyncd.conf
# 增加并发连接数
max connections = 500
# 启用压缩
dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz *.lzma *.xz *.zst
# 优化传输参数
timeout = 600
max verbosity = 1
# 客户端性能优化参数
# rsync -avz –progress \
–compress-level=6 \
–partial \
–inplace \
–bwlimit=10240 \
–timeout=600 \
/source/data/ backup@192.168.1.51::backup/
# 输出案例如下:
sending incremental file list
./
file1.txt
1,234,567 100% 1.18MB/s 0:00:01 (xfr#1, to-chk=2/5)
sent 1,234,567 bytes received 123 bytes 246,938.00 bytes/sec
total size is 10,000,000 speedup is 8.10
安全加固配置
# firewall-cmd –permanent –add-port=873/tcp
# firewall-cmd –reload
# 输出案例如下:
success
# 限制访问IP
# vi /etc/rsyncd/rsyncd.conf
hosts allow = 192.168.1.0/24, 10.0.0.0/8
hosts deny = *
# 使用SSH隧道加密
# rsync -avz -e “ssh -p 22 -i /root/.ssh/id_rsa” \
/source/data/ root@192.168.1.51:/dest/data/
# 输出案例如下:
sending incremental file list
./
file1.txt
sent 1,234,567 bytes received 123 bytes 246,938.00 bytes/sec
total size is 10,000,000 speedup is 8.10
监控与日志配置
# vi /etc/rsyncd/rsyncd.conf
transfer logging = yes
log format = %t %a %m %f %b %l
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
# 日志轮转配置
# vi /etc/logrotate.d/rsyncd
/var/log/rsyncd.log {
daily
rotate 30
compress
delaycompress
missingok
notifempty
create 0644 root root
}
# 监控脚本
# vi /usr/local/bin/check_rsync.sh
#!/bin/bash
# 检查rsync服务状态
if ! pgrep -x “rsync” > /dev/null; then
echo “Rsync service is not running!” | mail -s “Rsync Alert” admin@fgedu.net.cn
systemctl restart rsyncd
fi
# 检查磁盘空间
USAGE=$(df -h /backup | tail -1 | awk ‘{print $5}’ | sed ‘s/%//’)
if [ $USAGE -gt 80 ]; then
echo “Backup disk usage is ${USAGE}%” | mail -s “Disk Space Alert” admin@fgedu.net.cn
fi
# 设置定时检查
# crontab -e
*/5 * * * * /usr/local/bin/check_rsync.sh
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
