Linux使用inotify+unison实现数据、静态资源双向(多向)实时同步

在特定的情况下需要在多个Linux服务器上做指定文件文件夹的实时同步,一个服务器修改了文件其它服务器的文件能保持一致.本博客非常适用于需要在两台、多台linux服务器做静态资源同步的同学,整个过程简单、实用,另外 inotify + unison稳定可靠。

环境准备

Centos服务器1:139.199.152.84
Centos服务器2:111.230.103.208
指定同步的文件夹:/usr/Tomcat/image与/usr/Tomcat/upload 目录两个服务器都创建好目录

在这里插入图片描述

配置步骤

1.安装Objective Caml compiler

服务器1与服务器2都要安装
# yum install make  gcc gcc-c++
# cd/tmp
# wget http://caml.inria.fr/pub/distrib/ocaml-4.03/ocaml-4.03.0.tar.gz 
# tar  -zxvf ocaml-4.03.0.tar.gz
# cd ocaml-4.03.0
# ./configure
# make world opt
# make install

2.安装Unison

服务器1与服务器2都要安装
# yum install ctags-etags
# cd/tmp
# wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.48.4.tar.gz
# mkdir unison-2.48.4 && cd unison-2.48.4
# tar -zxvf /tmp/unison-2.48.4.tar.gz
# cd src
# make UISTYLE=text THREADS=true
# cp unison /usr/local/bin/
//有版本信息出现则安装成功
# unison -version

3.安装inotify

先安装扩展包源,否则inotify-tools找不到
#yum install epel-release
#yum -y install inotify-tools

4.配置双机SSH信任

服务器1与服务器2同样安装
# yum install rsync -y 

5.注册秘钥

在服务器139.199.152.84配置
# ssh-keygen
//连续按三次空格
# cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys 
//给权限
# chmod 700 ~/.ssh
# chmod 600 ~/.ssh/authorized_keys
//需要输入一次密码 
# rsync -avz /root/.ssh/authorized_keys root@111.230.103.208:/root/.ssh/authorized_keys
在服务器111.230.103.208配置
# ssh-keygen
//连续按三次空格
# cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys 
//给权限
# chmod 700 ~/.ssh
# chmod 600 ~/.ssh/authorized_keys
//需要输入一次密码 
# rsync -avz /root/.ssh/authorized_keys root@111.230.103.208:/root/.ssh/authorized_keys

在这里插入图片描述
测试是否成功只要不用输入密码说明配置成功

服务器1测试
#ssh -p 22  111.230.103.208 date
服务器2测试  
#ssh -p 22  139.199.152.84 date

在这里插入图片描述
6.配置双机同步

首先在服务器139.199.152.84配置

在服务器139.199.152.84配置
//先进入/tem/uniso/src/目录
#mkdir -p /root/.unison/
#vim /root/.unison/default.prf
//以下是配置的内容
#Unison preferences file 
//root是配置的两个目录本地和远程,注意远程IP地址后面是两个//
root = /usr/Tomcat/ 
root = ssh://root@111.230.103.208//usr/Tomcat/
//path 指定的文件夹如果是Tomcat整个目录不用指定path
path = upload
path = image 
#force =
#ignore =
batch = true 
maxthreads = 300
#repeat = 1
#retry = 3 
owner = true 
group = true
//使用ssh压缩传输方式
perms = -1 
fastcheck = false
//true表示通过文件创建时间来比较两地文件,若为false比较文件的内容
rsync = false 
//保持同步过来保持读写权限
sshargs = -C 
xferbycopying = true
//默认值是true,表示当需要同步的两个目录一个为空时,unison将停止,这里设置为false,即便为空unison也不会停止运转
confirmbigdel = false 
log = true
logfile = /root/.unison/unison.log

在这里插入图片描述

然后在服务器111.230.103.208配置

在服务器111.230.103.208配置
//先进入/tem/uniso/src/目录
#mkdir -p /root/.unison/
#vim /root/.unison/default.prf
//配置的内容
#Unison preferences file 
//root是配置的两个目录本地和远程,注意远程IP地址后面是两个//
root = /usr/Tomcat/ 
root = ssh://root@139.199.152.84//usr/Tomcat/
//path 指定的文件夹如果是Tomcat整个目录不用指定path
path = upload
path = image 
#force =
#ignore =
batch = true 
maxthreads = 300
#repeat = 1
#retry = 3 
owner = true 
group = true
//使用ssh压缩传输方式
perms = -1 
fastcheck = false
//true表示通过文件创建时间来比较两地文件,若为false比较文件的内容
rsync = false 
//保持同步过来保持读写权限
sshargs = -C 
xferbycopying = true
//默认值是true,表示当需要同步的两个目录一个为空时,unison将停止,这里设置为false,即便为空unison也不会停止运转
confirmbigdel = false
log = true
logfile = /root/.unison/unison.log

在这里插入图片描述
7.在两台服务器创建下面脚本文件

两个服务器都要创建.sh脚本
//到usr目录下创建
# cd usr/
# vim unison.sh

//以下为sh脚本中的内容:
# /bin/bash
src="/usr/Tomcat/" 
/usr/bin/inotifywait -mrq -e create,delete,modify,move $src | while read line; do
/usr/local/bin/unison 
echo -n "$(date +%F-%T) $line" >> /var/log/inotify.log
done

//执行脚本
#sh unison.sh

在这里插入图片描述
可以让脚本后台执行

//让脚本后台执行
# chmod +x  unison.sh
# nohup ./unison.sh >/dev/null 2>&1 &

8.如何让unison.sh脚本开机自启动?

用vim编辑rc.local开机启动脚本

# vim /etc/rc.d/rc.local
//将上边的unison.sh启动命令放入rc.local最后一行,注意unison.sh的存放目录,以下为编辑内容
nohup /usr/unison.sh >/dev/null 2>&1 &

在这里插入图片描述

修改完毕后保存脚本

注意:一定要查看rc.local是否能够获取系统启动权限,正常情况下rc.local文件是绿色的,否则你需要执行下面代码,给到rc.local权限

# chmod  755  /etc/rc.d/rc.local

好了 现在你可以重启系统测试一下脚本启动情况了,重启之后输入以下命令查看unison是否启动

# ps -ef | grep unison

如果自启动成功了 会显示如下结果
在这里插入图片描述
没什么问题你就可以愉快的使用unison+inotify进行文件同步了

9.最后在简单看下如何实现多台服务器资源同步
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来包瓜子儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值