一、rsync远程同步
1.rsync基本概述
(1)sync同步
(2)async异步
(3)rsync远程同步
2.rsync的特点
可以镜像保存整个目录树和文件系统
可以保留原有权限,owner,group,时间,软硬链接,文件acl,文件属性等
传输效率高,使用同步算法
支持匿名传输,方便网站镜像,安全性高
3、rsync与scp的区别
两者都可以实现远程同步,但是相对⽐⽽⾔,rsync能⼒更强
① ⽀持增量备份
② 数据同步时保持⽂件的原有属性
4.rsyn的使用
安装rsync软件包
root@localhost \~\]# yum -y install rsync \[root@localhost \~\]# which rsync /usr/bin/rsync
push 推,相当于上传;
pull 拉,相当于下载;
(1)本地同步
同步文件的内容,文件的属性,文件的新增,文件的修改,文件的删除(--delete)
在家目录中创建一些文件,将文件同步到opt下
root@localhost \~\]# cd \[root@localhost \~\]# mkdir folder
在folder目录下创建f1,f2,f3
root@localhost \~\]# mkdir folder/f{1..3} \[root@localhost \~\]# tree folder/ folder/ ├── f1 ├── f2 └── f3 3 directories, 0 files
在folder目录下的f1下创建file0,file1,file2,file3,file4
root@localhost \~\]# touch folder/f1/file{0..4} \[root@localhost \~\]# tree folder/ folder/ ├── f1 │ ├── file0 │ ├── file1 │ ├── file2 │ ├── file3 │ └── file4 ├── f2 └── f3 3 directories, 5 files
同步文件
将folder目录下的文件传到opt目录
rsync -av /目录 /tmp 同步目录
root@localhost \~\]# rsync -av folder/ /opt/ \[root@localhost opt\]# ls a.txt f1 f2 f3 \[root@localhost opt\]# tree /opt/ /opt/ ├── a.txt ├── f1 │ ├── file0 │ ├── file1 │ ├── file2 │ ├── file3 │ └── file4 ├── f2 └── f3
使用rsync命令进行同步
rsync -avR 保存相对路径,也就是同步目录
root@localhost \~\]# rsync -avR folder/ /opt/ \[root@localhost \~\]# tree /opt/ /opt/ └── folder ├── f1 │ ├── file0 │ ├── file1 │ ├── file2 │ ├── file3 │ └── file4 ├── f2 └── f3 4 directories, 5 files
现在不传输到opt目录,就在本地的及格目录传
root@localhost \~\]# tree folder/ folder/ ├── f1 │ ├── file0 │ ├── file1 │ ├── file2 │ ├── file3 │ └── file4 ├── f2 └── f3 3 directories, 5 files
将f1下的文件同步到f2下
rsync -av /目录/ /tmp/ 同步目录下的文件
root@localhost \~\]# rsync -av folder/f1/ folder/f2/
在f1底下创建file5文件
root@localhost \~\]# touch folder/f1/file5 \[root@localhost \~\]# tree folder/ folder/ ├── f1 │ ├── file0 │ ├── file1 │ ├── file2 │ ├── file3 │ ├── file4 │ └── file5 ├── f2 │ ├── file0 │ ├── file1 │ ├── file2 │ ├── file3 │ └── file4 └── f3
3 directories, 11 files
再次将f1下的文件同步到f2下,发现新创建的file5也被同步过去了
root@localhost \~\]# rsync -av folder/f1/ folder/f2/ \[root@localhost \~\]# tree folder/ folder/ ├── f1 │ ├── file0 │ ├── file1 │ ├── file2 │ ├── file3 │ ├── file4 │ └── file5 ├── f2 │ ├── file0 │ ├── file1 │ ├── file2 │ ├── file3 │ ├── file4 │ └── file5 └── f3 3 directories, 12 files
删除文件
删除f1下的file0文件
root@localhost \~\]# rm -rf folder/f1/file0 \[root@localhost \~\]# tree folder/ folder/ ├── f1 │ ├── file1 │ ├── file2 │ ├── file3 │ ├── file4 │ └── file5 ├── f2 │ ├── file0 │ ├── file1 │ ├── file2 │ ├── file3 │ ├── file4 │ └── file5 └── f3 3 directories, 11 files
再次将f1下的文件同步到f2下,发现删除的file0在f2中仍然存在
root@localhost \~\]# rsync -av folder/f1/ folder/f2/ \[root@localhost \~\]# tree folder/ folder/ ├── f1 │ ├── file1 │ ├── file2 │ ├── file3 │ ├── file4 │ └── file5 ├── f2 │ ├── file0 │ ├── file1 │ ├── file2 │ ├── file3 │ ├── file4 │ └── file5 └── f3 3 directories, 11 files
使用--delete 进行删除同步,将f1 下的文件同步到f2下
root@localhost \~\]# rsync -av --delete folder/f1/ folder/f2/ sending incremental file list deleting file0 sent 115 bytes received 21 bytes 272.00 bytes/sec total size is 0 speedup is 0.00
发现f2中的file0文件也被删除了
root@localhost \~\]# tree folder/ folder/ ├── f1 │ ├── file1 │ ├── file2 │ ├── file3 │ ├── file4 │ └── file5 ├── f2 │ ├── file1 │ ├── file2 │ ├── file3 │ ├── file4 │ └── file5 └── f3 3 directories, 10 files
由此看出:文件的增加会同步,而文件的删除并不会
rsync语法
rsync [选项] 原数据位置 目录位置
-v 详细模式输出
-a 归档模式,递归的⽅式传输⽂件,并保持⽂件的属性
-R 保留相对路径
修改文件
对f1中的file1文件进行修改,然后编辑文件内容
root@localhost \~\]# vim folder/f1/file1 \[root@localhost \~\]# cat folder/f1/file1 大家好,我是阿优,超级无敌阿优! \[root@localhost \~\]# cat folder/f2/file1 发现修改了f1中的内容,f2目录中没有发生改变 再次使用--delete进行同步 \[root@localhost \~\]# rsync -av --delete folder/f1/ folder/f2/ sending incremental file list ./ file1 sent 217 bytes received 38 bytes 510.00 bytes/sec total size is 47 speedup is 0.18 发现f2中的file1文件也被修改了 \[root@localhost \~\]# cat folder/f2/file1 大家好,我是阿优,超级无敌阿优!
由此得出:文件的修改也会被rsync同步
root@localhost \~\]# touch folder/f1/file0 -m -d "2024-7-14 00:00" \[root@localhost \~\]# rsync -av --delete folder/f1/ folder/f2/ \[root@localhost \~\]# touch folder/f1/file0 -m -d "2024-7-14 00:00" \[root@localhost \~\]# rsync -av --delete folder/f1/ folder/f2/ \[root@localhost \~\]# ls -l folder/f1/file0 -rw-r--r--. 1 root root 0 7月 14 00:00 folder/f1/file0
#给组用户增加写的权限
root@localhost \~\]# chmod g+w folder/f1/file0 \[root@localhost \~\]# ls -l folder/f1/file0 -rw-rw-r--. 1 root root 0 7月 14 00:00 folder/f1/file0 \[root@localhost \~\]# rsync -av --delete folder/f1/ folder/f2/ \[root@localhost \~\]# ls -l folder/f2/file0 -rw-rw-r--. 1 root root 0 7月 14 00:00 folder/f2/file0 #同步文件内容的修改、文件的删除,以及文件的属性的修改
(2)远程同步
向另一台主机 /tmp目录同步数据
root@localhost \~\]# rsync -av folder/ [email protected]:/tmp/ [email protected]'s password:
要实现远程同步,要求对另一台主机也要安装rsync
远程主机上操作:
在tmp目录下新建一个大小为300M,名为lajiwenjian的文件
dd if=/dev/zero of=/tmp/lajiwenjian bs=300M count=1
查看tmp下的文件
ls -lh /tmp/
将文件同步到192.168.1.20的原主机上
rsync -a [email protected]::
关闭防火墙
systemctl stop firewalld
关闭selinux
setenforce 0
原主机上操作:
#从远程主机拉取数据
root@localhost \~\]# rsync -av [email protected]:/tmp/lajiwenjian /tmp/ [email protected]'s password: receiving incremental file list lajiwenjian sent 43 bytes received 314,649,690 bytes 15,348,767.46 bytes/sec total size is 314,572,800 speedup is 1.00 #查看,发现lajiwenjian已经存在 \[root@localhost \~\]# ls -l /tmp/ 总用量 307200 -rw-r--r--. 1 root root 314572800 7月 18 11:04 lajiwenjian
由此证明:两台主机是可以进行远程同步数据的
(3)服务器项目同步
对原主机进行免密操作
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:xoC7J0cpoMXgcvndhrxNRzX7BVbRtYi1vQC0Md9d2+4 [email protected] The key's randomart image is: +---\[RSA 2048\]----+ \|. .=o.ooB\| \|.o . . .O+=.B\| \|..\* . . .o.= =+\| \|.+ o + \* . ..o.\| \|. + \* S . ...\| \| + \* . . \| \| o + . E\| \| + \| \| \| +----\[SHA256\]-----+ \[root@localhost \~\]# ssh-copy-id [email protected]
再次从远程主机拉取数据
root@localhost \~\]# rsync -av [email protected]:/tmp/lajiwenjian /tmp/ receiving incremental file list sent 20 bytes received 51 bytes 6.76 bytes/sec total size is 314,572,800 speedup is 4,430,602.82
检查rsync服务是否启动
root@localhost \~\]# systemctl status rsyncd \[root@localhost \~\]# systemctl start rsyncd \[root@localhost \~\]# netstat -lntup \| grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:\* LISTEN 2664/rsync tcp6 0 0 :::873 :::\* LISTEN 2664/rsync
找到服务配置文件
root@localhost \~\]# find / -name "rsync\*conf" /etc/rsyncd.conf \[root@localhost \~\]# vim /etc/rsyncd.conf
创建多级目录
root@localhost \~\]# mkdir -p /app/studentweb/src/main/java/co/goho/ayou.studentweb \[root@localhost \~\]# tree /app/ /app/ └── studentweb └── src └── main └── java └── co └── goho └── ayou.studentweb 7 directories, 0 files
在多级目录下创建.java文件
root@localhost \~\]touch /app/studentweb/src/main/java/co/goho/ayou.studentweb/File{0..9}.java \[root@localhost \~\]# tree /app/ /app/ └── studentweb └── src └── main └── java └── co └── goho └── ayou.studentweb ├── File0.java ├── File1.java ├── File2.java ├── File3.java ├── File4.java ├── File5.java ├── File6.java ├── File7.java ├── File8.java └── File9.java 7 directories, 10 files \[root@localhost \~\]# ls /app/ studentweb
检测app项目
进入app目录下的studentweb目录
root@localhost \~\]# cd /app/studentweb/ 编辑配置文件 \[root@localhost studentweb\]# vim /etc/rsyncd.conf 重新启动rsyncd服务 \[root@localhost studentweb\]# systemctl restart rsyncd 注:备份服务器不需要启动rsyncd服务 #在y主机提供了一个针对app/下项目的rsync服务 \[root@localhost studentweb\]# tree /app/ /app/ └── studentweb └── src └── main └── java └── co └── goho └── ayou.studentweb ├── File0.java ├── File1.java ├── File2.java ├── File3.java ├── File4.java ├── File5.java ├── File6.java ├── File7.java ├── File8.java └── File9.java 7 directories, 10 files
从原主机对数据进行同步
rsync -av 原 ::目标目录
root@dongdong \~\]#rsync -a [email protected]:: \[root@dongdong \~\]#rsync -ac [email protected]::app /tmp/ 查看tmp目录 \[root@dongdong \~\]# ls -l /tmp/ 再次同步 \[root@dongdong \~\]# rsync -av [email protected]::app /tmp/ 再次查看 \[root@dongdong \~\]# ls -l /tmp/ \[root@dongdong \~\]# tree /tmp/src/ /tmp/src/ └── main └── java └── co └── goho └── ayou.studentweb ├── File0.java ├── File1.java ├── File2.java ├── File3.java ├── File4.java ├── File5.java ├── File6.java ├── File7.java ├── File8.java └── File9.java 5 directories, 10 files
发现原主机的数据被成功同步给远程主机了
二、自动化推取文件
1.检查并启动rsync服务
检查rsync服务是否启动
root@localhost \~\]# netstat -lntup \| grep rsync 启动rsync服务 \[root@localhost \~\]# systemctl start rsyncd 再次检查rsync服务是否启动 \[root@localhost \~\]# netstat -lntup \| grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:\* LISTEN 3368/rsync tcp6 0 0 :::873 :::\* LISTEN 3368/rsync
(1)查看rsync所在的位置
root@localhost \~\]# which rsync /usr/bin/rsync
(2)编辑计划任务--每分钟推取一次
root@localhost \~\]# crontab -e \*/1 \* \* \* \* /usr/bin/rsync -av /app/studentweb/ [email protected]:/tmp/
若文件没有被修改,则没必要推送
(3)编辑计划任务,删除计划任务
root@localhost \~\]# crontab -e crontab: installing new crontab 您在 /var/spool/mail/root 中有新邮件
(4)另外开一台主机进行验证
删除/tmp下的所有内容
root@dongdong \~\]# rm -rf /tmp/\* 在原主机计划任务编辑完成后,查看/tmp目录 \[root@dongdong \~\]# ls /tmp/ src
可以发现/tmp目录下多了src,是从原主机同步过来的
3.给rsyncd服务添加密码
(1)编辑配置文件
添加两属性
root@localhost \~\]# vim /etc/rsyncd.conf auth users=user0,user1 #secrets file=/etc/rsync.secrets
(2)创建编辑rsync密码文件
账号:密码
root@localhost \~\]# vim /etc/rsync.secrets tom:tom jerry:jerry
(3) 给密码文件添加权限
root@localhost \~\]# ls -l /etc/rsync.secrets -rw-r--r--. 1 root root 28 7月 18 15:12 /etc/rsync.secrets \[root@localhost \~\]# #chmod 600 /etc/rsync.secrets
(4)重启rsyncd服务
root@localhost \~\]# systemctl restart rsyncd
4.安装监听工具
(1)安装inotify-tools软件包
root@localhost \~\]# yum -y install inotify-tools
此处安装完成后,会生成以下两个文件
(2)查看inotifywait的位置
root@localhost \~\]# which inotifywait /usr/bin/inotifywait
(3)创建并编辑脚本文件
root@localhost \~\]# vim inotify.sh #!/bin/bash /usr/bin/inotifywait -mrq -e modify,delete,create,attrib,move /app/studentweb \| while read events do rsync -av /app/studentweb/ [email protected]:/tmp/ done
(4)进行查看
root@localhost \~\]# ls anaconda-ks.cfg d0 echo.txt folder inotify.sh list vuehtml000
发现我们创建的inotify.sh脚本文件是存在的
(5)对脚本文件进行改名,将其改为inotiftest.sh
root@localhost \~\]# mv inotify.sh inotiftest.sh
(6)对更改后的脚本文件的用户权限进行修改
root@localhost \~\]# chmod 700 inotiftest.sh \[root@localhost \~\]# ls anaconda-ks.cfg d0 echo.txt folder inotiftest.sh list vuehtml000
(7)创建测试文件,及编辑所创建的文件,以便测试
在studentweb的目录下创建名为 天天好心情!的文件
root@localhost \~\]# touch /app/studentweb/天天好心情! 创建名为天天好心情 的文件 \[root@localhost \~\]# touch /app/studentweb/天天好心情 创建名为/woshidongdong的文件 \[root@localhost \~\]# touch /app/studentweb/woshidongdong \[root@localhost \~\]# vim /app/studentweb/woshidongdong 查看编辑文件的内容 \[root@localhost \~\]# cat /app/studentweb/woshidongdong 天天好心情
(8)运行脚本文件
root@localhost \~\]# ./inotiftest.sh sending incremental file list ./ .woshidongdong.swp woshidongdong 天天好心情 天天好心情!
(9)将原主机的脚本转入后台运行
root@localhost \~\]# nohup ./inotiftest.sh \& \[3\] 17277 \[root@localhost \~\]# nohup: 忽略输入并把输出追加到"nohup.out"
(10)在另一台主机进行测试
root@dongdong \~\]# rm -rf /tmp/\* \[root@dongdong \~\]# ls /tmp/ src woshidongdong 天天好心情 天天好心情!
发现我们所创建的文件和内容也被同步到tmp目录下了