CentOS 6.x Inotify+Rsync
![](https://images2015.cnblogs.com/blog/721057/201612/721057-20161213111200729-2091128346.png)
yum -y install lrzsz
[root@rsync ~]#
mount -t nfs 10.6.100.75:/volume1/pacebackup /home/nfs/
[root@rsync ~]#
[root@rsync ~]#
[root@rsync ~]# df -h
文件系统 容量 已用 可用 已用%% 挂载点
/dev/xvda3 195G 5.8G 180G 4% /
tmpfs 497M 228K 496M 1% /dev/shm
/dev/xvda1 194M 29M 156M 16% /boot
10.6.100.75:/volume1/pacebackup
5.5T 4.8T 638G 89% /home/nfs
[root@rsync ~]# vim /etc/sysctl.conf
fs.inotify.max_user_watches=99999999
CentOS使用inotify+rsync实时同步
inotify是Linux下的一个文件系统事件监控机制(简单说就是用于监控某个文件夹的改动),作为dnotify的有效替代。inotify是一种强大的、细粒度的、异步的机制,它满足各种各样的文件监控需要。在单独使用rsync同步时,每次同步它会把全部的文件读取一遍,而inotify+rsync同步是触发式同步。假设被镜像端IP为10.6.0.106,镜像端IP为10.6.0.220。
一、被镜像(同步)端
linux内核2.6.13之后就支持inotify了,确认方法:
ls /proc/sys/fs/inotify
如果有以下三项就支持:
max_queued_events max_user_instances max_user_watches
安装rsync:
yum install rsync -y
安装inotify:
官网:https://github.com/rvoicilas/inotify-tools
cd /tmp
wget --no-check-certificate http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar -zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install
默认安装到/usr/local/bin目录。
建立rsync密码文件:
vim /root/rsync.pass
输入密码:
123456
设置权限为600:
chmod 600 /root/rsync.pass
创建同步脚本:
src=/home/data
vim /root/rsync.sh
输入:
#!/bin/bash
src=/home/nfs/fir_fs_06/000/000/000
src=/home/nfs/fir_fs_06/000/000/016
src=/home/nfs/fir_fs_06/000/000/017
des=backup@10.6.0.220::rsync
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' \
-e modify,delete,create,attrib ${src} \
| while read x
do
/usr/bin/rsync -avz --delete --progress $src $des --password-file=/root/rsync.pass &&
echo "$x was rsynced" >> /var/log/rsync.log
done
注释:
inotifywait
-m:保持监听事件。
-r:递归查看目录。
-q:打印出事件。
-e modify,delete,create,attrib:监听写入,删除,创建,属性改变事件。
rsync
-a:存档模式,相当于使用-rlptgoD。
-v:详细模式输出。
-z:传输过程中压缩文件。
为脚本加执行权限:
chmod +x /root/rsync.sh
在rc.local加入自启动:
echo "/root/rsync.sh" >> /etc/rc.local
ps aux | grep rsync | grep -v grep
kill -9
[root@rsync ~]# kill -9 7694
[root@rsync ~]# ps aux | grep rsync | grep -v grep
/root/rsync.sh &
[root@rsync ~]# cd /home/nfs/fir_fs_06/000/000/016
[root@rsync 016]# touch a
[root@rsync 016]#
二、镜像(同步)端
安装rsync:
yum install rsync -y
编辑配置文件:
vim /etc/rsyncd.conf
输入:
uid = rsync
gid = rsync
use chroot = no
max connections = 10
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
hosts allow = *
[rsync]
path = /root/pacsrsync/
read only = no
list = no
auth users = backup
secrets file = /root/rsync.pass
建立密码文件:
vim /root/rsync.pass
输入:
backup:123456
格式为“用户名:密码”。
设置权限为600:
chmod 600 /root/rsync.pass
建立保存同步文档的目录:
mkdir /home/tzuchi/
chown nobody:nobody /home/tzuchi/
chown -R rsync:rsync /root/pacsrsync/
后台启动rsync:
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
在被镜像端上运行同步脚本:
/root/rsync.sh &
日志
> /var/log/rsyncd.log
tail -f /var/log/rsyncd.log
[root@n10 ~]# cd /root/pacsrsync/
[root@n10 pacsrsync]# mkdir 016
[root@n10 pacsrsync]# chown rsync.rsync 016
[root@n10 pacsrsync]# ll
总用量 44
drwx------ 634 rsync rsync 12288 6月 14 2016 000
drwxr-xr-x 2 rsync rsync 4096 6月 13 14:55 001
drwx------ 913 rsync rsync 20480 6月 14 2016 016
drwx------ 16 rsync rsync 4096 6月 13 14:27 262
drwxrwxrwx 8 rsync rsync 4096 6月 13 13:26 data
[root@n10 pacsrsync]#
[root@n10 pacsrsync]# mkdir 017
[root@n10 pacsrsync]# chown rsync.rsync 017
[root@n10 pacsrsync]# ll
总用量 48
drwx------ 634 rsync rsync 12288 6月 14 2016 000
drwxr-xr-x 2 rsync rsync 4096 6月 13 14:55 001
drwx------ 913 rsync rsync 20480 6月 14 2016 016
drwxr-xr-x 2 rsync rsync 4096 6月 14 07:24 017
drwx------ 16 rsync rsync 4096 6月 13 14:27 262
drwxrwxrwx 8 rsync rsync 4096 6月 13 13:26 data
[root@n10 pacsrsync]#
ps aux | grep rsync | grep -v grep
kill -9
ps aux | grep rsync | grep -v grep
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
vim /root/rsync.sh
#!/bin/bash
inotify_rsync_fun ()
{
dir=`echo $1 | awk -F"," '{print $1}'`
ip=`echo $1 | awk -F"," '{print $2}'`
des=`echo $1 | awk -F"," '{print $3}'`
user=`echo $1 | awk -F"," '{print $4}'`
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib ${dir} |while read DATE TIME DIR FILE
do
FILECHAGE=${DIR}${FILE}
/usr/bin/rsync -avz --delete --progress --password-file=/root/rsync.pass ${dir} ${user}@${ip}::${des} && echo "At ${TIME} on ${DATE}, file $FILECHAGE was backed up via rsync" >> /var/log/rsync.log
done
}
count=3
# localdir,host,rsync_module,user of rsync_module,
sync1="/home/nfs/fir_fs_06/000/000/000,10.6.0.220,dir1,backup"
sync2="/home/nfs/fir_fs_06/000/000/016,10.6.0.220,dir2,backup"
sync3="/home/nfs/fir_fs_06/000/000/017,10.6.0.220,dir3,backup"
#############################################################
#main
i=0
while [ ${i} -lt ${count} ]
do
i=`expr ${i} + 1`
tmp="sync"$i
eval "sync=\$$tmp"
inotify_rsync_fun "$sync" &
done
vim /etc/rsyncd.conf
uid = rsync
gid = rsync
use chroot = no
max connections = 0
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
hosts allow = *
[dir1]
path = /root/pacsrsync/
comment = rsync from everyone
read only = no
list = no
auth users = backup
secrets file = /root/rsync.pass
[dir2]
path = /root/pacsrsync/
comment = rsync from everyone
read only = no
list = no
auth users = backup
secrets file = /root/rsync.pass
[dir3]
path = /root/pacsrsync/017/
comment = rsync from everyone
read only = no
list = no
auth users = backup
secrets file = /root/rsync.pass