做公司網(wǎng)站需要的材料有哪些廣州抖音seo
一、rsync遠程同步
1.rsync基本概述
(1)sync同步
(2)async異步
(3)rsync遠程同步
2.rsync的特點
可以鏡像保存整個目錄樹和文件系統(tǒng)
可以保留原有權(quán)限,owner,group,時間,軟硬鏈接,文件acl,文件屬性等
傳輸效率高,使用同步算法
支持匿名傳輸,方便網(wǎng)站鏡像,安全性高
3、rsync與scp的區(qū)別
4.rsyn的使用
安裝rsync軟件包
[root@localhost ~]# yum -y install rsync[root@localhost ~]# which rsync
/usr/bin/rsync
push 推,相當于上傳;
pull 拉,相當于下載;
(1)本地同步
同步文件的內(nèi)容,文件的屬性,文件的新增,文件的修改,文件的刪除(--delete)
在家目錄中創(chuàng)建一些文件,將文件同步到opt下
[root@localhost ~]# cd
[root@localhost ~]# mkdir folder
在folder目錄下創(chuàng)建f1,f2,f3
[root@localhost ~]# mkdir folder/f{1..3}
[root@localhost ~]# tree folder/
folder/
├── f1
├── f2
└── f33 directories, 0 files
在folder目錄下的f1下創(chuàng)建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
└── f33 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
? ? └── f34 directories, 5 files
現(xiàn)在不傳輸?shù)給pt目錄,就在本地的及格目錄傳
[root@localhost ~]# tree folder/
folder/
├── f1
│???├── file0
│???├── file1
│???├── file2
│???├── file3
│???└── file4
├── f2
└── f33 directories, 5 files
將f1下的文件同步到f2下
rsync -av /目錄/?/tmp/? 同步目錄下的文件
[root@localhost ~]# rsync -av folder/f1/ folder/f2/
在f1底下創(chuàng)建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下,發(fā)現(xiàn)新創(chuàng)建的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
└── f33 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
└── f33 directories, 11 files
再次將f1下的文件同步到f2下,發(fā)現(xiàn)刪除的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
└── f33 directories, 11 files
使用--delete 進行刪除同步,將f1 下的文件同步到f2下
[root@localhost ~]# rsync -av --delete folder/f1/ folder/f2/
sending incremental file list
deleting file0sent 115 bytes ?received 21 bytes ?272.00 bytes/sec
total size is 0 ?speedup is 0.00
發(fā)現(xiàn)f2中的file0文件也被刪除了
[root@localhost ~]# tree folder/
folder/
├── f1
│???├── file1
│???├── file2
│???├── file3
│???├── file4
│???└── file5
├── f2
│???├── file1
│???├── file2
│???├── file3
│???├── file4
│???└── file5
└── f33 directories, 10 files
由此看出:文件的增加會同步,而文件的刪除并不會
rsync語法
rsync [選項] 原數(shù)據(jù)位置 目錄位置
修改文件
對f1中的file1文件進行修改,然后編輯文件內(nèi)容
[root@localhost ~]# vim folder/f1/file1
[root@localhost ~]# cat folder/f1/file1
大家好,我是阿優(yōu),超級無敵阿優(yōu)!
[root@localhost ~]# cat folder/f2/file1
發(fā)現(xiàn)修改了f1中的內(nèi)容,f2目錄中沒有發(fā)生改變再次使用--delete進行同步
[root@localhost ~]# rsync -av --delete folder/f1/ folder/f2/
sending incremental file list
./
file1sent 217 bytes ?received 38 bytes ?510.00 bytes/sec
total size is 47 ?speedup is 0.18發(fā)現(xiàn)f2中的file1文件也被修改了
[root@localhost ~]# cat folder/f2/file1
大家好,我是阿優(yōu),超級無敵阿優(yōu)!
由此得出:文件的修改也會被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
#給組用戶增加寫的權(quán)限
[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
#同步文件內(nèi)容的修改、文件的刪除,以及文件的屬性的修改
(2)遠程同步
向另一臺主機 /tmp目錄同步數(shù)據(jù)
[root@localhost ~]# rsync -av folder/ root@192.168.1.10:/tmp/
root@192.168.1.10's password:?
要實現(xiàn)遠程同步,要求對另一臺主機也要安裝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 root@192.168.1.20::
關(guān)閉防火墻
systemctl stop firewalld
關(guān)閉selinux
?setenforce 0
原主機上操作:
#從遠程主機拉取數(shù)據(jù)
[root@localhost ~]# rsync -av root@192.168.1.10:/tmp/lajiwenjian /tmp/
root@192.168.1.10's password:?
receiving incremental file list
lajiwenjiansent 43 bytes ?received 314,649,690 bytes ?15,348,767.46 bytes/sec
total size is 314,572,800 ?speedup is 1.00
#查看,發(fā)現(xiàn)lajiwenjian已經(jīng)存在
[root@localhost ~]# ls -l /tmp/
總用量 307200
-rw-r--r--. 1 root root 314572800 7月 ?18 11:04 lajiwenjian
由此證明:兩臺主機是可以進行遠程同步數(shù)據(jù)的
(3)服務(wù)器項目同步
對原主機進行免密操作
[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 root@localhost.localdomain
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 root@192.168.1.10
再次從遠程主機拉取數(shù)據(jù)
[root@localhost ~]# rsync -av root@192.168.1.10:/tmp/lajiwenjian /tmp/
receiving incremental file listsent 20 bytes ?received 51 bytes ?6.76 bytes/sec
total size is 314,572,800 ?speedup is 4,430,602.82
啟動rsync服務(wù)
檢查rsync服務(wù)是否啟動
[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 ? ?
找到服務(wù)配置文件
[root@localhost ~]# find / -name "rsync*conf"
/etc/rsyncd.conf
[root@localhost ~]# vim /etc/rsyncd.conf
創(chuàng)建多級目錄
[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.studentweb7 directories, 0 files
在多級目錄下創(chuàng)建.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.java7 directories, 10 files
[root@localhost ~]# ls /app/
studentweb
檢測app項目
進入app目錄下的studentweb目錄
[root@localhost ~]# cd /app/studentweb/編輯配置文件
[root@localhost studentweb]# vim /etc/rsyncd.conf重新啟動rsyncd服務(wù)
[root@localhost studentweb]# systemctl restart rsyncd注:備份服務(wù)器不需要啟動rsyncd服務(wù)
#在y主機提供了一個針對app/下項目的rsync服務(wù)
[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.java7 directories, 10 files
?
遠程主機上操作:
從原主機對數(shù)據(jù)進行同步
rsync -av 原 ::目標目錄
[root@dongdong ~]#rsync -a root@192.168.1.20::
[root@dongdong ~]#rsync -ac root@192.168.1.20::app /tmp/查看tmp目錄
[root@dongdong ~]#?ls -l /tmp/再次同步
[root@dongdong ~]#?rsync -av root@192.168.1.20::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.java5 directories, 10 files
發(fā)現(xiàn)原主機的數(shù)據(jù)被成功同步給遠程主機了
二、自動化推取文件
1.檢查并啟動rsync服務(wù)
檢查rsync服務(wù)是否啟動
[root@localhost ~]# netstat -lntup | grep rsync? ? ? ? ??
啟動rsync服務(wù)
[root@localhost ~]# systemctl start rsyncd再次檢查rsync服務(wù)是否啟動
[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 ? ? ? ? ?
??
2.設(shè)置每一分鐘推送一次代碼
(1)查看rsync所在的位置
[root@localhost ~]# which rsync
/usr/bin/rsync
(2)編輯計劃任務(wù)--每分鐘推取一次
[root@localhost ~]# crontab -e
*/1 * * * * /usr/bin/rsync -av /app/studentweb/ root@192.168.1.10:/tmp/
若文件沒有被修改,則沒必要推送
(3)編輯計劃任務(wù),刪除計劃任務(wù)
[root@localhost ~]# crontab -e
crontab: installing new crontab
您在 /var/spool/mail/root 中有新郵件
(4)另外開一臺主機進行驗證
刪除/tmp下的所有內(nèi)容
[root@dongdong ~]# rm -rf /tmp/*
在原主機計劃任務(wù)編輯完成后,查看/tmp目錄
[root@dongdong ~]# ls /tmp/
src
可以發(fā)現(xiàn)/tmp目錄下多了src,是從原主機同步過來的
3.給rsyncd服務(wù)添加密碼
(1)編輯配置文件
????????添加兩屬性
[root@localhost ~]# vim /etc/rsyncd.conf
auth users=user0,user1
#secrets file=/etc/rsync.secrets
(2)創(chuàng)建編輯rsync密碼文件
賬號:密碼
[root@localhost ~]# vim /etc/rsync.secrets
tom:tomjerry:jerry
(3) 給密碼文件添加權(quán)限
[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服務(wù)
[root@localhost ~]# systemctl restart rsyncd
4.安裝監(jiān)聽工具
(1)安裝inotify-tools軟件包
[root@localhost ~]# yum -y install inotify-tools
此處安裝完成后,會生成以下兩個文件
(2)查看inotifywait的位置
[root@localhost ~]# which inotifywait
/usr/bin/inotifywait
(3)創(chuàng)建并編輯腳本文件
[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/ root@192.168.1.10:/tmp/
done
(4)進行查看
[root@localhost ~]# ls
anaconda-ks.cfg ?d0 ?echo.txt ?folder ?inotify.sh ?list ?vuehtml000
發(fā)現(xiàn)我們創(chuàng)建的inotify.sh腳本文件是存在的
(5)對腳本文件進行改名,將其改為inotiftest.sh
[root@localhost ~]# mv inotify.sh inotiftest.sh
(6)對更改后的腳本文件的用戶權(quán)限進行修改
[root@localhost ~]# chmod 700 inotiftest.sh?
[root@localhost ~]# ls
anaconda-ks.cfg ?d0 ?echo.txt ?folder ?inotiftest.sh ?list ?vuehtml000
(7)創(chuàng)建測試文件,及編輯所創(chuàng)建的文件,以便測試
在studentweb的目錄下創(chuàng)建名為 天天好心情!的文件
[root@localhost ~]# touch /app/studentweb/天天好心情!
創(chuàng)建名為天天好心情? 的文件
[root@localhost ~]# touch /app/studentweb/天天好心情創(chuàng)建名為/woshidongdong的文件
[root@localhost ~]# touch /app/studentweb/woshidongdong
[root@localhost ~]# vim /app/studentweb/woshidongdong?查看編輯文件的內(nèi)容
[root@localhost ~]# cat /app/studentweb/woshidongdong?
天天好心情
(8)運行腳本文件
[root@localhost ~]# ./inotiftest.sh?
sending incremental file list
./
.woshidongdong.swp
woshidongdong
天天好心情
天天好心情!
(9)將原主機的腳本轉(zhuǎn)入后臺運行
[root@localhost ~]# nohup ./inotiftest.sh &
[3] 17277
[root@localhost ~]# nohup: 忽略輸入并把輸出追加到"nohup.out"
(10)在另一臺主機進行測試
[root@dongdong ~]# rm -rf /tmp/*
[root@dongdong ~]# ls /tmp/
src ?woshidongdong ?天天好心情 ?天天好心情!
發(fā)現(xiàn)我們所創(chuàng)建的文件和內(nèi)容也被同步到tmp目錄下了
?