2010年10月20日 星期三

transfer over 1gb large files over internet via ssh

當你有大於 1GB 的資料要在網路上傳輸,
利用現有大部份的 httpd, ftpd 等伺服器都沒辦法識別傳輸這類大檔案。
假設大檔案位於本地端 source@localhost 帳號下,你要傳輸到遠地端 destination@remotehost 帳號下。
則你可以使用如下任一方法:

其中,port_num 為 ssh 的 tcp 連線埠號,預設值為22。
path/from/file 為 source@localhost 帳號下的大檔案。
path/to/file 為 destination@remotehost 帳號下打算存放大檔案的位置

A.利用sftp
remotehost> sftp  -oPort=port_num   source@localhost:path/from/file

B.利用scp
remotehost> scp   -P port_num     source@localhost:path/from/file     path/to/file
或
localhost> scp   -P port_num    path/from/file     destination@remotehost:path/to/file

C.利用rsync+ssh,具續傳功能,不怕中途斷線
remotehost> rsync --copy-links --progress --partial --append -vtpre 'ssh -p port_num '
source@localhost:path/from/file    path/to/file
或
localhost> rsync --copy-links --progress --partial --append -vtpre 'ssh -p port_num '
path/from/file    destination@remotehost:path/to/file


上述3方法適用於 Linux 或 Cygwin 平台。
尤其最後一個方法的 rsync 支援中斷續傳功能,很適合大資料夾之傳輸。

註:
1.若只想在本機硬碟之間進行大量資料夾之備份,也可使用如下 rsync 語法,只要少掉 e 參數即:
   $ rsync --stats --progress --partial --append -vtpr   path/from/folder/   path/to/folder/

2.rsync 參數說明:
   rsync source@localhost:path/from/folder/  destination@remotehost:path/to/folder/
     --copy-links  將符號連結(軟連結或捷徑)進行實體拷貝
     --progress   顯示傳輸進度
     --stats     顯示檔案統計資訊
     --partial   傳輸中斷時,去處端將留下接收一半的檔案
     --append    傳輸時,遇到去處端長度有較小的檔案將自動從來源端補齊剩餘長度
     --rsync-path="sudo rsync"   讓遠方切換特權模式,取得特權目錄內容
     -v    傳輸時列印檔名訊息
     -t    去處端維持來源端的修改時間
     -p    去處端維持來源端的存取權限
     -r    傳輸包含所有子資料夾
     -e 'ssh -p port_num'  利用後面的指令字串進出連線傳輸
     -n    乾跑,只看清單,不實際拷貝

3.如果整理重複目錄時,只想找出本機硬碟之間兩目錄的差異,
  可自訂如下批次檔 ~/bin/backup.sh:
    #!/bin/bash
    #
    #rsync --stats -hlPrtv source/ destin/
    # [P]: keep (partial)ly transferred files, show (progress) during transfer
    # [h]: output numbers in (human-readable) format
    # [l]: copy sym(links) as symlinks readable
    # [r]: get (recursive) into directories
    # [t]: preserve modification (times)
    # [v]: increase (verbose) level
    echo "rsync --stats --append --partial --progress -vtprn $*"
    rsync --stats --append --partial --progress -vtprn $*

  實際比對時,可利用如下指令,

    host> bash ~/bin/backup.sh    source_path/    destin_path/

  得到source_path/須要搬動到destin_path/的檔案清單。
  搬完後,source_path/ 目錄將完全包含於 destin_path/ 目錄,也就是
  前者有的後者必有,故前者目錄即使刪除也沒關係。

  又因為有參數-n,執行結果只列印差異,沒有實際搬動。
  故當兩者差異為0檔案,無須搬動,即可確認前目錄已完全同步到後目錄。
  這時就可安心刪除前目錄。

2 則留言:

seke 提到...

若雙方都有安裝filezilla軟體,好像也可以傳輸超過4GB檔案,但未試過.
http://filezilla-project.org/

seke 提到...

有時本機硬碟間的目錄拷貝,
想只留修改時間,不拷貝符號連結目錄
也可使用如下參數:
>rsync --stats -hlPrtv source/ destin/
# [P]: keep (partial)ly transferred files, show (progress) during transfer
# [h]: output numbers in (human-readable) format
# [l]: copy sym(links) as symlinks readable
# [r]: get (recursive) into directories
# [t]: preserve modification (times)
# [v]: increase (verbose) level