rsync
是一種遠(yuǎn)程和本地文件同步工具你稚,它可以實(shí)現(xiàn)遠(yuǎn)程和本地文件的增量同步材蛛,從而減少傳輸?shù)臄?shù)據(jù)量臊泌。
同步目錄
rsync -r dir1/ dir2
上面的命令表示把dir1下的所有文件復(fù)制到dir2,如果目標(biāo)目錄不存在將會(huì)自動(dòng)創(chuàng)建坏平。-r
表示遞歸拢操,包含該文件夾下的所有目錄和文件。注意舶替,dir1
后面的/
不能省略令境,省略之后會(huì)在dir2
下創(chuàng)建一個(gè)dir1
文件夾。
更常見的用法是使用-a
代替-r
顾瞪,它除了可以遞歸同步舔庶,還可以保留符號(hào)鏈接、特殊文件和設(shè)備文件陈醒、修改時(shí)間惕橙、組、所有者和權(quán)限等信息钉跷。
rsync -a dir1/ dir2
試運(yùn)行
在執(zhí)行rsync命令之前弥鹦,應(yīng)該始終檢查參數(shù)是否輸錯(cuò),rsync提供-n
參數(shù)可以試運(yùn)行命令爷辙,-v
表示輸出執(zhí)行過程的詳細(xì)信息
rsync -anv dir1/ dir2
與遠(yuǎn)程同步
本地傳輸?shù)竭h(yuǎn)程
rsync -a ~/dir1 username@remote_host:remote_directory
遠(yuǎn)程傳輸?shù)奖镜?/p>
rsync -a username@remote_host:~/dir1 local_directory
如果修改了ssh端口彬坏,需要加上-e
參數(shù)指定新的端口
rsync -a -e 'ssh -p 10021' ~/dir1 username@remote_host:remote_directory
壓縮傳輸
rsync
可以對(duì)傳輸?shù)膬?nèi)容進(jìn)行壓縮朦促,從而減少傳輸時(shí)間,具體做法是加上-z
參數(shù)
rsync -az ~/dir1 username@remote_host:remote_directory
續(xù)傳和進(jìn)度
-P
標(biāo)志結(jié)合了--progress
和--partial
栓始,可以恢復(fù)中斷和顯示傳輸進(jìn)度
rsync -azP ~/dir1 username@remote_host:remote_directory
完全同步
默認(rèn)情況下务冕,源目錄刪除了某個(gè)目錄,目標(biāo)目錄不會(huì)同步刪除混滔,可以使用--delete
參數(shù)來更改這個(gè)預(yù)設(shè)
rsync -a --delete ~/dir1 username@remote_host:remote_directory
排除和包含
--exclude
和--include
用來指定哪些目錄和文件需要排除或者包含進(jìn)來
rsync -a --exclude='node_modules' ~/dir1 username@remote_host:remote_directory
上述命令將不同步node_modules
文件夾洒疚,如果想保留目錄歹颓,可更改為node_modules/*
文件備份
rsync
支持對(duì)源文件進(jìn)行備份坯屿,語法如下
rsync -a --delete --backup --backup-dir=~/backups ~/dir1 username@remote_host:remote_directory