-
功能要求
通过rsync+inotify将数据库服务器上的数据指定目录实时同步到备份服务器。
-
环境说明
M:192.168.10.11 数据库服务器
S:192.168.10.13 备份服务器
-
备份服务器操作
# yum -y install rsync
# useradd rsync -s /sbin/nologin -M
# mkdir -pv /kazihuo/bak
# chown rsync.rsync /kazihuo/bak
# cat /etc/rsyncd.conf
pid file = /var/run/rsyncd.pid [Rsc-bak] uid = rsync gid = rsync path = /kazihuo/bak/ max connections = 200 log file = /var/log/rsyncd.log auth users = rsync-k secrets file = /etc/rsync.password
# cat /etc/rsync.password
rsync-k:000000
# chomd 600 /etc/rsync.password
# rsync --daemon
# ss -autnpl |grep rsync
tcp LISTEN 0 5 *:873 *:* users:(("rsync",pid=2383,fd=4)) tcp LISTEN 0 5 :::873 :::* users:(("rsync",pid=2383,fd=5))
-
数据库服务器操作
# ls /proc/sys/fs/inotify/
max_queued_events max_user_instances max_user_watches
#显示以上三个文件则表明支持inotify
# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
# tar axvf inotify-tools-3.14.tar.gz
# cd inotify-tools-3.14
# ./configure --prefix=/usr/local/inotify
# make && make install
# cd /usr/local/inotify/bin/
# ./inotifywait –help
-r –recursive #递归查询目录 -q –quiet -m –monitor #始终保持事件监听状态 --excludei #排除文件或目录时,不区分大小写 --timefmt #指定时间输出的格式 --format #打印使用指定的输出类似格式字符串 -e –event #指定需要监控的事件,如下: EVENT: access #文件或目录被访问 modify #文件或目录被修改 attrib #文件或目录属性被改变 close #文件或目录封闭 open #文件或目录被打开 moved_to #文件或目录被移动至另外一个目录 move #文件或目录移动 create #文件或目录被创建在当前目录 delete #文件或目录被删除 umount #文件系统被卸载
# mkdir -pv /root/ka #创建本地监控目录
# cat /etc/rsync.password
000000
# chmod 600 /etc/rsync.password # cat /scripts/inotify.sh
1 #!/bin/bash 2 # Defined parameter 3 host01=192.168.10.13 #inotify-slave的ip地址 4 src=/root/ka #本地监控的目录 5 dst=Rsc-bak #inotify-slave的rsync服务的模块名 6 user=rsync-k #inotify-slave的rsync服务的虚拟用户 7 rsync_passfile=/etc/rsync.password #本地调用rsync服务的密码文件 8 inotify_home=/usr/local/inotify #inotify的安装目录 9 #Judge 10 if [ ! -e "$src" ] || [ ! -e "${rsync_passfile}" ] || [ ! -e "${inotify_home}/bin/inotifywait" ] || [ ! -e "/usr/bin/rsync" ];then 11 echo "Check File and Folder" 12 fi 13 ${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib $src | while read files 14 do 15 cd $src && rsync -arz --delete ./ --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1 16 done
# nohup /scripts/inotify.sh &
-
功能测试操作
[root@M ~]# mkdir /root/ka/ccc
[root@S ~]# tail -f /var/log/rsyncd.log
2017/12/19 20:15:22 [12233] rsync to Rsc-bak/ from rsync-k@UNKNOWN (192.168.10.11) 2017/12/19 20:15:22 [12233] receiving file list 2017/12/19 20:15:22 [12233] sent 84 bytes received 207 bytes total size 973
[root@S ~]# ls /kazihuo/bak/
ccc
注:只有当源目录下文件或目录发生变化(增、删、改)时,相应的备份服务器才会执行实时同步动作!