inotify+rsync实现数据的实时同步备份
|
设备型号 |
功能 | IP |
| Cents 6.4 x86-64 | 备份服务器 | 192.168.95.200 |
| Cents 6.4 x86-64 | NFS服务器(需要备份数据) | 192.168.95.128 |
rsync安装使用请查看:http://www.ywlinux.com/archives/404
在NFS服务器上操作:
#统一创建存放个人源码工具的目录
mkdir -p /home/dongbo/tools
cd /home/dongbo/tools
#下载inotify3.14源码包
wget –no-check-certificate https://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
ls -l inotify-tools-3.14.tar.gz
#解压inotify源码包
tar -zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
#编译安装inotify
./configure –prefix=/usr/local/inotify-3.14
echo $?
make && make install
echo $?
cd ..
ln -sv /usr/local/inotify-3.14/ /usr/local/inotify
ls -l /usr/local/inotify
Events: 事件 access file or directory contents were read 文件或目录被(访问)读取r modify file or directory contents were written 文件或目录被写入w attrib file or directory attributes changed 文件或目录属性变更【理由:chmod更改属性】 close_write file or directory closed, after being opened in writeable mode 文件或目录被写关闭【理由:文件内容被更改】 close_nowrite file or directory closed, after being opened in read-only mode 文件或目录以只读方式打开后关闭 close file or directory closed, regardless of read/write mode 文件或目录被用编辑器(不管是读或写)关闭 open file or directory opened 文件或目录被用编辑器打开 moved_to file or directory moved to watched directory 文件或目录被移动进来【理由:mv目录内重命名】 moved_from file or directory moved from watched directory 文件或目录被移动出去 move file or directory moved to or from watched directory 文件或目录不管是移出或移进 create file or directory created within watched directory 文件或目录被创建【理由:mkdir创建目录】 delete file or directory deleted within watched directory 文件或目录被删除【理由:rm删除】 delete_self file or directory was deleted 文件或目录自删除 unmount file system containing file or directory unmounted 文件系统取消挂载
#测试监听创建文件目录 /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data #10/05/16 11:31 /data/xxx.txt #测试监听删除文件目录 /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete /data #10/05/16 11:33 /data/a.txt #测试监听创建,删除,修改文件目录 /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%f %H:%M' --format '%T %w%f' -e create,close_write,delete /data #创建inotify脚本 mkdir /server/scripts cd /server/scripts/ pwd cat >/server/scripts/inotifywait.sh << EOF #!/bin/sh inotify=/usr/local/inotify/bin/inotifywait $inotify -mrq --format '%w%f' -e create,close_write,delete /data \ |while read file do cd / && rsync -az ./data --delete rsync_backup@192.168.95.200::backup/ \ --password-file=/etc/rsync.password done EOF
#安装rsync客户端
yum install rsync -y
echo “dongbos” >>/etc/rsync.password
cat /etc/rsync.password
chmod 600 /etc/rsync.password
#加-x查看脚本执行过程
sh -x inotifywait.sh
#将inotify放入后台运行并加入rc.local开机启动
/bin/sh /server/scripts/intifywait.sh &
echo “/bin/sh /server/scripts/intifywait.sh &” >>/etc/rc.local
#文件为10-300K,传输并非在200-300个无延时,大于了会有延时
#优化:
[root@NFS-server scripts]# cd /proc/sys/fs/inotify/
[root@NFS-server inotify]# ls -l
total 0
-rw-r–r– 1 root root 0 May 10 12:26 max_queued_events
-rw-r–r– 1 root root 0 May 10 12:26 max_user_instances
-rw-r–r– 1 root root 0 May 10 12:26 max_user_watches
[root@NFS-server inotify]#
默认值:
[root@NFS-server inotify]# cat max_queued_events
16384
[root@NFS-server inotify]# cat max_user_instances
128
[root@NFS-server inotify]# cat max_user_watches
8192
[root@NFS-server inotify]#
调整:并发大可以调整大点;

浙公网安备 33010602011771号