hostnamectl --static set-hostname tCentos
1.下载文件到/usr/local/src
ocaml
unison
inotify-tools
2.安装inotify-tools
cd /usr/local/src
tar zvxf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify
make && make install
cd /usr/local/inotify/
echo "PATH=/usr/local/inotify/bin:$PATH" >/etc/profile.d/inotify.sh
source /etc/profile.d/inotify.sh
echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf
ldconfig -v | grep inotify
ln -sv /usr/local/inotify/include/ /usr/include/inotify
3.安装ocaml
cd /usr/local/src
tar zvxf ocaml-4.02.3.tar.gz
cd ocaml-4.02.3
./configure
make world opt && make install
4.安装unison
yum install ctags-etags -y
cd /usr/local/src
tar zvxf unison-2.48.3.tar.gz
cd unison-2.48.3
make UISTYLE=text #THREADS=true STATIC=true 表示使用命令行方式,加入线程支持以静态模式编译
make install
cp unison /usr/local/bin (make install会提示错误,cp unison /usr/local/bin,复制即可)
5.服务器A生成的公钥传到服务器B上
ssh-keygen -t rsa #生成ssh的密钥对
scp ~/.ssh/id_rsa.pub 192.168.1.41:/root/.ssh/img_rsa.pub
#生成的密钥在家目录的ssh文件中,ssh文件为隐藏文件,通过scp复制到服务器B上
cat img_rsa.pub >> authorized_keys
#在服务器A上把服务器B传来的公钥文件改名并存放到ssh目录下
chmod 600 authorized_keys #给公钥文件改权限为600
systemctl restart sshd
#重启sshd服务
6.服务器B生成的公钥传到服务器A上
ssh-keygen -t rsa #生成ssh的密钥对
scp ~/.ssh/id_rsa.pub 192.168.1.40:/root/.ssh/img01_rsa.pub
#生成的密钥在家目录的ssh文件中,ssh文件为隐藏文件,通过scp复制到服务器B上
cat img01_rsa.pub >> authorized_keys
#在服务器A上把服务器B传来的公钥文件改名并存放到ssh目录下
chmod 600 authorized_keys #给公钥文件改权限为600
systemctl restart sshd
#重启sshd服务
7.服务器A的同步脚本
mkdir -p /web/htdocs
#/bin/bash
ipB="192.168.1.41"
srcA="/web/htdocs"
dstB="/website"
/usr/local/inotify/bin/inotifywait -mrq -e create,delete,modify,move $srcA | while read line;do
/usr/local/bin/unison -batch $srcA ssh://$ipB/$dstB
echo -n "$line ">> /var/log/inotify.log 2>&1
echo `date|cut -d " " -f 1-4` >> /var/log/inotify.log 2>&1
done
赋予执行权限
chmod +x /data/sera.sh
监控形式启动,可以查看文件变更状态
sh -x /data/sera.sh
执行脚本并做开机启动:
chmod +x /etc/rc.d/rc.local
echo "sh /data/sera.sh &" >> /etc/rc.d/rc.local
8.服务器B的同步脚本
mkdir -p /website
#/bin/bash
ipA="192.168.1.40"
srcB="/website"
dstA="/web/htdocs"
/usr/local/inotify/bin/inotifywait -mrq -e create,delete,modify,move $srcB | while read line;do
/usr/local/bin/unison -batch $srcB ssh://$ipA/$dstA
echo -n "$line ">> /var/log/inotify.log
echo `date| cut -d " " -f 1-4` >> /var/log/inotify.log
done
赋予执行权限
chmod +x /data/serB.sh
监控形式启动,可以查看文件变更状态
sh -x /data/serB.sh
执行脚本并做开机启动:
chmod +x /etc/rc.d/rc.local
echo "sh /data/serB.sh &" >> /etc/rc.d/rc.local
#/bin/bash
UNISON=`ps -ef |grep -v grep|grep -c inotifywait`
if [ ${UNISON} -lt 1 ]
then
ip2="[email protected]:2222"
src2="/var/web2/"
dst2="/var/web1/"
/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line
do
/usr/local/bin/unison -batch -sshargs "-i /home/unison/.ssh/id_rsa" $src2 ssh://$ip2
/$dst2
echo -n "$line " >> /var/umelook-log/inotify/inotify$(date +%u).log
echo ` date +%F\ %T` >> /var/umelook-log/inotify/inotify$(date +%u).log
done
fi
ps -ef |grep -v grep|grep -c inotifywait
另外,原来官方的rc-local.service有点问题,把命令写入/etc/rc.local,然后启动rc-local.service会出问题,根本启动不了。后来偶在上面提供的网页里找到了解决办法。如下:修改/etc/systemd/system/rc-local.service
[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
新建/etc/rc.local文件
如果是简单的自定义命令,可写入/etc/rc.local,用上面的方法来处理。
#/bin/bash
srcB=/website/
dstA=/web/htdocs/
ipA=192.168.1.40
/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $srcB | while read line;do
/usr/local/bin/unison -batch $srcB ssh://$ipA/$dstA
echo -n "$line ">> /var/log/inotify.log
echo `date| cut -d " " -f 1-4` >> /var/log/inotify.log
done
echo ` date +%F\ %T` >> /var/umelook-log/inotify/inotify$(date +%u).log