在特定的情况下需要在多个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.最后在简单看下如何实现多台服务器资源同步