利用rsync斷點續傳scp中斷的檔案傳輸
伺服器經常需要備份,但是長時間的下載,難免會出現下載中斷的情況。
一直使用scp
下載備份好的檔案,以前資料量少的時候,直接重新下載就可以了。隨著網站上資料越來越多,下載的時間越來越長,完全重新開始變得不現實了。有時候下載到90%
以上的時候,失敗了,完全重傳是沒有必要的。
我們只需要下載缺失的部分就好了,幸運的是rsync
支援這種需求。
具體的做法是將如下的選項組合傳遞給rsync
,-checksum
(檔案分塊計算校驗和,只傳輸校驗和不一致的部分),-in-place
(將檔案原地替換,因為rsync
通常會寫一個臨時檔案,然後移動)和-no-whole-file
(告訴rsync
不要複製整個檔案,而是使用deltas
(部分傳輸))。
例如:
$ rsync -Pa --checksum --inplace --no-whole-file --progress remote.server:file local.file
下面是一個真實的例子:
$ rsync -Pa --checksum --inplace --no-whole-file --progress -e 'ssh -p 22' [email protected]:~/backup/wordpress.20181203.tar.gz wordpress.20181203.tar.gz receiving incremental file list sent 19 bytes received 76 bytes 0.22 bytes/sec total size is 8379088896 speedup is 88200935.75
整個過程非常的快,比完全重傳快多了。
scp
命令拷貝檔案非常好用,但在拷貝大檔案時存在一個問題就是連線經常會斷開,此時再使用scp
進行拷貝會重新拷貝檔案,為了解決這個問題。我們可以使用rsync
命令來進行斷點續傳。命令如下:
$ rsync -P --rsh=ssh xxx.zip 192.168.0.1:~/
為了使用方便可以設定alias
:
$ alias rscp="rsync -P --rsh=ssh"
參考連結
- ofollow,noindex" target="_blank">scp實現斷點續傳
- SCP無法續傳, 用rsync 實現Linux遠端拷貝(限速和斷點續傳)
- Force rsync to use delta transfer to fix corrupt remote file
- Linux rsync command
- Rsync使用非ssh預設埠從遠端伺服器同步檔案到本地