主服务器端,向从服务器端实时同步数据 master端IP地址:192.168.1.39(node1) slave端IP地址:192.168.1.40 (node2) 一、配置从服务器 在从服务器安装rsync,创建并配置rsync文件。
[root@node2 ~]# yum install -y rsync hosts allow = 192.168.1.39 hosts deny = * list = true uid = root gid = root pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log [node2] path = /data/node2 read only = no
2.在从服务器上创建需要同步的文件目录,并且启动rsync,查看进程和监听的端口。 [root@node2 ~]# mkdir -p /data/node2 [root@node2 ~]# rsync --daemon [root@node2 ~]# ps -ef | grep rsync| grep -v "grep" root 1723 1 0 22:17 ? 00:00:00 rsync --daemon [root@node2 ~]# netstat -anpt | grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1723/rsync tcp 0 0 :::873 :::* LISTEN 1723/rsync
3.手动测试rsync同步功能。 [root@node2 ~]# cd /data/node2/ [root@node2 node2]# echo "123456">a.txt [root@node2 node2]# echo "abcd">b.txt [root@node2 node2]# cat a.txt b.txt 123456 abcd [root@node1 ~]# yum install -y rsync(在master上安装rsync) [root@node1 ~]# mkdir -p /data/node1 [root@node1 ~]# rsync -avzP 192.168.1.40::node2 /data/node1/ receiving incremental file list ./ a.txt 7 100% 6.84kB/s 0:00:00 (xfer#1, to-check=1/3) b.txt 5 100% 0.12kB/s 0:00:00 (xfer#2, to-check=0/3) sent 68 bytes received 180 bytes 23.62 bytes/sec total size is 12 speedup is 0.05 [root@node1 ~]# cd /data/node1/ [root@node1 node1]# ls a.txt b.txt [root@node1 node1]# cat a.txt b.txt 123456 abcd 手动测试同步成功。
二、配置主服务器 编译安装inotify。
[root@node1 ~]# mkdir -p /taokey/tools [root@node1 ~]# cd /taokey/tools/ [root@node1 tools]# tar -zxf inotify-tools-3.14.tar.gz [root@node1 tools]# cd inotify-tools-3.14 [root@node1 inotify-tools-3.14]# ./configure [root@node1 inotify-tools-3.14]# make [root@node1 inotify-tools-3.14]# make install
2.配置inotify脚本。
#!/bin/bash host=192.168.1.40 data_dir=/data/node1/ dst=node2 /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $data_dir | while read files do #rsync -avzP $host::$dst $data_dir rsync -avzP --delete --progress $data_dir $host::$dst echo "${files} was rsynced" >> /tmp/rsync.log 2>&1 done [root@node1 inotify-tools-3.14]# chmod u x inotify_rsync.sh [root@node1 inotify-tools-3.14]# bash inotify_rsync.sh & [1] 4533 [root@node1 inotify-tools-3.14]# ps -ef | grep inoti | grep -v "grep" root 4533 1880 0 18:02 pts/1 00:00:00 bash inotify_rsync.sh root 4534 4533 0 18:02 pts/1 00:00:00 /usr/local/bin/inotifywait -mrq --timefmt %d/%m/%y %H:%M --format %T %w%f%e -e modify,delete,create,attrib /data/node1/ root 4535 4533 0 18:02 pts/1 00:00:00 bash inotify_rsync.sh
3.在主服务器上往从服务器上同步数据,测试。 [root@node1 ~]# cd /data/node1/ [root@node1 node1]# ls a.txt b.txt [root@node1 node1]# touch c.txt [root@node1 node1]# sending incremental file list ./ c.txt 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/4) sent 111 bytes received 30 bytes 13.43 bytes/sec total size is 12 speedup is 0.09 sending incremental file list sent 72 bytes received 8 bytes 7.62 bytes/sec total size is 12 speedup is 0.15 [root@node1 node1]# [root@node2 node2]# ls a.txt b.txt c.txt [root@node1 node1]# rm -rf a.txt [root@node1 node1]# sending incremental file list ./ deleting a.txt sent 61 bytes received 11 bytes 6.86 bytes/sec total size is 5 speedup is 0.07 [root@node1 node1]# [root@node2 node2]# ls b.txt c.txt 试验成功!