项目背景
在项目中遇到一个问题,需要做到两台服务器的指定文件夹实时同步,例如:A服务器上传了一张1.jpg图片,B服务器对应文件夹立马就同步出来一张1.jpg图片。
准备材料
两台(多台)linux服务器,互相联通,最好可以通过内网访问。这里以两台服务器内网访问为例,A服务器ip:192.168.0.100,B服务器ip:192.168.0.101 需要同步的文件夹,例如:A服务器:/www/A/upload B服务器/www/B/upload
配置环境
安装Objective Caml compiler,注意:两台服务器都安装
#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
安装Unison,注意:两台服务器都安装
#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
安装inotfy,注意:两台服务器都安装
#yum -y install inotify-tools
配置双机SSH信任,注意:两台服务器都配置
#yum install rsync -y
在A服务器配置
#ssh-keygen
//连续按三次空格
# cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
//给权限
# chmod 700 ~/.ssh
# chmod 600 ~/.ssh/authorized_keys
//需要输入一次密码 **注意:这里需要替换B服务器的ip**
# rsync -avz /root/.ssh/authorized_keys root@192.168.0.101:/root/.ssh/authorized_keys
在B服务器配置
#ssh-keygen
//连续按三次空格
# cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
//给权限
# chmod 700 ~/.ssh
# chmod 600 ~/.ssh/authorized_keys
//需要输入一次密码 **注意:这里需要替换A服务器的ip**
# rsync -avz /root/.ssh/authorized_keys root@192.168.0.100:/root/.ssh/authorized_keys
现在可以测试一下是否配置成功
//A服务器测试
#ssh -p 22 192.168.0.101 date
//B服务器测试
#ssh -p 22 192.168.0.100 date
//如果没有出现输入密码的现象,就说明配置成功
配置双机同步
//先进入/tem/uniso/src/目录
#mkdir -p /root/.unison/
#vim /root/.unison/default.prf
//配置的内容,以下内容里的“#”号也不能少
#Unison preferences file
//root是配置的两个目录本地和远程,注意远程IP地址后面是两个//
//这里是A服务器的文件夹地址
root = /www/A/upload/
//这里是B服务器的ip和文件夹地址
root = ssh://root@192.168.0.101//www/B/upload/
//path 指定的文件夹如果是upload整个目录不用指定path,如果要指定哪几个文件夹,可以在下方加上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
//先进入/tem/uniso/src/目录
#mkdir -p /root/.unison/
#vim /root/.unison/default.prf
//配置的内容,以下内容里的“#”号也不能少
#Unison preferences file
//root是配置的两个目录本地和远程,注意远程IP地址后面是两个//
//这里是B服务器的文件夹地址
root = /www/B/upload/
//这里是A服务器的ip和文件夹地址
root = ssh://root@192.168.0.100//www/A/upload/
//path 指定的文件夹如果是upload整个目录不用指定path,如果要指定哪几个文件夹,可以在下方加上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
创建脚本文件 注意:两台服务器都要创建
//到usr目录下创建
#cd /usr
#vim unison.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 &
至此,就请测试见证奇迹吧
实现多台同步的思路图 引用原文地址