RSYNC远程同步

ELK+filebeat+kafka

input
bootstrap_servers =>"192.168,233.21:9092,192.168.233.22:9092,192.168.233.23:9092
topic =>"nginx1"
type => "nginx kafka"
codec =>"ison"
auto_offset_reset => "latest"
#拉取最新数据,前面不管,从头开始earliest
decorate_events =>true
#传递给es数据库时,额外的添加kafka的属性数据
      }
      }
      output {
if "access" in [tags]{
elasticsearch {
hosts =>["192.168.233.10:9200","192.168.233.20:9200"]
index=>"nginx access-%{+YYYY.MM.dd}"
}
}
if "error" in [tags]{
elasticsearchf  {
hosts =>["192.168.233.10:9200","192.168.233.20:9200"]
index =>"nginx error-%{+YYYY.MM.dd}
}
}

配置nginx里filebeat

[root@nginx20 filebeat]# vim filebeat.yml
- type: log
  enabled: true
  paths:
    - /usr/local/nginx/logs/access.log
  tags: ["access"]
​
- type: log
  enabled: true
  paths:
    - /usr/local/nginx/logs/error.log
  tags: ["error"]
​
output.kafka:
  enabled: true
  # The Logstash hosts
  hosts: ["192.168.65.11:9092","192.168.65.12:9092","192.168.65.13:9092"]
  topic: "nginx2"
  
  [root@nginx2 filebeat]# ./filebeat -e -c filebeat.yml 
​

在logstash里添加配置文件kafka.conf

[root@test43 conf.d]# vim kafka.conf
input  {
      kafka {
bootstrap_servers =>"192.168,65.11:9092,192.168.12.12:9092,192.168.65.13:9092
      topics => "nginx2"
      type => "nginx_kafka"
      codec =>"json"
      auto_offset_reset => "latest"
      decorate_events => true
      }
      }
output {
      if "access" in [tags] {
      elasticsearch {
      hosts =>["192.168.65.44:9200","192.168.65.45:9200"]
      index=>"nginx_access-%{+YYYY.MM.dd}"
      }
}
      if "error" in [tags]{
      elasticsearchf  {
      hosts =>["192.168.65.44:9200","192.168.65.45:9200"]
      index =>"nginx_error-%{+YYYY.MM.dd}" 
        }
    }
}
wq!
​
[root@test43 conf.d]# logstash -f kafka.conf --path.data /opt/test5 &
​
创建主题
[root@mysql1 opt]# kafka-topics.sh --create --bootstrap-server 192.168.65.11:9092,192.168.65.12:9092,192.168.65.13:9092 --replication-factor 2 --partitions 3 --topic test8
​
查看主题
[root@mysql1 opt]# kafka-topics.sh --list --bootstrap-server 192.168.65.11:9092,192.168.65.12:9092,192.168.65.13:9092
nginx2
test8
test9
​
生产消息
[root@mysql1 opt]# kafka-console-producer.sh --broker-list 192.168.65.11:9092,192.168.65.12:9092,192.168.65.13:9092 --topic test8
>w
>b
>l
​
消费消息
[root@mysql2 ~]# kafka-console-consumer.sh --bootstrap-server 192.168.65.11:9092,192.168.65.12:9092,192.168.65.13:9092 --topic test8 --from-beginning
w
b
l
​

RSYNC远程同步

上行:客户端同步到服务端

下行:服务端同步到客户端

scp远程复制

开源的快速备份的工具,一般是系统自带的。

可以在不同主机之间同步整个目录树(目录)

在远程同步的任务中,负责发起rsync的叫做发起端,也就是服务端,负责响应同步请求的,就是客户端

rsync特点

1、支持拷贝文件,链接文件等等

2、可以同步整个目录

3、可以支持保留源文件或者目录的权限等等

4、可以实现增量同步

同步方式

1、完整备份

2、增量备份

常用的选项

1、-a 归档模式,保留权限

2、 -v 显示同步的详细过程

3、 -z 压缩,在传输的过程中对文件进行压缩

4、 -H 同步硬链接

5、 --delete 同步删除文件

6、 -l 同步快捷方式 连接文件

7、 -r 递归,所有

实验

192.168.65.43 服务端

192.168.65.44 客户端

inotify-tools

用inotify实现监控

inotify watch 监控变化

inotify wait 监控修改 创建 移动 删除 属性修改(权限修改,所有者,所在组)如果发生变动,立即输出结果

客户端获取服务端信息

客服端44服务端43

[root@test43 ~]# vim /etc/rsyncd.conf 
# /etc/rsyncd: configuration file for rsync daemon mode
​
# See rsyncd.conf man page for more options.
​
# configuration example:
​
 uid = root
 gid = root
 use chroot = yes
#是否禁锢在源目录
 address = 192.168.65.43
#监听地址
port 873
# max connections = 4
 pid file = /var/run/rsyncd.pid
 log file = /var/log.rsyncd.log
hosts allow = 192.168.65.0/24
dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z
#压缩同步文件
[test]
#共享模块的名称,通过模块的名称进行同步
path = /opt/test
#源目录,就是同步的目录
#comment = test
#备注信息
read only = no
#源目录,客户端可以读也可以写
auth users = backuper
#授权登录的账户名称
secrets file = /etc/rsyncd_users.db
#授权登录用户的密码文件
wq!                                                                                   
​
[root@test43 ~]#  vim /etc/rsyncd_users.db   #设置远程同步的密码
backuper:123456
~                 
[root@test43 ~]# chmod 600 /etc/rsyncd_users.db   #赋权只能给600的权限
[root@test43 ~]# mkdir /opt/test               #创建目录后要赋权
[root@test43 ~]# chmod 777 /opt/test
[root@test43 ~]# netstat -antp | grep 873
[root@test43 ~]# netstat -antp | grep rsync
[root@test43 ~]# systemctl restart rsyncd
[root@test43 ~]# netstat -antp | grep rsync
tcp        0      0 192.168.65.43:873       0.0.0.0:*               LISTEN      33058/rsync         
[root@test43 ~]# netstat -antp | grep 873
tcp        0      0 192.168.65.43:873       0.0.0.0:*               LISTEN      33058/rsync         
​
[root@test43 opt]# cd test
[root@test43 test]# ls
[root@test43 test]# echo 123 456 789
123 456 789
[root@test43 test]# echo 123 > 123
[root@test43 test]# echo 456 > 456
​
#在指定44客户端查看
[root@test44 opt]# rsync -avz backuper@192.168.65.43::test /opt
Password: 
receiving incremental file list
./
123
456
​
sent 65 bytes  received 175 bytes  96.00 bytes/sec
total size is 8  speedup is 0.03
​

客户端同步到服务端

44是客户端43是服务端

[root@test44 opt]# inotifywait -mrq -e modify,create,move,delete /opt/dly
m 持续监控
r递归整个目录 只要有变化包含子目录的变化全部记录
q 简化输出的信息
e 指定监控时间
[root@test44 opt]# tar -xf inotify-tools-3.14.tar.gz
[root@test44 opt]# yum -y install make
[root@test44 opt]# cd inotify-tools-3.14/
[root@test44 opt]# make -j 4 && make install
[root@test44 opt]# mkdir dly   #创建目录
[root@test44 opt]# chmod 777 dly   #赋权
[root@test44 opt]# cd dly  
[root@test44 dly]# touch 123      
[root@test44 dly]# mv 123 456
设置免密
[root@test44 dly]# rsync -avz --password-file=/etc/server.pass backuper@192.168.65.43::test /opt
#持续监控整个目录
[root@test44 opt]# inotifywait -mrq -e modify,create,move,delete, /opt/dly
/opt/dly/ CREATE 123
/opt/dly/ MOVED_FROM 123
/opt/dly/ MOVED_TO 456
​
#监控变化的配置文件
[root@test44 opt]# vim inotify.sh
#!/bin/bash
inotify_cmd="inotifywait -mrq -e modify,create,move,delete, /opt/dly"
rsync_cmd="rsync -azH --delete --password-file=/etc/server.pass /opt/dly backuper@192.168.65.43::test/"
$inotify_cmd | while read DIRECTORY EVENT FILE
do
  if [ $(pgrep rsync | wc -l) -le 0 ]
  then
    $rsync_cmd
    fi
  done  
#--delete全同步(两边的目录全部一致)
wq!
​
[root@test44 opt]# chmod 777 inotify.sh   #赋权
[root@test44 opt]# ./inotify.sh     #启动监控
#再开一个相同虚拟机
[root@test44 dly]# touch xy102
[root@test44 dly]# echo 123 > xy102
#写入的内容会同步到test43
[root@test43 test]# cd dly
[root@test43 dly]# ls
456  xy102
[root@test43 dly]# cat xy102
123
#在44主机上写入
[root@test44 dly]# touch test1
#在43上查看
[root@test43 dly]# ll
总用量 4
-rw-r--r--. 1 root root 0 8月   6 14:15 456
-rw-r--r--. 1 root root 0 8月   6 15:00 test1
-rw-r--r--. 1 root root 4 8月   6 14:58 xy102
#在客户端上的操作会同步到服务端,服务端的操作不对客户端造成影响

1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值