1.简介:
rsync官方网站: https://www.samba.org/ftp/rsync/rsync.html
rsync是可以实现增量备份的工具。配合任务计划,rsync能实现定时或间隔同步,配合inotify或sersync,可以实现触发式的实时同步。
2.服务端配置:
服务器ip :192.168.1.101
配置文件参数参考说明:
https://www.cnblogs.com/-xuan/p/10554060.html
*注释不能和参数同行
*fake super= yes 注意新版本没有改参数会有错误
vim /etc/rsyncd.conf
######### 全局配置参数 ##########
port=888 # 指定rsync端口。默认873
uid = rsync # rsync服务的运行用户,默认是nobody,文件传输成功后属主将是这个uid
gid = rsync # rsync服务的运行组,默认是nobody,文件传输成功后属组将是这个gid
use chroot = no # rsync daemon在传输前是否切换到指定的path目录下,并将其监禁在内
max connections = 200 # 指定最大连接数量,0表示没有限制
timeout = 300 # 确保rsync服务器不会永远等待一个崩溃的客户端,0表示永远等待
motd file = /var/rsyncd/rsync.motd # 客户端连接过来显示的消息
pid file = /var/run/rsyncd.pid # 指定rsync daemon的pid文件
lock file = /var/run/rsync.lock # 指定锁文件
log file = /var/log/rsyncd.log # 指定rsync的日志文件,而不把日志发送给syslog
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # 指定哪些文件不用进行压缩传输
fake super = yes #*特别注意*新版本中不使用数据推送会有错误
###########下面指定模块,并设定模块配置参数,可以创建多个模块###########
[test] # 模块ID
path = /longshuai/ # 指定该模块的路径,该参数必须指定。启动rsync服务前该目录必须存在。rsync请求访问模块本质就是访问该路径。
ignore errors # 忽略某些IO错误信息
read only = false # 指定该模块是否可读写,即能否上传文件,false表示可读写,true表示可读不可写。所有模块默认不可上传
write only = false # 指定该模式是否支持下载,设置为true表示客户端不能下载。所有模块默认可下载
list = true # 客户端请求显示模块列表时,该模块是否显示出来,设置为false则该模块为隐藏模块。默认true
#hosts allow = 10.0.0.0/24 # 指定允许连接到该模块的机器,多个ip用空格隔开或者设置区间
#hosts deny = 0.0.0.0/32 # 指定不允许连接到该模块的机器
auth users = rsync_test # 指定连接到该模块的用户列表,只有列表里的用户才能连接到模块,用户名和对应密码保存在secrts file中,
# 这里使用的不是系统用户,而是虚拟用户。不设置时,默认所有用户都能连接,但使用的是匿名连接
secrets file = /etc/rsync.passwd # 保存auth users用户列表的用户名和密码,每行包含一个username:passwd。由于"strict modes"
# 默认为true,所以此文件要求非rsync daemon用户不可读写。只有启用了auth users该选项才有效。
#[xiaofang] # 以下定义的是第二个模块
#path=/xiaofang/
#read only = false
#ignore errors
#comment = anyone can access #模块描述
3.创建密码文件:
vim /etc/rsync.password
rsync_test:123456
#授权
chmod 600 /etc/rsync.password
4.创建用户
useradd rsync -s /sbin/nologin -M
5.创建共享文件夹并授权
mkdir -p /home/test
chown -R rsync.rsync /home/test
#查看文件夹
ls -ld /test/
drwxr-xr-x 2 rsync rsync 4096 12月
5.启动rsync
rsync --daemon
#查看
ps -aux |grep rsync
6.在另一台服务器上查看共享目录
rsync -a 192.168.1.101::
#如果能显示模块名称说明成功
7.客户端推送测试
服务器ip192.168.1.102
mkdir -p /home/test
cd /home/test
touch /home/test/com_test{1..10}
vim /etc/rsync.passwd
123456
chmod 600 /etc/rsync.passwd
#将服务器/home/test 目录中的数据文件推送到搭建rsync服务的共享目录下
rsync -avrt /home/test/ rsync_test@192.168.1.101::test --password-file=/etc/rsync.passwd
8.客户端拉取:将共享目录下的数据拉取到本地
#密码文件需要授权
vim /etc/rsync.passwd
123456
chmod 600 /etc/rsync.passwd
rsync -avzrt rsync_test@192.168.1.101::test/ /home/test1 --password-file=/etc/rsync.passwd
9.开机自启动
#将启动写入 rc.local
echo "/usr/bin/rsync --daemon" >> /etc/rc.local
#守护进程
grep daemon /etc/rc.local
#授予 rc.local 可执行权限
chmod +x /etc/rc.d/rc.local
10.实时同步