Rsync是一款专业的数据同步工具,轻松实现本地或者远程数据同步、增量备份,目前是中小型企业中使用率最高的数据备份工具。在企业中为实现数据的实时同步,一般会借助inotify-tools对文件或者目录状态进行监控,两者相结合提高同步工作效率。
实验环境
操作系统:RHEL6.10
Rsync:3.1.3
inotify-tools:3.20.1
安装教程
第一步 安装依赖包
[root@wanghualang ~]# yum -y install gcc automake libtool
第二步 下载、解压源码包
[root@wanghualang ~]# cd /usr/local/src/
[root@wanghualang src]# wget --no-check-certificate https://download.samba.org/pub/rsync/src/rsync-3.1.3.tar.gz
[root@wanghualang src]# wget --no-check-certificate https://github.com/rvoicilas/inotify-tools/archive/3.20.1.tar.gz
[root@wanghualang src]# tar xzf rsync-3.1.3.tar.gz
[root@wanghualang src]# tar xzf 3.20.1.tar.gz
第三步 编译安装Rsync、inotify-tools
[root@wanghualang ~]# cd /usr/local/src/rsync-3.1.3
[root@wanghualang rsync-3.1.3]# ./configure--prefix=/usr/local/rsync --disable-ipv6
[root@wanghualang rsync-3.1.3]# make
[root@wanghualang rsync-3.1.3]# make install
[root@wanghualang ~]# cd /usr/local/src/inotify-tools-3.20.1
[root@wanghualang inotify-tools-3.20.1]# sh autogen.sh
[root@wanghualang inotify-tools-3.20.1]# ./configure--prefix=/usr/local/inotify-tools
[root@wanghualang inotify-tools-3.20.1]# make
[root@wanghualang inotify-tools-3.20.1]# make install
第四步 配置环境变量
[root@wanghualang ~]# echo "exportPATH="$PATH:/usr/local/inotify-tools/bin:/usr/local/rsync/bin"">>/etc/profile
[root@wanghualang ~]# source /etc/profile
第五步测试手动同步,并保持数据一致
[root@wanghualang ~]# mkdir /{test1,test2}
[root@wanghualang ~]# touch /test1/1
[root@wanghualang ~]# rsync -av /test1/ /test2/ --delete
第六步 测试实时同步,并保持数据一致
[root@wanghualang ~]# vim auto_rsync.sh
#!/bin/bash
#源目录
SOURCE_DIRECTORY="/test1/"
#备份目录
BACKUP_DIRECTORY="/test2/"
#源目录中的文件发生创建、修改、移动、删除操作时将自动触发同步备份目录,且备份目录与源目录数据保持一致
inotifywait -mrq -e create,modify,move,delete $SOURCE_DIRECTORY | while read file
do
rsync -a $SOURCE_DIRECTORY $BACKUP_DIRECTORY --delete
done
[root@wanghualang ~]# sh auto_rsync.sh &
[root@wanghualang ~]# touch /test1/a
[root@wanghualang ~]# ls /test2/
[root@wanghualang ~]# echo "1">/test1/a
[root@wanghualang ~]# cat /test2/a
[root@wanghualang ~]# mv /test1/a /test1/b
[root@wanghualang ~]# ls /test2/
[root@wanghualang ~]# rm -f /test1/*
[root@wanghualang ~]# ls /test2/