阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

rsync+inotify实现数据单向实时同步

95次阅读
没有评论

共计 3523 个字符,预计需要花费 9 分钟才能阅读完成。

主服务器端,向从服务器端实时同步数据
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
试验成功!

————————————– 分割线 ————————————–

Rsync+inotify 实现 Git 数据实时同步备份 http://www.linuxidc.com/Linux/2014-10/108298.htm

Rsync 实现文件备份同步详解 http://www.linuxidc.com/Linux/2014-09/106967.htm

Rsync 同步两台服务器 http://www.linuxidc.com/Linux/2014-09/106574.htm

CentOS 6.5 下 Rsync 远程同步 http://www.linuxidc.com/Linux/2014-05/101084.htm

Ubuntu Linux 下用 Rsync 进行数据备份和同步配制 http://www.linuxidc.com/Linux/2014-03/97592.htm

Linux 使用 Rsync 客户端与服务端同步目录进行备份 http://www.linuxidc.com/Linux/2014-02/97068.htm

Rsync 的详细介绍:请点这里
Rsync 的下载地址:请点这里

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-20发表,共计3523字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中