用于多台或单台机器内备份数据 可以增量,可以全量 本地模式、远程模式、守护进程模式
备份机器backup一般和负载均衡, web服务器, 数据库, 在同一网段, 所以需要异地的再复制一份backup(异地容灾)
本地模式
远程模式
偶尔用用
scp -rp /etc root@backup:/tmp -P # r:递归rsync -avz /etc root@backup:/tmp # a:递归并保留原信息,v:显示详情,z:自动打包解包,--delete完全同步
守护进程模式(常用)
服务端配置
- 复制配置文件
cat >/etc/rsyncd.conf<<EOF##rsyncd.conf start####rsyncd 20221111fake super = yesuid =rsyncgid =rsyncuse chroot = nomax connections = 200timeout = 300pid file = /var/run/rsyncd.pidlock file = /var/run/rsync.locklog file = /var/log/rsyncd.logignore errorsread only = falselist = false#hosts allow = 172.16.1.0/24##hosts deny = 0.0.0.0/32auth users = rsync_backupsecrets file = /etc/rsync.password[data]comment = "backup dir by Rdymy"path = /dataEOF
```bash useradd -s /sbin/nologin -M rsync echo ‘rsync_backup:1’ >/etc/rsync.password chmod 600 /etc/rsync.password##rsyncd.conf start####rsyncd 20221111fake super = yes # 不开启c7报错uid =rsync # 服务端运行的虚拟用户!!!gid =rsync # 同上!!!use chroot = nomax connections = 200 # 最大连接数timeout = 300 # 连接超时时间pid file = /var/run/rsyncd.pid # 存放服务的pid号lock file = /var/run/rsync.lock # 服务的锁文件,防止重复运行log file = /var/log/rsyncd.log # 服务端日志目录,不配置默认到/var/log/message!!!ignore errors # 忽略错误read only = false #可以进行读写list = false # 关闭rsync在客户端显示模块列表的功能#hosts allow = 172.16.1.0/24 # 只准许哪些ip或网段访问!!!##hosts deny = 0.0.0.0/32 # 拒绝哪些网段访问!!!auth users = rsync_backup # 服务端验证用户名!!!secrets file = /etc/rsync.password # 服务端验证密码文件!!![data] # 模块名!!!comment = "backup dir by Rdymy" # 注释path = /data # 模块对应的目录!!!
创建同步目录
mkdir xxx chown rsync.rsync xxx
```bash
systemctl restart rsyncd
systemctl enable rsyncd
ps -ef | grep rsync # 检查服务是否启动
ss -lntup | grep rsync # 检查端口是否开启
客户端配置并测试
echo '1' >/etc/rsync.pass
chmod 600 /etc/rsync.pass
rsync -avz /etc/hosts rsync_backup@backup::data --password-file=/etc/rsync.pass
#less /var/log/rsyncd.log # 有问题可以看看日志
原理总结(了解)
排障
rsync: failed to connect to exam_backup (47.110.82.86): Connection timed out (110)
rsync error: error in socket IO (code 10) at clientserver.c(127) [sender=3.1.3]
# 解决方法
放行阿里云安全组873端口(ss -lntup | grep rsync)
包括防火墙,selinux什么的没关也记得关了
@ERROR: auth failed on module data
rsync error: error starting client-server protocol (code 5) at main.c(1648或1649) [sender=3.1.2]
# 解决方法
密码文件的属主属组可能给了匿名用户,要给当前用户比如root
(耗了我好几个小时,注意看/var/log/rsync.log,解决不了的问题可以第二天再处理,时间宝贵)
其他可能:密码文件不存在,密码写错,未指定客户端用户
