最近需要上线一个公司展厅项目,项目中主要是后台图片管理。因此它基本不会出现多人同时修改同一图片的情况,这样做双机的情况下,Web目录最好是双向同步。
在Linux下做WEB目录文件同步,一般有如下几种方式:
nfs实现web数据共享
rsync +inotify实现web数据同步
rsync+sersync更快更节约资源实现web数据同步
unison+inotify实现web数据双向同步
他们各有优缺点,这里我根据实际情况,选择方案4。
2. Unison简介Unison是一款跨平台的文件同步工具,不仅支持本地对本地同步,也支持通过SSH、RSH和Socket等网络协议进行同步。更棒的是,Unison支持双向同步操作,你既可以从A同步到B,也可以从B同步到A,这些都不需要额外的设定。
官方文档:
~bcpierce/unison//download/releases/stable/unison-2.48.4-manual.html
CentOS7.2 2台:
show160 10.1.0.160
show161 10.1.0.161
Objective Caml compiler (version 3.11.2 or later) 官网地址:
cd /tmpwget
tar -zxvf ocaml-4.03.0.tar.gz
cd ocaml-4.03.0
./configure
make configure
make world opt
make install
5. 安装unison
如果需要同步到远程目录,则远程机器也需要安装unison。
yum -y install ctags-etags # 缺少此安装包时下面make步骤会报错cd /tmp
wget ~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 # 有版本信息出现,则安装成功
6. 安装inotify
inotify官方地址:https://en.wikipedia.org/wiki/Inotify
yum -y install inotify-tools7. 配置双机ssh信任
show160上生成密钥,不输入私钥密码。
[root@show160 src]# ssh-keygenGenerating 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:
d6:3b:8a:47:23:04:5d:31:9a:97:d2:d3:5c:1b:f7:a3 root@show160
The key's randomart image is:
+--[ RSA 2048]----+
| . .+. o . |
| . .+ = . + . |
| .+ = o . ..|
| .o o . .|
| . S . E |
| ..o . |
| o .o |
| ... . |
| ... |
+-----------------+
[root@show160 src]# cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
[root@show160 src]# chmod 700 ~/.ssh
[root@show160 src]# chmod 600 ~/.ssh/authorized_keys
[root@show160 src]# rsync -avz /root/.ssh/authorized_keys root@10.1.0.161:/root/.ssh/authorized_keys
show161上生成密钥,不输入私钥密码。
[root@show161 tomcat]# ssh-keygenGenerating 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:
e8:b4:f7:91:ad:a0:83:fb:00:55:c2:c6:2c:65:08:91 root@show161
The key's randomart image is:
+--[ RSA 2048]----+
|o+ *+ . |
|E o.=o |
| o. |
| . . |
| . o S |
| . o . o |
| ..o o o . |
| ...o o o |
| .oo. o |
+-----------------+
[root@show161 tomcat]# cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
[root@show161 tomcat]# rsync -avz /root/.ssh/authorized_keys root@10.1.0.160:/root/.ssh/authorized_keys
在2台机器上分别ssh对方IP,能无密码登录则表示配置成功。
8. unison的使用