【LeetCode】332. Reconstruct Itinerary 重新安排行程(Medium)(JAVA)
目錄轉載連結:
http://www.ruanyifeng.com/blog/2020/08/rsync.html
https://www.cnblogs.com/f-ck-need-u/p/7220009.html#blog21
https://www.cnblogs.com/0820LL/p/9627047.html
安裝
如果本機或者遠端計算機沒有安裝 rsync,可以用下面的命令安裝。
# Debian $ sudo apt-get install rsync # Red Hat $ sudo yum install rsync # Arch Linux $ sudo pacman -S rsync
基本用法
一、local(本機使用)
1.1 -r
引數
本機使用 rsync 命令時,可以作為cp
和mv
命令的替代方法,將源目錄同步到目標目錄。
$ rsync -r source destination
上面命令中,-r
表示遞迴,即包含子目錄。注意,-r
是必須的,否則 rsync 執行不會成功。source
目錄表示源目錄,destination
表示目標目錄。
如果有多個檔案或目錄需要同步,可以寫成下面這樣。
$ rsync -r source1 source2 destination
上面命令中,source1
、source2
都會被同步到destination
目錄。
1.2 -a
引數
-a
引數可以替代-r
,除了可以遞迴同步以外,還可以同步元資訊(比如修改時間、許可權等)。由於 rsync 預設使用檔案大小和修改時間決定檔案是否需要更新,所以-a
比-r
更有用。下面的用法才是常見的寫法。
$ rsync -a source destination
目標目錄destination
如果不存在,rsync 會自動建立。執行上面的命令後,源目錄source
被完整地複製到了目標目錄destination
下面,即形成了destination/source
的目錄結構。
如果只想同步源目錄source
裡面的內容到目標目錄destination
,則需要在源目錄後面加上斜槓。
$ rsync -a source/ destination
上面命令執行後,source
目錄裡面的內容,就都被複制到了destination
目錄裡面,並不會在destination
下面建立一個source
子目錄。
1.3 -n
引數
如果不確定 rsync 執行後會產生什麼結果,可以先用-n
或--dry-run
引數模擬執行的結果。
$ rsync -anv source/ destination
上面命令中,-n
引數模擬命令執行的結果,並不真的執行命令。-v
引數則是將結果輸出到終端,這樣就可以看到哪些內容會被同步。
1.4 --delete
引數
預設情況下,rsync 只確保源目錄的所有內容(明確排除的檔案除外)都複製到目標目錄。它不會使兩個目錄保持相同,並且不會刪除檔案。如果要使得目標目錄成為源目錄的映象副本,則必須使用--delete
引數,這將刪除只存在於目標目錄、不存在於源目錄的檔案。
$ rsync -av --delete source/ destination
上面命令中,--delete
引數會使得destination
成為source
的一個映象。
1.5 --exclude
引數
有時,我們希望同步時排除某些檔案或目錄,這時可以用--exclude
引數指定排除模式。
$ rsync -av --exclude='*.txt' source/ destination
# 或者
$ rsync -av --exclude '*.txt' source/ destination
上面命令排除了所有 TXT 檔案。
注意,rsync 會同步以"點"開頭的隱藏檔案,如果要排除隱藏檔案,可以這樣寫--exclude=".*"
。
如果要排除某個目錄裡面的所有檔案,但不希望排除目錄本身,可以寫成下面這樣。
$ rsync -av --exclude 'dir1/*' source/ destination
多個排除模式,可以用多個--exclude
引數。
$ rsync -av --exclude 'file1.txt' --exclude 'dir1/*' source/ destination
多個排除模式也可以利用 Bash 的大擴號的擴充套件功能,只用一個--exclude
引數。
$ rsync -av --exclude={'file1.txt','dir1/*'} source/ destination
如果排除模式很多,可以將它們寫入一個檔案,每個模式一行,然後用--exclude-from
引數指定這個檔案。
$ rsync -av --exclude-from='exclude-file.txt' source/ destination
1.6 --include
引數
--include
引數用來指定必須同步的檔案模式,往往與--exclude
結合使用。
$ rsync -av --include="*.txt" --exclude='*' source/ destination
上面命令指定同步時,排除所有檔案,但是會包括 TXT 檔案。
二、遠端同步
2.1 SSH協議
rsync 除了支援本地兩個目錄之間的同步,也支援遠端同步。它可以將本地內容,同步到遠端伺服器。
#push(推) 本地內容 同步到遠端伺服器
$ rsync -av source/ username@remote_host:destination
也可以將遠端內容同步到本地。
#pull(拉) 遠端內容 同步到本地
$ rsync -av username@remote_host:source/ destination
rsync 預設使用 SSH 進行遠端登入和資料傳輸。
由於早期 rsync 不使用 SSH 協議,需要用-e
引數指定協議,後來才改的。所以,下面-e ssh
可以省略。
$ rsync -av -e ssh source/ user@remote_host:/destination
但是,如果 ssh 命令有附加的引數,則必須使用-e
引數指定所要執行的 SSH 命令。
$ rsync -av -e 'ssh -p 2234' source/ user@remote_host:/destination
上面命令中,-e
引數指定 SSH 使用2234埠。
2.2 rsync協議
語法格式:
#pull(拉) 遠端內容 同步到本地
$ rsync -av --port=埠號 username@remote_host::module/ destination
或者
$ rsync -av rsync://username@remote_host:port/module/ destination
#push(推) 本地內容 同步到遠端伺服器
$ rsync -av SRC/ username@remote_host::module/destination
或者
$ rsync -av SRC/ rsync://username@remote_host:port/module/destination
地址中的module
並不是實際路徑名,而是rsync服務端配置檔案 /etc/rsyncd.conf
裡面配置的模組名,模組裡面會包含一些使用者名稱、密碼、路徑等認證資訊。
2.3 rsync --daemon模式示例
伺服器端:只有一個,放置 rysncd.conf 預設的位置 /etc/rsyncd.conf
客戶端:有多個,放置密碼檔案
使用 rsync --daemon 的步驟
第一步 在伺服器端編寫 rsync.conf,位置任意,預設為 /etc/rsync.conf
第二步 在伺服器端編寫使用者的賬號密碼檔案(即rsyncd.secrets) 許可權必須是600
第三步 啟動 sudo rsync --daemon
第三步 如果想無密碼傳輸,在客戶端編寫相應使用者的密碼檔案(即rsync.passwd) 許可權必須是600
第四步 在客戶端使用 rsync 進行傳輸 指定模組和密碼檔案
預設"rsync --daemon"讀取的配置檔案為 /etc/rsyncd.conf
,有些版本的系統上可能該檔案預設不存在。rsyncd.conf的配置見man rsyncd.conf。以下是部分內容:
# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:
# uid = nobody #rsync服務的執行使用者,預設是nobody,檔案傳輸成功後屬主將是這個uid
# gid = nobody #rsync服務的執行組,預設是nobody,檔案傳輸成功後屬組將是這個gid
# use chroot = yes #rsync daemon在傳輸前是否切換到指定的path目錄下,並將其監禁在內
# max connections = 4 #指定最大連線數量,0表示沒有限制
# pid file = /var/run/rsyncd.pid #指定rsync daemon的pid檔案
# exclude = lost+found/ #
# transfer logging = yes #
# timeout = 900 #確保rsync伺服器不會永遠等待一個崩潰的客戶端,0表示永遠等待
# ignore nonreadable = yes #
# dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
#指定哪些檔案不用進行壓縮傳輸
# [ftp1] #模組ID
# path = /home/ftp #指定該模組的路徑,該引數必須指定。啟動rsync服務前該目錄必須存在。rsync請求訪問模組本質就是訪問該路徑。
# comment = ftp export area #
以下是常見的配置項,也算是一個配置示例:
#### 全域性配置引數
port = 888 # 指定rsync埠。預設873
uid = rsync # rsync服務的執行使用者,預設是nobody,檔案傳輸成功後屬主將是這個uid
gid = rsync # rsync服務的執行組,預設是nobody,檔案傳輸成功後屬組將是這個gid
use chroot = no # rsync daemon在傳輸前是否切換到指定的path目錄下,並將其監禁在內
max connections = 200 # 指定最大連線數量,0表示沒有限制
timeout = 300 # 確保rsync伺服器不會永遠等待一個崩潰的客戶端,0表示永遠等待
motd file = /var/rsyncd/rsync.motd # 客戶端連線過來顯示的訊息
pid file = /var/run/rsyncd.pid # 指定rsync daemon的pid檔案
lock file = /var/run/rsync.lock # 指定鎖檔案
log file = /var/log/rsyncd.log # 指定rsync的日誌檔案,而不把日誌傳送給syslog
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # 指定哪些檔案不用進行壓縮傳輸
#### 下面指定模組,並設定模組配置引數,可以建立多個模組
[test1] # 模組ID
path = /tmp/test1 # 指定該模組的路徑,該引數必須指定。啟動rsync服務前該目錄必須存在。rsync請求訪問模組本質就是訪問該路徑。
ignore errors # 忽略某些IO錯誤資訊
read only = false # 指定該模組是否可讀寫,即能否上傳檔案,false表示可讀寫,true表示可讀不可寫。所有模組預設不可上傳
write only = false # 指定該模式是否支援下載,設定為true表示客戶端不能下載。所有模組預設可下載
list = false # 客戶端請求顯示模組列表時,該模組是否顯示出來,設定為false則該模組為隱藏模組。預設true
hosts allow = 10.0.0.0/24 # 指定允許連線到該模組的機器,多個ip用空格隔開或者設定區間
hosts deny = 0.0.0.0/32 # 指定不允許連線到該模組的機器
auth users = rsync_backup # 指定連線到該模組的使用者列表,只有列表裡的使用者才能連線到模組,使用者名稱和對應密碼儲存在secrts file中,
# 這裡使用的不是系統使用者,而是虛擬使用者。不設定時,預設所有使用者都能連線,但使用的是匿名連線
secrets file = /etc/rsyncd.passwd # 儲存auth users使用者列表的使用者名稱和密碼,每行包含一個username:passwd。由於"strict modes"
# 預設為true,所以此檔案要求非rsync daemon使用者不可讀寫。只有啟用了auth users該選項才有效。
[test2] # 以下定義的是第二個模組
path=/tmp/test2
read only = false
ignore errors
comment = anyone can access
在伺服器端啟動
$sudo rsync --daemon
# 如果指定配置檔案(不指定配置檔案,預設為/etc/rsync.conf)
$sudo rsync --daemon --config=/path/rsync.conf
檢視 rsync --daemon 的執行
$ps -ef | grep -v grep | grep rsync
在客戶端進行同步
$rsync -avzP --port=埠號 ./testFile.txt [email protected]::test1 --password-file=rsync.passwd # 將本地客戶端的檔案推到伺服器端 使用test1模組的配置
$rsync -avzP ./testFile.txt [email protected]::test2 --password-file=rsync.passwd # 將本地客戶端的檔案推到伺服器端 使用test2模組的配置
$rsync -avzP [email protected]::test1 ./ --password-file=rsync.passwd # 將伺服器端的資料拉到本地客戶端
2.4 rsync --daemon模式專案實戰
專案介紹:
此專案實戰是客戶端 pull(拉)方法
服務端配置rsyncd.conf相關資訊
客戶端執行命令 把遠端內容同步到本地
$ rsync -av --port=埠號 username@remote_host::module/ destination
或者
$ rsync -av rsync://username@remote_host:port/module/ destination
角色分配表
伺服器 | ip | 工具 | 系統 | 目錄 |
---|---|---|---|---|
Server | 192.168.2.221 | rsync | Centos7.2 | s_test |
Client | 192.168.2.222 | rsync | Centos7.2 | c_test |
Server端配置
安裝rsync
$ yum -y install rsync
更改配置檔案
$ vim /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
hosts allow = 192.168.2.222
port = 5699
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[test1]
path = /tmp/s_test/
comment = Hello My Dear!
ignore errors
read only = no
write only = no
hosts allow = 192.168.2.222
hosts deny = *
list = false
uid = root
gid = root
auth users = onetest #設定允許連線伺服器的賬戶,此賬戶可以是系統中不存在的使用者
secrets file = /etc/rsync.password #密碼驗證檔名,該檔案許可權要求為只讀,建議為600,僅在設定auth users後有效
建立使用者與密碼認證檔案
-
自定義密碼為 123456 (此密碼並非登入密碼,只是一個 rsync 與 client 伺服器進行互動的憑證,可自定義)
-
建立rsync.password檔案,並寫入【使用者名稱:密碼】,記住此處的密碼!
[root@localhost ~]# vim /etc/rsync.password onetest=123456
-
需要給密碼檔案 600 許可權
[root@localhost ~]# chmod 600 /etc/rsync.password
-
啟動rsync (以守護程序方式啟動)
[root@localhost ~]#rsync --daemon
注意:修改
rsyncd.conf
配置檔案後,記得需要重啟 rsync 服務[root@localhost ~]#killall rsync [root@localhost ~]#rsync --daemon [root@localhost ~]#lsof -i :873 #可檢視是否已啟動
Clinet配置
伺服器IP 192.168.2.222 (只需安裝,配置檔案也無需修改,不需要啟動)
[root@localhost ~]#yum -y install rsync
注意:客戶端只需要寫密碼,服務端是使用者加密碼
[root@localhost ~]#vim /etc/rsync.password
123456
執行命令
[root@localhost ~]#rsync -avz --port 5699 --password-file=/etc/rsync.password [email protected]::test /c_test
上面的命令是從 服務端 192.168.2.221伺服器上/tmp/s_test/ 目錄裡的內容拉取到 客服端 /c_test 目錄裡
注意事項:
rsync --daemon 開啟需要管理員許可權
埠是否開啟,預設是873,使用 netstat 命令檢視埠資訊
防火牆問題,使用 iptables 修改防火牆允許的訪問埠
rsync 加密傳輸
使用 ssh -c 引數指定加密演算法
rsync -azrP -e "ssh -c aes256-ctr -p 2020" filename [email protected]:/home/leo/
2.4 rsync + inotify
專案介紹:
角色分配表
伺服器 | ip | 工具 | 系統 | 目錄 |
---|---|---|---|---|
Server | 192.168.2.221 | rsync | Centos7.2 | s_test |
Client | 192.168.2.222 | rsync,inotify-tools | Centos7.2 | c_test |
Server端配置
安裝rsync
$ yum -y install rsync
更改配置檔案
$ vim /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
hosts allow = 192.168.2.222
port = 5699
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[inotify]
path = /tmp/s_test/
comment = Hello My Dear!
ignore errors
read only = no
write only = no
hosts allow = 192.168.2.222
hosts deny = *
list = false
uid = root
gid = root
auth users = onetest #設定允許連線伺服器的賬戶,此賬戶可以是系統中不存在的使用者
secrets file = /etc/rsync.password #密碼驗證檔名,該檔案許可權要求為只讀,建議為600,僅在設定auth users後有效
建立使用者與密碼認證檔案
-
自定義密碼為 123456 (此密碼並非登入密碼,只是一個 rsync 與 client 伺服器進行互動的憑證,可自定義)
-
建立rsync.password檔案,並寫入【使用者名稱:密碼】,記住此處的密碼!
[root@localhost ~]# vim /etc/rsync.password onetest=123456
-
需要給密碼檔案 600 許可權
[root@localhost ~]# chmod 600 /etc/rsync.password
-
啟動rsync (以守護程序方式啟動)
[root@localhost ~]#rsync --daemon
注意:修改
rsyncd.conf
配置檔案後,記得需要重啟 rsync 服務[root@localhost ~]#killall rsync [root@localhost ~]#rsync --daemon [root@localhost ~]#lsof -i :873 #可檢視是否已啟動
Clinet配置
伺服器IP 192.168.2.222 (只需安裝,配置檔案也無需修改,不需要啟動)
[root@localhost ~]#yum -y install rsync
注意:客戶端只需要寫密碼,服務端是使用者加密碼
[root@localhost ~]#vim /etc/rsync.password
123456
安裝inotify-tools
[root@localhost ~]#yum -y install inotify-tools
指令碼
[root@localhost ~]#vim inotify.sh
#!/bin/bash
host=192.168.2.221 #server的ip(備份伺服器)
src=/c_test/ #所要監控的備份目錄(此處可以自定義,但是要保證存在)
des=inotify #自定義的模組名,需要與server端定義的一致
password=/etc/rsync.password #密碼檔案
user=onetest #使用者名稱
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files
do
rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
小結
雙冒號 :: 是用在 rsync 協議裡面的,
冒號 : 一般用在ssh協議裡面,這兩種用法各有千秋:
rsync協議你需要在rsync服務端配置模組,這增加了運維工作量,但是安全,因為不需要對客戶公開伺服器帳號密碼。
ssh協議方便,不需配置,拿
到伺服器帳號密碼即可開工,但是對客戶是暴露的,有安全風險。
還需要注意的是用rsync協議認證的時候,後面跟的是模組名,而不是路徑,這點要注意。