1. 程式人生 > >Linux學習-1031(rsync同步工具 上)

Linux學習-1031(rsync同步工具 上)

 

 

 10.28 rsync工具介紹

10.29/10.30 rsync常用選項

10.31 rsync通過ssh同步

一、 rsync工具介紹

    rsync是一個同步工具,在日常的運維中常會用到。它可以本地同步,也實現可以遠端兩臺機器同步。

     比如有個需求:有A、B目錄兩個目錄,A目錄檔案一直在不定時的增加,想要把A目錄裡面的資料拷貝到B目錄下去。並且要求每小時拷貝一次,如果用cp來實現只能覆蓋,並且浪費磁碟IO。這種情況就可以使用rsync,它可以增量的同步。

    rsync安裝:

    yum -y install rsync

    rsync格式:

 rsync [OPTION] … SRC   DEST    //OPTION為引數,SRC源,DEST目標
 rsync [OPTION] … SRC   [[email protected]]host:DEST  //遠端拷貝,如果不寫user就預設使用當前的使用者
 rsync [OPTION] … [[email protected]

]host:SRC   DEST  // 反向拷貝,把遠端機器上的檔案或目錄拷到本地
 rsync [OPTION] … SRC   [[email protected]]host::DEST // 多了::  ,後面在講
 rsync [OPTION] … [[email protected]]host::SRC   DEST 

    rsync示例:

1、/etc/passwd 拷貝到/tmp/目錄下並且改名為1.txt

    rsync -av /etc/passwd /tmp/1.txt

    -a:後面在說

    -v:視覺化顯示,如:傳輸速度、檔案大小

[[email protected] system]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd

sent 1,574 bytes  received 35 bytes  3,218.00 bytes/sec
total size is 1,482  speedup is 0.92
[[email protected] system]# 

2、遠端拷貝

rsync -av /etc/passwd [email protected]:/tmp/

[[email protected] home]# rsync -av /etc/passwd [email protected]:/tmp/
[email protected]'s password: 
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(638) [sender=3.1.2]
[[email protected] home]# 
[[email protected] home]# rsync -av /etc/passwd [email protected]:/tmp/
[email protected]'s password: 
sending incremental file list
passwd

sent 1,568 bytes  received 34 bytes  246.46 bytes/sec
total size is 1,482  speedup is 0.93
[[email protected] home]# 

 

   

二、rsync常用選項

  • -a 包含-rtplgoD
  •  -r 同步目錄時要加上,類似cp時的-r選項
  •  -v 同步時顯示一些資訊,讓我們知道同步的過程
  •  -l 保留軟連線
  •  -L 加上該選項後,同步軟連結時會把原始檔給同步
  •  -p 保持檔案的許可權屬性
  •  -o 保持檔案的屬主
  •  -g 保持檔案的屬組
  •  -D 保持裝置檔案資訊
  •  -t 保持檔案的時間屬性
  •  --delete 刪除DEST中SRC沒有的檔案
  •  --exclude 過濾指定檔案,如--exclude “logs”會把檔名包含logs的檔案或者目錄過濾掉,不同步
  •  -P 顯示同步過程,比如速率,比-v更加詳細
  •  -u 加上該選項後,如果DEST中的檔案比SRC新,則不同步
  •  -z 傳輸時壓縮

 

示例:

 1、rsync同步目錄

     rsync -av /root/test/ /tmp/test_dest/

      同步目錄時,檔案結尾不能少了"/"

[[email protected] ~]# rsync -av /root/test/ /tmp/test_dest/
sending incremental file list
created directory /tmp/test_dest
./
1.txt
2.cap
sendmail

sent 1,598,652 bytes  received 113 bytes  3,197,530.00 bytes/sec
total size is 1,598,002  speedup is 1.00

加上-L 引數後,會把引數 -l 的含義給覆蓋掉。-L會把軟連結所指向的原始檔給拷貝過去

 

2、刪除目標中原始檔中沒有的內容

rsync -av --delete /root/test/ /tmp/test_dest/

test_dest目標目錄中有666.txt,但是源目錄test目錄中沒有這個檔案。通過

 

3、過濾檔案,過濾掉所有.txt檔案

    rsync -av --exclude  "*.txt" /root/test/ /tmp/test_dest/

[[email protected] tmp]# rsync -av --exclude  "*.txt" /root/test/ /tmp/test_dest/
sending incremental file list

sent 110 bytes  received 12 bytes  244.00 bytes/sec
total size is 250,208  speedup is 2,050.89
[[email protected] tmp]# ls /tmp/test_dest/
2.cap  sendmail

可以支援多此過濾:

rsync -avL --exclude "*.txt" --exclude="11*"  /root/test/ /tmp/test_dest/

4、-P 引數, 顯示同步過程

    rsync -avP    /root/test/ /tmp/test_dest/

[[email protected] tmp]# rsync -avP    /root/test/ /tmp/test_dest/
sending incremental file list
created directory /tmp/test_dest
./
1.txt
      1,347,794 100%  179.16MB/s    0:00:00 (xfr#1, to-chk=3/5)
1111.txt -> /tmp/1.txt
2.cap
          2,360 100%  329.24kB/s    0:00:00 (xfr#2, to-chk=1/5)
sendmail
        247,848 100%   26.26MB/s    0:00:00 (xfr#3, to-chk=0/5)

sent 1,598,687 bytes  received 116 bytes  3,197,606.00 bytes/sec
total size is 1,598,012  speedup is 1.00
[[email protected] tmp]# 

 

5、-u引數 ,目標中的檔案比源新,則不同步

    修改/tmp/test_dest/1.txt檔案

    

    加上-u引數,再執行同步發現1.txt並沒有被之前的檔案覆蓋掉

    

    6、-z 引數,壓縮傳輸

        如果傳輸檔案比較大可以加上這個引數

        rsync -avz    /root/test/ /tmp/test_dest/

五、rsync通過ssh同步

  • rsync -av test1/ 192.168.133.132:/tmp/test2/
  • rsync -av -e "ssh -p 22" test1/ 192.168.133.132:/tmp/test2/

    兩臺機器,可以ping通 並且都安裝了rsrnc包

    遠端機器同步:

    rsync  -av /etc/passwd 193.112.38.222:/tmp/test222

    輸入222這個伺服器的密碼就可以進行同步了

[[email protected] ~]# rsync  -av /etc/passwd 193.112.38.222:/tmp/test222
[email protected]'s password: 
sending incremental file list
passwd

sent 1,568 bytes  received 34 bytes  188.47 bytes/sec
total size is 1,482  speedup is 0.93

將遠端機器上的檔案拉取到本地:

 rsync  -av 193.112.38.222:/tmp/test222  /tmp/test333

[[email protected] ~]# rsync -avP 193.112.38.222:/tmp/test222 /tmp/test333
[email protected]'s password: 
receiving incremental file list
test222
          1,482 100%    1.41MB/s    0:00:00 (xfr#1, to-chk=0/1)

sent 30 bytes  received 1,562 bytes  212.27 bytes/sec
total size is 1,482  speedup is 0.93
[[email protected] ~]# ls /tmp/test333 
/tmp/test333

指定埠進行傳輸:

如果對方機器不是預設的22埠,可以-e指定埠進行傳輸

rsync  -av  -e "ssh -p 22" /etc/passwd 193.112.38.222:/tmp/test222