练习(能力提升)

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@2 ~]# rm -rf /tmp/*
[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/
├── shiweiyan.txt
├── src
│ └── main
│ └── java
│ └── co
│ └── goho
│ └── jiaqi.studentweb
│ ├── file0
│ ├── file1
│ ├── file2
│ ├── file3
│ ├── file4
│ ├── file5
│ ├── file6
│ ├── file7
│ ├── file8
│ └── file9
└── tiantian.txt
[root@1 ~]# cat /var/log/app1.log
2024/07/18 11:18:53 [1410] rsync on app1/ from UNKNOWN (10.0.0.14)
2024/07/18 11:18:53 [1410] building file list
2024/07/18 11:18:53 [1410] sent 753 bytes received 250 bytes total size 0
2024-07-18 20:30:34 出现事件
2024-07-18 20:30:37 出现事件
2024-07-18 20:41:40 出现事件 /app1/studentweb/ CREATE tiantian.txt
2024-07-18 i20:41:40 出现事件 /app1/studentweb/ CREATE tiantian.txt
2024-07-18 20:41:40 出现事件 /app1/studentweb/ CREATE tiantian.txt
2024-07-18 20:41:41 出现事件 /app1/studentweb/ ATTRIB tiantian.txt
2024-07-18 i20:41:41 出现事件 /app1/studentweb/ ATTRIB tiantian.txt
2024-07-18 20:41:41 出现事件 /app1/studentweb/ ATTRIB tiantian.txt
2024-07-18 20:44:43 出现事件 /app1/studentweb/ ATTRIB tiantian.txt
2024-07-18 i20:44:43 出现事件 /app1/studentweb/ ATTRIB tiantian.txt
2024-07-18 20:44:43 出现事件 /app1/studentweb/ ATTRIB tiantian.txt
2024-07-18 20:44:43 出现事件 /app1/studentweb/ ATTRIB tiantian.txt
  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值