安装 rsync 远程同步 + inotify 监控

rsync 远程同步

rsync的简介

  • 生产环境下,怎么样去保持数据的一致性,且跨地域管理,操作不能费事?
  • 所以 rsync这种快速增量备份的工具就出现了,你要问为什么,问就是“因果关系”,开个玩笑。
    • rsyn(Remote Sync):远程同步,是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用
    • 它支持本地复制,或者与其他SSH、rsync主机同步
    • 官方网站为:http://rsync.samba.org ,作为一种最常用的文件备份工具,rsync往往是Linux和UNIX系统默认安装的基本组件之一。
      在这里插入图片描述

rsync 的优缺点

  • 缺点:
    1、执行备份的时间固定,延迟明显、实时性差
    2、当同步源长时间不变化时,密集的定期任务是不必要的

  • 优点:
    1、一旦同步源出现变化,立即启动备份
    2、只要同步源无变化,则不执行备份

iNotify 监控

  • inotify 是一种文件系统的变化通知机制,如文件增加、删除等事件可以立刻让用户态得知。

1、Inotify 不需要对被监视的目标打开文件描述符,而且如果被监视目标在可移动介质上,那么在 umount 该介质上的文件系统后,被监视目标对应的 watch 将被自动删除,并且会产生一个 umount 事件。

2、Inotify 既可以监视文件,也可以监视目录。

3、Inotify 使用系统调用而非 SIGIO 来通知文件系统事件。

4、Inotify 使用文件描述符作为接口,因而可以使用通常的文件 I/O 操作select 和 poll 来监视文件系统的变化

实例(rsync + inotify)

配置 rsync 远程同步

源端设置

1、查看服务器端是否装有rsync
rpm -q rsync

2、编辑配置文件
vi  /etc/rsyncd.conf                            

uid = nobody                                   ####去掉#号
gid = nobody                                   ####去掉#号
use chroot = yes                                   ####去掉#号
address = 20.0.0.49                              ####去掉#号,本地IP地址
port 873                                   ####去掉#号,rsync端口号
# max connections = 4
log file = /var/log/rsyncd.log                                   ####去掉#号
pid file = /var/run/rsyncd.pid                                   ####去掉#号
hosts allow = 20.0.0.0/24                                   ####去掉#号,允许网段地址连接
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

[wwwroot]                                                  ####修改,添加以下内容
path = /var/www/html
comment = www.good.cn
read only = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
auth users = cf
secrets file = /etc/rsyncd_users.db

3、安装http服务
yum -y install httpd

4、启动服务、过滤端口
rsync --daemon

netstat -anpt | grep rsync


5、在/var/www/html 下创建一个文件
vi aaa.txt
随便编辑一些内容

在发起端

6、去客户端执行同步(两种方式)
rpm -q rsync            ####查看客户端是否安装 rsync ;没有要安装
rsync -avz cf@20.0.0.49::wwwroot /var/www/html            ###输入密码
rsync -avz rsync://用户名@服务器端地址/共享模块名 目标目录          ###输入密码

7、查看/var/www/html 目录是否有同步过来的文件(在客户端也就是发起端)

免交互同步

8、在服务端(源端)修改 /var/www/html 目录下的aaa.txt文件,或者重修创建一个新文件(自定义)
mv aaa.txt ccc.txt

9、在客服端创建一个密码文件(自定义)
vi /etc/rsync.pass
添加密码:abc123


10、启用免交互同步(注意、密码文件、用户名、IP地址、目标文件 要对应起来)
rsync -avz --delete --password-file=/etc/rsync.pass cf@20.0.0.49::wwwroot /var/www/html

11、验证
去 /var/www/html 查看下查看,原先的aaa.txt 文件变为了 ccc.txt文件
  • 总结:一个最为主要的 /etc/rsyncd.conf 配置文件,设置好,都是围绕这个配置文件来设置的,注意一些细节的地方

配置 inotify 监控(工具)

在发起端

  • 在哪一端做同步就得在哪一端做监控
1、修改内核参数
[root@localhost opt]# vi /etc/sysctl.conf                 ####添加以下内容

fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576


2、刷新
sysctl -p

3、将 inotify 监控工具包 inotify-tools-3.14.tar.gz 上传到/opt
[root@localhost opt]#               ####上传软件包
rz waiting to receive.
[root@localhost opt]# ll
total 352
-rw-r--r-- 1 root root 358772 Oct 23 14:51 inotify-tools-3.14.tar.gz
[root@localhost abc]# tar zxvf inotify-tools-3.14.tar.gz 
[root@localhost opt]# cd inotify-tools-3.14/


4、安装环境
[root@localhost inotify-tools-3.14]# yum -y install gcc gcc-c++ make
[root@localhost inotify-tools-3.14]# ./configure 
[root@localhost inotify-tools-3.14]# make && make install


5、开启监控
[root@localhost inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html 

再次开启一个子终端

在子终端中切换到/var/www/html  ,创建或者删除文件,原发起端的监控就会记录下来


6、原发起端关闭监控,创建一个监控脚本
[root@localhost inotify-tools-3.14]# cd /opt
[root@localhost opt]# vi inotify.sh              ####注意密码文件、用户、IP地址 ; 这里是监控方,发起端同步到源端的地址也就是同步目标源端的地址20.0.0.49

#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/rsync.pass /var/www/html/ cf@20.0.0.49::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
        if [ $(pgrep rsync | wc -l) -le 0 ];then
           $RSYNC_CMD
        fi
done

[root@localhost opt]# chmod +x inotify.sh               ####给脚本文件授权


7、脚本执行前的确认工作
[root@localhost opt]# ls -l /var/www/
total 0
drwxr-xr-x 2 root root  6 Nov  5  2018 cgi-bin
drwxr-xr-x 2 root root 24 Oct 23 16:24 html                 ####给与html 最好权限

[root@localhost opt]# chmod 777 /var/www/html/                 ####给予最高权限

在源端

8、去源端中也要修改/html 的权限,并且修改/etc/rsyncd.conf 文件中 read only = yes 选项,这项是只读,它将不会让你进行写操作
1[root@localhost html]# ls -l /var/www
total 0
drwxr-xr-x 2 root root  6 Nov  5  2018 cgi-bin
drwxr-xr-x 2 root root 24 Oct 23 16:18 html

[root@localhost html]# chmod 777 /var/www/html/
[root@localhost html]# ls -l /var/www
total 0
drwxr-xr-x 2 root root  6 Nov  5  2018 cgi-bin
drwxrwxrwx 2 root root 24 Oct 23 16:18 html


2[root@localhost html]# vi /etc/rsyncd.conf 
read only = no               ###修改成 no



9、重启 rsync
[root@localhost html]# netstat -anpt | grep rsync
tcp        0      0 20.0.0.49:873           0.0.0.0:*               LISTEN      21146/rsync         
[root@localhost html]# kill -9 21146
[root@localhost html]# netstat -anpt | grep rsync
[root@localhost html]# rsync --daemon
failed to create pid file /var/run/rsyncd.pid: File exists                       ####重启后显示,还有一个/var/run/rsyncd.pid 文件正在运行,无法重启,所以直接删了它

[root@localhost html]# cd /var/run
[root@localhost run]# rm -rf rsyncd.pid
[root@localhost run]# rsync --daemon           ###开启守护进程

在发起端

10、去发起端开启监控
[root@localhost opt]# ./inotify.sh


11、再进入发起端之前开启的第二台终端上,进行同步验证
[root@localhost html]# echo "wo shi chuan qi" > jinmao.txt                         ###在/var/www/html/写入文件


在监控上可以看到写入的信息:
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync: chgrp "/.jinmao.txt.U7UOVY" (in wwwroot) failed: Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync: chgrp "/jinmao.txt" (in wwwroot) failed: Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]


前往源端查看/var/www/html/ 中是否存在 good.txt:
[root@localhost html]# ll
total 12
-rw------- 1 nobody nobody 28 Oct 23 17:25 good.txt
-rw-r--r-- 1 root   root   23 Oct 23 11:42 index.html
-rw------- 1 nobody nobody 28 Oct 23 17:25 pig.txt
[root@localhost html]# cat pig.txt 
jin ye xing guang shan shan
[root@localhost html]# ll
total 8
-rw-r--r-- 1 root   root   23 Oct 23 11:42 index.html
-rw------- 1 nobody nobody 16 Oct 23 17:29 jinmao.txt
[root@localhost html]# cat jinmao.txt 
wo shi chuan qi             ####显示“我是传奇”

- 总结:
1、内核参数文件修改
2、安装编译语言环境
3、监控脚本的编写
4、工作目录给与权限
5、重启进程前先要停止进程


到此结束,感谢浏览

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值