rsync+inotify

[root@1 ~]# firewalld-cmd --zone=public --add-port=9999/tcp
[root@1 ~]# chmod -R g+w /code //-R 递归赋予权限
[root@1 ~]# ssh-copy-id -p9999 用户 @IP 地址
[root@1 ~]# scp -P9999 文件 用户 @IP 地址:详细地址
   
firewall-cmd --zone=public --add-port=9999/tcp 这条命令的作用是在防火墙的 public
域中添加一个允许 TCP 协议、端口号为 9999 的访问规则。
这意味着网络流量可以通过端口 9999 并使用 TCP 协议与您的系统进行通信。
比如说,如果您在系统上运行了一个特定的服务,该服务使用 TCP 协议并监听在端口 9999 上,那 么执行此命令后,外部网络可以访问到这个服务。
再比如,您搭建了一个基于 TCP 协议、端口 9999 Web 应用,在执行此命令之前,外部请求可能 会被防火墙阻挡,执行之后,外部用户就能够正常访问该应用。但需要注意的是,添加此规则后,也会 增加一定的安全风险,因为它打开了一个新的网络访问通道。所以在添加端口开放规则时,应确保您确 切知道自己的需求和潜在的安全影响。
chmod -R g+w /code 这条命令的作用是递归地为 /code 目录及其内部的所有文件和子目录赋予
所属组的写权限。
这意味着该目录所属组的用户可以对其中的内容进行修改和写入操作。
例如,如果您有一个开发团队,团队成员都属于同一个组,通过执行此命令,组内的成员就能够对
/code 目录下的文件和子目录进行修改和保存新的内容。 再比如,如果 /code 目录下包含了多个项目文件夹,执行该命令后,组内成员可以在这些项目中添加、修改或删除文件,方便团队成员之间的协作和数据共享。 但需要注意的是,过度赋予权限可能会导致安全风险,如果不小心,可能会造成重要文件被误修改 或删除。
1 RSYNC 介绍
rsync 是一个强大的文件同步工具,常用于在不同的系统之间高效地复制和同步文件及目录。
它具有以下一些显著的特点和优势:
1 、增量备份: rsync 只会传输源目录和目标目录之间有差异的部分,这大大减少了数据传输量,
提高了同步效率。
例如,如果您有一个大型的文件库,并且只对其中的几个文件进行了修改, rsync 只会传输这
些修改过的文件,而不是整个文件库。
2 、数据完整性:它能够确保数据在传输过程中的完整性,通过校验和来验证文件是否正确传输。
3 、多种传输方式:支持通过本地连接(如本地磁盘)、 SSH 等方式进行文件同步。
4 、保留权限和属性:可以保留文件和目录的权限、所有者、组、时间戳等属性。
5 、排除特定文件或目录:通过配置,可以指定排除某些不需要同步的文件或目录。
在实际应用中, rsync 常用于服务器之间的数据备份、文件同步、版本控制等场景。以下是一个基
本的 rsync 命令示例:
rsync -avz source_directory destination_directory
其中:
-a 表示归档模式,保留权限、属性等。
-v 表示详细模式,显示更多信息。
-z 表示在传输过程中进行压缩,以减少网络带宽使用。
1 rsync 的好姐妹
sync 同步:刷新⽂件系统缓存,强制将修改过的数据块写⼊磁盘,并且更新超级块。
async 异步:将数据先放到缓冲区,再周期性(⼀般是 30s )的去同步到磁盘。
rsync 远程同步: == remote synchronous ==
数据同步过程
sync 数据同步 => 保存⽂件(⽬标) => 强制把缓存中的数据写⼊磁盘(⽴即保存),实时性 要求⽐较⾼的场景
asyn 数据异步 => 保存⽂件(⽬标) => 将数据先放到缓冲区,再周期性(⼀般是 30s )的去同 步到 磁盘,适合⼤批量数据同步的场景
2 rsync 特点
可以镜像保存整个⽬录树和⽂件系统
可以保留原有的权限 (permission,mode) owner,group, 时间 ( 修改时间 ,modify time) ,软硬 链接,
⽂件 acl ,⽂件属性 (attributes) 信息等
传输 == 效率⾼ == ,使⽤同步算法,只⽐较变化的(增量备份)
file1.txt file2.txt file3.txt(A 服务器 )
rsync 实现数据同步 => 只同步 file3.txt => 增量备份 file1.txt file2.txt(B 服务器 )
⽀持匿名传输,⽅便⽹站镜像;也可以做验证,加强安全
3 rsync scp 的区别
两者都可以实现远程同步,但是相对⽐⽽⾔, rsync 能⼒更强
① ⽀持增量备份 ② 数据同步时保持⽂件的原有属性
2 ups 中继器介绍
UPS 中继器是在不间断电源( UPS )系统中起到信号增强和扩展覆盖范围作用的设备。
3 、安装 rsync
[root@1 ~]# rpm -aq |grep rsync // 查看是否已经安装 rsync
[root@1 ~]# yum list installed | grep rsync // 查看是否已经安装 rsync
[root@1 ~]# yum -y install rsync // 默认没有安装,通过 yum 进行安装 rsync 软件
[root@1 ~]# which rsync // 查看 rsync 的所在位置
/usr/bin/rsync
[root@1 ~]# find / -name "rsync*" // 查找 rsync 相关文件
4 rsync 基本语法使用
push :推,相当于上传; pull :拉,相当于下载
rsync [ 选项 ] 原数据位置 目标位置
文件的增加、修改、删除、属性修改,会被 rsync 同步
-v 详细模式输出
-a 归档模式,递归的 式传输 件,并保持 件的属性, equals -rlptgoD
-r 递归拷 ⻉⽬
-l 保留软链接
-p 保留原有权限
-t 保留原有时间(修改)
-g 保留属组权限
-o 保留属主权限
-D 等于 --devices --specials 表示 b,c,s,p 类型的
-R 保留相对路径
-H 保留硬链接
-A 保留 ACL 策略
-e 指定要执 的远程 shell 命令, ssh 更改端 选项
-E 保留可执 权限
-X 保留扩展属性信息 a 属性
1 )在家目录中创建一些文件
[root@1 ~]# mkdir folder
[root@1 ~]# mkdir folder/f{1..3}
[root@1 ~]# ls folder/
f1 f2 f3
[root@1 ~]# touch folder/f1/file{0..4}
[root@1 ~]# tree folder/
folder/
├── f1
│ ├── file0
│ ├── file1
│ ├── file2
│ ├── file3
│ └── file4
├── f2
└── f3
3 directories, 5 files
2 )使用 rsync 进行测试(文件增加)(同步文件或同步目录)
[root@1 ~]# rsync -av folder/* /opt // folder 目录下的文件同步到 opt 目录下
sending incremental file list
f1/
f1/file0
f1/file1
f1/file2
f1/file3
f1/file4
f2/
f3/
sent 378 bytes received 123 bytes 1,002.00 bytes/sec
total size is 0 speedup is 0.00
[root@1 ~]# tree /opt // 查看是否同步成功
/opt
├── f1
│ ├── file0
│ ├── file1
│ ├── file2
│ ├── file3
│ └── file4
├── f2
└── f3
3 directories, 5 files
[root@1 ~]# rm -rf /opt/*
[root@1 ~]# rsync -av folder /opt // folder 目录同步过去
sending incremental file list
folder/
folder/f1/
folder/f1/file0
folder/f1/file1
folder/f1/file2
folder/f1/file3
folder/f1/file4
folder/f2/
folder/f3/
sent 412 bytes received 131 bytes 1,086.00 bytes/sec
total size is 0 speedup is 0.00
[root@1 ~]# tree /opt // 查看同步效果
/opt
└── folder
├── f1
│ ├── file0
│ ├── file1
│ ├── file2
│ ├── file3
│ └── file4
├── f2
└── f3
4 directories, 5 files
[root@1 ~]# rm -rf /opt/*
[root@1 ~]# rsync -avR folder/ /opt // folder 目录同步过去 -R 表示保留相对路径
[root@1 ~]# tree /opt // 查看同步效果
/opt
└── folder
├── f1
│ ├── file0
│ ├── file1
│ ├── file2
│ ├── file3
│ └── file4
├── f2
└── f3
4 directories, 5 files
[root@1 ~]# touch folder/f1/file5
[root@1 ~]# rsync -av folder/f1/ folder/f2/
[root@1 ~]# tree folder/
3 )使用 rsync 进行测试(文件删除)
[root@1 ~]# rm -rf folder/f1/file0
[root@1 ~]# rsync -av --delete folder/f1/ folder/f2/
[root@1 ~]# tree folder/
4 )使用 rsync 进行测试(文件修改)
[root@1 ~]# vim folder/f1/file1
哈哈
[root@1 ~]# cat folder/f1/file1
哈哈
[root@1 ~]# rsync -av folder/f1/ folder/f2/
[root@1 ~]# cat folder/f2/file1
哈哈
5 )使用 rsync 进行测试(文件属性改变)
[root@1 ~]# touch folder/f1/file1 -m -d "2024-7-14 00:00"
[root@1 ~]# rsync -av folder/f1/ folder/f2/
[root@1 ~]# ls -l folder/f2/file1
[root@1 ~]# chmod g+w folder/f1/file1
[root@1 ~]# rsync -av folder/f1/ folder/f2/
[root@1 ~]# ls -l folder/f2/file1
6 )远程传输文件
要实现远程同步,要求两台主机都应该安装 rsync
[root@1 ~]# dd if=/dev/zero of=/tmp/lajiwenjian bs=300M count=1
[root@2 ~]# yum -y install rsync
[root@1 ~]# rsync -av /tmp/lajiwenjian root@10.0.0.20:/tmp/
[root@2 ~]# ls -lh /tmp/lajiwenjian
-rw-r--r--. 1 root root 300M 7 18 11:05 /tmp/lajiwenjian
总结:
1 )本地同步
1 、安装 rsync
2 rsync -av 源地址 目标地址
3 、同步的内容:文件的新增、修改、删除( --delete )、属性(时间、权限)
4 rsync -av / 目录 / /tmp 同步目录下的文件
rsync -av / 目录 /tmp/ 同步目录
如果源目录不以 / 结尾,整个目录同步包含目录文件,带 / ,只同步目录下的文件
5 rsync -avR 保存相对路径 ,其实也就是同步了目录
-R 保留目录的相对路径,也会携带目录
2 )远程同步
1 、要求两台主机要应该安装 rsync 服务
2 、用法和本地同步相同
rsync -av root@10.0.0.11:/opt/ /tmp 拉取 pull
rsync -av 用户 @ 主机地址:目的地址 源地址
rsync -av /tmp/ root@10.0.0.11 /tmp 推送 push
rsync -av 源地址 用户 @ 主机地址:目的地址
需要输入密码,需要免密,这个需要的密码也是使用 ssh 服务验证,所以,直接免密设置好了之
后就直接支持 rsync
3 )同步服务器的设置
1 、修改配置文件
vim /etc/rsyncd.conf
[abc]
path=/app/javaproject/
log file=/var/log/rsyncd.log
2 、重启 rsync 服务
systemctl restart rsyncd
3 、在另一台主机上拉取 rsync 服务中的项目
测试是否能够检测到项目 检测到配置的 abc
rsync -a root@10.0.0.11 ::
abc
rsync -av root@10.0.0.11 :: /tmp
1 、修改项目名称进行拉取
[root@1 ~]# systemctl start rsyncd // 启动 rsync 服务
[root@1 ~]# systemctl status rsyncd
[root@1 ~]# vim /etc/rsyncd.conf // 查看项目
[root@2 ~]# rm -rf /tmp/* // 清空 tmp 目录
[root@2 ~]# ls /tmp
[root@2 ~]# rsync -a root@10.0.0.11:: // 查看服务器项目
app
[root@2 ~]# rsync -av root@10.0.0.11::app /tmp/ // 拉取服务器项目
[root@1 ~]# vim /etc/rsyncd.conf // 修改配置文件
[efg]
path=/app/studentweb/
log file=/var/log/rsync.log
[root@1 ~]# systemctl restart rsyncd // 重启服务
[root@2 ~]# rsync -av root@10.0.0.11::efg /tmp/ // 需要修改名字
[root@1 ~]# systemctl restart rsyncd // 重启服务
2 、自动化推送拉取文件
2 分钟自动推送一次代码(使用计划任务每 2 分钟推送一次任务)
[root@1 ~]# which rsync // 找到 rsync which 地址
/usr/bin/rsync
[root@1 ~]# crontab -e // 编辑计划任务
*/2 * * * * /usr/bin/rsync -av /app/studentweb/ root@10.0.0.20:/tmp/
[root@2 ~]# rm -rf /tmp/* // 删除 tmp 目录下的文件
[root@2 ~]# ls /tmp/ // 查看是否自动推送成功
src
如果文件根本没有修改,就没有必要推送
自动监听文件被修改的行为,再自动推送
编辑计划任务,删除计划任务
[root@1 ~]# crontab -e
3 、为 rsyncd 服务添加密码
编辑配置文件 /etc/rsyncd.conf
添加两个属性
auth users=user0 user1
secrets file=/etc/rsync.secrets
[root@1 ~]# vim /etc/rsyncd.conf // 配置文件添加两行内容(账号和密码文件的路径)
[efg]
path=/app/studentweb/
log file=/var/log/rsync.log
auth users=tom,jarry
secrets file=/etc/rsync.secrets
[root@1 ~]# vim /etc/rsync.secrets // 创建密码文件编辑 rsync 密码
tom:tom
jarry:jarry
[root@1 ~]# chmod 600 /etc/rsync.secrets // /etc/rsync.secrets 添加权限
[root@2 ~]# rsync -av tom@10.0.0.11::efg /tmp/
Password:
receiving incremental file list
4 、实现代码实时同步
1 inotify 介绍
inotify 是一个 Linux 内核提供的用于监控文件系统事件的机制。
它允许应用程序监控文件或目录的各种操作,例如创建、修改、删除、移动等。
以下是 inotify 的一些关键特点和用途:
特点:
1. 实时性:能够实时地通知应用程序文件系统的变化。
2. 细粒度监控:可以精确到单个文件或目录的操作级别。
用途:
1. 文件同步工具:比如在多个服务器之间保持文件同步,当源端文件发生变化时,及时同步到目
标端。
2. 日志监控:监控日志文件的更新,以便及时处理新的日志内容。
2 )直接安装 inotify-tools
监听指定目录,一旦目录发上修改,就执行指定的指令
[root@1 ~]# yum -y install inotify-tools
inotify 监听
安装后会有两条指令
inotifywait 等待 inotifywatch 一直监视(在 /usr/bin 下)
[root@1 ~]# inotifywait -mr /app/
Setting up watches. Beware: since -r was given, this may take a while!
Watches established.
|
新开一个窗口
[root@1 ~]# touch /app/studentweb/nihao.txt
查看原先窗口
新窗口中
[root@1 ~]# vim /app/studentweb/nihao.txt
查看原窗口
一旦 inotifywait 监听到目录中的改变,就直接推送
2 )编写 inotify.sh 脚本
[root@1 ~]# which inotifywait
/usr/bin/inotifywait
[root@1 ~]# vim inotify.sh
#!/bin/bash // 指定编译工具
/usr/bin/inotifywait -mrq -e modify,delete,create,attrib,move
/app/studentweb | while read events
do
rsync -av --delete /app/studentweb/ root@10.0.0.20:/tmp/
done
[root@1 ~]# chmod +x inotify.sh
[root@1 ~]# ./inotify.sh
|
新开一终端执行下面命令
[root@1 ~]# touch /app/studentweb/tiantian.txt
[root@2 ~]# ls /tmp/
nihao.txt src tiantian.txt
[root@1 ~]# touch /app/studentweb/fuwocheng.txt
[root@2 ~]# ls /tmp/
fuwocheng.txt nihao.txt src tiantian.txt
5 、让 .sh 文件一直执行下去
[root@1 ~]# nohup ./inotify.sh & // 放入后台一直运行
练习
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 ~]# 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 出现事
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值