美文网首页rsync服务jok20190717
rsync+inotify-tools主从文件同步

rsync+inotify-tools主从文件同步

作者: __For丶dream | 来源:发表于2017-04-06 08:28 被阅读147次

1. 双机

1.1. 机器IP

173.168.1.131 //主
173.168.1.132 //从

1.2. 同步目录

/home/www

2. SSH2免密码登录

2.1. 生成rsa秘钥

使主机能免密码登录从机

在主机执行

ssh-keygen -t rsa

2.2. 公钥传输给从机

在主机执行

scp /root/.ssh/id_rsa.pub root@173.168.1.132:/root/id_rsa.pub

在从机执行

cat /root/id_rsa.pub >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys

2.3. 验证

在主机执行

ssh root@173.168.1.132

如果不需要密码可以登录则成功

3. Rsync

3.1. 安装

主从都需要安装

yum install rsync -y

3.2. 配置rsync密码

主机执行

echo password > /etc/rsync.passwd
chmod 0600 /etc/rsync.passwd

在从机上执行

echo root:password > /etc/rsync.passwd
chmod 0600 /etc/rsync.passwd

3.3. 配置rsync

在从机上执行

vi /etc/rsyncd.conf

uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[www]
path = /home/www/
comment = www file
ignore errors
read only = no
write only = no
hosts allow = 173.168.1.131
hosts deny = *
auth users = root
secrets file = /etc/rsync.passwd

3.4. 启动rsync服务

在从机上执行

rsync --daemon --port=873 --config=/etc/rsyncd.conf

4. Inotify-tools

4.1. 下载inotify-tools

主机下载安装,监控文件变化

wget https://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

4.2. 安装inotify-tools

tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install

4.3. 使用inotify-tools

在主机编写shell脚本vi /root/push.sh

#!/bin/bash
/usr/local/bin/inotifywait -mrq --timefmt '%y%m%d %H%M%S' --format '%T %w %e %f' --exclude "exclude/" -e delete -e moved_to -e moved_from -e close_write -e create /home/www/ | while read file
do
echo ${file} >> /var/log/inotify/www_inotifywait.log
DATENOW=$(date +%Y-%m-%d:%H:%M:%S)
state=`echo ${file} | awk '{print $4}'`
echo $state
case "${state}" in
CLOSE_WRITE,CLOSE|MOVED_TO|MOVED_FROM|DELETE)
echo $RSYNC_TIME:$RSYNC_FILE >> /var/log/inotify/www_filecreate.log
echo "---------start-----------"
echo $RSYNC_FILE

( rsync -vzrtog --delete --exclude "exclude/" --password-file=/etc/rsync.passwd /home/www/  root@173.168.1.132::www ) &

echo "---------end-----------"
echo $DATENOW > /var/log/inotify/www_tbstat.log
;;
DELETE,ISDIR )
echo "delete ,is dirr"
;;
esac
done
chmod +x /root/push.sh
/root/push.sh > /dev/null 2>&1 &

5. 添加目录同步

修改从机配置文件,重启从机rsync

vi /etc/rsyncd.conf

[xxx]
path = /xxx/
comment = xxx file
ignore errors
read only = no
write only = no
hosts allow = 173.168.1.131
hosts deny = *
auth users = root
secrets file = /etc/rsync.passwd

主机添加shell脚本vi /root/xxx.sh

#!/bin/bash
/usr/local/bin/inotifywait -mrq --timefmt '%y%m%d %H%M%S' --format '%T %w %e %f' --exclude "xxx/" -e delete -e moved_to -e moved_from -e close_write -e create /xxx/ | while read file
do
echo ${file} >> /var/log/inotify/xxx_inotifywait.log
DATENOW=$(date +%Y-%m-%d:%H:%M:%S)
state=`echo ${file} | awk '{print $4}'`
echo $state
case "${state}" in
CLOSE_WRITE,CLOSE|MOVED_TO|MOVED_FROM|DELETE)
echo $RSYNC_TIME:$RSYNC_FILE >> /var/log/inotify/xxx_filecreate.log
echo "---------start-----------"
echo $RSYNC_FILE

( rsync -vzrtog --delete --exclude "xxx/" --password-file=/etc/rsync.passwd /home/www/  root@173.168.1.132::xxx ) &

echo "---------end-----------"
echo $DATENOW > /var/log/inotify/xxx_tbstat.log
;;
DELETE,ISDIR )
echo "delete ,is dirr"
;;
esac
done
chmod +x /root/xxx.sh
/root/xxx.sh > /dev/null 2>&1 &

相关文章

网友评论

    本文标题:rsync+inotify-tools主从文件同步

    本文链接:https://www.haomeiwen.com/subject/snrfattx.html