使用rsync+inotify实现/www目录实时同步

一、实现bak-server

1.1安装rsync

# yum -y install rsync

1.2修改配置文件

# vi /etc/rsyncd.conf
  #添加下面内容
uid=test
gid=test
reverse lookup = no

[www]
path=/www
read only=no
auth users=rsyncuser
secrets file=/etc/rsync.pas

1.3 创建同步目录,生成密码文件

# mkdir /www
# echo "rsyncuser:magedu" > /etc/rsync.pas	
# chmod 600 /etc/rsync.pas		

1.4 启动服务

# systemctl enable --now rsyncd
# ss -ntl
State       Recv-Q Send-Q Local Address:Port                Peer Address:Port              
LISTEN      0      5                  *:873                            *:*                  
LISTEN      0      128                *:111                            *:*                  
LISTEN      0      128                *:22                             *:*                  
LISTEN      0      100        127.0.0.1:25                             *:*                  
LISTEN      0      5               [::]:873                         [::]:*                  
LISTEN      0      128             [::]:111                         [::]:*                  
LISTEN      0      128             [::]:22                          [::]:*                  
LISTEN      0      100            [::1]:25                          [::]:* 

二、实现data-server

2.1安装相关包

# wget -O  /etc/yum.repos.d/epel.repo  http://mirrors.aliyun.com/repo/epel-7.repo
# yum install inotify-tools --enablerepo=epel -y
# yum -y install rsync 

2.2创建密码文件

# mkdir /www
# echo "rsyncuser:magedu" > /etc/rsync.pas
# chmod 600 /etc/rsync.pa

三、使用脚本实现实时同步

# cat inotify_rsync.sh
#!/bin/bash
SRC='/www' 
DEST='rsyncuser@10.0.0.17::www'

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

四、测试

#在客户端测试
[root@data-sever www]# rm -rf test.txt
[root@data-sever www]# dd if=/dev/zero of=f1.img bs=1M count=1000
[root@data-sever www]# dd if=/dev/zero of=f2.img bs=1M count=10
[root@data-sever www]# rm -rf f1.img


#在服务端观察实时同步情况
[root@bak-sever www]# watch -n0.5 ls -l .  

实时同步情况如下:

posted @ 2021-12-29 11:40  火火7412  阅读(59)  评论(2编辑  收藏  举报