CentOS 7 rsync +inotify配置文件实时同步
环境准备:
机器1:10.9.19.244 centos 7.2
机器2:10.9.19.213 centos 7.2
同步说明:10.9.19.244::/data/h5/pubic/ ---> 10.9.19.213::/usr/local/nginx/html/www/c/
安装:
机器1:10.9.19.213
1、关闭selinux
sudo vim /etc/selinux/config
SELINUX=disabled #修改为disabled
sudo setenforce 0
2、安装rsync
sudo yum -y install rsync
sudo vim /etc/rsyncd.conf
max connections = 4
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
exclude = lost+found/
transfer logging = yes
timeout = 900
ignore nonreadable = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
[rsyncdata]
path = /usr/local/nginx/html/www/c/
ignore errors = yes
read only = no
write only = yes
hosts allow = 10.9.19.244
hosts deny = *
list = false
uid = root
gid = root
auth users = rsyncuser
secrets file = /etc/rsyncd.passwd
sudo vim /etc/rsyncd.passwd
rsyncuser:sdjI823yo
sudo chmod 600 /etc/rsyncd.passwd
sudo systemctl start rsyncd
机器2:10.9.19.244
1、关闭selinux
sudo vim /etc/selinux/config
SELINUX=disabled #修改为disabled
sudo setenforce 0
2、安装rsync
sudo yum -y install rsync
sudo vim /etc/rsyncd.passwd
sdjI823yo
sudo chmod 600 /etc/rsyncd.passwd
3、测试
sudo rsync -auv --password-file=/etc/rsyncd.passwd /data/h5/public/ rsyncuser@10.9.19.213::rsyncdata
4、安装inotify
首先查看系统是否支持inotify功能 ls -l /proc/sys/fs/inotify/
centos 7是支持的
下载inotify
wget http://downloads.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
编译安装
sudo yum groupinstall "Development Tools"
sudo tar -zxvf inotify-tools-3.13.tar.gz -C /usr/local/src/
cd /usr/local/src/inotify-tools-3.13/
sudo ./configure --prefix=/usr/local/inotify
sudo make
sudo make install
编写同步脚本
[ops@em01 ~]$ cat /usr/local/bin/rsync_data.sh
#!/bin/bash
date=`date +%Y%m%d`
host=10.9.19.213
src=/data/h5/publish
dst=rsyncdata
user=rsyncuser
rsync_passible=/etc/rsyncd.passwd
inotify_home=/usr/local/inotify
#if [ ! -e "$src" ]\
# || [ ! -e "$rsync_passible" ]\
# || [ ! -e "$inotify_home/bin/inotifywait" ]\
# || [ ! -e "/usr/bin/rsync" ]
# then
# echo "File is not enough to run inotify!"
# exit 9
#fi
${inotify_home}/bin/inotifywait -mrq --format "%w%f" -e close_write,modify,attrib,delete,create $src \
| while read line
do
# [ ! -e $line ] && continue
echo '$line'=$line > /tmp/rsync_$date.log
cd $src && rsync -aruz -R --delete ./ --timeout=104 $user@$host::$dst --password-file=${rsync_passible} >/dev/null 2>&1
done
exit 0
sudo nohup sh /usr/local/bin/rsync_data.sh & #后台运行
然后测试!