rsync+inotify实现数据实时同步

inotify简介

inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,Linux内核从2.6.13开始引入,允许监控程序打开一个独立文件描述符,并针对事件集监控一个或者多个文件,例如打开、关闭、移动/重命名、删除、创建或者改变属性。

配置inotify需要rsync服务能直接传输数据(免密码传输)。

rsync配置请参考我的另一篇利用rsync进行数据同步。

服务端已配置完毕,inotify配置在客户端。

1.[[email protected] ~]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz    ##inotify下载

2.将安装包解压到/usr/local目录。

[[email protected] ~]# tar -xzvf inotify-tools-3.14.tar.gz -C /usr/local/

3.创建安装目录。

[[email protected] local]# mkdir inotify

4.inotify配置

[[email protected] inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify

5.编译于安装

[[email protected] inotify-tools-3.14]# make&&make install

6.写一个脚本实现当客户端/tmp/tuwei/下的文件有变化时,把此目录下的数据自动同步到服务器端的/rsydata下

vim /server/scripts/inotify/inotify.sh

#!/bin/bash

host01=192.168.1.10

src=/tmp/tuwei/

dst=tuwei

user=rsync_backup

rsync_passfile=/etc/rsync.password

inotify_home=/usr/local/inotify

#judge the file and server,it is the basic

if [ ! -e "$src" ] \

|| [ ! -e "${rsync_passfile}" ] \

|| [ ! -e "${inotify_home}/bin/inotifywait" ] \

|| [ ! -e "/usr/bin/rsync" ];

then

echo "pls check your file and try again"

exit 1

fi

${inotify_home}/bin/inotifywait -mrq --timefmt ‘%d%m%y %H:%M‘ --format ‘%T %w%f‘ -e close_write,delete,modify,create,attrib $src \

| while read file

do

/usr/bin/rsync -az  --delete $src   [email protected]$host01::$dst --password-file=${rsync_passfile}

done

脚本内容如上。

修改脚本权限。chmod +x  /server/scripts/inotify/inotify.sh

后台执行脚本 sh /server/scripts/inotify/inotify.sh &

[[email protected] tuwei]# sh /server/scripts/inotify/inotify.sh &

[1] 15111

测试验证

测试前

服务端

[[email protected] rsydata]# pwd

/rsydata

[[email protected] rsydata]# ll

total 0

客户端

[[email protected] tuwei]# pwd

/tmp/tuwei

[[email protected] tuwei]# ll

total 0

客户端添加文件。

[[email protected] tuwei]# touch {1..5}

[[email protected] tuwei]# ll

total 0

-rw-r--r--. 1 root root 0 Apr 16 21:50 1

-rw-r--r--. 1 root root 0 Apr 16 21:50 2

-rw-r--r--. 1 root root 0 Apr 16 21:50 3

-rw-r--r--. 1 root root 0 Apr 16 21:50 4

-rw-r--r--. 1 root root 0 Apr 16 21:50 5

服务端

[[email protected] rsydata]# ll

total 0

-rw-r--r-- 1 root root 0 Apr 16 21:50 1

-rw-r--r-- 1 root root 0 Apr 16 21:50 2

-rw-r--r-- 1 root root 0 Apr 16 21:50 3

-rw-r--r-- 1 root root 0 Apr 16 21:50 4

-rw-r--r-- 1 root root 0 Apr 16 21:50 5

客户端删除文件1  2

[[email protected] tuwei]# rm -rf 1 2

[[email protected] tuwei]# ll

total 0

-rw-r--r--. 1 root root 0 Apr 16 21:50 3

-rw-r--r--. 1 root root 0 Apr 16 21:50 4

-rw-r--r--. 1 root root 0 Apr 16 21:50 5

服务端

[[email protected] rsydata]# ll

total 0

-rw-r--r-- 1 root root 0 Apr 16 21:50 3

-rw-r--r-- 1 root root 0 Apr 16 21:50 4

-rw-r--r-- 1 root root 0 Apr 16 21:50 5

其他操作类似。

时间: 2024-08-03 15:32:32

rsync+inotify实现数据实时同步的相关文章

sersync基于rsync+inotify实现数据实时同步

一.环境描述 需求:服务器A与服务器B为主备服务模式,需要保持文件一致性,现采用sersync基于rsync+inotify实现数据实时同步 主服务器A:192.168.1.23 从服务器B:192.168.1.243 实时同步/var/atlassian目录到从服务器. 二.实施 1.从服务器192.168.1.243 rsync服务搭建 1.1安装软件包 wget http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz tar xf r

利用rsync+inotify实现数据实时同步脚本文件

将代码放在Server端,实现其它web服务器同步.首先创建rsync.shell,rsync.shell代码如下: #!/bin/bash host1=133.96.7.100 host2=133.96.7.101 host3=133.96.7.102 src=/data/www/ dst1=web1 dst2=web2 dst3=web3 user1=web1 user2=web2 user3=web3 /usr/local/inotify/bin/inotifywait -mrq --ti

[转帖]sersync基于rsync+inotify实现数据实时同步

sersync基于rsync+inotify实现数据实时同步 https://www.jianshu.com/p/d532a34e5cc5 前言 提到数据同步就必然会谈到rsync,一般简单的服务器数据传输会使用ftp/sftp等方式,但是这样的方式效率不高,不支持差异化增量同步也不支持实时传输. 原文地址:https://www.cnblogs.com/nbxcnxvcnb/p/12393252.html

rsync+inotify实现数据实时同步备份

在实际生产环境当中,我们总会遇见需要把一些重要数据进行备份,且随着应用系统规模的增大,对数据的安全性.可靠性.时效性要求还是比较高的, 因此我自己有在用rsync+inotify来实现数据实时同步备份,下面记录下操作步骤,以防日后自己忘记. 实验背景: 操作系统          IP         机器名        角色 CentOS 7.2       172.16.22.1     nginx01        数据源(服务器端) CentOS 7.2       172.16.22

rsync + inotify 实现数据实时同步

要求:两台Web服务器实现数据同步(我这里使用的是Centos 6.2-x64) 服务器一:172.16.11.126 服务器二:172.16.11.127 一.配置ssh备份源172.16.11.126(这里推荐使用专用的普通用户,注意相应的权限问题,如遇特殊情况使用root用户也可以,即不用考虑权限问题了. ) 1.新建备份用户rget rput 分别用来上传下载 [root@localhost ~]#  useradd rget [root@localhost ~]#  useradd r

通过rsync+inotify实现数据实时备份同步

一.环境描述 测试环境 需求:服务器A与服务器B为主备服务模式,需要保持文件一致性,现采用sersync基于rsync+inotify实现数据实时同步 环境描述: 主服务器172.26.7.50 ,从服务器172.26.7.51 实时同步/home/ 及/download 目录到从服务器 二.实施方法 1.从服务器172.26.7.51 rsync服务搭建 1.1下载软件包至从服务器 下载地址:http://rsync.samba.org/ftp/rsync/src 可根据环境需求下载相应的软件

基于rsync+inotify实现数据实时同步传输

前言 与传统的cp.tar备份方式相比,rsync具有安全性高.备份迅速.支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,但随着文件数量的增大和实时同步的要求,rsync已不能满足需求,随之rsync+inotify便应运而生.本文将讲解rsync的基础知识和如何基于rsync+inotify实现数据实时同步传输. rsync相关介绍 rsync(remote sync)是一款快速增量备份工具(远程同步),支持本地复制,或者与其他SSH(安全传输).rsync主机同步.

Linux下Rsync+Inotify-tools实现数据实时同步

说明: 一.先安装好rsync的服务端和客户端: http://douya.blog.51cto.com/6173221/1573968 二.安装,使用inotify-tools,实时同步 1.查看服务器内核是否支持inotify ll /proc/sys/fs/inotify   #列出文件目录,出现下面的内容,说明服务器内核支持inotify -rw-r--r-- 1 root root 0 Mar  7 02:17 max_queued_events -rw-r--r-- 1 root r

centos 6.9使用Rsync+Inotify-tools实现数据实时同步

说明: 操作系统:CentOS 6.9 源服务器:192.168.1.222 备份服务器:192.168.1.1.233 目的:把源服务器上/backup目录实时同步到备份服务器的/backup目录下 一.在备份服务器安装Rsync服务端 1.关闭SELINUX vi /etc/selinux/config #编辑防火墙配置文件 #SELINUX=enforcing #注释掉 #SELINUXTYPE=targeted #注释掉 SELINUX=disabled #增加 :wq! #保存,退出