Linux之遠程文件傳輸


 

1)scp

scp命令用於在Linux下進行遠程拷貝文件的命令,和它類似的命令有cp,不過cp只是在本機進行拷貝不能跨服務器,而且scp傳輸是加密的。可能會稍微影響一下速度。當你服務器硬盤變為只讀read only system時,用scp可以幫你把文件移出來。另外,scp還非常不占資源,不會提高多少系統負荷,在這一點上,rsync就遠遠不及它了。雖然 rsync比scp會快一點,但當小文件眾多的情況下,rsync會導致硬盤I/O非常高,而scp基本不影響系統正常使用。

  

參數:
-1:使用ssh協議版本1;
-2:使用ssh協議版本2;
-4:使用ipv4;
-6:使用ipv6;
-B:以批處理模式運行;
-C:使用壓縮;
-F:指定ssh配置文件;
-l:指定寬帶限制;
-o:指定使用的ssh選項;
-P:指定遠程主機的端口號;
-p:保留文件的最后修改時間,最后訪問時間和權限模式;
-q:不顯示復制進度;
-r:以遞歸方式復制。

  示例:

從遠程復制到本地的scp命令與上面的命令雷同,只要將從本地復制到遠程的命令后面2個參數互換順序就行了。



從遠處復制文件到本地目錄
scp root@10.10.10.10:/opt/soft/nginx-0.5.38.tar.gz /opt/soft/
從10.10.10.10機器上的/opt/soft/的目錄中下載nginx-0.5.38.tar.gz 文件到本地/opt/soft/目錄中。

從遠處復制到本地
scp -r root@10.10.10.10:/opt/soft/mongodb /opt/soft/
從10.10.10.10機器上的/opt/soft/中下載mongodb目錄到本地的/opt/soft/目錄來。


scp -C -P 8932 /usr/local/openresty/nginx/logs/access.log.gz jiaoni@172.16.114.47:/home/jiaoni/lanlang

 

 

2)ssh登陸

遠程登錄服務器

ssh -l root -p 188 192.168.1.2
ssh -l 用戶名 -p 端口號 主機名或IP
輸入密碼即可

 

 

3)rsync

速度:第一次的rsync同步源的整個內容目錄到目標位置。之后,rsync只傳遞改變的塊或字節到目標位置,使傳輸非常快。
安全:rsync在傳輸中允許使用ssh協議加密數據。
更少的帶寬:rsync分別在發送和接收端對數據進行壓縮和解壓縮。所以在使用rsync傳輸數據時使用的帶寬總是比使用其他文件傳輸協議來的少。
特權:不需要特殊權限來安裝和執行rsync

$ rsync options source destination
source和destination可以是本地或遠程。在遠程的情況下,需要指定登錄名、遠程服務器的名稱和位置。

 

1. 在本地服務器上同步兩個目錄

用rsync – zvr在本地服務器上同步兩個目錄

$ rsync -zvr /var/opt/installation/inventory/ /root/temp
building file list ... done
sva.xml
svB.xml
.
sent 26385 bytes  received 1098 bytes  54966.00 bytes/sec
total size is 44867  speedup is 1.63

 

在上面rsync例子中:
• –z:允許壓縮
• –v:冗長
• –r:遞歸
注:Rsync不會同步源文件或目錄的時間戳

$ ls -l /var/opt/installation/inventory/sva.xml /root/temp/sva.xml
-r--r--r-- 1 bin  bin  949 Jun 18  2009 /var/opt/installation/inventory/sva.xml
-r--r--r-- 1 root bin  949 Sep  2  2009 /root/temp/sva.xml

 

2. 用rsync –a. 同步時間戳

rsync的-a選項是archive的意思,加了之后有以下作用:
• 遞歸模式
• 同步軟鏈接
• 同步權限
• 同步時間戳
• 同步屬主和屬組
現在,我們執行與例1一樣的命令,不同的是使用-a選項。

$ rsync -azv /var/opt/installation/inventory/ /root/temp/
building file list ... done
./
sva.xml
svB.xml
.
sent 26499 bytes  received 1104 bytes  55206.00 bytes/sec
total size is 44867  speedup is 1.63

 

我們可以看到,rsync將源目錄下文件的時間戳也同步過來了。

$ ls -l /var/opt/installation/inventory/sva.xml /root/temp/sva.xml
-r--r--r-- 1 root  bin  949 Jun 18  2009 /var/opt/installation/inventory/sva.xml
-r--r--r-- 1 root  bin  949 Jun 18  2009 /root/temp/sva.xml

 

3. 只同步一個文件

指定文件名稱,只同步一個文件

$ rsync -v /var/lib/rpm/Pubkeys /root/temp/
Pubkeys

sent 42 bytes  received 12380 bytes  3549.14 bytes/sec
total size is 12288  speedup is 0.99

 

4. 將本地文件同步到遠程服務器

當同步到遠程服務器時,我們需要指定用戶名、遠程服務器的IP地址和目標路徑。格式為:username@remote_server_ip:path

$ rsync -avz /root/temp/ linuxprobe@192.168.200.10:/home/linuxprobe/temp/
Password:
building file list ... done
./
rpm/
rpm/Basenames
rpm/Conflictname

sent 15810261 bytes  received 412 bytes  2432411.23 bytes/sec
total size is 45305958  speedup is 2.87

 

5. 將遠程文件同步到本地服務器

$ rsync -avz linuxprobe@192.168.200.10:/var/lib/rpm /root/temp
Password:
receiving file list ... done
rpm/
rpm/Basenames
.
sent 406 bytes  received 15810230 bytes  2432405.54 bytes/sec
total size is 45305958  speedup is 2.87

 

6. 指定使用某一shell同步

遠程同步時,Rsync可以指定使用某一shell進行安全傳輸。如:使用rsync –e ssh選擇ssh進行安全連接。

$ rsync -avz -e ssh linuxprobe@192.168.200.10:/var/lib/rpm /root/temp
Password:
receiving file list ... done
rpm/
rpm/Basenames

sent 406 bytes  received 15810230 bytes  2432405.54 bytes/sec
total size is 45305958  speedup is 2.87

 

7. 不覆蓋目標位置已改變的文件

如果目標位置的文件已被修改,而我們不希望舊文件覆蓋它時,可以使用-u選項。下面例子中,目標位置的文件Basenames已經被修改,使用-u 選項是它不被舊文件覆蓋。

$ ls -l /root/temp/Basenames
total 39088
-rwxr-xr-x 1 root root        4096 Sep  2 11:35 Basenames

$ rsync -avzu linuxprobe@192.168.200.10:/var/lib/rpm /root/temp
Password:
receiving file list ... done
rpm/

sent 122 bytes  received 505 bytes  114.00 bytes/sec
total size is 45305958  speedup is 72258.31

$ ls -lrt
total 39088
-rwxr-xr-x 1 root root        4096 Sep  2 11:35 Basenames

 

8. 只同步目錄樹結構(不包括文件)

使用rsync - d選項只將目錄樹從源位置同步至目標位置。下面的示例中,以遞歸方式只同步目錄樹而不同步目錄中的文件。

$ rsync -v -d linuxprobe@192.168.200.10:/var/lib/ .
Password:
receiving file list ... done
logrotate.status
CAM/
YaST2/
acpi/

sent 240 bytes  received 1830 bytes  318.46 bytes/sec
total size is 956  speedup is 0.46

 

9. 查看rsync進度

使用rsync –progress選項可以查看rsync進度,如多少文件已經被拷貝,完成率是多少等

$ rsync -avz --progress linuxprobe@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receiving file list ...
19 files to consider
./
Basenames
     5357568 100%   14.98MB/s    0:00:00 (xfer#1, to-check=17/19)
Conflictname
       12288 100%   35.09kB/s    0:00:00 (xfer#2, to-check=16/19)
.
.
.
sent 406 bytes  received 15810211 bytes  2108082.27 bytes/sec
total size is 45305958  speedup is 2.87

 

10. 刪除目標位置創建的文件

若我們希望rsync時刪除在源位置不存在而在目標位置存在的文件,可以使用-delete選項。
此例中,我們在目標位置創建了一個新文件名為new-file.txt.,在同步過程中,-delete選項刪除了new-file.txt文件

# Source and target are in sync. Now creating new file at the target.
$ > new-file.txt

$ rsync -avz --delete linuxprobe@192.168.200.10:/var/lib/rpm/ .
Password:
receiving file list ... done
deleting new-file.txt
./

sent 26 bytes  received 390 bytes  48.94 bytes/sec
total size is 45305958  speedup is 108908.55

 

11. 在目標位置不創建新文件

使用-existing選項使得在同步時只同步目標位置存在的文件,而不創建新文件。

#在源位置創建新文件new-file.txt
[/var/lib/rpm ]$ > new-file.txt
#同步目錄
$ rsync -avz --existing root@192.168.1.2:/var/lib/rpm/ .
root@192.168.1.2's password:
receiving file list ... done
./

sent 26 bytes  received 419 bytes  46.84 bytes/sec
total size is 88551424  speedup is 198991.96

 

12.查看源和目標的不同文件或目錄

源位置:

$ ls -l /var/lib/rpm
-rw-r--r-- 1 root root  5357568 2010-06-24 08:57 Basenames
-rw-r--r-- 1 root root    12288 2008-05-28 22:03 Conflictname
-rw-r--r-- 1 root root  1179648 2010-06-24 08:57 Dirnames

目標位置:

$ ls -l /root/temp
-rw-r--r-- 1 root root    12288 May 28  2008 Conflictname
-rw-r--r-- 1 bin  bin   1179648 Jun 24 05:27 Dirnames
-rw-r--r-- 1 root root        0 Sep  3 06:39 Basenames

源和目標之間有兩個不同之處:1. 文件Dirnames屬主和屬組不同 2. 文件Basenames的大小不同
使用-i選項來顯示不同之處,如下所示

$ rsync -avzi linuxprobe@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receiving file list ... done
>f.st.... Basenames
.f....og. Dirnames

sent 48 bytes  received 2182544 bytes  291012.27 bytes/sec
total size is 45305958  speedup is 20.76
In the output it displays some 9 letters in front of the file name or directory name indicating the changes.

 

可以看到,在文件名或目錄名前有9個字符來代表不同之處。此例中,Basenames和Dirnames前的字符代表含義如下:

> 代表文件已被傳輸到本地主機。
f:代表這是個文件
s:代表文件大小發生變化
t:代表時間戳發生變化
o:屬主發生變化
g:屬組發生變化

13. 在傳輸時指定包括或排除某些文件
只傳輸’P’開頭的文件或目錄(使用rsync include),不傳輸其他任何文件(使用rsync exclude)

$ rsync -avz --include 'P*' --exclude '*' linuxprobe@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receiving file list ... done
./
Packages
Providename
Provideversion
Pubkeys

sent 129 bytes  received 10286798 bytes  2285983.78 bytes/sec
total size is 32768000  speedup is 3.19

 

14. 不傳輸大文件

使用rsync –max-size選項后,rsync將不傳輸大於指定大小的文件

$ rsync -avz --max-size='100K' linuxprobe@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receiving file list ... done
./
Conflictname
Group
Installtid
Name
Sha1header
Sigmd5
Triggername

sent 252 bytes  received 123081 bytes  18974.31 bytes/sec
total size is 45305958  speedup is 367.35

 

15. 傳輸整個文件

rsync的主要特征之一是它只傳輸改變的塊到目標位置,而不是傳輸整個文件。如果網絡帶寬對你不是問題(CPU有),您可以使用-w選項來傳輸整個文件。它會加速rsync過程,因為它不用在源和目標位置執行校驗和。

#  rsync -avzW  linuxprobe@192.168.200.10:/var/lib/rpm/ /root/temp
Password:
receiving file list ... done
./
Basenames
Conflictname
Dirnames
Filemd5s
Group
Installtid
Name

sent 406 bytes  received 15810211 bytes  2874657.64 bytes/sec
total size is 45305958  speedup is 2.87

 



總結: 使用遠程文件傳輸
rsync -avzW --progress 源目錄  目標目錄
參數: a保持屬性
        v. 長格式
        z 壓縮

    --progress. 顯示進度

 














免責聲明!

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



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