自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在Linux上復(fù)制文件/文件夾到遠(yuǎn)程系統(tǒng)?

系統(tǒng) Linux
從一個(gè)服務(wù)器復(fù)制文件到另一個(gè)服務(wù)器,或者從本地到遠(yuǎn)程復(fù)制是 Linux 管理員的日常任務(wù)之一。

[[258145]]

 從一個(gè)服務(wù)器復(fù)制文件到另一個(gè)服務(wù)器,或者從本地到遠(yuǎn)程復(fù)制是 Linux 管理員的日常任務(wù)之一。

我覺(jué)得不會(huì)有人不同意,因?yàn)闊o(wú)論在哪里這都是你的日常操作之一。有很多辦法都能處理這個(gè)任務(wù),我們?cè)囍右愿爬?。你可以挑一個(gè)喜歡的方法。當(dāng)然,看看其他命令也能在別的地方幫到你。

我已經(jīng)在自己的環(huán)境下測(cè)試過(guò)所有的命令和腳本了,因此你可以直接用到日常工作當(dāng)中。

通常大家都傾向 scp,因?yàn)樗俏募?fù)制的原生命令native command之一。但本文所列出的其它命令也很好用,建議你嘗試一下。

文件復(fù)制可以輕易地用以下四種方法。

  • scp:在網(wǎng)絡(luò)上的兩個(gè)主機(jī)之間復(fù)制文件,它使用 ssh 做文件傳輸,并使用相同的認(rèn)證方式,具有相同的安全性。
  • rsync:是一個(gè)既快速又出眾的多功能文件復(fù)制工具。它能本地復(fù)制、通過(guò)遠(yuǎn)程 shell 在其它主機(jī)之間復(fù)制,或者與遠(yuǎn)程的 rsync 守護(hù)進(jìn)程daemon 之間復(fù)制。
  • pscp:是一個(gè)并行復(fù)制文件到多個(gè)主機(jī)上的程序。它提供了諸多特性,例如為 scp 配置免密傳輸,保存輸出到文件,以及超時(shí)控制。
  • prsync:也是一個(gè)并行復(fù)制文件到多個(gè)主機(jī)上的程序。它也提供了諸多特性,例如為 ssh 配置免密傳輸,保存輸出到 文件,以及超時(shí)控制。

方式 1:如何在 Linux 上使用 scp 命令從本地系統(tǒng)向遠(yuǎn)程系統(tǒng)復(fù)制文件/文件夾?

scp 命令可以讓我們從本地系統(tǒng)復(fù)制文件/文件夾到遠(yuǎn)程系統(tǒng)上。

我會(huì)把 output.txt 文件從本地系統(tǒng)復(fù)制到 2g.CentOS.com 遠(yuǎn)程系統(tǒng)的 /opt/backup 文件夾下。

  1. # scp output.txt root@2g.CentOS.com:/opt/backup
  2.  
  3. output.txt 100% 2468 2.4KB/s 00:00

從本地系統(tǒng)復(fù)制兩個(gè)文件 output.txtpasswd-up.sh 到遠(yuǎn)程系統(tǒng) 2g.CentOs.com/opt/backup 文件夾下。

  1. # scp output.txt passwd-up.sh root@2g.CentOS.com:/opt/backup
  2.  
  3. output.txt 100% 2468 2.4KB/s 00:00
  4. passwd-up.sh 100% 877 0.9KB/s 00:00

從本地系統(tǒng)復(fù)制 shell-script 文件夾到遠(yuǎn)程系統(tǒng) 2g.CentOs.com/opt/back 文件夾下。

這會(huì)連同shell-script 文件夾下所有的文件一同復(fù)制到/opt/back 下。

  1. # scp -r /home/daygeek/2g/shell-script/ root@:/opt/backup/
  2.  
  3. output.txt 100% 2468 2.4KB/s 00:00
  4. ovh.sh 100% 76 0.1KB/s 00:00
  5. passwd-up.sh 100% 877 0.9KB/s 00:00
  6. passwd-up1.sh 100% 7 0.0KB/s 00:00
  7. server-list.txt 100% 23 0.0KB/s 00:00

方式 2:如何在 Linux 上使用 scp 命令和 Shell 腳本復(fù)制文件/文件夾到多個(gè)遠(yuǎn)程系統(tǒng)上?

如果你想復(fù)制同一個(gè)文件到多個(gè)遠(yuǎn)程服務(wù)器上,那就需要?jiǎng)?chuàng)建一個(gè)如下面那樣的小 shell 腳本。

并且,需要將服務(wù)器添加進(jìn) server-list.txt 文件。確保添加成功后,每個(gè)服務(wù)器應(yīng)當(dāng)單獨(dú)一行。

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

  1. # file-copy.sh
  2.  
  3. #!/bin/sh
  4. for server in `more server-list.txt`
  5. do
  6. scp /home/daygeek/2g/shell-script/output.txt root@$server:/opt/backup
  7. done

完成之后,給 file-copy.sh 的文件設(shè)置可執(zhí)行權(quán)限。

  1. # chmod +x file-copy.sh

然后運(yùn)行腳本完成復(fù)制。

  1. # ./file-copy.sh
  2.  
  3. output.txt 100% 2468 2.4KB/s 00:00
  4. output.txt 100% 2468 2.4KB/s 00:00

使用下面的腳本可以復(fù)制多個(gè)文件到多個(gè)遠(yuǎn)程服務(wù)器上。

  1. # file-copy.sh
  2.  
  3. #!/bin/sh
  4. for server in `more server-list.txt`
  5. do
  6. scp /home/daygeek/2g/shell-script/output.txt passwd-up.sh root@$server:/opt/backup
  7. done

下面結(jié)果顯示所有的兩個(gè)文件都復(fù)制到兩個(gè)服務(wù)器上。

  1. # ./file-cp.sh
  2.  
  3. output.txt 100% 2468 2.4KB/s 00:00
  4. passwd-up.sh 100% 877 0.9KB/s 00:00
  5. output.txt 100% 2468 2.4KB/s 00:00
  6. passwd-up.sh 100% 877 0.9KB/s 00:00

使用下面的腳本遞歸地復(fù)制文件夾到多個(gè)遠(yuǎn)程服務(wù)器上。

  1. # file-copy.sh
  2.  
  3. #!/bin/sh
  4. for server in `more server-list.txt`
  5. do
  6. scp -r /home/daygeek/2g/shell-script/ root@$server:/opt/backup
  7. done

上述腳本的輸出。

  1. # ./file-cp.sh
  2.  
  3. output.txt 100% 2468 2.4KB/s 00:00
  4. ovh.sh 100% 76 0.1KB/s 00:00
  5. passwd-up.sh 100% 877 0.9KB/s 00:00
  6. passwd-up1.sh 100% 7 0.0KB/s 00:00
  7. server-list.txt 100% 23 0.0KB/s 00:00
  8.  
  9. output.txt 100% 2468 2.4KB/s 00:00
  10. ovh.sh 100% 76 0.1KB/s 00:00
  11. passwd-up.sh 100% 877 0.9KB/s 00:00
  12. passwd-up1.sh 100% 7 0.0KB/s 00:00
  13. server-list.txt 100% 23 0.0KB/s 00:00

方式 3:如何在 Linux 上使用 pscp 命令復(fù)制文件/文件夾到多個(gè)遠(yuǎn)程系統(tǒng)上?

pscp 命令可以直接讓我們復(fù)制文件到多個(gè)遠(yuǎn)程服務(wù)器上。

使用下面的 pscp 命令復(fù)制單個(gè)文件到遠(yuǎn)程服務(wù)器。

  1. # pscp.pssh -H 2g.CentOS.com /home/daygeek/2g/shell-script/output.txt /opt/backup
  2.  
  3. [1] 18:46:11 [SUCCESS] 2g.CentOS.com

使用下面的 pscp 命令復(fù)制多個(gè)文件到遠(yuǎn)程服務(wù)器。

  1. # pscp.pssh -H 2g.CentOS.com /home/daygeek/2g/shell-script/output.txt ovh.sh /opt/backup
  2.  
  3. [1] 18:47:48 [SUCCESS] 2g.CentOS.com

使用下面的 pscp 命令遞歸地復(fù)制整個(gè)文件夾到遠(yuǎn)程服務(wù)器。

  1. # pscp.pssh -H 2g.CentOS.com -r /home/daygeek/2g/shell-script/ /opt/backup
  2.  
  3. [1] 18:48:46 [SUCCESS] 2g.CentOS.com

使用下面的 pscp 命令使用下面的命令復(fù)制單個(gè)文件到多個(gè)遠(yuǎn)程服務(wù)器。

  1. # pscp.pssh -h server-list.txt /home/daygeek/2g/shell-script/output.txt /opt/backup
  2.  
  3. [1] 18:49:48 [SUCCESS] 2g.CentOS.com
  4. [2] 18:49:48 [SUCCESS] 2g.Debian.com

使用下面的 pscp 命令復(fù)制多個(gè)文件到多個(gè)遠(yuǎn)程服務(wù)器。

  1. # pscp.pssh -h server-list.txt /home/daygeek/2g/shell-script/output.txt passwd-up.sh /opt/backup
  2.  
  3. [1] 18:50:30 [SUCCESS] 2g.Debian.com
  4. [2] 18:50:30 [SUCCESS] 2g.CentOS.com

使用下面的命令遞歸地復(fù)制文件夾到多個(gè)遠(yuǎn)程服務(wù)器。

  1. # pscp.pssh -h server-list.txt -r /home/daygeek/2g/shell-script/ /opt/backup
  2.  
  3. [1] 18:51:31 [SUCCESS] 2g.Debian.com
  4. [2] 18:51:31 [SUCCESS] 2g.CentOS.com

方式 4:如何在 Linux 上使用 rsync 命令復(fù)制文件/文件夾到多個(gè)遠(yuǎn)程系統(tǒng)上?

rsync 是一個(gè)即快速又出眾的多功能文件復(fù)制工具。它能本地復(fù)制、通過(guò)遠(yuǎn)程 shell 在其它主機(jī)之間復(fù)制,或者在遠(yuǎn)程 rsync 守護(hù)進(jìn)程daemon 之間復(fù)制。

使用下面的 rsync 命令復(fù)制單個(gè)文件到遠(yuǎn)程服務(wù)器。

  1. # rsync -avz /home/daygeek/2g/shell-script/output.txt root@:/opt/backup
  2.  
  3. sending incremental file list
  4. output.txt
  5.  
  6. sent 598 bytes received 31 bytes 1258.00 bytes/sec
  7. total size is 2468 speedup is 3.92

使用下面的 rsync 命令復(fù)制多個(gè)文件到遠(yuǎn)程服務(wù)器。

  1. # rsync -avz /home/daygeek/2g/shell-script/output.txt passwd-up.sh root@2g.CentOS.com:/opt/backup
  2.  
  3. sending incremental file list
  4. output.txt
  5. passwd-up.sh
  6.  
  7. sent 737 bytes received 50 bytes 1574.00 bytes/sec
  8. total size is 2537 speedup is 3.22

使用下面的 rsync 命令通過(guò) ssh 復(fù)制單個(gè)文件到遠(yuǎn)程服務(wù)器。

  1. # rsync -avzhe ssh /home/daygeek/2g/shell-script/output.txt root@2g.CentOS.com:/opt/backup
  2.  
  3. sending incremental file list
  4. output.txt
  5.  
  6. sent 598 bytes received 31 bytes 419.33 bytes/sec
  7. total size is 2.47K speedup is 3.92

使用下面的 rsync 命令通過(guò) ssh 遞歸地復(fù)制文件夾到遠(yuǎn)程服務(wù)器。這種方式只復(fù)制文件不包括文件夾。

  1. # rsync -avzhe ssh /home/daygeek/2g/shell-script/ root@2g.CentOS.com:/opt/backup
  2.  
  3. sending incremental file list
  4. ./
  5. output.txt
  6. ovh.sh
  7. passwd-up.sh
  8. passwd-up1.sh
  9. server-list.txt
  10.  
  11. sent 3.85K bytes received 281 bytes 8.26K bytes/sec
  12. total size is 9.12K speedup is 2.21

方式 5:如何在 Linux 上使用 rsync 命令和 Shell 腳本復(fù)制文件/文件夾到多個(gè)遠(yuǎn)程系統(tǒng)上?

如果你想復(fù)制同一個(gè)文件到多個(gè)遠(yuǎn)程服務(wù)器上,那也需要?jiǎng)?chuàng)建一個(gè)如下面那樣的小 shell 腳本。

  1. # file-copy.sh
  2.  
  3. #!/bin/sh
  4. for server in `more server-list.txt`
  5. do
  6. rsync -avzhe ssh /home/daygeek/2g/shell-script/ root@2g.CentOS.com$server:/opt/backup
  7. done

上面腳本的輸出。

  1. # ./file-copy.sh
  2.  
  3. sending incremental file list
  4. ./
  5. output.txt
  6. ovh.sh
  7. passwd-up.sh
  8. passwd-up1.sh
  9. server-list.txt
  10.  
  11. sent 3.86K bytes received 281 bytes 8.28K bytes/sec
  12. total size is 9.13K speedup is 2.21
  13.  
  14. sending incremental file list
  15. ./
  16. output.txt
  17. ovh.sh
  18. passwd-up.sh
  19. passwd-up1.sh
  20. server-list.txt
  21.  
  22. sent 3.86K bytes received 281 bytes 2.76K bytes/sec
  23. total size is 9.13K speedup is 2.21

方式 6:如何在 Linux 上使用 scp 命令和 Shell 腳本從本地系統(tǒng)向多個(gè)遠(yuǎn)程系統(tǒng)復(fù)制文件/文件夾?

在上面兩個(gè) shell 腳本中,我們需要事先指定好文件和文件夾的路徑,這兒我做了些小修改,讓腳本可以接收文件或文件夾作為輸入?yún)?shù)。當(dāng)你每天需要多次執(zhí)行復(fù)制時(shí),這將會(huì)非常有用。

  1. # file-copy.sh
  2.  
  3. #!/bin/sh
  4. for server in `more server-list.txt`
  5. do
  6. scp -r $1 root@2g.CentOS.com$server:/opt/backup
  7. done

輸入文件名并運(yùn)行腳本。

  1. # ./file-copy.sh output1.txt
  2.  
  3. output1.txt 100% 3558 3.5KB/s 00:00
  4. output1.txt 100% 3558 3.5KB/s 00:00

方式 7:如何在 Linux 系統(tǒng)上用非標(biāo)準(zhǔn)端口復(fù)制文件/文件夾到遠(yuǎn)程系統(tǒng)?

如果你想使用非標(biāo)準(zhǔn)端口,使用下面的 shell 腳本復(fù)制文件或文件夾。

如果你使用了非標(biāo)準(zhǔn)Non-Standard端口,確保像下面 scp 命令那樣指定好了端口號(hào)。

  1. # file-copy-scp.sh
  2.  
  3. #!/bin/sh
  4. for server in `more server-list.txt`
  5. do
  6. scp -P 2222 -r $1 root@2g.CentOS.com$server:/opt/backup
  7. done

運(yùn)行腳本,輸入文件名。

  1. # ./file-copy.sh ovh.sh
  2.  
  3. ovh.sh 100% 3558 3.5KB/s 00:00
  4. ovh.sh 100% 3558 3.5KB/s 00:00

如果你使用了非標(biāo)準(zhǔn)Non-Standard端口,確保像下面 rsync 命令那樣指定好了端口號(hào)。

  1. # file-copy-rsync.sh
  2.  
  3. #!/bin/sh
  4. for server in `more server-list.txt`
  5. do
  6. rsync -avzhe 'ssh -p 2222' $1 root@2g.CentOS.com$server:/opt/backup
  7. done

運(yùn)行腳本,輸入文件名。

  1. # ./file-copy-rsync.sh passwd-up.sh
  2. sending incremental file list
  3. passwd-up.sh
  4.  
  5. sent 238 bytes received 35 bytes 26.00 bytes/sec
  6. total size is 159 speedup is 0.58
  7.  
  8. sending incremental file list
  9. passwd-up.sh
  10.  
  11. sent 238 bytes received 35 bytes 26.00 bytes/sec
  12. total size is 159 speedup is 0.58

 

責(zé)任編輯:龐桂玉 來(lái)源: Linux中國(guó)
相關(guān)推薦

2017-05-03 15:30:38

LinuxMeld比較文件夾

2020-12-28 06:44:45

FedoraLinux RPM文件

2018-01-24 15:35:22

Linux隱藏文件管理器

2014-02-12 10:58:05

Linux文件夾文件權(quán)限

2015-08-05 09:40:26

2019-04-26 09:50:21

Linux壓縮文件文件夾

2018-02-25 09:48:36

LinuxUbuntu文件系統(tǒng)

2016-12-20 09:47:17

Linux命令復(fù)制文件到多個(gè)目錄

2018-01-09 09:00:01

Linux命令文件壓縮

2015-06-01 12:19:03

FedoraCentOSSamba

2019-06-10 08:15:52

Linux命令

2022-08-09 15:30:41

Linux

2019-09-16 11:40:49

Linux交換文件

2023-12-20 22:04:17

Linux二進(jìn)制文件

2014-12-03 08:53:59

eCryptFS加密文件

2021-07-10 11:20:44

FreeDOS歸檔文件

2018-06-25 13:10:16

Linux復(fù)制重命名

2022-07-21 11:31:28

UbuntuWindowsLinux

2023-03-21 09:31:30

Linux命令行文件夾

2014-04-09 10:16:28

Linux文件系統(tǒng)fsck工具
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)