温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

rsync+inotify实时备份

发布时间:2020-06-26 21:20:14 来源:网络 阅读:471 作者:yueyuanyiluo 栏目:移动开发

rsync+inotify实时备份

为什么要使用inotify

远程同步方式,我们一般采用rsync命令、或者守护进程的方式。

优点:具有安全性高、备份迅速、支持增量备份。

缺点:1、rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。

2、rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,实际是异步同步,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。

 

inotify 是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,当文件有任何变动时,就触发rsync同步,解决了同步数据的实时性问题。

意思:

inotify可以支持一对一、一对多的实时同步,支持单机实时备份、灾难切换,也可以实现在负载均衡LB下同时更新、上线多台RS服务器或者保持数据一致性。

实时同步实例:

同步顺序:青岛监控机10.32.232.238(监控主机)——> 10.32.232.239(监控备机)

Web1:10.32.232.238(rsyncclient,inotify)

Web2:10.32.232.239(rsyncserver)

同步的目录是 /usr/local/nagios/etc , 实现监控备机配置文件与主机实时同步。

一、配置 rsync服务器端(238

1.先使用 rpm –qa 命令查看系统中是否已经安装了 rsync 软件包,如果有我们可以不用进行安装,如果没有我们可以挂载上 CentOS 的系统光盘进行安装(当然,我们也可以选择使用源码包编译安装)

[root@232-239-qd-monitor~]# rpm -qa | grep rsync

rsync-3.0.6-9.el6_4.1.x86_64

[root@232-239-qd-monitor~]#

2. Rsync安装好之后没自己的配置文件,创建 rsync 的配置文件

[root@232-239-qd-monitortmp]# cat /etc/rsyncd.conf

##rsyncd.confstart##

uid= root

gid= root

usechroot = no

maxconnections = 200     ##最大连接数

timeout= 300             ##超时时间

pidfile = /var/run/rsyncd.pid

lockfile = /var/run/rsync.lock

logfile = /var/log/rsyncd.log

ignoreerrors

readonly = false        ##只读为假,可以上传

list= false

hostsallow = 10.32.0.0/24     ##允许网段

hostsdeny = 0.0.0.0/32

authusers = rsync_backup      ##用户

secretsfile = /etc/rsync.password   ##密码文件

[nagios]                       ##模块

path= /usr/local/nagios/etc   ##模块目录

[wangxu]

path= /tmp

#rsync_config_______________end

3.建立 rsync 用户名和密码文件

[root@232-238-qd-monitor~]#  echo "rsync_backup:monitor " >>  /etc/rsync.password

4.为 /etc/rsync.password 授权为 600(这个文件的权限必须是 600)

[root@232-238-qd-monitor~]# chmod -R 600 /etc/rsync.password

二、客户端(239)配置:

1.设置 rsync 客户端的密码文件,客户端只需要设置 rsync 同步的密码即可,不用设置用户名

[root@232-239-qd-monitor~]# echo "monitor" >>  /etc/rsync.password

4.   为 /etc/rsync.password 授权为 600(这个文件的权限必须是 600)

[root@232-239-qd-monitor~]# chmod -R 600 /etc/rsync.password

三、测试rsync守护进程。

[root@232-239-qd-monitortmp]# rsync --daemon    ####启动

[root@232-239-qd-monitortmp]# netstat -lntp | grep rsync

tcp        0     0 0.0.0.0:873                0.0.0.0:*                   LISTEN      36834/rsync        

tcp        0     0 :::873                     :::*                        LISTEN      36834/rsync        

[root@232-239-qd-monitortmp]#

[root@232-239-qd-monitortmp]# pwd

/tmp

[root@232-239-qd-monitortmp]# ls

10.32.231.177_mongodb_cacti_stats.txt  10.32.232.222_mongodb_cacti_stats.txt  10.32.232.225_mongodb_cacti_stats.txt  sms.log

10.32.231.186_mongodb_cacti_stats.txt  10.32.232.224_mongodb_cacti_stats.txt  10.32.232.226_mongodb_cacti_stats.txt

[root@232-239-qd-monitortmp]#

——————————————————————————————————————————————————

[root@232-238-qd-monitortmp]# pwd

/tmp

[root@232-238-qd-monitortmp]# rsync -avz  rsync_backup@10.32.232.239::wangxu//tmp/ --password-file=/etc/rsync.password            #### 同步

receivingincremental file list

./

10.32.231.177_mongodb_cacti_stats.txt

10.32.231.186_mongodb_cacti_stats.txt

10.32.232.222_mongodb_cacti_stats.txt

10.32.232.224_mongodb_cacti_stats.txt

10.32.232.225_mongodb_cacti_stats.txt

10.32.232.226_mongodb_cacti_stats.txt

 

sent220 bytes  received 12073 bytes  24586.00 bytes/sec

totalsize is 170894  speedup is 13.90

[root@232-238-qd-monitortmp]# ls

10.32.231.177_mongodb_cacti_stats.txt  10.32.232.222_mongodb_cacti_stats.txt  10.32.232.225_mongodb_cacti_stats.txt 

10.32.231.186_mongodb_cacti_stats.txt  10.32.232.224_mongodb_cacti_stats.txt  10.32.232.226_mongodb_cacti_stats.txt 

[root@232-238-qd-monitortmp]#

Delete 用法:

本地为空,远端目录直接清空。有使远端rsync服务端丢数据的风险。

[root@232-238-qd-monitortmp]# ls

[root@232-238-qd-monitortmp]# rsync -avz --delete /tmp/ rsync_backup@10.32.232.239::wangxu/  --password-file=/etc/rsync.password

sendingincremental file list

./

deletingsms.log

deletingmonitor_bak.sh

deleting10.32.232.226_mongodb_cacti_stats.txt

deleting10.32.232.225_mongodb_cacti_stats.txt

deleting10.32.232.224_mongodb_cacti_stats.txt

deleting10.32.232.222_mongodb_cacti_stats.txt

deleting10.32.231.186_mongodb_cacti_stats.txt

deleting10.32.231.177_mongodb_cacti_stats.txt

 

sent55 bytes  received 12 bytes  134.00 bytes/sec

totalsize is 0  speedup is 0.00

[root@232-238-qd-monitortmp]# ls

[root@232-238-qd-monitortmp]#

 

[root@232-239-qd-monitortmp]# ls

10.32.231.177_mongodb_cacti_stats.txt  10.32.232.222_mongodb_cacti_stats.txt  10.32.232.225_mongodb_cacti_stats.txt  monitor_bak.sh

10.32.231.186_mongodb_cacti_stats.txt  10.32.232.224_mongodb_cacti_stats.txt  10.32.232.226_mongodb_cacti_stats.txt  sms.log

[root@232-239-qd-monitortmp]# ls

[root@232-239-qd-monitortmp]#

四、配置initify(web1)

wgethttp://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

tar  -zxf inotify-tools-3.14.tar.gz

cdinotify-tools-3.14

 ./configure && make && makeinstall

1、详解inotify

root@232-238-qd-monitorbin]# pwd

/usr/local/bin

[root@232-238-qd-monitorbin]# ls inotifywa*

inotifywait  inotifywatch

[root@232-238-qd-monitorbin]#

inotifywait用于等待文件或文件集上的一个特定事件,它可以监控任何文件和目录设置,并且可以递归地监控整个目录树。
     inotifywatch用于收集被监控的文件系统统计数据,包括每个inotify事件发生多少次等信息。

Inotify 可以监视的文件系统事件包括:

IN_ACCESS,即文件被访问

IN_MODIFY,文件被 write

IN_ATTRIB,文件属性被修改,如 chmod、chown、touch 等

IN_CLOSE_WRITE,可写文件被 close

IN_CLOSE_NOWRITE,不可写文件被 close

IN_OPEN,文件被 open

IN_MOVED_FROM,文件被移走,如 mv

IN_MOVED_TO,文件被移来,如 mv、cp

IN_CREATE,创建新文件

IN_DELETE,文件被删除,如 rm

IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己

IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己

IN_UNMOUNT,宿主文件系统被 umount

IN_CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)

IN_MOVE,文件被移动,等同于(IN_MOVED_FROM | IN_MOVED_TO)

备注:上面的文件也包括目录

 

Inotifywait是一个监控等待事件,可以配合shell脚本使用它,下面介绍一下常用的一些参数:

 -m,即--monitor,表示始终保持事件监听状态。
 -r,即--recursive,表示递归查询目录。
 -q,即--quiet,表示打印出监控事件。
 -e,即--event,通过此参数可以指定要监控的事件,常见的事件有modify、delete、create、attrib等。
更详细的请参看man  inotifywait。

inotify相关参数

inotify定义了下列的接口参数,可以用来限制inotify消耗kernel memory的大小。由于这些参数都是内存参数,因此,可以根据应用需求,实时的调节其大小。
   /proc/sys/fs/inotify/max_queued_evnets     
      表示调用inotify_init时分配给inotifyinstance中可排队的event的数目的最大值,超出这个值的事件被丢弃,但会触发IN_Q_OVERFLOW事件。
   /proc/sys/fs/inotify/max_user_instances
       表示每一个real user ID可创建的inotifyinstatnces的数量上限。
   /proc/sys/fs/inotify/max_user_watches
       表示每个inotify instatnces可监控的最大目录数量。如果监控的文件数目巨大,需要根据情况,适当增加此值的大小

[root@232-238-qd-monitorinotify-tools-3.14]# cat /proc/sys/fs/inotify/max_queued_events

16384

2inotify监控脚本

[root@232-238-qd-monitor wangxu]# catrsync_inotify.sh

#!/bin/bash

#para

host01=10.32.232.239        ##同步IP,可以设置多个

src=/usr/local/nagios/etc     ##同步的本地目录

dst=nagios                  ##rsync服务端模块

user=rsync_backup           ##rsync用户

rsync_passfile=/etc/rsync.password

inotify_home=/usr/local

#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' -eclose_write,delete,create,attrib $src \

|while read file

        do

        #rsync -avzP --delete --timeout=100--password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null2>&1

         cd $src && rsync -aruz -R--delete ./  --timeout=100$user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1

        done

exit0

[root@232-238-qd-monitorwangxu]#

[

2、执行脚本。

[root@232-238-qd-monitoretc]# sh /root/wangxu/rsync_inotify.sh &

[root@232-238-qd-monitorwangxu]# ps -ef | grep inotify

root      8534 26546  0 09:56 pts/0    00:00:00 grep inotify

root     30432 26546  0 09:48 pts/0    00:00:00 sh /root/wangxu/rsync_inotify.sh

root     30433 30432  0 09:48 pts/0    00:00:00 /usr/local/bin/inotifywait -mrq--timefmt %d/%m/%y %H:%M --format %T %w%f -e close_write,delete,create,attrib/usr/local/nagios/etc

root     30434 30432  0 09:48 pts/0    00:00:00 sh /root/wangxu/rsync_inotify.sh

[root@232-238-qd-monitorwangxu]#

3、检查测试。

[root@232-238-qd-monitor etc]# ls

cgi.cfg   htpasswd.bak.20140326  htpasswd.users      nagios.cfg       nrpe.cfg objects.yuan  resource.cfg

htpasswd  htpasswd.bk            htpasswd.users.bak  nagios.cfg.yuan  objects  pnp           server

[root@232-238-qd-monitor etc]#

 

[root@232-239-qd-monitor etc]# pwd

/usr/local/nagios/etc

[root@232-239-qd-monitor etc]# ls

cgi.cfg   htpasswd.bak.20140326  htpasswd.users      nagios.cfg       nrpe.cfg objects.yuan  resource.cfg

htpasswd  htpasswd.bk            htpasswd.users.bak  nagios.cfg.yuan  objects  pnp           server

[root@232-239-qd-monitoretc]#

 

[root@232-238-qd-monitoretc]# mkdir test

[root@232-238-qd-monitoretc]# echo "123" >> wangxu.txt

[root@232-238-qd-monitoretc]# ls

cgi.cfg   htpasswd.bak.20140326  htpasswd.users      nagios.cfg       nrpe.cfg objects.yuan  resource.cfg  test

htpasswd  htpasswd.bk            htpasswd.users.bak  nagios.cfg.yuan  objects  pnp           server        wangxu.txt

[root@232-238-qd-monitoretc]#

 

[root@232-239-qd-monitor etc]# ls

cgi.cfg   htpasswd.bak.20140326  htpasswd.users      nagios.cfg       nrpe.cfg objects.yuan  resource.cfg  test

htpasswd  htpasswd.bk            htpasswd.users.bak  nagios.cfg.yuan  objects  pnp           server        wangxu.txt

[root@232-239-qd-monitoretc]#

 



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI