主服务器端,向从服务器端实时同步数据
master端IP地址:192.168.1.39(node1)
slave端IP地址:192.168.1.40 (node2)
一、配置从服务器
- 在从服务器安装rsync,创建并配置rsync文件。
[[email protected] ~]# 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,查看进程和监听的端口。
[[email protected] ~]# mkdir -p /data/node2 [[email protected] ~]# rsync --daemon [[email protected] ~]# ps -ef | grep rsync| grep -v "grep" root 1723 1 0 22:17 ? 00:00:00 rsync --daemon [[email protected] ~]# 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同步功能。
[[email protected] ~]# cd /data/node2/ [[email protected] node2]# echo "123456">a.txt [[email protected] node2]# echo "abcd">b.txt [[email protected] node2]# cat a.txt b.txt 123456 abcd [[email protected] ~]# yum install -y rsync(在master上安装rsync) [[email protected] ~]# mkdir -p /data/node1 [[email protected] ~]# 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 [[email protected] ~]# cd /data/node1/ [[email protected] node1]# ls a.txt b.txt [[email protected] node1]# cat a.txt b.txt 123456 abcd 手动测试同步成功。
二、配置主服务器
- 编译安装inotify。
[[email protected] ~]# mkdir -p /taokey/tools [[email protected] ~]# cd /taokey/tools/ [[email protected] tools]# tar -zxf inotify-tools-3.14.tar.gz [[email protected] tools]# cd inotify-tools-3.14 [[email protected] inotify-tools-3.14]# ./configure [[email protected] inotify-tools-3.14]# make [[email protected] 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 [[email protected] inotify-tools-3.14]# chmod u+x inotify_rsync.sh [[email protected] inotify-tools-3.14]# bash inotify_rsync.sh & [1] 4533 [[email protected] 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.在主服务器上往从服务器上同步数据,测试。
[[email protected] ~]# cd /data/node1/ [[email protected] node1]# ls a.txt b.txt [[email protected] node1]# touch c.txt [[email protected] 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 [[email protected] node1]# [[email protected] node2]# ls a.txt b.txt c.txt [r[email protected] node1]# rm -rf a.txt [[email protected] 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 [[email protected] node1]# [[email protected] node2]# ls b.txt c.txt 试验成功!
时间: 2024-12-05 14:40:18