1. 程式人生 > 其它 >使用SCP或Rsync實現Linux主機之間檔案、目錄的複製

使用SCP或Rsync實現Linux主機之間檔案、目錄的複製

我們知道Linux本機的檔案拷貝可以使用cp命令,它不能在Linux主機之間拷貝資料。本文介紹SCP和Rsync這兩種實現Linux主機間的資料拷貝工具。

@

目錄

SCP 和 Rsync區別

SCP(secure copy) 是基於ssh協議的安全拷貝,用於將檔案/目錄安全地從本地主機傳輸到遠端主機。

Rsync (remote synchronize)也可以實現同步本地主機和遠端主機的檔案/目錄,和SCP不同之處在於,首次複製時,Rsync會複製整個目錄,在後面的複製中,不會複製相同的內容,只對差異檔案做更新,scp是把所有檔案都複製過去。Rsync廣泛用於備份和映象。

下面介紹它們的簡單使用方法。

SCP

一般情況下Linux伺服器都有scp命令,如果沒有,可通過如下方式安裝:

yum -y install openssh-clients # centos
apt-get install openssh-client # Ubuntu

複製檔案/目錄到遠端主機

scp source_file_name user@destination_host:destination_folder # 複製檔案
scp -r source_directory user@destination_host:destination_folder # 複製目錄

案例1:複製檔案到遠端主機

[root@Client ~]# scp text.txt [email protected]:/root
The authenticity of host '192.168.20.40 (192.168.20.40)' can't be established.
ECDSA key fingerprint is SHA256:tS6tueeKp9vBLDvxgsxIgCCaGMQWs9+5E167qz2ZB9c.
ECDSA key fingerprint is MD5:82:04:10:14:57:52:0a:05:d9:9b:ae:6e:3f:3f:68:98.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.20.40' (ECDSA) to the list of known hosts.
[email protected]'s password: 
test.txt                                                                                                                     100%   12     6.0KB/s   00:00    
[root@Client ~]# 

從遠端主機複製檔案/目錄

scp user@source_host:source_file_name local_destination_folder # 複製檔案
scp -r user@source_host:source_file_name local_destination_folder # 複製目錄

案例2:從遠端主機複製檔案

[root@Client ~]# scp [email protected]:/root/test40.txt /root
[email protected]'s password: 
test40.txt                                                                                                                   100%   12     4.2KB/s   00:00    
[root@Client ~]# ll | grep test40.txt
-rw-r--r--   1 root    root          12 7月   6 09:41 test40.txt
[root@Client ~]# 

-r引數用於遞迴的複製整個目錄,SCP更多的引數使用方法可參考:https://manned.org/scp.1

本地檔案/目錄複製

如果不指定遠端主機地址,可以實現和cp目錄一樣的功能:

scp source_file destination_folder # 複製檔案
scp -r source_directory destination_folder # 複製目錄

案例3:本地檔案複製

[root@Client ~]# mkdir test
[root@Client ~]# scp test40.txt /root/test
[root@Client ~]# ll /root/test/
總用量 4
-rw-r--r-- 1 root root 12 7月   6 09:49 test40.txt
[root@Client ~]# 

Rsync

安裝方法:

yum install rsync      # centos
apt-get install rsync  # Ubuntu

選項引數

Rsync可用的選項引數很多,下面介紹幾個常用引數,更多引數使用方法可參考https://manned.org/rsync.1 ,或者使用rsync -hman rsync命令檢視文件說明。

選項 功能
-t 將原始檔的修改時間(modify time)同步到目標機器
-I --ignore-times
-r 遞迴,用於目錄複製
-a 遞迴同步,還可以同步元資訊(比如修改時間、許可權等)
-v 列印複製過程
-l 拷貝符號連線
--delete 刪除目標目錄中多餘的檔案,也就是保持兩個目錄相同,使得目標目錄成為源目錄的映象副本

複製檔案/目錄到遠端主機

如果複製的目標目錄不存在,會自動建立,語法格式和SCP一樣:

rsync source_file_name/ user@destination_host:destination_folder # 複製檔案
rsync -r source_file_name/ user@destination_host:destination_folder # 複製目錄

案例1:複製檔案、目錄到遠端主機

[root@Client ~]# rsync test.txt [email protected]:/root
[email protected]'s password: 
[root@Client ~]# 
[root@Client ~]# rsync -rvl test/ [email protected]:/root/test222
[email protected]'s password: 
sending incremental file list
created directory /root/test222
./
test2.txt
test40.txt

sent 187 bytes  received 93 bytes  62.22 bytes/sec
total size is 12  speedup is 0.04

從遠端主機複製檔案/目錄

rsync user@source_host:source_file_name local_destination_folder # 複製檔案
rsync -r user@source_host:source_file_name local_destination_folder # 複製目錄

案例1:複製遠端主機檔案到本機

[root@Client ~]# rsync [email protected]:/root/test40.txt /root
[email protected]'s password: 
[root@Client ~]# ll test40.txt
-rw-r--r-- 1 root root 12 7月   8 11:11 test40.txt
[root@Client ~]# 

其它用法

複製指定型別的檔案

僅複製py檔案:

rsync *.py user@destination_host:destination_folder

複製多個檔案

[root@Client ~]# rsync test1.txt test2.txt test{5,6,7}.txt [email protected]:/root
[email protected]'s password: 
[root@Client ~]# 

遠端主機上檢視是否複製成功

[root@Server ~]# ls | grep -E "test[0-9]{1}.txt"
test1.txt
test2.txt
test5.txt
test6.txt
test7.txt
[root@Server ~]# 

從遠端主機複製多個檔案(先刪除本地檔案):

[root@Client ~]# rsync [email protected]:/root/test1.txt :test2.txt [email protected]:test{5,6,7}.txt /root
[email protected]'s password: 
[root@Client ~]# ls | grep -E "test[0-9]{1}.txt"
test1.txt
test2.txt
test5.txt
test6.txt
test7.txt

本地檔案/目錄複製

和scp命令一樣,rsync也可以用於在本機進行檔案複製。

rsync source_file destination_folder # 複製檔案
rsync -r source_directory destination_folder # 複製目錄

總結

rsync工具只對差異檔案做更新的特性,在多個伺服器之間同步檔案非常有用,通常跳過寫一個自動化指令碼來實現批量同步,但是,你也許已經發現了,在執行同步命令時,需要輸入目標主機的密碼,在主機很多的情況下就不方便了。

一種解決方案是可以使用expect實現自動化互動,另一種方法是配置伺服器之間ssh免密登入,因為scp和rsync預設使用ssh協議。ssh免密登入配置方法可參考配置多臺伺服器之間ssh免密登入

--THE END--

歡迎關注公眾號:「測試開發小記」及時接收最新技術文章!