两台服务器双向同步文件,centos6.5上进行两台服务器之间的文件双向同步

由于用户的头像在 PC 端和 APP 端都可以进行更改,所以需要同步两台服务器的头像目录,为便可以相互访问。Django 服务器的存储目录为 “/home/django/wwwroot/stores/avatar”,PHP 服务器的存储目录为 “/usr/local/nginx/html/data/upload/shop/avatar”。我选择了用 “unison+inotify” 的方案来解决,步骤如下:

保证两台服务器之间可以通过 ssh 无密码访问

分别在 server1(192.168.1.120)和 server2(192.168.1.121)下,创建秘钥

[root@template ~]# cd /home/src/

[root@template ~]# mkdir ~/.ssh

[root@template ~]# chmod 700 ~/.ssh

生成 RSA 密钥

[root@template ~]# ssh-keygen -t rsa

(然后连续三次回车)

添加密钥到授权密钥文件中

[root@template ~]# cd ~/.ssh

[root@template ~]# ssh "-p 22" 192.168.1.120 cat /root/.ssh/id_rsa.pub >> authorized_keys #小写p

[root@template ~]# ssh "-p 22" 192.168.1.121 cat /root/.ssh/id_rsa.pub >> authorized_keys

[root@template ~]# scp -P 22 authorized_keys 192.168.1.121:/root/.ssh/ #大写P

[root@template ~]# chmod 600 /root/.ssh/authorized_keys

在服务器 server2 上操作

[root@template ~]# chmod 600 /root/.ssh/authorized_keys

分别在两台机器上执行如下测试

[root@template ~]# ssh -p 22 192.168.1.120 date

[root@template ~]# ssh -p 22 192.168.1.121 date

至此用户授权完成。

软件安装,server1 和 server2 都得安装

首先安装 ocaml

[root@template src]# wget http://caml.inria.fr/pub/distrib/ocaml-4.02/ocaml-4.02.3.tar.gz

[root@template src]# tar xvf ocaml-4.02.3.tar.gz

[root@template src]# cdocaml-4.02.3

[root@template ocaml-4.02.3]# ./configure

[root@template ocaml-4.02.3]# make world opt

[root@template ocaml-4.02.3]# make install

[root@template ocaml-4.02.3]# cd ..

安装 unison

[root@template src]# yum -y install ctags-etags

[root@template src]# wget wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.48.3.tar.gz

[root@template src]# tar xvf unison-2.48.3.tar.gz

[root@template src]# cdunison-2.48.3

[root@template unison-2.48.3]# make UISTYLE=text THREADS=true

[root@template unison-2.48.3]# cp unison /usr/local/bin

[root@template unison-2.48.3]# cd ..

安装 inotify

[root@template src]# curl -O https://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

[root@template src]# tar xvf inotify-tools-3.14.tar.gz

[root@template src]# cdinotify-tools-3.14

[root@template inotify-tools-3.14]# ./configure

[root@template inotify-tools-3.14]# make

[root@template inotify-tools-3.14]# make install

[root@template inotify-tools-3.14]# cd ..

到此所需的软件都已安装完毕,可以在 server1 服务器上执行这个命令,来查看两台服务器之间是否可以同步文件

[root@template src]# unison -batch /home/django/wwwroot/stores/avatar ssh://192.168.1.121//usr/local/nginx/html/data/upload/shop/avatar

如果这时候抱如下错误:

[root@template src]# /usr/local/bin/inotifywait: error whileloading shared libraries: libinotify

可以执行下这个命令:

[root@template src]# ln -sv /usr/local/lib/libinotify* /usr/lib/

执行成功后,看目录下的文件是否同步。

创建.sh 脚本来执行同步

server1 上创建脚本/root/inotify.sh(chmod a+x /root/inotify.sh) :

#/bin/bash

ip2="192.168.1.121"

src2="/home/django/wwwroot/stores/avatar/"

dst2="/usr/local/nginx/html/data/upload/shop/avatar/"

/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | whilereadline; do

/usr/local/bin/unison -batch $src2 ssh://$ip2/$dst2

echo -n "$line" >> /usr/local/logs/inotify.log

echo `date | cut -d " " -f1-4` >> /usr/local/logs/inotify.log

done

server2 上创建脚本/root/inotify.sh(chmod a+x /root/inotify.sh) :

#/bin/bash

ip2="192.168.1.120"

src2="/usr/local/nginx/html/data/upload/shop/avata/"

dst2="/home/django/wwwroot/stores/avatar/"

/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | whilereadline; do

/usr/local/bin/unison -batch $src2 ssh://$ip2/$dst2

echo -n "$line" >> /usr/local/logs/inotify.log

echo `date | cut -d " " -f1-4` >> /usr/local/logs/inotify.log

done

最后分别在 server1 和 server2 上执行上面两个脚本,这样两台服务器的目录会保持相互实时同步了!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值