其他分享
首页 > 其他分享> > inotify/sersync/lsyncd + rsync的实时同步流程

inotify/sersync/lsyncd + rsync的实时同步流程

作者:互联网

inotify + rsync实时同步

1、准备工作
1)内核版本必须在2.6.13以上
[root@nfs-31 ~]#uname -r
3.10.0-862.el7.x86_64
2)看是否存在3个文件系统,存在才支持
[root@nfs-31 ~]#ls -l /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Apr 20 20:08 max_queued_events
-rw-r--r-- 1 root root 0 Apr 20 20:08 max_user_instances
-rw-r--r-- 1 root root 0 Apr 20 20:08 max_user_watches
系统文件解释
max_user_watches:    设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)
默认只能监控8192个文件

max_user_instances:    设置每个用户可以运行的inotifywait或inotifywatch命令的进程数
默认每个用户可以开启inotify服务128个进程

max_queued_events:    设置inotify实例事件(event)队列可容纳的事件数量
默认监控事件队列长度为16384

2、安装inotifty-tools工具
[root@nfs-31 ~]#yum install inotify-tools -y
检查生成的软件命令
[root@nfs-31 ~]# rpm -ql inotify-tools |head -2
/usr/bin/inotifywait
/usr/bin/inotifywatch
3、编辑脚本
[root@nfs-31 /afei]#cat test_inotify.sh 
#!/bin/bash
path=/afei
backup_server=172.16.1.41
export RSYNC_PASSWORD=111111

/usr/bin/inotifywait -mrq -e modify,delete,create,attrib,move /afei | while read line
do
    echo ${line}
    rsync -az --delete /afei/ rsync_user@${backup_server}::data
done
4、执行脚本就行测试
[root@nfs-31 /afei]#bash  test_inotify.sh

sersync + rsync实时同步

1、在nfs-31服务器下载sersync程序
cd /opt && wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz
2、解压
[root@nfs-31 /opt]#tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz 
GNU-Linux-x86/
GNU-Linux-x86/sersync2
GNU-Linux-x86/confxml.xml
3、重命名,查看配置文件
[root@nfs-31 /opt]#mv GNU-Linux-x86 sersync-2-5-4
[root@nfs-31 /opt]#cd sersync-2-5-4/

4、该软件就俩文件,一个二进制,一个配置文件
[root@nfs-31 /opt/sersync-2-5-4]#ls
confxml.xml  sersync2
[root@nfs-31 /opt/sersync-2-5-4]#file sersync2 
sersync2: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.15, BuildID[sha1]=c41f3d73f3d4fe1d6931d93a4e64fe805769b28f, stripped
[root@nfs-31 /opt/sersync-2-5-4]#file confxml.xml
confxml.xml: XML 1.0 document, ASCII text

5、修改配置文件
1)备份原来的
[root@nfs-31 /opt/sersync-2-5-4]#cp confxml.xml{,.bak}
[root@nfs-31 /opt/sersync-2-5-4]#ls
confxml.xml  confxml.xml.bak  sersync2
2)改配置文件
<sersync>
        <localpath watch="/afei">
            <remote ip="172.16.1.41" name="data"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-az"/>
            <auth start="true" users="rsync_user" passwordfile="/etc/rsync.passwd"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->


6、启动程序
[root@nfs-31 /opt/sersync-2-5-4]#./sersync2 -rdo confxml.xml
7、进入/afei目录


9、在nfs-31输入数据
[root@nfs-31 /afei]#for i in {1..1000}; do echo $i ; echo $i > ${i}.log;sleep 0.1;done
10、在rsync-41上监测
[root@rsync-41 /data]#while true;do ls ;sleep 0.1;done

lsyncd+rsync实时同步

1、下载lsyncd服务
[root@nfs-31 /afei]#yum install lsyncd -y
2、保存原来配置文件
[root@nfs-31 ~]#cp  /etc/lsyncd.conf{,.bak}

3、修改配置文件(监控一个目录)
[root@nfs-31 ~]#cat /etc/lsyncd.conf 
settings {
  logfile = "/var/log/lsyncd/lsyncd.log",
  statusFile = "/var/log/lsyncd/lsyncd.status",
  inotifyMode = "CloseWrite",
  maxProcesses = 8,
}
sync {
  default.rsync,
  source  = "/afei/",
  target  = "rsync_user@10.0.0.41::data",
  delete= true,
  exclude = {".*"},
  delay = 1,
  rsync = {
    binary = "/usr/bin/rsync",
    archive = true,
    compress = true,
    verbose = true,
    password_file = "/etc/rsync.passwd",
    _extra = {"--bwlimit=200"}
  }
}

4、nfs-31放入密码文件
[root@nfs-31 ~]#cat  /etc/rsync.passwd
111111
修改权限
[root@nfs-31 ~]#chmod 600 /etc/rsync.passwd
[root@nfs-31 ~]#ll   /etc/rsync.passwd
-rw------- 1 root root 7 Apr 25 21:01 /etc/rsync.passwd

5、启动
[root@nfs-31 ~]#systemctl start lsyncd
6、查看状态
[root@nfs-31 ~]#systemctl status lsyncd

7、测试
nfs-31服务器
[root@nfs-31 /afei]#for i in {1..100};do echo ${i};echo ${i} > ${i}.log;sleep 0.1;done
rsync-41服务器
[root@rsync-41 /data]#for i in {1..100};do ls;sleep 0.1;done

web-7 (1.web-7的网页根目录数据来自于nfs共享目录/nfs-nginx-data/ ,要求该目录下的所有用户映射为www(uid=11111),允许读写)

nfs-31 (2.在web-7的nginx网站根目录下创建首页文件后,触发实时同步,备份到rsync-41机器上)

rsync-41 (3.数据备份服务器)

nfs-31

[root@nfs-31 ~]#cat /afei-nfs.sh 
#!/bin/bash

#定义PATH变量
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

#1)下载nfs工具包,下载rsync服务
yum install nfs-utils rpcbind -y
yum install rsync  -y

#2)启动服务
systemctl restart rpcbind
systemctl restart nfs-server

#3)设置开机自启
systemctl is-enabled nfs
systemctl enable rpcbind nfs

#4)创建挂载目录/nfs-nginx-data
mkdir -p /nfs-nginx-data

#5)挂载硬盘到 /nfs-nginx-data
#mount /dev/sdb1 /nfs-nginx-data/

#6)先创建www用户
useradd www  -u 11111  -M -s /nfs-nginx-data

#7)修改配置文件
cat > /etc/exports <<EOF
/nfs-nginx-data    172.16.1.7(rw,sync,all_squash,anonuid=11111,anongid=11111)
EOF

#8)更新nfs配置文件
exportfs -r

#9)修改文件/nfs-nginx-data属主属组
chown -R www:www /nfs-nginx-data

#进行实时同步数据
#1)下载lsyncd服务
yum install lsyncd -y
#2)拷贝原来的配置文件
cp /etc/lsyncd.conf{,.bak}
#3)修改配置文件/etc/lsyncd.conf
cat > /etc/lsyncd.conf << EOF
settings {
  logfile = "/var/log/lsyncd/lsyncd.log",
  statusFile = "/var/log/lsyncd/lsyncd.status",
  inotifyMode = "CloseWrite",
  maxProcesses = 8,
}
sync {
  default.rsync,
  source  = "/nfs-rsync-data",
  target  = "rsync_user@10.0.0.41::tantan01",
  delete= true,
  exclude = {".*"},
  delay = 1,
  rsync = {
    binary = "/usr/bin/rsync",
    archive = true,
    compress = true,
    verbose = true,
    password_file = "/etc/rsync.pwd",
    _extra = {"--bwlimit=200"}
  }
}
EOF

#4)密码放入密码文件
echo "111111" > /etc/rsync.pwd
#5)改密码文件权限,拒绝其他人访问
chmod 600 /etc/rsync.pwd
#5)启动
systemctl start lsyncd

rsync-41

[root@rsync-41 ~]#cat /afei-rsync.sh 
#!/bin/bash

#1、下载rsync服务
yum install rsync  -y

#2、定义PATH变量
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

#3、部署rsync服务
#1)修改配置文件
cat > /etc/rsyncd.conf << 'EOF'
uid = afei 
gid = afei 
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_user
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[tantan01]
comment = yuchaoit.cn about rsync
path = /tantan01

[tantan02]
path = /tantan02
EOF
#2)创建用户,数据目录
#创建用户
useradd -u 2000 -M -s /sbin/nologin afei

#创建数据目录
mkdir /tantan01
mkdir /tantan02

#修改属主属组
chown -R afei:afei /tantan01/
chown -R afei:afei /tantan02/

#3)创建虚拟用户密码文件与授权
#创建虚拟用户密码
echo 'rsync_user:111111' > /etc/rsync.passwd
#给密码文件授权
chmod 600 /etc/rsync.passwd 
#4)运行rsyncd服务
systemctl start rsyncd




web-7

[root@web-7 ~]#cat /afei-nginx.sh 
#!/bin/bash
#定义PATH变量
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
#1)下载nginx服务
yum install  nginx -y
#2)创建用户www
useradd www -M -s /sbin/nologin
#3)修改nginx配置文件user nginx 改为 www
sed -i  's#user nginx#user www#g' /etc/nginx/nginx.conf
#4)启动nginx
systemctl start nginx
#5)下载nfs-utils
yum install   nfs-utils  -y
#6)启动nfs-utils服务
systemctl start rpcbind
#7)挂载
mount -t nfs 172.16.1.31:/nfs-nginx-data  /usr/share/nginx/html
#8)创建数据
cd /usr/share/nginx/html && echo '人生无常,大肠包小肠' > index.html 

标签:rsync,sersync,inotify,31,nginx,nfs,usr,root
来源: https://www.cnblogs.com/Xafei/p/16452384.html