Centos7 rsync+inotify实现实时同步更新

走远了吗. 提交于 2019-11-28 06:13:44

inotify slave部署  

   把master上指定文件下载到本地的主机指定目录

 yum install rsync –y
 [root@localhost ~]# useradd rsync -s /sbin/nologin -M
 [root@localhost ~]# mkdir -p /home/yxh/back
 [root@localhost ~]# chown rsync.rsync /home/yxh/back/

  echo rsync_backup:yxh >>/etc/rsync.password
   rsync_backup 为用户名
   yxh  为密码

[root@localhost ~]# chmod 600 /etc/rsync.password
[root@localhost ~]# rsync --daemon
[root@localhost ~]# ss -tunlp | grep rsync
tcp    LISTEN     0      5         *:873                   *:*                   users:(("rsync",pid=12133,fd=4))
tcp    LISTEN     0      5        :::873                  :::*                   users:(("rsync",pid=12133,fd=5))

重启rsync
[root@localhost back]# ps -ef | grep rsync
root      13472      1  0 20:10 ?        00:00:00 rsync --daemon
root      14185   9059  0 20:19 pts/1    00:00:00 grep --color=auto rsync
[root@localhost back]# kill -9 13472
[root@localhost back]# ps -ef | grep rsync
root      14253   9059  0 20:19 pts/1    00:00:00 grep --color=auto rsync
[root@localhost back]# rsync --daemon
failed to create pid file /var/run/rsyncd.pid: File exists
[root@localhost back]# rm -fr /var/run/rsyncd.pid
[root@localhost back]# ls
[root@localhost back]# rsync --daemon
安装流程
[root@localhost etc]# vi rsyncd.conf

# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

uid = rsync
gid = rsync
use chroot = no
max connections = 400
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

[backup]
  path = /home/yxh/back/
  ignore errors
  read only = no
  write only = no
  list = false
  fake super = yes
  auth users = rsync_backup
  secrets file = /etc/rsync.password
rsyncd.conf

 

inotify master部署

      master主机上的文件发生变化的时候     slave主机会自动进行同步变化的文件

[root@node2 ~]# yum install rsync –y
[root@node2 ~]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@node2 ~]# tar zxf inotify-tools-3.14.tar.gz
[root@node2 ~]# cd inotify-tools-3.14
[root@node2 inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
[root@node2 inotify-tools-3.14]# mkdir -p /home/yxh/back/
[root@node2 inotify-tools-3.14]# echo "yxh" >/etc/rsync.password
[root@node2 inotify-tools-3.14]# chmod 600 /etc/rsync.password


测试同步
[root@node2 back]# ls
test.txt
[root@node2 back]#  rsync  -avz /home/yxh/back/test.txt rsync_backup@192.168.11.175::backup --password-file=/etc/rsync.password

sending incremental file list
test.txt

sent 100 bytes  received 43 bytes  286.00 bytes/sec
total size is 7  speedup is 0.05
安装步骤
[root@node2 local]# vi inotify.sh

#!/bin/bash

host01=192.168.11.175
user=rsync_backup
rsync_passfile=/etc/rsync.password
inotify_home=/usr/local/inotify

#judge
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi

${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do

#  rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1
cd $src && rsync -aruz -R --delete ./  --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
exit 0
同步脚本

 

3.执行脚本实现自动更新

   [root@node2 local]# sh inotify.sh &

[root@node2 local]# sh inotify.sh &
[1] 32260

在master上添加一个test2.txt文件保存
[root@node2 back]# ls
test.txt
[root@node2 back]# vi test2.txt
[root@node2 back]# ls
test2.txt  test.txt


然后到slave节点上查看指定目录
test2.txt被自动同步到本地来
[root@localhost back]# ls
test2.txt  test.txt
[root@localhost back]# vi test2.txt


加入开机启动
# echo  "/bin/bash  /home/yxh/inotify.sh &" >>/etc/rc.local
View Code

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!