源码包的安装、rsync同步、inotify监测

一、源码包的安装

  1.源码包的作用:yum 使用的是rpm包,rpm包安装的不能指定安装位置

          源码包可以按需选择/定制,及时修复bug ,适用于各种平台

  2、大致过程:源码包——>make gcc将源码包变成可执行的程序---->运行安装

  3.这就要求make,gcc软件支持,yum 下安装make 和gcc

  4、下载源码包 wget  http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

  5 、解压源码包:[root@localhost ~]# tar -xf inotify-tools-3.14.tar.gz -C /opt/

  解压完可以在解压目录下查看:

[root@localhost ~]# ls /opt
inotify-tools-3.14  rh
[root@localhost ~]# ls /opt/inotify-tools-3.14
aclocal.m4  ChangeLog     config.h.in  configure     COPYING  INSTALL     libinotifytools  Makefile.am  man      NEWS    src
AUTHORS     config.guess  config.sub   configure.ac  depcomp  install-sh  ltmain.sh        Makefile.in  missing  README

  6、./configure配置{目的就是为了指定安装目录和功能模块 并且此条命令可以检测是否以及安装了gcc }

    但是一定要注意,该配置是源码在哪就在哪操作,即要cd 到刚解压到的目录中去

[root@localhost ~]# cd /opt/inotify-tools-3.14 #cd 不能忘
[root@localhost inotify-tools-3.14]# ls
aclocal.m4  ChangeLog     config.h.in  configure     COPYING  INSTALL     libinotifytools  Makefile.am  man      NEWS    src
AUTHORS     config.guess  config.sub   configure.ac  depcomp  install-sh  ltmain.sh        Makefile.in  missing  README

  使用./configure --prefix =指定的安装目录位置

  (比如这里指定/mnt/myrpm,但是此条命令结束,/mnt/myrpm并不会生成,这里只是指定)

  若gcc未装,则会报类似gcc--->no 的错误

[root@localhost inotify-tools-3.14]# ./configure --prefix=/mnt/myrpm
checking for a BSD-compatible install... /bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make sets $(MAKE)... (cached) yes
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables... 
checking for suffix of object files... o

  7.make编译,生成可执行的二进制文件

  #make  命令即可

  8、make install 安装 将编译好的文件复制到安装目录,这里才真正生成 之前指定的安装文件

  查看一下:

 

[root@localhost inotify-tools-3.14]# ls /mnt/myrpm/
bin  include  lib  share
[root@localhost inotify-tools-3.14]# ls /mnt/myrpm//bin
inotifywait  inotifywatch

二、rsync同步

  rsync [选项] 源目录 目标目录

复制:完全拷贝到目标文件下

同步:增量拷贝,只传输传输变化过的数据

选项:-n : 测试同步过程不做实际修改

   --delete:删除目标文件夹中多余的文档

   -a :归档模式

   -v:显示详细的操作信息

   -z: 传输过程中启用压缩/解压

1. 本地同步:  rsync 本地目录 1   本地目录2     (同步整个文件夹)

      rsync 本地目录1/     本地目录2      (同步目录下的文件)

如:

[root@localhost ~]# mkdir -p /haha/happy /xixi

[root@localhost ~]# rsync -av /haha /xixi
sending incremental file list
haha/
haha/happy/
haha/happy/1.txt

sent 165 bytes received 47 bytes 424.00 bytes/sec
total size is 6 speedup is 0.03
[root@localhost ~]# ls /xixi
haha

这里/haha 时把目录haha 也同步了

[root@localhost ~]# rsync -av /haha/ /xixi
sending incremental file list
./
happy/
happy/1.txt

sent 152 bytes received 46 bytes 396.00 bytes/sec
total size is 6 speedup is 0.03

[root@localhost ~]# ls /xixi
haha happy

这里用了/haha/ 则只同步了haha下的内容,一般情况下都是这样的同步

  2.远程同步:

  上行: rsync  [选项] user@host:远程目录   本地目录

  下行:rsync   [选项] 本地目录  user@host:远程目录

[root@localhost ~]# rsync -av /haha/ root@192.168.142.138:/haha
root@192.168.142.138's password:
sending incremental file list
created directory /haha
./
happy/
happy/1.txt

sent 156 bytes received 74 bytes 5.17 bytes/sec
total size is 6 speedup is 0.03
[root@localhost ~]#

可以看出来,远程同步时需要密码验证,这里可以使用公钥和私钥来做到免密码的同步

只要同步一方A的私钥和另一方握有A公钥的B 实现公钥和私钥的匹配,即可免密

这里ssh-keygen 来生成公钥私钥

[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:8boQ5FfByR1t/kwWvsg5L70YqIOxNHy4xXLEdbfykrQ root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| o.o.o 
| +o..oo |
| . o o .oo o|
| o * o.oo|
| + S . o B=.|
| X * .E oo|
| o % . .= |
| = o. .oo |
| ... ....|
+----[SHA256]---

使用ssh-copy-id root@B的IP来将A的公钥发送给B

公钥私钥的生成位置为/root/.ssh

B中的/root/.ssh/authorized-keys 为公钥存在位置

[root@localhost ~]# ssh-copy-id root@192.168.142.138

此时,再使用同步就不需要密码了

[root@localhost ~]# rsync -av /haha/ root@192.168.142.138:/haha
sending incremental file list

sent 93 bytes received 13 bytes 70.67 bytes/sec
total size is 6 speedup is 0.06

三、实时监测

同步之后,要做到只源文档内容发生变化,那么同步的文档也必须实时改变,至于怎么样知道文档内容改变了,这就需要inotify来进行监测

一般会将inotify这个包放在/usr/local下,把源码下载时的inotify的包cp 到/usr/local下,然后再经过./configure   make   make install即可

这也是在A下操作的

[root@localhost ~]# ls /usr/local/bin
inotifywait inotifywatch

观察到inotifywait就是成功了,inotifywait 是监测目录变化所用的程序

比如在/haha下再新建一个目录(另开一个终端操作),可以看到inotifywait 下发生了变化

[root@localhost local]# inotifywait -rq /haha/
/haha/ CREATE,ISDIR xixixi

################################################################################

目前只是监测成功了,如何做到一监测到目录下的变化就自动同步,这里需要借助shell脚本来实现

shell脚本所在位置:/root/rsync.sh

#!/bin/bash
 while inotifywait -rqq /haha/
  do
    rsync -a --delete /haha/ root@192.168.142.138:/haha/
  done

[root@localhost ~]# ls -ld /root/rsync.sh
-rw-r--r--. 1 root root 112 9月 18 14:54 /root/rsync.sh
[root@localhost ~]# chmod +x /root/rsync.sh
[root@localhost ~]# ls -ld /root/rsync.sh
-rwxr-xr-x. 1 root root 112 9月 18 14:54 /root/rsync.sh

要给脚本执行权限

这个时候运行这个脚本[root@localhost ~]# /root/rsync.sh

再开一个终端改变A里的/haha B中的/haha 紧接着改变

原文地址:https://www.cnblogs.com/zhanglei97/p/11542309.html