8.8. rsync+inotify完成时时同步

Date:

8.8.1. 服务器环境

rsync服务端

rsync客户端

8.8.2. rsync+inotify安装基础

  1. 首先完成rsync的服务端和客户端配置,然后可以从客户端向rsync服务端拉取和推送数据;
  2. inotify安装在rsync客户端,需要检查rsync客户端环境是否满足inotify安装条件;
    • 检查rsync客户端内核是否高于2.6.13

    • 检查是否存在目录 /proc/sys/fs/inotify/

检查过程:

1[root@web_01 ~]# rsync -avz rsync://rsync_backup@192.168.161.137/web /tmp --password-file=/etc/rsync.password
2receiving incremental file list
3
4sent 61 bytes  received 308 bytes  738.00 bytes/sec
5total size is 29924  speedup is 81.09
6[root@web_01 ~]# ls /proc/sys/fs/inotify/
7max_queued_events  max_user_instances  max_user_watches
8[root@web_01 ~]# uname -r
92.6.32-504.el6.x86_64

8.8.3. inotify安装配置

8.8.3.1. inotify安装

下载源码包:

1[root@web_01 ~]# mkdir /home/tools
2[root@web_01 ~]# cd /home/tools/
3[root@web_01 ~]# wget https://sourceforge.net/projects/inotify-tools/files/inotify-tools/3.13/inotify-tools-3.13.tar.gz --no-check-certificate

编辑安装:

 1[root@web_01 tools]# ls
 2inotify-tools-3.13.tar.gz
 3[root@web_01 tools]# tar zxf inotify-tools-3.13.tar.gz
 4[root@web_01 tools]# cd inotify-tools-3.13
 5[root@web_01 inotify-tools-3.13]# ./configure --prefix=/usr/local/inotify-tools-3.13
 6[root@web_01 inotify-tools-3.13]# make && make install
 7
 8[root@web_01 inotify-tools-3.13]# ln -s /usr/local/inotify-tools-3.13 /usr/local/inotify
 9[root@web_01 inotify-tools-3.13]# ll /usr/local/inotify
10lrwxrwxrwx. 1 root root 29 Sep 10 04:24 /usr/local/inotify -> /usr/local/inotify-tools-3.13

参数调整:

调整之前:

1[root@web_01 ~]# cat /proc/sys/fs/inotify/max_queued_events
216384
3[root@web_01 ~]# cat /proc/sys/fs/inotify/max_user_watches
48192
5[root@web_01 ~]# cat /proc/sys/fs/inotify/max_user_instances
6128

inotify压力测试

rsync客户端用inotify监听本地,然后和rsync服务端实时同步脚本如下:

 1#!/bin/bash
 2#20181029
 3#
 4###################################
 5hostip=192.168.161.137
 6src=/tmp/
 7dst=data
 8user=rsync_backup
 9rsync_passfile=/etc/rsync.password
10inotify_home=/usr/local/inotify
11
12#judge
13if [ ! -e "$src" ] \
14|| [ ! -e "${rsync_passfile}" ] \
15|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
16|| [ ! -e "/usr/bin/rsync" ];
17then
18    echo "Check File and Folder!"
19    exit 9
20fi
21
22${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' \
23--format '%T %w%f' -e close_write,delete,create,attrib $src \
24while read file
25    do
26        #rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} \
27        #$src$user@$hostip::$dst >/dev/null 2>&1
28        cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$hostip::$dst \
29        --password-file=${rsync_passfile} >/dev/null 2>&1
30    done
31exit 0

脚本2:

 1#!/bin/bash
 2#20181029
 3#
 4###################################
 5hostip=192.168.161.137
 6src=/tmp/
 7dst=data
 8user=rsync_backup
 9rsync_passfile=/etc/rsync.password
10inotify_home=/usr/local/inotify
11
12#judge
13if [ ! -e "$src" ] \
14|| [ ! -e "${rsync_passfile}" ] \
15|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
16|| [ ! -e "/usr/bin/rsync" ];
17then
18    echo "Check File and Folder!"
19    exit 9
20fi
21
22${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' \
23--format '%T %w%f' -e close_write,delete,create,attrib $src \
24while read line
25    do
26        [ ! -e "$line" ] && continue
27        rsync -aruz -R --delete $line --timeout=100 $user@$hostip::$dst \
28        --password-file=${rsync_passfile} >/dev/null 2>&1
29    done
30exit 0