如何在Linux上復(fù)制文件/文件夾到遠(yuǎn)程系統(tǒng)?
從一個(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ù)制的原生命令之一。但本文所列出的其它命令也很好用,建議你嘗試一下。
文件復(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)程 之間復(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
文件夾下。
# scp output.txt root@2g.CentOS.com:/opt/backup
output.txt 100% 2468 2.4KB/s 00:00
從本地系統(tǒng)復(fù)制兩個(gè)文件 output.txt
和 passwd-up.sh
到遠(yuǎn)程系統(tǒng) 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
從本地系統(tǒng)復(fù)制 shell-script
文件夾到遠(yuǎn)程系統(tǒng) 2g.CentOs.com
的 /opt/back
文件夾下。
這會(huì)連同shell-script
文件夾下所有的文件一同復(fù)制到/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 腳本復(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ú)一行。
最終,你想要的腳本就像下面這樣:
# 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
的文件設(shè)置可執(zhí)行權(quán)限。
# chmod +x file-copy.sh
然后運(yùn)行腳本完成復(fù)制。
# ./file-copy.sh
output.txt 100% 2468 2.4KB/s 00:00
output.txt 100% 2468 2.4KB/s 00:00
使用下面的腳本可以復(fù)制多個(gè)文件到多個(gè)遠(yuǎn)程服務(wù)器上。
# 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
下面結(jié)果顯示所有的兩個(gè)文件都復(fù)制到兩個(gè)服務(wù)器上。
# ./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
使用下面的腳本遞歸地復(fù)制文件夾到多個(gè)遠(yuǎn)程服務(wù)器上。
# 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 命令復(fù)制文件/文件夾到多個(gè)遠(yuǎn)程系統(tǒng)上?
pscp
命令可以直接讓我們復(fù)制文件到多個(gè)遠(yuǎn)程服務(wù)器上。
使用下面的 pscp
命令復(fù)制單個(gè)文件到遠(yuǎn)程服務(wù)器。
# pscp.pssh -H 2g.CentOS.com /home/daygeek/2g/shell-script/output.txt /opt/backup
[1] 18:46:11 [SUCCESS] 2g.CentOS.com
使用下面的 pscp
命令復(fù)制多個(gè)文件到遠(yuǎn)程服務(wù)器。
# 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
命令遞歸地復(fù)制整個(gè)文件夾到遠(yuǎn)程服務(wù)器。
# pscp.pssh -H 2g.CentOS.com -r /home/daygeek/2g/shell-script/ /opt/backup
[1] 18:48:46 [SUCCESS] 2g.CentOS.com
使用下面的 pscp
命令使用下面的命令復(fù)制單個(gè)文件到多個(gè)遠(yuǎn)程服務(wù)器。
# 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
命令復(fù)制多個(gè)文件到多個(gè)遠(yuǎn)程服務(wù)器。
# 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
使用下面的命令遞歸地復(fù)制文件夾到多個(gè)遠(yuǎn)程服務(wù)器。
# 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 命令復(fù)制文件/文件夾到多個(gè)遠(yuǎn)程系統(tǒng)上?
rsync
是一個(gè)即快速又出眾的多功能文件復(fù)制工具。它能本地復(fù)制、通過(guò)遠(yuǎn)程 shell 在其它主機(jī)之間復(fù)制,或者在遠(yuǎn)程 rsync
守護(hù)進(jìn)程 之間復(fù)制。
使用下面的 rsync
命令復(fù)制單個(gè)文件到遠(yuǎn)程服務(wù)器。
# 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
命令復(fù)制多個(gè)文件到遠(yuǎn)程服務(wù)器。
# 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
命令通過(guò) ssh
復(fù)制單個(gè)文件到遠(yuǎn)程服務(wù)器。
# 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
命令通過(guò) ssh
遞歸地復(fù)制文件夾到遠(yuǎn)程服務(wù)器。這種方式只復(fù)制文件不包括文件夾。
# 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 腳本復(fù)制文件/文件夾到多個(gè)遠(yuǎn)程系統(tǒng)上?
如果你想復(fù)制同一個(gè)文件到多個(gè)遠(yuǎn)程服務(wù)器上,那也需要?jiǎng)?chuàng)建一個(gè)如下面那樣的小 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 腳本從本地系統(tǒng)向多個(gè)遠(yuǎn)程系統(tǒng)復(fù)制文件/文件夾?
在上面兩個(gè) shell 腳本中,我們需要事先指定好文件和文件夾的路徑,這兒我做了些小修改,讓腳本可以接收文件或文件夾作為輸入?yún)?shù)。當(dāng)你每天需要多次執(zhí)行復(fù)制時(shí),這將會(huì)非常有用。
# file-copy.sh
#!/bin/sh
for server in `more server-list.txt`
do
scp -r $1 root@2g.CentOS.com$server:/opt/backup
done
輸入文件名并運(yùn)行腳本。
# ./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 系統(tǒng)上用非標(biāo)準(zhǔn)端口復(fù)制文件/文件夾到遠(yuǎn)程系統(tǒng)?
如果你想使用非標(biāo)準(zhǔn)端口,使用下面的 shell 腳本復(fù)制文件或文件夾。
如果你使用了非標(biāo)準(zhǔn)端口,確保像下面 scp
命令那樣指定好了端口號(hào)。
# 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
運(yùn)行腳本,輸入文件名。
# ./file-copy.sh ovh.sh
ovh.sh 100% 3558 3.5KB/s 00:00
ovh.sh 100% 3558 3.5KB/s 00:00
如果你使用了非標(biāo)準(zhǔn)端口,確保像下面 rsync
命令那樣指定好了端口號(hào)。
# 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
運(yùn)行腳本,輸入文件名。
# ./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