unison+inotify部署
功能介绍
unison:
- Unison是一款跨windows/linux/MAC OS平台的文件同步工具,不仅支持本地对本地同步,也支持通过SSH、RSH和Socket等网络协议与远程主机进行同步。
- Unison支持双向同步操作,即任何一端数据发生改变,都会更新到对端;若要实现实时同步需使用inotify配合
- 支持增量同步,每次同步完成后会记录文件状态,下次同步时,以上次的状态为起点开始同步
rsync:rsync同步是单向的,客户端同步服务器端,而客户端进行文件的删除、修改等操作无法同步到服务端。
nfs:若nfs-server服务器挂掉,则整个网络共享文件失效
unison安装
依赖于ocaml编译器(Objective Caml compiler)
- 安装依赖编译器ocaml
下载链接:戳这里
[root@gupt-registry unison+inotify]# tar zxf ocaml-4.03.0.tar.gz -C /usr/local/
[root@gupt-registry unison+inotify]# cd /usr/local/ocaml-4.03.0/
[root@gupt-registry ocaml-4.03.0]# ./configure
[root@gupt-registry ocaml-4.03.0]# make world opt
[root@gupt-registry ocaml-4.03.0]# make install
[root@gupt-registry ocaml-4.03.0]# ocaml -version
'The OCaml toplevel, version 4.03.0
- 安装unison
下载地址:戳这里
[root@gupt-registry unison+inotify]# yum install ctags-etags #unison make时的依赖包
[root@gupt-registry unison+inotify]# tar zxf unison-2.48.4.tar.gz -C /usr/local/
[root@gupt-registry unison+inotify]# mv /usr/local/src /usr/local/unison-2.48.4
[root@gupt-registry unison+inotify]# cd /usr/local/unison-2.48.4/
[root@gupt-registry unison-2.48.4]# make UISTYLE=text THREADS=true #使用命令行方式,加入线程支持以静态模式编译
1. 接下来如果直接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/’: Not a directory
make: *** [doinstall] Error 1
因为在install的过程中,unison默认是将unison文件复制到/root/bin/目录下的,但是linux没有此目录,所以我们需要创建该目录
[root@gupt-registry unison-2.48.4]# mkdir /root/bin
[root@gupt-registry unison-2.48.4]# cp unison /root/bin/
[root@gupt-registry unison-2.48.4]# make install
[root@gupt-registry unison-2.48.4]# ls /root/bin/
unison unison-2.48
[root@gupt-registry unison-2.48.4]# cp /root/bin/unison* /usr/local/bin/
[root@gupt-registry unison-2.48.4]# rm -fr /root/bin/
2. 不需要install,make完成后会生成unison二进制文件,直接复制到PATH路径下,即可使用
[root@gupt-registry unison-2.48.4]# cp unison /usr/local/bin/
通过unison -version查看是否正常。
以上操作均可在一台服务器上完成,然后将unison二进制文件复制到所需服务器上
inotify-tools安装
下载地址:戳这里
[root@gupt-registry unison+inotify]# tar zxf inotify-tools-3.14.tar.gz -C /usr/local/
[root@gupt-registry unison+inotify]# cd /usr/local/inotify-tools-3.14/
[root@gupt-registry inotify-tools-3.14]# ./configure
[root@gupt-registry inotify-tools-3.14]# make
[root@gupt-registry inotify-tools-3.14]# make install
配置ssh双机认证
[root@localhost mnt]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
41:37:25:c1:b9:f8:1e:a9:25:6c:65:ca:f2:da:cf:bb root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
| ..=+. |
| . .oo |
| .. . |
| ..+ |
| oS= . |
| . * = |
| + = . |
| .o.. |
| ....Eo |
+-----------------+
接下来操作为复制公钥到远程服务器
[root@localhost mnt]# scp /root/.ssh/id_rsa.pub root@192.168.168.42:/root/.ssh/
远程主机将公钥导入到authorized_keys文件中
[root@gupt-registry .ssh]# cat id_rsa.pub > authorized_keys
也可使用ssk-copy-id完成上述步骤
创建完密钥对后使用如下命令完成上述过程
[root@gupt-registry .ssh]# ssh-copy-id root@192.168.168.202
请注意:.ssh权限700,authorized_keys权限600
部署监控脚本
创建/script/inotify.sh脚本
[root@localhost ~]# cat /script/inotify.sh
#!/bin/bash
IP1=192.168.168.42
src1=/script/test
dst1=/script/test
/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src1 | while read line; do /usr/local/bin/unison -batch ${src1} ssh://${IP1}/${dst1}
echo -n "$line " >> /var/log/inotify/inotify$(date +%u).log
echo `date | cut -d " " -f1-4` >> /var/log/inotify/inotify$(date +%u).log
done
[root@localhost ~]# mkdir /var/log/inotify
[root@localhost ~]# cd /script/
[root@localhost script]# nohup /script/inotify.sh &
[1] 14903
[root@localhost script]# nohup: ignoring input and appending output to ‘nohup.out’
[root@localhost script]# ps aux | grep inotify
root 14903 0.0 0.0 113180 1176 pts/0 S 14:58 0:00 /bin/bash /script/inotify.sh
root 14904 0.0 0.0 6524 376 pts/0 S 14:58 0:00 /usr/local/bin/inotifywait -mrq -e create,delete,modify,move /script/test
root 14905 0.0 0.0 113180 384 pts/0 S 14:58 0:00 /bin/bash /script/inotify.sh
root 15096 0.0 0.0 112708 936 pts/0 R+ 14:58 0:00 grep --color=auto inotify
在另一台服务器上也创建该脚本
[root@gupt-registry ~]# cat /script/inotify.sh
#!/bin/bash
IP1=192.168.168.202
src1=/script/test
dst1=/script/test
inotifywait -mrq -e create,delete,modify,move $src1 | while read line; do unison -batch $src1 ssh://$IP1/$dst1
echo -n "$line " >> /var/log/inotify/inotify$(date +%u).log
echo `date | cut -d " " -f1-4` >> /var/log/inotify/inotify$(date +%u).log
done
[root@gupt-registry ~]# mkdir /var/log/inotify/
[root@gupt-registry ~]# cd /script/
[root@gupt-registry script]# nohup /script/inotify.sh &
[1] 41707
[root@gupt-registry script]# nohup: ignoring input and appending output to ‘nohup.out’
[root@gupt-registry script]# ps aux | grep inotify
root 41707 0.0 0.0 113176 1176 pts/0 S 15:05 0:00 /bin/bash /script/inotify.sh
root 41708 0.0 0.0 6520 376 pts/0 S 15:05 0:00 inotifywait -mrq -e create,delete,modify,move /script/test
root 41709 0.0 0.0 113176 388 pts/0 S 15:05 0:00 /bin/bash /script/inotify.sh
root 41725 0.0 0.0 112708 944 pts/0 R+ 15:05 0:00 grep --color=auto inotify
最后,就可以测试在/script/test目录下操作了,测试文件是否同步。
在实际测试中,发现在传输大文件的过程中(如课件之类),会出现传输成功后部分.unison.tmp文件不被清除的情况,虽不影响正常使用,可手动清除,但仍会占用存储空间,不知何故。