Rsync + Rsyncd 从入门到项目实战:自动化备份全攻略
一:rsync
rsync
是一个在本地或者远程之间进行文件增量传输的工具
语法
rsync [选项] 源路径 目标路径
选项
-r,--recursive 文件夹传输 -v, --verbose 显示详细输出 -l, --links 传输软链接 -z, --compress 传输时压缩数据 -u, --update 仅传输增量部分(包括文件内容改变) -P,--partial --progress 显示进度
-a, --archive 归档模式,保留权限、所有者、时间戳等,等同于 -rlpogtD -p, --perms 保留文件权限。 -o, --owner 保留文件所有者(需 root 权限)。 -g, --group 保留文件所属组。 -t, 保留文件修改时间。 -D 保留设备文件和特殊文件
--delete 目标目录与源目录保持一致的传输(覆盖)
--bwlimit=SPEED 限制传输速率(单位 Kbit/s)
--exclude=PATTERN 排除指定文件/文件夹(指定排除规则,可以在命令行中多次使用,添加多个排除规则。) --exclude-from=FILE 排除指定文件/文件夹(从文件中读取排除规则,文件可以包含多个规则,每行一个)
案例
限速传输
rsync -avP --bwlimit=500kb /source /dest
严格同步
rsync -avP --delete /source/ /dest/
传输并排除
-- exclude=PATTERN
rsync -avP --exclude='tmp/' --exclude='*.bak' /source /dest
--exclude-from=FILE
rsync --exclude-from='exclude-list.txt' /source/ /dest/
cat exclude-list.txt *.log temp/ *.bak .DS_Store
二:rsyncd
1)安装部署
下载
[root@hadoop108 ~]# yum install -y rsync
启动服务
[root@hadoop108 ~]# systemctl start rsyncd [root@hadoop108 ~]# systemctl enable rsyncd
检查
[root@hadoop108 ~]# systemctl status rsyncd [root@hadoop108 ~]# ps -ef | grep -i rsync [root@hadoop108 ~]# ss -lntup | grep -i rsync
添加用户
为什么要创建这么一个用户?
rsync
服务默认以系统用户身份运行。如果直接以root
运行,一旦服务被入侵,攻击者可能获得root
权限,危害整个系统,所以通过创建其他用户隔离服务。[root@hadoop108 ~]# useradd -Ms /sbin/nologin rsync
创建密码文件,并且赋予权限
密码格式 -> 用户名:密码
[root@hadoop108 ~]# echo 'rsync_backup:000000' > /etc/rsync.password [root@hadoop108 ~]# chmod 600 /etc/rsync.password
创建共享目录,修改目录所属
[root@hadoop108 ~]# mkdir -p /data [root@hadoop108 ~]# chown -R rsync:rsync /data/
进行配置
uid:指定 rsyncd 进程运行时使用的用户
gid:指定 rsyncd 进程运行时使用的用户组
hosts allow:仅允许该网段的 IP 访问(白名单)
auth users:指定客户端认证用户
secrets file:存储认证用户密码的文件路径
[root@hadoop108 ~]# vim /etc/rsyncd.conf
fake super = yes uid = rsync gid = rsync use chroot = no max connections = 2000 timeout = 600 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log ignore errors read only = false list = false hosts allow = 172.16.1.0/24 auth users = rsync_backup secrets file = /etc/rsync.password [data] # 指定共享数据模块别名 comment = by mk 2030-11-11 path = /data # 指定共享目录位置
重启服务
[root@hadoop108 ~]# systemctl restart rsyncd
2)本地测试
配置映射
传输
rsync_backup@backup::data
rsyncd用户@[机器]::[共享数据模块别名][root@hadoop108 ~]# rsync -avz /etc/hostname rsync_backup@backup::data
查看
[root@hadoop108 ~]# ll /data 总用量 4 -rw-r--r-- 1 rsync rsync 10 7月 29 21:07 hostname
3)远程测试
配置映射
在远程准备测试数据
[root@hadoop107 ~]# echo "abc" > /opt/software/a.txt [root@hadoop107 ~]# cat /opt/software/a.txt abc
传输
密码传输
[root@hadoop107 ~]# rsync -avz /opt/software/a.txt rsync_backup@backup::data Password: sending incremental file list a.txt sent 95 bytes received 43 bytes 55.20 bytes/sec total size is 4 speedup is 0.03
免密传输
设置密码文件
[root@hadoop107 ~]# echo '000000' > /etc/rsync.client
赋予权限
[root@hadoop107 ~]# chmod 600 /etc/rsync.client
传输
[root@hadoop107 ~]# rsync -avz /opt/software/a.txt rsync_backup@backup::data --password-file=/etc/rsync.client sending incremental file list sent 47 bytes received 20 bytes 134.00 bytes/sec total size is 4 speedup is 0.06
查看
[root@hadoop108 ~]# ll /data 总用量 8 -rw-r--r-- 1 rsync rsync 4 8月 1 15:29 a.txt -rw-r--r-- 1 rsync rsync 10 8月 1 15:23 hostname
三:项目
1)项目架构
角色 | 主机名 | 外网 | 内网 |
---|---|---|---|
rsync 服务端 | backup | 192.168.2.108 | 172.16.1.108 |
rsync 客户端 | nfs01 | 192.168.2.107 | 172.16.1.107 |
rsync 客户端 | web01 | 192.168.2.104 | 172.16.1.104 |
2)服务端搭建
安装
rsyncd
服务参考
3.1 安装部署
,但是要准备好/opt/module/b1
和/opt/module/b2
目录,并且配置rsync:rsync
文件所属配置
[root@hadoop108 ~]# vim /etc/rsyncd.conf
fake super = yes uid = rsync gid = rsync use chroot = no max connections = 2000 timeout = 600 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log ignore errors read only = false list = false hosts allow = 172.16.1.0/24 auth users = rsync_backup secrets file = /etc/rsync.password [data] comment = by mk 2030-11-11 path = /opt/module/b1 [backup] comment = by jack 2040-12-12 path = /opt/module/b2
重启服务
[root@hadoop108 ~]# systemctl restart rsyncd
3)客户端搭建
在
nfs01
和web01
主机上设置密码# nfs01 [root@hadoop107 ~]# vim /etc/rsync.client 000000 # web01 [root@hadoop104 ~]# vim /etc/rsync.client 000000
设置密码文件权限
# nfs01 [root@hadoop107 ~]# chmod 600 /etc/rsync.client # web01 [root@hadoop104 ~]# chmod 600 /etc/rsync.client
4)测试连通性
分别在
nfs01
和web01
进行传输# nfs01 [root@hadoop107 ~]# rsync -avz /etc/hosts rsync_backup@backup::backup --password-file=/etc/rsync.client # web01 [root@hadoop104 ~]# rsync -avz /etc/hosts rsync_backup@backup::data --password-file=/etc/rsync.client
检查
5)客户端脚本备份
(1)打包备份 /etc/ 、 /var/spool/cron/
(2)存放在服务端的back模块
(3)备份发送到 rsync 服务端
(4)删除本地旧的备份,写入定时
客户端编写脚本
在
web01
上执行相同操作# nfs01 [root@hadoop107 module]# vim bak-conf.sh
#!/bin/bash # 01 var 变量 rsync_server_user=rsync_backup rsync_server_ip=172.16.1.108 # rsync服务器ip rsync_server_mod=backup # 备份模块别名 rsync_server_pass=/etc/rsync.client md5=bak.md5 date=`date +%F_%w` #date=`date +"%Y%m%d%H%M%S"` ip=`hostname -I | cut -d " " -f2` bak_dir=/opt/module/b2/${ip} bak_file=conf-${date}.tar.gz # 02 打包备份 mkdir -p ${bak_dir} tar -czvf ${bak_dir}/${bak_file} /etc/ /var/spool/cron/ # 2.1 md5 md5sum ${bak_dir}/${bak_file} > ${bak_dir}/${md5} # 03 rsync 传输备份到服务端 rsync -avzuP ${bak_dir} ${rsync_server_user}@${rsync_server_ip}::${rsync_server_mod} --password-file=${rsync_server_pass} # 04 清理旧的备份 # find + rm find ${bak_dir} -type f -name "*.tar.gz" -mtime +7 | xargs rm -rf
给脚本提权
# nfs01 [root@hadoop107 module]# chmod 755 bak-conf.sh
执行脚本
# nfs01 [root@hadoop107 module]# sh bak-conf.sh
查看
# backup [root@hadoop108 b2]# ll 总用量 0 drwxr-xr-x 2 rsync rsync 38 8月 1 16:49 172.16.1.104 drwxr-xr-x 2 rsync rsync 38 8月 1 16:49 172.16.1.107
定时任务备份
在
web01
上执行相同操作# nfs01 [root@hadoop107 module]# crontab -e * * * * * sh /opt/module/bak-conf.sh &>/dev/null
6)服务端脚本清理
- 清理旧的备份只保留 180 天,每周 1 的不要删除
- 发送邮件
编写脚本
[root@hadoop108 module]# vim backup_check.sh
02 清理旧的备份
-name "*.tar.gz"
:找出周1到周日的备份
! -name "*_1.tar.gz"
: 排除周一的备份,剩下的备份就是周二到周日的。03发送检查邮件(需要配置 /etc/mail.rc)
邮件内容:
/opt/module/b2/
的完整目录结构(类似ls -R
,但更直观)。邮件标题:例如
2025-08-01 备份结果
(需确保${date}
已定义或替换为具体日期)。#!/bin/bash # 01 var 变量 date=`date +%F` # 02 清理旧的备份 find /opt/module/b2/ -type f -name "*.tar.gz" ! -name "*_1.tar.gz" -mtime +180 | xargs rm -rf # 03 md5 find /opt/module/b2/ -type f -name "bak.md5" | xargs md5sum -c &>/opt/module/b2/bak_result.txt # 04 发送检查邮件(需要配置 /etc/mail.rc) cat /opt/module/b2/bak_result.txt | mail -s "${date} 备份结果" yj456@qq.com
给脚本提权
[root@hadoop108 module]# chmod 755 backup_check.sh
总结
⚡rsync:高效的文件增量传输工具,支持压缩、限速、过滤和目录镜像。
🖥️rsyncd:服务端守护进程,统一管理共享模块,结合用户认证、白名单提升安全性。
🗄️项目实践:
客户端定时打包关键配置文件,生成 md5 校验并传输到服务端;
服务端集中存储,定期清理过期文件并校验完整性,邮件通知结果。
价值:实现了 安全、高效、自动化、集中化 的企业级备份方案。