inotify+rsync同步、多台客户端实时同步数据脚本

目录

一、rsync远程同步

1、rsync的作用和特点

2、常见的备份工具和rsync软件包的安装

二、配置rsync同步数据

1.关闭防火墙

2.服务端下载rsync

3.修改配置文件

4.修改身份验证数据库权限

5.创建共享目录,并创建个测试文件

6.客户端开启数据同步

 7.免密设置

目录

一、rsync远程同步

1、rsync的作用和特点

2、常见的备份工具和rsync软件包的安装

二、配置rsync同步数据

1.关闭防火墙

2.服务端下载rsync

3.修改配置文件

4.修改身份验证数据库权限

5.创建共享目录,并创建个测试文件

6.客户端开启数据同步

 7.免密设置

8.ssh优化

五、多台客户端实时同步

1.把运行中的sh杀死

2.修改脚本 ,并放入后台运行

测试


五、多台客户端实时同步

1.把运行中的sh杀死

2.修改脚本 ,并放入后台运行

测试


一、rsync远程同步

1、rsync的作用和特点

  1)rsync的作用

       是一个数据备份工具

       通过rsync传输文件或者目录

  2)rsync的特点

       支持本地数据备份

       支持异地网络数据备份

       支持增量备份

       大文件或者小文件都可以使用rsync

2、常见的备份工具和rsync软件包的安装

1)常见的备份工具

cp

tar

scp

sftp

Rsync

2)rsync命令的常见选项

-a归档模式包含-rlptoD

-r递归模式,同步目录使用

-l同步符号连接

-v显示详细信息

-z传输进行压缩

-p保留权限

-t保持源文件时间标记

-g保留组标识

-o保留用户标识

-H保留硬链接文件

-A保留ACL属性

-D保留设备文件特殊文件使用

--delete:同步数据删除目标源文件数据

--checksum:校验同步数据

二、配置rsync同步数据

1.关闭防火墙
[root@cli2 ~] systemctl stop firewalld
[root@cli2 ~] setenforce 0
[root@cli2 ~] iptables -F
2.服务端下载rsync
[root@cli1 ~] yum -y install rsync
3.修改配置文件

 /etc/rsync.conf

uid = nobody 管理账户

gid = nobody 管理组

use chroot = yes 禁锢在源目录

pid file = /var/run/rsyncd.pid rsync服务的id号

port 873 rsync服务启动监听的端口

address = 192.168.100.10 rsync服务器监听的ip地址

hosts allow = 192.168.100.* 允许192.168.100.0网络同步数据

[name] 共享模块的名字

 path = /name rsync客户端同步根下的name目录数据

 comment = test 同步目录数据说明

 dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 文件后缀不需要压缩

 read only = yes 同步目录数据只有读取权限

 auth users = bob 同步验证账户bob

 secrets file = /etc/rsync_user.db 配置身份验证数据库

[root@master ~] vim /etc/rsyncd.conf  
uid = nobody
 gid = nobody
 use chroot = yes
 address=192.168.1.12  #本机IP
 port 873
 log file =/var/log/rsyncd.log
 pid file =/var/run/rsync.pid
 hosts allow = 192.168.1.0/24

 [wwwroot]
        path = /var/www/html
        comment = wwwroot
        read only = no
        dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
        auth users = haha
        secrets file = /etc/rsyncd_users.db
4.修改身份验证数据库权限
[root@master ~] vim /etc/rsyncd_users.db
haha:123456  #rsync的用户名和密码
[root@master ~] chmod 600 /etc/rsyncd_users.db
[root@master ~] rsync --daemon
[root@master ~] netstat -anpl|grep rsync
tcp        0      0 192.168.1.12:873        0.0.0.0:*               LISTEN      8933/rsync          
unix  2      [ ]         DGRAM                    41054    8933/rsync  
5.创建共享目录,并创建个测试文件
[root@master ~] mkdir -p /var/www/html
[root@master ~] touch /var/www/html/test.txt
6.客户端开启数据同步
[root@cli1 ~] rsync -avz --delete haha@192.168.1.12::wwwroot /root/wwwroot
Password: 
receiving incremental file list  
created directory /root/wwwroot #创建/root/wwwroot目录
./
test.txt  #获取了文件

sent 46 bytes  received 106 bytes  60.80 bytes/sec
total size is 0  speedup is 0.00


[root@cli1 ~] ls html/
test.txt  #成功
 7.免密设置
[root@cli1 ~] export RSYNC_PASSWORD=123456
[root@cli1 ~] rsync -avz --delete haha@192.168.1.12::wwwroot /root/html
receiving incremental file list

sent 20 bytes  received 63 bytes  166.00 bytes/sec
total size is 0  speedup is 0.00
8.ssh优化
[root@cli2 ~] vim /etc/ssh/sshd_config 

UseDNS yes

三、rsync测试

主服务器建立文件、133同步不用密码、134同步需要密码

[root@master ~] mkdir /var/www/html/12.txt
[root@cli1 html] rsync -avz --delete haha@192.168.1.12::wwwroot /root/html
receiving incremental file list
deleting 133.txt/
./

sent 27 bytes  received 70 bytes  194.00 bytes/sec
total size is 0  speedup is 0.00
[root@cli1 html]# rsync -avz --delete haha@192.168.1.12::wwwroot /root/html
receiving incremental file list
./
12.txt/

sent 31 bytes  received 110 bytes  13.43 bytes/sec
total size is 0  speedup is 0.00
[root@cli2 html] rsync -avz --delete haha@192.168.1.12::wwwroot /root/html
Password: 
receiving incremental file list
deleting 134.txt/
./
12.txt/

sent 31 bytes  received 110 bytes  14.84 bytes/sec
total size is 0  speedup is 0.00

四、配置rsync的实时同步数据

1、rsync同步数据的方式

1)rsync计划任务

同步容易延迟

无法实时同步数据

必须通过计划任务实现

灵活性差

2)inotify和rsync结合实时同步数据

需要通过脚本实现

3)inotifywait监控常见的命令选项

-e指定监控事件

-m持续监控

4)inotifywait监控事件的类型

create:创建目录或者文件触发同步

move:移动或者重命名触发同步

delete:删除数据触发同步

modify:修改数据触发同步

attrib:修改文件目录属性触发同步

2、安装inotify

[root@master ~] rz #拉取文件
[root@master ~] ll
总用量 356
-rw-r--r--. 1 root root 358772 7月  17 2020 inotify-tools-3.14.tar.gz
[root@master ~] tar xf inotify-tools-3.14.tar.gz 
[root@master ~] cd inotify-tools-3.14/



 3、开启免密

[root@master inotify-tools-3.14] ssh-keygen
+---[RSA 2048]----+
|       .o.==.    |
|        .+ o+    |
|        +. =     |
|       . oB * .  |
|        S. X B oo|
|       .  = O *.*|
|         o + X.*.|
|          = + +.o|
|         . oE.  .|
+----[SHA256]-----+
[root@master ~] ssh-copy-id -i root@192.168.1.133  #一台,多台都可以
[root@master ~] ssh-copy-id -i root@192.168.1.134

4.将本地的/var/www/html/目录下的文件同步到远程主机的/root/html目录下,同时保持文件的属性和权限。


[root@master ~] rsync -avz /var/www/html/ root@192.168.1.133:/root/html

5.inotifywait命令使用

[root@master inotify-tools-3.14]# inotifywait -m -e MOVE,DELETE,CREATE,ATTRIB /var/www/html
Setting up watches.
Watches established

-m参数表示以持续监视模式运行,即不会退出,而是持续输出监视到的事件。
-e参数指定要监视的事件类型,包括MOVE(移动)、DELETE(删除)、CREATE(创建)和ATTRIB(属性修改)。
/var/www/html是要监视的目录路径。
[root@master html] mkdir 1.txt 
[root@master inotify-tools-3.14] inotifywait -m -e MOVE,DELETE,CREATE,ATTRIB /var/www/html
Setting up watches.
Watches established.
/var/www/html/ CREATE,ISDIR 1.txt #同步数据

 5.脚本实时同步

[root@master ~] vim tongbu.sh 
#!/bin/bash
INW="inotifywait -rmq -e MOVE,DELETE,CREATE,ATTRIB /var/www/html"
RSY1="rsync -azH --delete /var/www/html/ root@192.168.1.133:/root/html"

$INW | while read DIRECTORY EVENT FILE
do
        $RSY1 $RSY2
done
[root@master ~] sh tongbu.sh &
[1] 12354

测试

[root@master html] touch 3.txt
[root@master html] touch 4.txt
[root@master html] touch 5.txt
 
[root@cli1 ~] ls html/   #自动同步
12.txt  1.txt  2.txt  3.txt  4.txt  5.txt  test.txt

五、多台客户端实时同步

1.把运行中的sh杀死
[root@master ~] jobs
[1]-  运行中               sh tongbu.sh &

[root@master ~] ps
   PID TTY          TIME CMD
  8532 pts/0    00:00:00 bash
  8594 pts/0    00:00:00 bash
 12355 pts/0    00:00:00 inotifywait
 12356 pts/0    00:00:00 sh
 12409 pts/0    00:00:00 inotifywait
 12434 pts/0    00:00:00 ps
[root@master ~] kill -9 12356
[1]-  已杀死               sh tongbu.sh
2.修改脚本 ,并放入后台运行

[root@master ~] sh tongbu.sh &
[1] 12435

#!/bin/bash
INW="inotifywait -rmq -e MOVE,DELETE,CREATE,ATTRIB /var/www/html"
RSY1="rsync -azH --delete /var/www/html/ root@192.168.1.133:/root/html"
RSY2="rsync -azH --delete /var/www/html/ root@192.168.1.134:/root/html"
$INW | while read DIRECTORY EVENT FILE
do
        $RSY1 && $RSY2
done
[root@master ~] sh tongbu.sh &
[1] 12435
测试
[root@master ~] touch /var/www/html/22222.txt
[root@cli1 ~] ls html/
12.txt  1.txt  22222.txt  2.txt  3.txt  4.txt  5.txt  test.txt
[root@cli2 ~] ls html/
12.txt  1.txt  22222.txt  2.txt  3.txt  4.txt  5.txt  test.txt

都同步了22222.txt文件

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我还能再学点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值