目录
一、 环境说明
二、 主服务器192.168.200.37部署inotify+rsync
1、关闭selinux否则客户端推送的文件过不来
2、开启防火墙tcp 873端口或关闭防火墙
3、服务端检查或安装Rsync+inotify-tools
6、 配置rsync认证文件/etc/web1.pass
7、 修改/etc/web1.pass的权限为600
8、配置服务能够启动
9、检查873端口是否成功监听
10、inotifywait使用说明
11、 创建脚本,实时触发rsync进行同步
三、 备份服务器192.168.200.41部署rsync
1.检查或安装Rsync服务端
2.创建同步存放目录
3、配置rsync认证文件/etc/web1.pass
4、 手动创建rsyncd.conf配置文件
一、环境说明
主服务器:192.168.200.37(安装rsync+inotify):也称源服务器
操作系统:CentOS release 6.10 (Final)
1. 关闭防火墙
2. 安装inotify+rsync
3. 配置rsync认证文件/etc/web1.pass(只配置密码),并修改权限600
4. 创建脚本/usr/local/bin/inotifyrsync.sh,实时触发rsync进行同步
5. 启动rsync:/usr/bin/rsync --daemon
备份服务器:192.168.200.41(安装rsync):也称目标服务器
操作系统:CentOS Linux release 7.6.1810 (Core)
1. 安装rsync
2. 创建同步存放目录(/data/37/data/center/test)
3. 配置rsync认证文件/etc/web1.pass(配置账号密码,如“www1:123456”),赋权600
4. 手动创建/etc/rsyncd.conf
5. 启动rsync:/usr/bin/rsync --daemon
二、主服务器192.168.200.37部署inotify+rsync
1、关闭selinux否则客户端推送的文件过不来
vi /etc/selinux/config #编辑防火墙配置文件
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存,退出
setenforce 0 #立即生效
2、开启防火墙tcp 873端口或关闭防火墙
873端口为Rsync默认端口)或关闭防火墙 ,为了测试方便建议先关闭防火墙
/etc/init.d/iptables stop
3、服务端检查或安装Rsync+inotify-tools
A. 检查是否安装rsync:rpm -qa rsync
B. 安装rsync
yum -y install rsync
systemctl start rsyncd
rsync --daemon #开启rsync
检查是否已安装inotify-tool
inotify-tool安装路径:/usr/local/bin
安装inotify-tools
先装依赖包gcc否则会报错
yum install –y gcc
tar xf inotify-tools-3.13.tar.gz
cd inotify-tools-3.13
./configure
make && make install
设置系统环境变量,添加软连接
echo "PATH=/usr/local/inotify/bin:$PATH" >>/etc/profile.d/inotify.sh
source /etc/profile.d/inotify.sh #使设置立即生效
echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf
ln -s /usr/local/inotify/include /usr/include/inotify
10、修改inotify默认参数(inotify默认内核参数值太小)
查看系统默认参数值
sysctl -a | grep max_queued_events
结果是:fs.inotify.max_queued_events = 16384
sysctl -a | grep max_user_watches
结果是:fs.inotify.max_user_watches = 8192
sysctl -a | grep max_user_instances
结果是:fs.inotify.max_user_instances = 128
输入以下命令修改参数:
sysctl -w fs.inotify.max_queued_events="99999999"
sysctl -w fs.inotify.max_user_watches="99999999"
sysctl -w fs.inotify.max_user_instances="65535"
vi /etc/sysctl.conf #添加以下代码
fs.inotify.max_queued_events = 99999999
fs.inotify.max_user_watches = 99999999
fs.inotify.max_user_instances = 65535
:wq! #保存退出
6、配置rsync认证文件/etc/web1.pass
(用于询问客户端用户的密码,此文件格式为“用户名:密码 ”,主服务器有同样的密码文件只需写入“密码”即可)
echo "123456" > /etc/web1.pass
cat /etc/web1.pass
提示123456
7、修改/etc/web1.pass的权限为600
(客户端的密码文件需要和服务器的密码文件权限一致)
chmod 600 /etc/web1.pass
ll /etc/web1.pass
提示-rw------- 1 root root 10 Mar 27 22:31 /etc/web1.pass
8、配置服务能够启动
启动rsync:/usr/bin/rsync --daemon
chkconfig rsync on
service xinetd start
提示Starting xinetd: [ OK ]
9、检查873端口是否成功监听
查看指定端口状态:netstat -anutp|grep 873
10、inotifywait使用说明
inotifywait命令使用
#!/bin/bash
#filename watchdir.sh
path=$1
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y/%H:%M' --format '%T %w %f' -e modify,delete,create,attrib $path
执行输出:
./watchdir.sh /data/wsdata/tools/
04/01/13/16:34 /data/wsdata/tools/ .j.jsp.swp
04/01/13/16:34 /data/wsdata/tools/ .j.jsp.swx
inotifywait命令参数
· -m是要持续监视变化。
· -r使用递归形式监视目录。
· -q减少冗余信息,只打印出需要的信息。
· -e指定要监视的事件列表。
· --timefmt是指定时间的输出格式。
· --format指定文件变化的详细信息。
11、创建脚本,实时触发rsync进行同步
[root@localhost bin]# cat /usr/local/bin/inotifyrsync.sh
#!/bin/bash
host_slave=192.168.200.41 # 对端想要同步的ip
master_src=/data/center/test/ #同步路径
inotify_home=/usr/local/ #inotifywait (这个试行程序的路径)
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,modify,delete,create,attrib $master_src \
while read file
do
rsync -az --delete ${master_src} rsynclsl@${host_slave}::web1 --password-file=/etc/web1.pass
chmod +x /usr/local/inotify/rsync.sh #添加脚本执行权限
三、备份服务器192.168.200.41部署rsync
1.检查或安装Rsync服务端
检查:rpm -qa rsync
安装:
yum install rsync xinetd #安装
vi /etc/xinetd.d/rsync #编辑配置文件,设置开机启动rsync
disable = on#修改为on
:wq! #保存退出
/etc/init.d/xinetd start #启动(CentOS中是以xinetd来管理Rsync服务的)
2.创建同步存放目录
(用于接收客户端发来的文件夹和文件)
mkdir /data/37/data/center/test
chmod 770 /data/37/data/center/test
3、配置rsync认证文件/etc/web1.pass
echo "www1:123456" > /etc/web1.pass
chmod 600 /etc/web1.pass
4、手动创建rsyncd.conf配置文件
(配置文件中最好把“#”和后面注释的文字删除)
vi /etc/rsyncd.conf #创建配置文件,添加以下代码
uid = root #rsync运行权限为root
gid = root #rsync运行权限为root
use chroot = no #是否让进程离开工作目录
max connections = 5 #最大并发连接数,0为不限制
timeout = 600 #超时时间
pid file = /var/run/rsyncd.pid #指定rsync的pid存放路径
lockfile = /var/run/rsyncd.lock #指定rsync的锁文件存放路径
log file = /var/log/rsyncd.log #指定rsync的日志存放路径
[web1] #模块名称
path = /data/37/data/center/test #跟客户端同步过来的文件存放的路径
ignore errors = yes #忽略一些无关的I/O错误
read only = no #客户端是否能拉(PULL)
write only = no #客户端是否能推(PUSH)
hosts allow = 192.168.200.37 #客户端主机ip
hosts deny = * #黑名单,*表示任何主机
list = yes
uid = root #以root的身份去获取文件
gid = root
auth users = web1 #认证此模块的用户名
secrets file = /etc/web1.pass #指定存放“用户名:密码”格式的文件
exclude = test1/ #排除目录,多个之间使用空格隔开
[web2]
path = /data/38/data/center/test
ignore errors
read only = no
write only = no
hosts allow = 192.168.200.38
hosts deny = *
list = false
uid = root
gid = root
auth users = www2
secrets file = /etc/web2.pass
exclude = test1/
[web3]
path = /data/39/data/center/test
ignore errors
read only = no
write only = no
hosts allow = 192.168.200.39
hosts deny = *
list = false
uid = root
gid = root
auth users = www3
secrets file = /etc/web3.pass