centos7下rsync+inotify脚本实现文件同步,.NET CORE客户端文件更新后自动重启服务
源服务器IP:192.168.8.51
目标服务器IP:192.168.8.79
安装前源服务器及目标服务器均需关闭FIREWALLD\SELINUX防火墙
sestatus | grep status systemctl disable firewalld.service setenforce 0
一、源服务器安装
源服务器需要安装inotify-tools、rsync 服务,inotify脚本服务放置此主机上监控源文件目录变化:
安装inotify-tools
yum -y intsll inotify-tools
安装rsync并配置开机启动
yum -y intsll inotify-tools
systemctl enable --now rsyncd.service
密码配置文件rsync.pas 放置在/etc目录下,并修改权限为600
123456
chmod 600 /etc/rsync.pas
重启rsync服务
systemctl restart rsyncd
准备inotify_rsync.sh 脚本来监控源文件目录
#!/bin/bash # chkconfig: 2345 10 90 # description: resind SRC='/wwwroot/web/ma_tm_system/' DEST='rsyncuser@192.168.8.79::backup' rpm -q rsync &> /dev/null || yum -y install rsync inotifywait -mrq --exclude=".*\.swp" --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %w %f' -e create,delete,moved_to,close_write,attrib ${SRC} |while read DATE TIME DIR FILE;do FILEPATH=${DIR}${FILE} rsync -az --delete --password-file=/etc/rsync.pas $SRC $DEST && echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync" >> /var/log/changelist.log done
脚本放置在 /etc/init.d/下,并增加可执行权限,并配置开机启动。
chmod +x /etc/init.d/inotify_rsync.sh
chkconfig --add inotify_rsync.sh
二、目标服务器:192.168.8.79安装:
1、安装inotify-tools
yum -y intsll inotify-tools
2、安装rsync并配置开机启动
yum -y intsll inotify-tools
systemctl enable --now rsyncd.service
3、备份rsync原配置文件及密码文件,并用准备好的配置文件覆盖。
cp /etc/rsyncd.conf /etc/rsyncd.conf.bak
准备好的 rsyncd.conf 配置文件
# /etc/rsyncd: configuration file for rsync daemon mode # See rsyncd.conf man page for more options. # configuration example: # uid = nobody # gid = nobody # use chroot = yes # max connections = 4 # pid file = /var/run/rsyncd.pid # exclude = lost+found/ # transfer logging = yes # timeout = 900 # ignore nonreadable = yes # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # [ftp] # path = /home/ftp # comment = ftp export area uid = root gid = root max connections = 0 ignore errors exclude = lost+found/ log file = /var/log/rsyncd.log pid file = /var/run/rsyncd.pid lock file = /var/run/rsyncd.lock reverse lookup = no [backup] path = /wwwroot/web/ma_tm_system/ comment = backup dir read only = no auth users = rsyncuser secrets file = /etc/rsync.pas
4、密码配置文件rsync.pas 放置在/etc目录下,并修改权限为600
rsyncuser:123456
chmod 600 /etc/rsync.pas
重启rsync服务
systemctl restart rsyncd
5、目标服务器增加脚本用于监控WEB目录发生变化重启Core 服务。
inotify_resetCore.sh
#!/bin/bash # chkconfig: 2345 10 90 # description: resind SRC='/wwwroot/web/ma_tm_system/' inotifywait -mrq --exclude=".*\.swp" --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %w %f' -e create,delete,moved_to,close_write,attrib ${SRC} |while read DATE TIME DIR FILE; do systemctl restart Core.service done
脚本放置在 /etc/init.d/下,并增加可执行权限,并配置开机启动。
chmod +x /etc/init.d/inotify_resetCore.sh
chkconfig --add inotify_rsync.sh