inotifywait+rsyn 定制脚本:http数据同步

inotifywait 用于监控某个文件目录,当这个目录下发生变化的时候打印变化信息

   详细介绍:https://blog.csdn.net/ck784101777/article/details/98059643

rsyn 用于同步文件目录,如目录1与目录2,执行rsyn命令后目录2将被同步

   详细介绍:https://blog.csdn.net/ck784101777/article/details/98056201

 

实现此案例需要按照如下步骤进行。

步骤一:准备两台主机

svr7,pc207

双方的目录均为/var/www/html/,如果安装了httpd,此目录会自动出现。

1)确认svr7的目录内容

  1. [root@svr7 ~]# yum -y install httpd
  2. .. ..
  3. [root@svr7 ~]# ls /var/www/html/                     //向目录下提供一些测试文件
  4. libreoffice

2)确认pc207的目录内容

  1. [root@pc207 ~]# yum -y install httpd
  2. .. ..
  3. [root@pc207 ~]# ls /var/www/html                 //初始目录无数据
  4. [root@pc207 ~]#

 

步骤二:为svr7配置到pc207的SSH密钥对验证,实现免密码交互

1)检查当前用户是否已经有可用的SSH密钥对文件

  1. [root@svr7 ~]# ls ~/.ssh/id_*
  2. /root/.ssh/id_rsa /root/.ssh/id_rsa.pub

如果找不到id_rsa、id_rsa.pub密钥对文件,则需要执行下列操作创建:

  1. [root@svr7 ~]# ssh-keygen
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):     //按回车,确认存放位置
  4. Enter passphrase (empty for no passphrase):     //按回车,确认不要密码
  5. Enter same passphrase again:                     //再次按回车,确认
  6. Your identification has been saved in /root/.ssh/id_rsa.
  7. Your public key has been saved in /root/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. 00:a7:cb:2d:9d:b8:8a:df:f5:ff:5b:ed:bd:04:10:fe root@svr7
  10. The key's randomart image is:
  11. +--[ RSA 2048]----+
  12. | . . . |
  13. | + . . |
  14. | . . o |
  15. | . = o o |
  16. | = + S E |
  17. | o .. |
  18. | . . ...|
  19. | . o . . ....|
  20. |..o . ....o. .+|
  21. +-----------------+

2)将当前用户的SSH公钥部署到远程主机

  1. [root@svr7 ~]# ssh-copy-id root@192.168.4.207
  2. The authenticity of host '192.168.4.207 (192.168.4.207)' can't be established.
  3. ECDSA key fingerprint is d3:16:2c:9a:9d:91:28:c8:74:9c:af:2d:04:82:c9:66.
  4. Are you sure you want to continue connecting (yes/no)? yes         //首次连yes确认
  5. root@192.168.4.207's password:                     //验证对方的密码
  6.  
  7. Number of key(s) added: 1
  8.  
  9. Now try logging into the machine, with: "ssh 'root@192.168.4.207'"
  10. and check to make sure that only the key(s) you wanted were added.

3)验证免密码登录效果

  1. [root@svr7 ~]# ssh root@192.168.4.207
  2. Last login: Fri Jan 13 09:52:08 2017 from 192.168.4.110
  3. [root@pc207 ~]#                                     //确认已免密码连入远程主机
  4. [root@pc207 ~]# exit                                 //退出SSH登录环境
  5. 登出
  6. Connection to 192.168.4.207 closed.
  7. [root@svr7 ~]#                                     //已反对原客户机

 

步骤三:编写同步脚本并测试效果

1)编写脚本文件/root/isync.sh

  1. [root@svr7 ~]# vim /root/isync.sh
  2. #!/bin/bash
  3. FROM_DIR="/var/www/html/"     
  4. RSYNC_CMD="rsync -az --delete $FROM_DIR root@192.168.4.207:/var/www/html"
  5. while inotifywait -rqq -e modify,move,create,delete,attrib $FROM_DIR
  6. do
  7. $RSYNC_CMD
  8. done &
  9. [root@svr7 ~]# chmod +x /root/isync.sh

2)运行脚本

  1. [root@svr7 ~]# /root/isync.sh
  2. [root@svr7 ~]# pgrep -l inotify                     //确认任务在运行
  3. 56494 inotifywait

3)测试同步效果

在svr7上向/var/www/html/目录下添加一个测试网页(触发同步):

  1. [root@svr7 ~]# touch /var/www/html/a.txt
  2. [root@svr7 ~]# ls /var/www/html/
  3. a.txt libreoffice

在pc207上检查/var/www/html/目录,内容应该已经与svr7上的同名目录一致:

 
  1. [root@pc207 ~]# ls /var/www/html
  2. a.txt libreoffice

4)结束测试后,在svr7上停止监控任务

  1. [root@svr7 ~]# pkill -9 inotify
  2. [root@svr7 ~]# pgrep -l inotify                     //确认已没有监控任务
  3. [root@svr7 ~]#

 

    

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值