由于是在生产环境操作,所以权限限制比较多,遇到了各种奇奇怪怪的问题,网上的各类解决办法都看过了,坑也挨个踩了一遍,现在记录一下。
服务端 | 客户端 |
192.168.10.11 | 10.1.203.20 |
rsync既可以拉,也可以推。客户端可以访问服务端,服务端无法与客户端通信。所以rsync只能采取拉的操作方式,将服务端文件定时拉取到客户端进行备份
### 服务端和客户端配置文件相同
uid = rsync
gid = rsync
use chroot = no
max connections = 10
motd file = /etc/rsyncd.motd
lock file=/var/run/rsync.lock
log file=/var/log/rsyncd.log
[backup]
path =/backup129/ ###客户端可以注释此行
comment = backup file
ignore errrors
list=false
auth users= backup
secrets file=/etc/rsync.password ###客户端可以注释此行
注意区别:
客户端密码文件与服务端密码文件是不同的,客户端只写密码,不写用户!!!
服务端密码文件:
backup:123456
客户端端密码文件:
123456
同步脚本
rsync -av backup@192.168.10.11::backup /backup129 --password-file=/etc/rsync.password
[root@linux-p backup129]# rsync -av backup@192.168.10.11::backup /backup129 --password-file=/etc/rsync.password
*****************************************************
Welcome To Use The Rsync services!
*****************************************************
receiving incremental file list
./
11floor_1.log
11floor_2.log
11floor_3.log
sent 84 bytes received 289 bytes 32.43 bytes/sec
total size is 26 speedup is 0.07
[root@linux-p backup129]# ls
注意点1
密码文件要设置权限:600
chmod 600 /etc/rsync.password
注意点2
共享目录权限,权限是指uid = rsync所代表的用户,设置为755,所有者设置rsync
chmod 755 -R /backup129/
chown rsync:rsync -R /backup129/
注意点3
873端口加入防火墙
firewall-cmd --permanent --add-port=873/tcp
firewall-cmd --reload
注意点4
这个一定得注意,加入selinux设置为disable就可以,但是生成环境不能这样操作哦,要按照下面进行
setsebool -P rsync_export_all_ro=1
注意点5
守护进程模式启动,实际上只需要服务端守护进程启动即可,客户端无需启动守护进程,当然启动也没事。
方式有二种,推荐第一种
方式1:
systemctl start rsyncd
方式2:
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
用第二种方式启动,无法通过systemctl status rsyncd 查看状态
[root@linux-p backup129]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; disabled; vendor preset: disabled)
Active: active (running) since 二 2021-09-07 16:48:13 CST; 1min 34s ago
Main PID: 6331 (rsync)
Tasks: 1
CGroup: /system.slice/rsyncd.service
└─6331 /usr/bin/rsync --daemon --no-detach
9月 07 16:48:13 linux-p systemd[1]: Started fast remote file copy program daemon.
注意点6
配置文件中的UID代表的用户必须真实存在,auth users= backup代表的用户是虚拟的,不需要存在
创建用户,建议采用无登录方式创建,安全
useradd rsync -s /sbin/nologin -M
注意点7
有问题,查看日志文件
[root@linux-p backup129]# tail -f /var/log/rsyncd.log
2021/09/07 15:53:41 [5741] building file list
2021/09/07 15:53:41 [5741] rsync: opendir "." (in backup) failed: Permission denied (13)
2021/09/07 15:55:47 [5779] params.c:Parameter() - Ignoring badly formed line in config file: ignore errrors
2021/09/07 15:55:47 [5779] name lookup failed for 10.1.203.35: Name or service not known
2021/09/07 15:55:47 [5779] connect from UNKNOWN (10.1.203.35)
2021/09/07 15:55:47 [5779] rsync on backup/ from backup@UNKNOWN (10.1.203.35)
2021/09/07 15:55:47 [5779] building file list
2021/09/07 15:55:47 [5779] sent 448 bytes received 91 bytes total size 26
2021/09/07 16:47:07 [4145] sent 0 bytes received 0 bytes total size 0
2021/09/07 16:48:13 [6331] rsyncd version 3.1.2 starting, listening on port 873
常见错误
错误1
错误一:
@ERROR: auth failed on module backup
rsync: connection unexpectedly closed (90 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(150)
说明:这是因为密码设置错了,无法登入成功,检查一下rsync.pwd,看客服是否匹配。还有服务器端没启动rsync 服务也会出现这种情况。
错误二:
password file must not be other-accessible
continuing without password file
Password:
说明:这是因为rsyncd.pwd rsyncd.sec的权限不对,应该设置为600。如:chmod 600 rsyncd.pwd
错误三:
@ERROR: chroot failed
rsync: connection unexpectedly closed (75 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(150)
说明:这是因为你在 rsync.conf 中设置的 path 路径不存在,要新建目录才能开启同步
错误四:
rsync: failed to connect to 218.107.243.2: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(104) [receiver=2.6.9]
说明:防火墙问题导致,这个最好先彻底关闭防火墙,排错的基本法就是这样,无论是S还是C,还有ignore errors选项问题也会导致
错误五:
@ERROR: access denied to www from unknown (192.168.1.123)
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(359)
说明:此问题很明显,是配置选项host allow的问题,初学者喜欢一个允许段做成一个配置,然后模块又是同一个,致使导致
错误六:
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(244) [generator=2.6.9]
rsync error: received SIGUSR1 (code 19) at main.c(1182) [receiver=2.6.9]
说明:导致此问题多半是服务端服务没有被正常启动,到服务器上去查查服务是否有启动,然后查看下 /var/run/rsync.pid 文件是否存在,最干脆的方法是杀死已经启动了服务,然后再次启动服务或者让脚本加入系统启动服务级别然后shutdown -r now服务器
错误七:
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(604) [sender=2.6.9]
说明:原数据目录里没有数据存在
错误八:
@ERROR: auth failed on module bachup
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:服务器端该模块(backup)需要验证用户名密码,但客户端没有提供正确的用户名密码,认证失败。 提供正确的用户名密码解决此问题
。