192.168.1.11是server1,
192.168.1.22是server2。
【1】安装inotify-tools
各大linux发行版本都有inotify-tools软件包,建议通过yum方式安装:
[[email protected] ~]#yum install inotify-tools
[[email protected] ~]#yum install inotify-tools
******说明开始*********************************
郁闷的是!公司服务器上竟然没有找到inotify-tools这个包!
于是只能网上搜包,编译安装了:
[[email protected] ~]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[[email protected] ~]# tar xzvf inotify-tools-3.14.tar.gz
[[email protected] ~]# cd inotify-tools-3.14
[[email protected] ~]# ./configure
[[email protected] ~]# make
[[email protected] ~]# make install
[[email protected] ~]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[[email protected] ~]# tar xzvf inotify-tools-3.14.tar.gz
[[email protected] ~]# cd inotify-tools-3.14
[[email protected] ~]# ./configure
[[email protected] ~]# make
[[email protected] ~]# make install
******说明结束*********************************
【2】安装unison
通过源码包编译安装unison,需要Objective Caml compiler。
1)安装ocaml到/usr/local/src(建议版本3.0以上,安装路径可自己定义)
[[email protected] ~]# cd /usr/local/src
[[email protected] src]# wget http://caml.inria.fr/pub/distrib/ocaml-3.12/ocaml-3.12.1.tar.gz
[[email protected] src]# tar -zxvf ocaml-3.12.1.tar.gz
[[email protected] src]# cd ocaml-3.12.1
[[email protected] ocaml-3.12.0]# ./configure
[[email protected] ocaml-3.12.0]# make world opt
[[email protected] ocaml-3.12.0]# make install
2)编译安装unison(unison版本可以自行去网上下载)
[[email protected] src]# wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.40.63.tar.gz
[[email protected] src]# tar -xzvf unison-2.40.63.tar.gz
[[email protected] src]# cd unison-2.40.63
[[email protected] unison-2.40.63]# make UISTYLE=text
[[email protected] unison-2.40.63]# make install
******说明开始*********************************
执行make install过程中,可能会出现以下错误提示:
mv: cannot stat ‘/root/bin//unison‘: No such file or directory
make: [doinstall] Error 1 (ignored)
cp unison /root/bin/
cp: cannot create regular file ‘/root/bin/‘: Is a directory
make: *** [doinstall] Error 1
用百度在线翻译了一下,大致意思是:找不到/root/bin目录。
因为unison默认是将unison文件拷贝到/root/bin目录,但Linux默认没有该目录,所以我们需要将生成的可执行文件unison复制到系统PATH目录。
[[email protected] unison-2.40.63]# whereis $PATH //查看系统PATH目录
bin: /usr/local/bin
[[email protected] unison-2.40.63]# cp unison /usr/local/bin
******说明结束*********************************
3)将可执行文件unison上传到server2主机
[[email protected] unison-2.40.63]# scp unison [email protected]:/root/
******说明开始*********************************
远程连接可能会出现:
Are you sure you want to continue connecting (yes/no)?
别犹豫,敲个 yes 吧,然后出现:
[email protected]‘s password:
别发呆,输入server2的密码吧,然后出现:
unison 100% 1473KB 36k/s 00:40
说明unison已经成功上传至server2了。
双机没配置信任key之前,你远程连接都需要输入密码滴!
******说明结束*********************************
4)登陆server2主机,将unison复制到PATH目录
[[email protected] ~]# cp unison /usr/local/bin
【3】配置双机信任(ssh key)
1)server1创建key
[[email protected] ~]# ssh-keygen -t rsa
******说明开始*********************************
是不是出现了好多英文句子,看不懂不怪你,我也看不懂,哈哈!
用百度在线翻译一下,大致意思是:什么公钥,密钥存放位置了,是否需要私钥密码了之类的。
别理会,敲回车就成!之后,将生成一对密钥,id_rsa(私钥文件)和id_rsa.pub(公钥文件),保存在/root/.ssh/目录下。
******说明结束*********************************
2)将server1的公钥添加到server2的authorized_keys文件
--将钥文件传到server2主机
[[email protected] ~]# scp ~/.ssh/id_rsa.pub [email protected]:/root
******说明开始*********************************
远程连接可能会出现:
Are you sure you want to continue connecting (yes/no)?
别犹豫,敲个 yes 吧,然后出现:
[email protected]‘s password:
别发呆,输入server2的密码吧,然后出现:
id_rsa.pub 100% 394 0.4KB/s
说明公钥已经成功上传至server2了。
双机没配置信任key之前,远程连接都需要输入密码滴!
******说明结束*********************************
--登录server2,将公钥添加到authorized_keys文件中
[[email protected] ~]# mkdir .ssh
[[email protected] ~]# chmod 700 .ssh
[[email protected] ~]# mv ~/id_rsa.pub ~/.ssh/authorized_keys
[[email protected] ~]# chmod 600 ~/.ssh/authorized_keys
3)server2创建key
[[email protected] ~]# ssh-keygen -t rsa
******此处说明参看上面**************************
4)将server2的公钥添加到server1的authorized_keys文件
--将钥文件传到server1主机
[[email protected] ~]# scp ~/.ssh/id_rsa.pub [email protected]:/root
******此处说明参看server1**************************
--登录server1,将公钥添加到authorized_keys文件中
[[email protected] ~]# mv ~/id_rsa.pub ~/.ssh/authorized_keys
******说明开始*********************************
此处不用新建.ssh文件夹,不用chmod权限了?
server1当时创建key时候,.ssh等文件夹就已经存在了!
******说明结束*********************************
5)重启server1,server2的ssh服务
[[email protected] ~]# /etc/init.d/sshd restart
[[email protected] ~]# /etc/init.d/sshd restart
【4】测试是否配置成功
[[email protected] ~]# ssh [email protected] date
[[email protected] ~]# ssh [email protected] date
不用提示输入密码即可互相得到对方的时间,就证明配置成功了。
【5】使用说明
在任意一个server机上执行unison命令:
[[email protected] ~]# unison -bath /home/server1image/ ssh://192.168.1.22//home/server2image/
【6】创建.sh脚本来执行同步
1)server1上创建脚本unison2.sh:
#/bin/bash
ip2="192.168.1.22"
src2="/home/server1image/"
dst2="/home/server2image/"
/usr/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line; do
/usr/bin/unison -batch $src2 ssh://$ip2/$dst2
echo -n "$line " >> /var/log/inotify.log
echo `date | cut -d " " -f1-4` >> /var/log/inotify.log
done
2)server2上创建脚本unison1.sh:
#/bin/bash
ip1="192.168.1.11"
src1="/home/server2image/"
dst1="/home/server1image/"
/usr/bin/inotifywait -mrq -e create,delete,modify,move $src1 | while read line; do
/usr/bin/unison -batch $src1 ssh://$ip1/$dst1
echo -n "$line " >> /var/log/inotify.log
echo `date | cut -d " " -f1-4` >> /var/log/inotify.log
done
【7】执行.sh脚本
server1,server2各自执行.sh即可。