當前位置:
首頁 > 知識 > 如何在 Linux 上複製文件/文件夾到遠程系統?

如何在 Linux 上複製文件/文件夾到遠程系統?

如何在 Linux 上複製文件/文件夾到遠程系統?

打開今日頭條,查看更多圖片

從一個伺服器複製文件到另一個伺服器,或者從本地到遠程複製是 Linux 管理員的日常任務之一。

-- Prakash Subramanian

從一個伺服器複製文件到另一個伺服器,或者從本地到遠程複製是 Linux 管理員的日常任務之一。

我覺得不會有人不同意,因為無論在哪裡這都是你的日常操作之一。有很多辦法都能處理這個任務,我們試著加以概括。你可以挑一個喜歡的方法。當然,看看其他命令也能在別的地方幫到你。

我已經在自己的環境下測試過所有的命令和腳本了,因此你可以直接用到日常工作當中。

通常大家都傾向 scp,因為它是文件複製的 原生命令(native command)之一。但本文所列出的其它命令也很好用,建議你嘗試一下。

文件複製可以輕易地用以下四種方法。

  • scp:在網路上的兩個主機之間複製文件,它使用 ssh 做文件傳輸,並使用相同的認證方式,具有相同的安全性。
  • rsync:是一個既快速又出眾的多功能文件複製工具。它能本地複製、通過遠程 shell 在其它主機之間複製,或者與遠程的 rsync 守護進程(daemon) 之間複製。
  • pscp:是一個並行複製文件到多個主機上的程序。它提供了諸多特性,例如為 scp 配置免密傳輸,保存輸出到文件,以及超時控制。
  • prsync:也是一個並行複製文件到多個主機上的程序。它也提供了諸多特性,例如為 ssh 配置免密傳輸,保存輸出到 文件,以及超時控制。

方式 1:如何在 Linux 上使用 scp 命令從本地系統向遠程系統複製文件/文件夾?

scp 命令可以讓我們從本地系統複製文件/文件夾到遠程系統上。

我會把 output.txt 文件從本地系統複製到 2g.CentOS.com 遠程系統的 /opt/backup 文件夾下。


# scp output.txt root@2g.CentOS.com:/opt/backup

output.txt 100% 2468 2.4KB/s 00:00

從本地系統複製兩個文件 output.txt 和 passwd-up.sh 到遠程系統 2g.CentOs.com 的 /opt/backup 文件夾下。


# scp output.txt passwd-up.sh root@2g.CentOS.com:/opt/backup

output.txt 100% 2468 2.4KB/s 00:00

passwd-up.sh 100% 877 0.9KB/s 00:00

從本地系統複製 shell-script 文件夾到遠程系統 2g.CentOs.com 的 /opt/back 文件夾下。

這會連同shell-script 文件夾下所有的文件一同複製到/opt/back 下。


# scp -r /home/daygeek/2g/shell-script/ root@:/opt/backup/

output.txt 100% 2468 2.4KB/s 00:00

ovh.sh 100% 76 0.1KB/s 00:00

passwd-up.sh 100% 877 0.9KB/s 00:00

passwd-up1.sh 100% 7 0.0KB/s 00:00

server-list.txt 100% 23 0.0KB/s 00:00

方式 2:如何在 Linux 上使用 scp 命令和 Shell 腳本複製文件/文件夾到多個遠程系統上?

如果你想複製同一個文件到多個遠程伺服器上,那就需要創建一個如下面那樣的小 shell 腳本。

並且,需要將伺服器添加進 server-list.txt 文件。確保添加成功後,每個伺服器應當單獨一行。

最終,你想要的腳本就像下面這樣:


# file-copy.sh

#!/bin/sh

for server in `more server-list.txt`

do

scp /home/daygeek/2g/shell-script/output.txt root@$server:/opt/backup

done

完成之後,給 file-copy.sh 文件設置可執行許可權。


# chmod +x file-copy.sh

最後運行腳本完成複製。


# ./file-copy.sh

output.txt 100% 2468 2.4KB/s 00:00

output.txt 100% 2468 2.4KB/s 00:00

使用下面的腳本可以複製多個文件到多個遠程伺服器上。


# file-copy.sh

#!/bin/sh

for server in `more server-list.txt`

do

scp /home/daygeek/2g/shell-script/output.txt passwd-up.sh root@$server:/opt/backup

done

下面結果顯示所有的兩個文件都複製到兩個伺服器上。


# ./file-cp.sh

output.txt 100% 2468 2.4KB/s 00:00

passwd-up.sh 100% 877 0.9KB/s 00:00

output.txt 100% 2468 2.4KB/s 00:00

passwd-up.sh 100% 877 0.9KB/s 00:00

使用下面的腳本遞歸地複製文件夾到多個遠程伺服器上。


# file-copy.sh

#!/bin/sh

for server in `more server-list.txt`

do

scp -r /home/daygeek/2g/shell-script/ root@$server:/opt/backup

done

上述腳本的輸出。


# ./file-cp.sh

output.txt 100% 2468 2.4KB/s 00:00

ovh.sh 100% 76 0.1KB/s 00:00

passwd-up.sh 100% 877 0.9KB/s 00:00

passwd-up1.sh 100% 7 0.0KB/s 00:00

server-list.txt 100% 23 0.0KB/s 00:00

output.txt 100% 2468 2.4KB/s 00:00

ovh.sh 100% 76 0.1KB/s 00:00

passwd-up.sh 100% 877 0.9KB/s 00:00

passwd-up1.sh 100% 7 0.0KB/s 00:00

server-list.txt 100% 23 0.0KB/s 00:00

方式 3:如何在 Linux 上使用 pscp 命令複製文件/文件夾到多個遠程系統上?

pscp 命令可以直接讓我們複製文件到多個遠程伺服器上。

使用下面的 pscp 命令複製單個文件到遠程伺服器。


# pscp.pssh -H 2g.CentOS.com /home/daygeek/2g/shell-script/output.txt /opt/backup

[1] 18:46:11 [SUCCESS] 2g.CentOS.com

使用下面的 pscp 命令複製多個文件到遠程伺服器。


# pscp.pssh -H 2g.CentOS.com /home/daygeek/2g/shell-script/output.txt ovh.sh /opt/backup

[1] 18:47:48 [SUCCESS] 2g.CentOS.com

使用下面的 pscp 命令遞歸地複製整個文件夾到遠程伺服器。


# pscp.pssh -H 2g.CentOS.com -r /home/daygeek/2g/shell-script/ /opt/backup

[1] 18:48:46 [SUCCESS] 2g.CentOS.com

使用下面的 pscp 命令使用下面的命令複製單個文件到多個遠程伺服器。


# pscp.pssh -h server-list.txt /home/daygeek/2g/shell-script/output.txt /opt/backup

[1] 18:49:48 [SUCCESS] 2g.CentOS.com

[2] 18:49:48 [SUCCESS] 2g.Debian.com

使用下面的 pscp 命令複製多個文件到多個遠程伺服器。


# pscp.pssh -h server-list.txt /home/daygeek/2g/shell-script/output.txt passwd-up.sh /opt/backup

[1] 18:50:30 [SUCCESS] 2g.Debian.com

[2] 18:50:30 [SUCCESS] 2g.CentOS.com

使用下面的命令遞歸地複製文件夾到多個遠程伺服器。


# pscp.pssh -h server-list.txt -r /home/daygeek/2g/shell-script/ /opt/backup

[1] 18:51:31 [SUCCESS] 2g.Debian.com

[2] 18:51:31 [SUCCESS] 2g.CentOS.com

方式 4:如何在 Linux 上使用 rsync 命令複製文件/文件夾到多個遠程系統上?

rsync 是一個即快速又出眾的多功能文件複製工具。它能本地複製、通過遠程 shell 在其它主機之間複製,或者在遠程 rsync 守護進程(daemon) 之間複製。

使用下面的 rsync 命令複製單個文件到遠程伺服器。


# rsync -avz /home/daygeek/2g/shell-script/output.txt root@:/opt/backup

sending incremental file list

output.txt

sent 598 bytes received 31 bytes 1258.00 bytes/sec

total size is 2468 speedup is 3.92

使用下面的 rsync 命令複製多個文件到遠程伺服器。


# rsync -avz /home/daygeek/2g/shell-script/output.txt passwd-up.sh root@2g.CentOS.com:/opt/backup

sending incremental file list

output.txt

passwd-up.sh

sent 737 bytes received 50 bytes 1574.00 bytes/sec

total size is 2537 speedup is 3.22

使用下面的 rsync 命令通過 ssh 複製單個文件到遠程伺服器。


# rsync -avzhe ssh /home/daygeek/2g/shell-script/output.txt root@2g.CentOS.com:/opt/backup

sending incremental file list

output.txt

sent 598 bytes received 31 bytes 419.33 bytes/sec

total size is 2.47K speedup is 3.92

使用下面的 rsync 命令通過 ssh 遞歸地複製文件夾到遠程伺服器。這種方式只複製文件不包括文件夾。


# rsync -avzhe ssh /home/daygeek/2g/shell-script/ root@2g.CentOS.com:/opt/backup

sending incremental file list

./

output.txt

ovh.sh

passwd-up.sh

passwd-up1.sh

server-list.txt

sent 3.85K bytes received 281 bytes 8.26K bytes/sec

total size is 9.12K speedup is 2.21

方式 5:如何在 Linux 上使用 rsync 命令和 Shell 腳本複製文件/文件夾到多個遠程系統上?

如果你想複製同一個文件到多個遠程伺服器上,那也需要創建一個如下面那樣的小 shell 腳本。


# file-copy.sh

#!/bin/sh

for server in `more server-list.txt`

do

rsync -avzhe ssh /home/daygeek/2g/shell-script/ root@2g.CentOS.com$server:/opt/backup

done

上面腳本的輸出。


# ./file-copy.sh

sending incremental file list

./

output.txt

ovh.sh

passwd-up.sh

passwd-up1.sh

server-list.txt

sent 3.86K bytes received 281 bytes 8.28K bytes/sec

total size is 9.13K speedup is 2.21

sending incremental file list

./

output.txt

ovh.sh

passwd-up.sh

passwd-up1.sh

server-list.txt

sent 3.86K bytes received 281 bytes 2.76K bytes/sec

total size is 9.13K speedup is 2.21

方式 6:如何在 Linux 上使用 scp 命令和 Shell 腳本從本地系統向多個遠程系統複製文件/文件夾?

在上面兩個 shell 腳本中,我們需要事先指定好文件和文件夾的路徑,這兒我做了些小修改,讓腳本可以接收文件或文件夾作為輸入參數。當你每天需要多次執行複製時,這將會非常有用。


# file-copy.sh

#!/bin/sh

for server in `more server-list.txt`

do

scp -r $1 root@2g.CentOS.com$server:/opt/backup

done

輸入文件名並運行腳本。


# ./file-copy.sh output1.txt

output1.txt 100% 3558 3.5KB/s 00:00

output1.txt 100% 3558 3.5KB/s 00:00

方式 7:如何在 Linux 系統上用非標準埠複製文件/文件夾到遠程系統?

如果你想使用非標準埠,使用下面的 shell 腳本複製文件或文件夾。

如果你使用了 非標準(Non-Standard)埠,確保像下面 scp 命令那樣指定好了埠號。


# file-copy-scp.sh

#!/bin/sh

for server in `more server-list.txt`

do

scp -P 2222 -r $1 root@2g.CentOS.com$server:/opt/backup

done

運行腳本,輸入文件名。


# ./file-copy.sh ovh.sh

ovh.sh 100% 3558 3.5KB/s 00:00

ovh.sh 100% 3558 3.5KB/s 00:00

如果你使用了 非標準(Non-Standard)埠,確保像下面 rsync 命令那樣指定好了埠號。


# file-copy-rsync.sh

#!/bin/sh

for server in `more server-list.txt`

do

rsync -avzhe "ssh -p 2222" $1 root@2g.CentOS.com$server:/opt/backup

done

運行腳本,輸入文件名。


# ./file-copy-rsync.sh passwd-up.sh

sending incremental file list

passwd-up.sh

sent 238 bytes received 35 bytes 26.00 bytes/sec

total size is 159 speedup is 0.58

sending incremental file list

passwd-up.sh

sent 238 bytes received 35 bytes 26.00 bytes/sec

total size is 159 speedup is 0.58


via: https://www.2daygeek.com/linux-scp-rsync-pscp-command-copy-files-folders-in-multiple-servers-using-shell-script/

作者: Prakash Subramanian 選題: lujun9972 譯者: LuuMing 校對: wxy

本文由 LCTT 原創編譯, Linux中國 榮譽推出


點擊「了解更多」可訪問文內鏈接

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 Linux技術 的精彩文章:

Linux 上最好的五款音樂播放器
5 個好用的開發者 Vim 插件

TAG:Linux技術 |