如何在 Linux 上復制文件/文件夾到遠程系統?


從一個服務器復制文件到另一個服務器,或者從本地到遠程復制是 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@2g.CentOS.com:/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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM