实验环境:还是上一篇文章中的实验环境
PS:上一篇文章我写了如何使用rsync同步文件到备份服务器,但是有一个缺点就是计算机不能够检测到电脑上面哪些数据发生了变化,因此需要使用inotify来监测哪些文件发生了变化。让后将变化了的文件通过rsync时时同步到备份服务器。
一、创建下载软件的文件夹
[root@linux-node2 ~]# [ -d /home/silence/tools ] ||mkdir -p /home/silence/tools && cd /home/silence/tools
[root@linux-node2 tools]# ll /home/silence/tools/
total 0
[root@linux-node2 tools]# pwd
/home/silence/tools
二、下载编译安装inotify
[root@linux-node2 tools]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@linux-node2 tools]# ls -lh inotify-tools-3.14.tar.gz
-rw-r--r-- 1 root root 351K Mar 14 2010 inotify-tools-3.14.tar.gz
[root@linux-node2 tools]# tar xf inotify-tools-3.14.tar.gz
[root@linux-node2 tools]# cd inotify-tools-3.14
[root@linux-node2 inotify-tools-3.14]# mkdir /application
[root@linux-node2 inotify-tools-3.14]# ./configure --prefix=/application/inotify-tools-3.14 && make && make install
[root@linux-node2 inotify-tools-3.14]# ln -s /application/inotify-tools-3.14/ /application/inotify
三、测试inotify
开启两个终端,终端一用于使用inotify,终端二用于创建和删除文件
终端一上
[root@linux-node2 tools]# /application/inotify/bin/inotifywait -mrq --timefmt '%y%m%d%H:%M' --format '%T %w%f' -e create,close_write,delete /tmp/
终端二上
[silence@linux-node2 ~]$ for name in `seq 5`;do touch /tmp/test${name}.txt;sleep 1;done;
终端一上面的检测
[root@linux-node2 tools]# /application/inotify/bin/inotifywait -mrq --timefmt '%y%m%d%H:%M' --format '%T %w%f' -e create,close_write,delete /tmp/
17010620:24 /tmp/test1.txt
17010620:24 /tmp/test1.txt
17010620:24 /tmp/test2.txt
17010620:24 /tmp/test2.txt
17010620:24 /tmp/test3.txt
17010620:24 /tmp/test3.txt
17010620:24 /tmp/test4.txt
17010620:24 /tmp/test4.txt
17010620:24 /tmp/test5.txt
17010620:24 /tmp/test5.txt
四、利用脚本配合rsync同步变化了的文件
脚本内容如下
[root@linux-node2 scripts]# cat rsync-backup.sh
#!/bin/sh
# rsync-backup.sh
inotify=/application/inotify/bin/inotifywait
$inotify -mrq --format '%w%f' -e create,close_write,delete /data/ \
|while read file
do
rsync -az /data/ --delete rsync_backup@linux-node1.com::backup \
--password-file=/etc/rsync.password
done
测试脚本
终端二
[silence@linux-node2 ~]$ sudo touch /data/{a..d}.txt
终端一
[root@linux-node2 scripts]# sh -x /server/scripts/rsync-backup.sh
+ inotify=/application/inotify/bin/inotifywait
+ /application/inotify/bin/inotifywait -mrq --format %w%f -e create,close_write,delete /data/
+ read file
+ rsync -az /data/ --delete rsync_backup@linux-node1.com::backup --password-file=/etc/rsync.password
……………………………………中间内容省略………………………………………………
+ rsync -az /data/ --delete rsync_backup@linux-node1.com::backup --password-file=/etc/rsync.password
+ read file
查看是否同步成功
[root@linux-node1 ~]# ll /backup/
total 0
-rw-r--r-- 1 rsync rsync 0 Jan 6 20:35 a.txt
-rw-r--r-- 1 rsync rsync 0 Jan 6 20:35 b.txt
-rw-r--r-- 1 rsync rsync 0 Jan 6 20:35 c.txt
-rw-r--r-- 1 rsync rsync 0 Jan 6 20:35 d.txt
在linux-node2.com主机
[silence@linux-node2 ~]$ sudo rm -f /data/*
在backup上面查看
[root@linux-node1 ~]# ll /backup/
total 0
挂载在后台运行
[root@linux-node2 scripts]# /bin/bash /server/scripts/rsync-backup.sh &
[1] 27048