练习(rsync同步与inotify监控)

1、本地同步

 环境准备
 [root@1 ~]# yum -y install rsync
 [root@1 ~]# mkdir aa
 [root@1 ~]# mkdir bb
 [root@1 ~]# touch aa/file{0..9}
 [root@1 ~]# tree aa
 aa
 ├── file0
 ├── file1
 ├── file2
 ├── file3
 ├── file4
 ├── file5
 ├── file6
 ├── file7
 ├── file8
 └── file9
 0 directories, 10 files
 [root@1 ~]# tree bb
 bb
 0 directories, 0 files
 练习
 [root@1 ~]# rsync -av aa/ bb      //同步aa目录下的文件
 sending incremental file list
 ./
 file0
 file1
 file2
 file3
 file4
 file5
 file6
 file7
 file8
 file9
 sent 574 bytes  received 209 bytes  1,566.00 bytes/sec
 total size is 0  speedup is 0.00
 [root@1 ~]# tree bb
 bb
 ├── file0
 ├── file1
 ├── file2
 ├── file3
 ├── file4
 ├── file5
 ├── file6
 ├── file7
 ├── file8
 └── file9
 0 directories, 10 files

2、远程同步

 环境准备
 [root@2 ~]# rm -rf /tmp/*
 [root@2 ~]# ls /tmp
 练习
 [root@1 ~]# rsync -av aa root@10.0.0.20:/tmp/   //同步aa目录
 sending incremental file list
 aa/
 aa/file0
 aa/file1
 aa/file2
 aa/file3
 aa/file4
 aa/file5
 aa/file6
 aa/file7
 aa/file8
 aa/file9
 sent 585 bytes  received 210 bytes  1,590.00 bytes/sec
 total size is 0  speedup is 0.00
 [root@2 ~]# tree /tmp/
 /tmp/
 └── aa
     ├── file0
     ├── file1
     ├── file2
     ├── file3
     ├── file4
     ├── file5
     ├── file6
     ├── file7
     ├── file8
     └── file9
 1 directory, 10 files

3、远程同步一个项目

 环境准备
 [root@1 ~]# mkdir -p /app1/studentweb/src/main/java/co/goho/jiaqi.studentweb
 [root@1 ~]# touch /app1/studentweb/src/main/java/co/goho/jiaqi.studentweb/file{0..9}
 [root@1 ~]# tree /app1/
 /app1/
 └── studentweb
     └── src
         └── main
             └── java
                 └── co
                     └── goho
                         └── jiaqi.studentweb
                             ├── file0
                             ├── file1
                             ├── file2
                             ├── file3
                             ├── file4
                             ├── file5
                             ├── file6
                             ├── file7
                             ├── file8
                             └── file9
 [root@1 ~]# vim /etc/rsyncd.conf 
 [app]
 path=/app/studentweb/
 log file=/var/log/rsync.log
 auth users=tom,jerry
 secrets file=/etc/rsync.secrets
 [app1]
 path=/app1/studentweb/
 log file=/var/log/app1.log
 [root@1 ~]# systemctl restart rsyncd
 练习
 [root@2 ~]# rsync -a root@10.0.0.11::
 app             
 app1
 [root@2 ~]# rsync -av root@10.0.0.11::app1 /tmp/
 receiving incremental file list
 ./
 src/
 src/main/
 src/main/java/
 src/main/java/co/
 src/main/java/co/goho/
 src/main/java/co/goho/jiaqi.studentweb/
 src/main/java/co/goho/jiaqi.studentweb/file0
 src/main/java/co/goho/jiaqi.studentweb/file1
 src/main/java/co/goho/jiaqi.studentweb/file2
 src/main/java/co/goho/jiaqi.studentweb/file3
 src/main/java/co/goho/jiaqi.studentweb/file4
 src/main/java/co/goho/jiaqi.studentweb/file5
 src/main/java/co/goho/jiaqi.studentweb/file6
 src/main/java/co/goho/jiaqi.studentweb/file7
 src/main/java/co/goho/jiaqi.studentweb/file8
 src/main/java/co/goho/jiaqi.studentweb/file9
 sent 245 bytes  received 733 bytes  1,956.00 bytes/sec
 total size is 0  speedup is 0.00
 [root@2 ~]# tree /tmp/
 /tmp/
 ├── aa
 │   ├── file0
 │   ├── file1
 │   ├── file2
 │   ├── file3
 │   ├── file4
 │   ├── file5
 │   ├── file6
 │   ├── file7
 │   ├── file8
 │   └── file9
 └── src
     └── main
         └── java
             └── co
                 └── goho
                     └── jiaqi.studentweb
                         ├── file0
                         ├── file1
                         ├── file2
                         ├── file3
                         ├── file4
                         ├── file5
                         ├── file6
                         ├── file7
                         ├── file8
                         └── file9
 ​
 7 directories, 20 files

4、监控测试

 环境准备
 [root@1 ~]# yum -y install inotify-tools-devel.x86_64
 [root@1 ~]# inotifywa
 inotifywait   inotifywatch  
 练习
 [root@1 ~]# inotifywait -mr /app1/studentweb/
 Setting up watches.  Beware: since -r was given, this may take a while!
 Watches established.
 |
 新开一个窗口
 [root@1 ~]# touch /app1/studentweb/shiweiyan.txt
 查看原窗口

 新窗口中输入
 [root@1 ~]# vim /app1/studentweb/shiweiyan.txt
 I eat food
 查看原窗口

5、设置计划任务进行自动实时同步

 环境准备
 [root@2 ~]# rm -rf /tmp/*
 [root@1 ~]# which rsync
 /usr/bin/rsync
 练习
 [root@1 ~]# crontab -e
 */1 * * * * /usr/bin/rsync -av /app1/studentweb/ root@10.0.0.20:/tmp/
 [root@2 ~]# tree /tmp/
 /tmp/
 ├── shiweiyan.txt
 └── src
     └── main
         └── java
             └── co
                 └── goho
                     └── jiaqi.studentweb
                         ├── file0
                         ├── file1
                         ├── file2
                         ├── file3
                         ├── file4
                         ├── file5
                         ├── file6
                         ├── file7
                         ├── file8
                         └── file9
 ​
 6 directories, 11 files

6、编写shell脚本进行自动监控实时同步

环境准备
[root@1 ~]# rsync -av /app1/studentweb/ root@10.0.0.20:/tmp/
sending incremental file list
./
shiweiyan.txt
tiantian.txt
src/
src/main/
src/main/java/
src/main/java/co/
src/main/java/co/goho/
src/main/java/co/goho/jiaqi.studentweb/
src/main/java/co/goho/jiaqi.studentweb/file0
src/main/java/co/goho/jiaqi.studentweb/file1
src/main/java/co/goho/jiaqi.studentweb/file2
src/main/java/co/goho/jiaqi.studentweb/file3
src/main/java/co/goho/jiaqi.studentweb/file4
src/main/java/co/goho/jiaqi.studentweb/file5
src/main/java/co/goho/jiaqi.studentweb/file6
src/main/java/co/goho/jiaqi.studentweb/file7
src/main/java/co/goho/jiaqi.studentweb/file8
src/main/java/co/goho/jiaqi.studentweb/file9
sent 907 bytes  received 279 bytes  790.67 bytes/sec
total size is 11  speedup is 0.01
[root@2 ~]# tree /tmp/
/tmp/
├── shiweiyan.txt
├── src
│   └── main
│       └── java
│           └── co
│               └── goho
│                   └── jiaqi.studentweb
│                       ├── file0
│                       ├── file1
│                       ├── file2
│                       ├── file3
│                       ├── file4
│                       ├── file5
│                       ├── file6
│                       ├── file7
│                       ├── file8
│                       └── file9
└── tiantian.txt
6 directories, 12 files
[root@1 ~]# crontab -e    //dd删除计划任务
[root@1 ~]# which inotifywait
/usr/bin/inotifywait
 测试
[root@1 ~]# vim inotifyapp1.sh
#!/bin/bash
/usr/bin/inotifywait -mrq -e modify,delete,create,attrib,move /app1/studentweb | while read events
do
    rsync -av --delete /app1/studentweb/ root@10.0.0.20:/tmp/
    echo "`date +%F\ %T`出现事件$events" >> /var/log/app1.log 2>&1
done
[root@1 ~]# chmod +x inotifyapp1.sh 
[root@1 ~]# nohup ./inotifyapp1.sh &   
[4] 2072
[root@1 ~]# nohup: 忽略输入并把输出追加到"nohup.out"

[root@1 ~]# 
[root@1 ~]# touch /app1/studentweb/tiantian.txt
[root@2 ~]# tree /tmp
/tmp
├── haha.txt
├── shiweiyan.txt
├── src
│   └── main
│       └── java
│           └── co
│               └── goho
│                   └── jiaqi.studentweb
│                       ├── file0
│                       ├── file1
│                       ├── file2
│                       ├── file3
│                       ├── file4
│                       ├── file5
│                       ├── file6
│                       ├── file7
│                       ├── file8
│                       └── file9
└── tiantian.txt
6 directories, 13 files
[root@1 ~]# cat /var/log/app1.log 
2024-07-18 22:33:56出现事件/app1/studentweb/ CREATE haha.txt
2024-07-18 22:33:56出现事件/app1/studentweb/ ATTRIB haha.txt
[root@1 ~]# vim /app1/studentweb/haha.txt
看,灰机
[root@2 ~]# cat /tmp/haha.txt 
看,灰机
[root@1 ~]# cat /var/log/app1.log 

#!/bin/bash  
/usr/bin/inotifywait -mrq -e modify,delete,create,attrib,move /app1/studentweb | while read events  
do
    rsync -av --delete /app1/studentweb/ root@10.0.0.20:/tmp/
    echo "`date +%F\ %T`出现事件$events" >> /var/log/app1.log 2>&1
done

这段脚本是一个使用 inotifywait 工具进行文件系统事件监测,并在监测到事件时执行 rsync 同步操作以及记录日志的 Bash 脚本。

 #!/bin/bash 

这是 Bash 脚本的开头声明,告诉系统使用 /bin/bash 解释器来执行此脚本。

 /usr/bin/inotifywait -mrq -e modify,delete,create,attrib,move /app1/studentweb | while read events  

/usr/bin/inotifywait 用于监测指定目录 /app1/studentweb 中的文件系统事件。

  • -m 表示持续监测,而不是在监测到一个事件后就退出。

  • -r 表示递归监测子目录。

  • -q 表示以安静模式运行,减少不必要的输出。

  • -e 后面指定要监测的事件类型,包括修改(modify)、删除(delete)、创建(create)、属性更改(attrib)和移动(move)。

监测到的事件会通过管道 | 传递给 while read events 部分进行处理。

while read events 是一个在 Bash 脚本中常见的结构。

它的作用是从前面命令的输出中逐行读取数据,并将每一行的数据赋值给变量 events ,然后在 while 循环中对这一行的数据进行处理。

do-done 循环中,执行了以下两个操作:

  • rsync -av --delete /app1/studentweb/ root@10.0.0.20:/tmp/ :使用 rsync 工具将 /app1/studentweb 目录以递归、保持文件属性和删除目标端多余文件的方式同步到远程主机 10.0.0.20/tmp/ 目录。

  • echo "date +%F\ %T出现事件 $events" >> /var/log/app1.log 2>&1 :记录当前的日期时间和监测到的事件到 /var/log/app1.log 日志文件中。2>&1 表示将标准错误输出也重定向到标准输出,一起写入日志文件。

例如,如果在 /app1/studentweb 目录中创建了一个新文件,脚本会立即触发 rsync 同步操作,并在日志中记录事件信息。

这段脚本常用于实时同步文件,并对文件系统的更改进行监控和记录,以确保数据的一致性和可追溯性

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值