安装部署 rsync + innotify 实现 单向数据和双向数据的自动时实同步

目标:将server2上的/web目录实时同步到server1的 /web 上

一.安装部署rsync服务实现数据同步

部署目标服务器:

1.安装rsync并开启服务

[root@server1 ~]# yum install -y rsync
[root@server1 ~]# systemctl start rsyncd
[root@server1 ~]# systemctl enable rsyncd

2.配置rsync

[root@server1 ~]# mkdir /web
[root@server1 ~]# vim /etc/rsyncd.conf  
## 通用配置
log file=/var/log/rsuncd.log        #指定日志存放位置
pid file=/var/run/rsyncd.pid        #指定pid存放位置
lock file=/var/run/rysnc.lock       #支持max connections参数的锁文件
secrets file=/etc/rsync.pass        #指定用户认证文件,保存用户名及密码
motd file=/etc/rsyncd.motd          #rsync启动时保存欢迎信息的文件位置

## 模块配置
[web]                    #自定义模块名称
path=/web                #数据目录位置
comment=/web             #模块名称注释说明
uid=root                 #设置rsync运行权限为root
gid=root                 #设置rsync运行组权限为root
port=873                 #设置默认端口号
use chroot=no            #默认为true,表示不同步软连接文件,改为no表示同步软连接
read only=no             #设置rsync服务端为读写文件
list=no                  #不显示rsync服务端的资源列表
max connections=200      #最大连接数
timeout=600              #超时时间
auth users=root          #执行数据同步的用户名,多个用逗号隔开
hosts allow=172.25.66.2  #允许进行同步的IP地址,多个IP用都好隔开

3.编写用户认证文件

[root@server1 ~]# vim /etc/rsync.pass
#################
root:westos         #用户:密码

4.权限设置

[root@server1 ~]# chmod 600 /etc/rsyncd.conf
[root@server1 ~]# chmod 600 /etc/rsync.pass

5.重启服务

[root@server1 ~]# systemctl restart rsyncd
[root@server1 ~]# netstat -antp|grep :873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      3413/rsync          
tcp6       0      0 :::873                  :::*                    LISTEN      3413/rsync    

  部署源服务器:

1.安装rsync并开启

[root@server2 ~]# yum install -y rsync
[root@server2 ~]# systemctl start rsyncd
[root@server2 ~]# systemctl enable rsyncd

2.编写用户认证文件

[root@server2 ~]# vim /etc/rsync.pass
#################
westos

3.权限配置

[root@server2 ~]# chmod 600 /etc/rsyncd.pass

测试:

(1).在源服务器的/web目录下创建文件

[root@server2 ~]# mkdir /web
[root@server2 ~]# cd /web/
[root@server2 web]# ls
[root@server2 web]# touch file{1..3}
[root@server2 web]# ls
file1  file2  file3

(2).通过运行rsync命令,将源服务器/web目录下的文件同步到目标服务器的/web目录下

[root@server2 web]# rsync -avH  --port=873  --progress  --delete /web/  root@172.25.66.1::web --password-file=/etc/rsync.pass

注释:
-a                        #归档模式,表示递归传输并保持文件属性。
-v                        #显示rsync过程中详细信息。可以使用"-vvvv"获取更详细信息。
-H                        #保留硬链接;
--port                    #连接daemon时使用的端口号,默认为873端口。
--progress                #显示文件同步时的百分比进度、传输速率。
--delete                  #以SRC为主,对DEST进行同步。多则删之,少则补之。
--password-file           #指定密码文件;
root@172.25.66.1::web     #用户@IP::模块名称

(3).检测目标服务器/web目录下是否出现了同步的文件

[root@server1 ~]# cd /web
[root@server1 web]# ls
file1  file2  file3

在目标服务器上可以查看到源服务器上创建的文件,表明数据同步成功

二.安装部署inotify服务实现数据时实同步

在源服务器上:

点击此处即可下载 inotify

1.下载并解压inotify

[root@server2 ~]# ls
inotify-tools-3.13.tar.gz
[root@server2 ~]# tar zxf inotify-tools-3.13.tar.gz
[root@server2 ~]# ls
inotify-tools-3.13  inotify-tools-3.13.tar.gz

2.源码编译安装

[root@server2 ~]# cd inotify-tools-3.13
#安装依赖包
[root@server2 inotify-tools-3.13]# yum install -y gcc
#configure配置
[root@server2 inotify-tools-3.13]# ./configure --prefix=/usr/local/inotify
#编译与安装
[root@server2 inotify-tools-3.13]# make && make install

3.配置inotify

(1).修改环境变量

[root@server2 ~]# vim /etc/profile
#################
export PATH=$PATH:/usr/local/inotify/bin

[root@server2 ~]# source /etc/profile

(2).导出inotify库文件

[root@server2 ~]# echo "/usr/local/inotify/lib" > /etc/ld.so.conf.d/inotify.conf
[root@server2 ~]# cat /etc/ld.so.conf.d/inotify.conf
/usr/local/inotify/lib
[root@server2 ~]# ldconfig

(3).导出inotify头文件

[root@server2 ~]# ln -s /usr/local/inotify/include/ /usr/include/inotify

(4).修改inotify内核参数(默认inotify参数太小)

[root@server2 ~]# vim /etc/sysctl.conf
#################
fs.inotify.max_queued_events = 9999999
fs.inotify.max_user_watches = 99999999
fs.inotify.max_user_instances = 65535
#使之生效
[root@server2 ~]# sysctl -p
fs.inotify.max_queued_events = 9999999
fs.inotify.max_user_watches = 99999999
fs.inotify.max_user_instances = 65535

4.编写脚本

[root@server2 ~]# mkdir /scripts
[root@server2 ~]# cd /scripts/
[root@server2 scripts]# vim rsync.sh
#!/bin/bash

srcdir=/web/
dstdir=web
excludedir=/usr/local/inotify/exclude.list
rsyncuser=root
rsyncpassdir=/etc/rsync.pass
dstip=172.25.66.1

/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib $src
dir | while read file
do
/usr/bin/rsync -vzrtopg --delete --progress $srcdir $rsyncuser@$dstip::$dstdir --password-file=$rsyncpassdir
echo "${file} was rsynced" >> /var/log/rsync.log 2>&1
done

注释:
-v        #显示rsync过程中详细信息。
-z        #传输时进行压缩提高效率。
-rtopg    #归档模式,表示递归传输并保持文件属性,等同于-a;

5.添加可执行权限

[root@server2 scripts]# chmod +x rsync.sh

测试:

#脚本执行没有报错即可
[root@server2 scripts]# /scripts/rsync.sh 

三.实现自动时实同步

在源服务器上:

1.设置定时任务

[root@server2 scripts]# crontab -e
crontab: installing new crontab
[root@server2 scripts]# crontab -l
* * * * * /scripts/rsync.sh &> /dev/null

2.设置开机自启

[root@server2 scripts]# vim /etc/rc.d/rc.local
#######################
/scripts/rsync.sh &> /dev/null

测试:

(1).在源服务器上创建文件

[root@server2 scripts]# cd /web/
[root@server2 web]# ls
file1  file2  file3
[root@server2 web]# touch file{4..8}
[root@server2 web]# ls
file1  file2  file3  file4  file5  file6  file7  file8
[root@server2 web]# date
Wed Aug 21 18:44:49 CST 2019

(2).等待1min后,发现会自动同步到目标服务器上,说明是数据自动时实同步成功

[root@server1 ~]# date
Wed Aug 21 18:45:56 CST 2019
[root@server1 ~]# cd /web/
[root@server1 web]# ls
file1  file2  file3  file4  file5  file6  file7  file8

四.双向数据同步

目标:将server1上的/web目录实时同步到server2的 /web 上

部署目标服务器:

1.安装部署rsync

[root@server2 ~]# rpm -aq rsync
rsync-3.0.9-17.el7.x86_64
[root@server2 ~]# systemctl status rsyncd

2.配置rsync

[root@server2 ~]# vim /etc/rsyncd.conf 
log file=/var/log/rsuncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rysnc2.lock
secrets file=/etc/rsync2.pass
motd file=/etc/rsyncd.motd

[web]
path=/web
comment=/web
uid=root
gid=root
port=873
use chroot=no
read only=no
list=no
max connections=200
timeout=600
auth users=root
hosts allow=172.25.66.1

3.编写用户认证文件

[root@server2 ~]# vim /etc/rsync2.pass
#################
root:westos

4.权限设置

[root@server2 ~]# ll /etc/rsyncd.conf
-rw------- 1 root root 310 Aug 22 16:44 /etc/rsyncd.conf
[root@server2 ~]# chmod 600 /etc/rsync2.pass

5.重启服务

[root@server2 ~]# systemctl restart rsyncd
[root@server2 ~]# netstat -antp|grep :873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      2561/rsync          
tcp        0      0 172.25.66.2:59184       172.25.66.1:873         TIME_WAIT   -                   
tcp6       0      0 :::873                  :::*                    LISTEN      2561/rsync   

部署源服务器:

1.安装rsync

[root@server1 ~]# rpm -aq rsync
rsync-3.0.9-17.el7.x86_64
[root@server1 ~]# systemctl status rsyncd

2.编写用户认证文件

[root@server1 ~]# vim /etc/rsync2.pass
#################
westos

3.权限设置

[root@server1 ~]# chmod 600 /etc/rsync2.pass

测试:

(1).在源服务器的/web目录下创建文件

[root@server1 ~]# cd /web/
[root@server1 web]# ls
file1  file2  file3  file4  file5  file6  file7  file8
[root@server1 web]# touch westos
[root@server1 web]# ls
file1  file2  file3  file4  file5  file6  file7  file8  westos

(2).通过运行rsync命令,将源服务器/web目录下的文件同步到目标服 务器的/web目录下

[root@server1 web]# rsync -avH  --port=873  --progress  --delete /web/  root@172.25.66.2::web --password-file=/etc/rsync2.pass

(3).检测目标服务器/web目录下是否出现了同步的文件

[root@server2 ~]# cd /web/
[root@server2 web]# ls
file1  file2  file3  file4  file5  file6  file7  file8  westos

有了上述操作的基础,那么无论是实现双向数据同步还是双向自动时实同步实质上都是一样的,只是此时的源服务器与目标服务器互换了而已。那双向自动时实同步的实现,就是将在server2结点上的操作,在server1结点上执行一遍即可。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值