CentOS文件双向同步

CentOS文件双向同步

以下内容是linux服务器中双向(多向)同步的部署过程

  • 系统:CentOS 6.5 x64
  • 环境: 
    • 服务器A:192.168.1.6,同步目录:/var/www
    • 服务器B:192.168.1.7,同步目录:/var/www
  • unison版本: unison-2.32.52
  • inotify版本:inotify-tools-3.14

安装unison

首先安装Objective Caml compiler (version 3.11.2 or later) 
下载地址:http://caml.inria.fr

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
yum install -y gcc
./configure
make world opt
make install

cd ..
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

安装unison 
如果需要同步到远程目录,则远程机器也需要安装unison。 
下载地址:www.seas.upenn.edu/~bcpierce/unison/

yum -y install ctags-etags  # 缺少此安装包时下面make步骤会报错
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 ~/src/unison-2.48.4.tar.gz
cd src/
make UISTYLE=text THREADS=true
cp unison /usr/local/bin/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

注: 
UISTYLE=text THREADS=true STATIC=true表示使用命令行方式,加入线程支持以静态模式编译

安装inotify-tools

inotify官方地址:https://en.wikipedia.org/wiki/Inotify

cd ~/src/
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar -zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

配置双机ssh信任

安装ssh
先查看是否已经安装,看有没有sshd
chkconfig --list #该命令可以看到本机上的服务和启动状态,以后设置服务开机子自启动也会用到

yum -y install openssh-server

安装完毕,查看 chkconfig --list sshd

手动启动sshd
service sshd start

设置开机自启动
chkconfig sshd on

查看是否设置成功,2-5都是on就是设置成功
# chkconfig -- list sshdsshd 0 :off 1 :off 2 :on 3 :on 4 :on 5 :on 6 :off

此时在宿主机外通过ssh远程连接工具远程到宿主机ip(10.0.75.1 )的端口(3222)应该就可以了

一、实现原理

使用一种被称为”公私钥”认证的方式来进行ssh登录。”公私钥”认证方式简单的解释是: 
首先在客户端上创建一对公私钥(公钥文件:~/.ssh/id_rsa.pub;私钥文件:~/.ssh/id_rsa),然后把公钥放到服务器上 
(~/.ssh/authorized_keys),自己保留好私钥。当ssh登录时,ssh程序会发送私钥去和服务器上的公钥做匹配。如果匹配成功就可 
以登录了。

二、实验环境 
A机:192.168.1.6 
B机:192.168.1.7

三、Linux/Unix双机建立信任

3.1 在A机生成证书

在A机root用户下执行ssh-keygen命令,在需要输入的地方,直接回车,生成建立安全信任关系的证书。

# ssh-keygen  -t  rsa
  • 1

注意:在程序提示输入passphrase时直接输入回车,表示无证书密码。 
上述命令将生成私钥证书id_rsa和公钥证书id_rsa.pub,存放在用户家目录的.ssh子目录中。

3.2 查看~/.ssh生成密钥的文件

# cd ~/.ssh
# ll
  • 1
  • 2

3.3 A对B建立信任关系

将公钥证书id_rsa.pub复制到机器B的root家目录的.ssh子目录中,同时将文件名更换为authorized_keys,此时需要输 
入B机的root用户密码(还未建立信任关系)。建立了客户端到服务器端的信任关系后,客户端就可以不用再输入密码,就可以从服务器端拷贝数据了。

# scp -r id_rsa.pub 192.168.1.7:/root/.ssh/authorized_keys
  • 1

3.4 B对A建立信任关系

在B机上执行同样的操作,建立B对A的信任关系。

# ssh-keygen -t rsa
# cd ~/.ssh/
# ll
# scp -r id_rsa.pub 192.168.1.6:/root/.ssh/authorized_keys (yum install openssh-clients
  • 1
  • 2
  • 3
  • 4

测试 ssh root@192.168.1.6 /// ssh root@192.168.1.7

如果连接反应慢,请修改以下两参数

/etc/ssh/sshd_config 
GSSAPIAuthentication no 
UseDNS no

然后重启service sshd restart

添加脚本

在192.168.1.6服务器A上添加脚本:

mkdir /script
vim /script/inotify.sh
  • 1
  • 2

inotify.sh脚本内容

######################以下是脚本内容#########################
#/bin/bash
UNISON=`ps -ef |grep -v grep|grep -c inotifywait`
if [ ${UNISON} -lt 1 ]
then
ip2="192.168.1.7"
src2="/var/www/"
dst2="/var/www/ "
/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line
do
/usr/local/bin/unison -batch $src2 ssh://$ip2/$dst2
echo -n "$line " >> /var/log/inotify/inotify$(date +%u).log
echo ` date +%F\ %T " " -f1-4` >> /var/log/inotify/inotify$(date +%u).log
done
fi
###########################################################
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在192.168.1.7服务器上添加脚本:

mkdir /script
vim /script/inotify.sh
  • 1
  • 2

inotify.sh脚本内容

######################以下是脚本内容#########################
#/bin/bash
UNISON=`ps -ef |grep -v grep|grep -c inotifywait`
if [ ${UNISON} -lt 1 ]
then
ip2="192.168.1.6"
src2="/var/www/"
dst2="/var/www/ "
/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line
do
/usr/local/bin/unison -batch $src2 ssh://$ip2/$dst2
echo -n "$line " >> /var/log/inotify/inotify$(date +%u).log
echo ` date +%F\ %T " " -f1-4` >> /var/log/inotify/inotify$(date +%u).log
done
fi
###########################################################
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在二台服务器上修改脚本权限:

chmod a+x /script/inotify.sh
  • 1

在计划任务中添加任务(原本在/etc/rc.local下添加开机启动的,但出问题,脚本并不执行)

crontab –e
#unison+inotify
* * * * * /bin/sh /script/inotify.sh > /dev/null 2>&1 &
  • 1
  • 2
  • 3

测试: 
重启电脑,测试二台服务器中/var/www的内容是否能同步 
不重启电脑,手动执行脚本也可以测试

sh /script/inotify
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值