守护进程模式搭建
1.环境准备
2.安装rsync(做备份的服务器都安装)
[root@backup ~]# yum install -y rsync
3.服务端配置
[root@backup ~]# vim /etc/rsyncd.conf #编辑配置文件
uid = rsync #指定运行程序的用户
gid = rsync #指定运行程序的用户
port = 873 #服务的监听端口
fake super = yes #不需要root用户启动,安全机制
use chroot = no #限制操作目录(出于安全考虑)
max connections = 200 #最大可连接数量(可调整)
timeout = 600 #超时时间(可调整)
ignore errors #忽略错误
read only = false #关闭只读权限
list = false #查看模块列表
auth users = rsync_backup #指定授权的虚拟用户
secrets file = /etc/rsync.passwd #指定授权的虚拟用户的密码
log file = /var/log/rsyncd.log #日志文件
#####################################
[backup] #模块名称
comment = welcome to oldboyedu backup! #注释
path = /backup 真实的文件目录
注意:配置文件写完后注意检查是否出现书写错误,完成配置文件后创建文件中需要手动创建的用户以及目录。
4.服务端创建用户
[root@backup ~]# useradd rsync -s /sbin/nologin -M
5.服务端创建密码文件
两种方式:
第一种:[root@backup ~]# vim /etc/rsync.passwd
rsync_backup:123456
第二种:[root@backup ~]# echo "rsync_backup:123456" > /etc/rsync.passwd
<推荐使用第二种,第一种密码后面不注意按了个空格就凉凉~~~~~>
###不管用什么方法创建,记得一定要授权!!!
[root@backup ~]# chmod 600 /etc/rsync.passwd
授权:600或者700权限都可以,建议600
6.服务端创建真实文件目录
[root@backup ~]# mkdir /backup
[root@backup ~]# chown -R rsync.rsync /backup/
7.服务端启动服务
[root@backup ~]# systemctl start rsyncd
[root@backup ~]# netstat -lntp ##验证服务是否启动
8.客户端验证推送
1)输入密码的方式
##推送数据
[root@web01 ~]# rsync -avz file2 rsync_backup@172.16.1.41::backup
Password: 123456
sending incremental file list
file2
sent 87 bytes received 43 bytes 37.14 bytes/sec
total size is 0 speedup is 0.00
#拉取数据
[root@web01 ~]# rsync -avz rsync_backup@172.16.1.41::backup ./
Password:
receiving incremental file list
./
file3
sent 53 bytes received 173 bytes 64.57 bytes/sec
total size is 0 speedup is 0.00
2)指定密码文件的方式
#客户端配置密码文件
[root@web01 ~]# vim /etc/rsyncd.password
123456
[root@web01 ~]# echo "123456" > /etc/rsyncd.password
#授权
[root@web01 ~]# chmod 600 /etc/rsyncd.password
#推送数据
[root@web01 ~]# rsync -avz file3 rsync_backup@172.16.1.41::backup --password-file=/etc/rsyncd.password
sending incremental file list
file4
sent 87 bytes received 43 bytes 86.67 bytes/sec
total size is 0 speedup is 0.00
#拉取数据
[root@web01 ~]# rsync -avz rsync_backup@172.16.1.41::backup ./ --password-file=/etc/rsyncd.password
receiving incremental file list
./
file5
sent 50 bytes received 211 bytes 174.00 bytes/sec
total size is 0 speedup is 0.00
3)配置环境变量的方式
[root@web01 ~]# export RSYNC_PASSWORD=123456
#拉取数据
[root@web01 ~]# rsync -avz rsync_backup@172.16.1.41::backup ./
#推送数据
[root@web01 ~]# rsync -avz file3 rsync_backup@172.16.1.41::backup
rsync常见报错:
1. [root@web01 ~]# rsync -avz rsync_backup@172.16.1.41::backu ./
@ERROR: Unknown module 'backu'
##原因:
模块名字与服务端配置没有对应,检查模块名称
2. [root@web01 ~]# rsync -avz /tmp/ rsync_backup@172.16.1.41::backup
sending incremental file list
rsync: failed to write xattr user.rsync.%stat for "." (in backup): Permission denied (13)
rsync: failed to set times on "." (in backup): Operation not permitted (1)
./
sent 176 bytes received 205 bytes 762.00 bytes/sec
total size is 0 speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]
##原因:
1.权限不足,服务端目录权限过高
2.检查firewalld,selinux是否关闭
3.[root@web01 ~]# rsync -avz /tmp/ rsync_back@172.16.1.41::backup
@ERROR: auth failed on module backup
##原因:
1.虚拟用户错误
2.密码错误
3.密码文件权限不是600
4.服务端密码文件不存在
4. [root@web01 ~]# rsync -avz /tmp/ rsync_backup@172.16.1.41::/backup
ERROR: The remote path must start with a module name not a /
#原因:
守护进程模式 :: 后面跟的是模块名字,而不是目录名字
5.[root@web01 ~]# rsync -avz /tmp/ rsync_backup@172.16.1.41::backup
rsync: failed to connect to 172.16.1.41 (172.16.1.41): Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(125) [sender=3.1.2
##原因:
服务端服务没有启动
6.[root@web01 ~]# rsync -avz /tmp/ rsync_backup@172.16.1.41::backup
@ERROR: chdir failed
##原因:
服务端文件目录不存在