rsync工具的介紹與經常使用選項,rsync經過ssh同步

rsync工具介紹

一個系統管理員,數據備份是必不可少,在Linux系統下數據備份的工具不少,其中重點介紹就是rsync工具,rsync不只能夠遠程同步數據,還能夠本地同步數據,且不會覆蓋之前的數據在已經存在的數據狀況下,而是先判斷已經存在的數據和新的數據差別,只有不一樣的時候纔會把不一樣的部分覆蓋。vim

如下舉個例子;bash

[root@ask-02 ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd服務器

sent 1428 bytes received 31 bytes 2918.00 bytes/sec
total size is 1354 speedup is 0.93ssh

能夠看到上列中把/etc/passwd複製到了tmp目錄下並更名爲1.txtide

關於rsync的命令格式工具

  • rsync [OPTION]... SRC DEST //將數據同步(複製)到本地指定的路徑下。
  • rsync [OPTION]... SRC [USER@]HOST:DEST //將文件複製遠程同步到指定的用戶的指定路徑下,上面的例子沒有指定user默認就是root。
  • rsync [OPTION]... [USER@]HOST:SRC DEST //從遠程目錄同步數據到本地。
  • rsync [OPTION]... [USER@]HOST::SRC DEST //從遠程目錄同步數據到本地,加了兩個冒號驗證方式不一樣。
  • rsync [OPTION]... SRC [USER@]HOST::DEST //將文件複製遠程同步到指定的用戶的指定路徑下,加了兩個冒號驗證方式不一樣。

rsync經常使用選項

rsync命令各項的含義以下遞歸

  • -a:這是歸檔模式,表示已遞歸方式傳輸文件,並保持全部屬性,它等同於 -rlptgoD。-a選項後面能夠跟一個 --no-OPTION,表示關閉 -rlptgoD中的某一個,好比 -a--no-l等同 -rlptgoD。
  • -r:表示已遞歸模式處理子目錄。它主要是針對目錄來講的,若是單獨傳一個文件不須要加-r選項,可是傳輸目錄時必須加。
  • -v:表示打印一些信息,好比文件列表、文件數量等。
  • -l:表示保留軟連接。
  • -L:表示像對待常規文件同樣處理軟鏈接。若是是SRC中有軟鏈接文件,則加上該選項後,將會把軟鏈接指向的目標文件複製到DST。
  • -p:表示保持文件權限。
  • -o:表示保持文件屬主信息。
  • -g:表示保持文件屬組信息。
  • -D:表示保持設備文件信息。
  • -t:表示保持文件時間信息。
  • --delete:表示刪除DST中SRC沒有的文件。
  • --exclude=PATTERN:表示指定排除不須要傳輸的文件,等號後面跟文件名,能夠是通用字符模式(好比*.txt)。
  • -P(--progress):表示在同步的過程當中能夠看到同步的過程狀態,好比統計要同步的文件數量、同步的文件傳輸速度等。
  • -u:表示把DST中比SRC還新的文件排除掉,不會覆蓋。
  • -z:加上該選項,將會在傳輸過程當中壓縮。

經常使用選項示例演示rem

首先建立須要演示的文件;同步

[root@ask-02 ~]# mkdir rsync-Z
[root@ask-02 ~]# cd rsync-Z/
[root@ask-02 rsync-Z]# mkdir test1
[root@ask-02 rsync-Z]# cd test1/
[root@ask-02 test1]# touch 1 2 3 /root/123.txt
[root@ask-02 test1]# ln -s /root/123.txt ./123.txt
[root@ask-02 test1]# ls -l
總用量 0
-rw-r--r--. 1 root root 0 1月 30 16:33 1
lrwxrwxrwx. 1 root root 13 1月 30 16:34 123.txt -> /root/123.txt
-rw-r--r--. 1 root root 0 1月 30 16:33 2
-rw-r--r--. 1 root root 0 1月 30 16:33 3it

使用-av選項;

[root@ask-02 rsync-Z]# rsync -av test1/ /tmp/test2/
sending incremental file list
created directory /tmp/test2
./
1
123.txt -> /root/123.txt
2
3

sent 209 bytes received 75 bytes 568.00 bytes/sec
total size is 13 speedup is 0.05
[root@ask-02 rsync-Z]# ls -l /tmp/test2/
總用量 0
-rw-r--r--. 1 root root 0 1月 30 16:33 1
lrwxrwxrwx. 1 root root 13 1月 30 16:34 123.txt -> /root/123.txt
-rw-r--r--. 1 root root 0 1月 30 16:33 2
-rw-r--r--. 1 root root 0 1月 30 16:33 3

以上示例中我給目錄後都加了斜槓,這是由於若是你不加斜槓,命令會自動建立一個test2目錄,而後將test1目錄放進test2中去,加了斜槓便解決這個問題,因此之後要養成加斜槓的習慣。

使用-L選項;

[root@ask-02 rsync-Z]# rm -rf /tmp/test2
[root@ask-02 rsync-Z]# rsync -avL test1/ /tmp/test2/
sending incremental file list
created directory /tmp/test2
./
1
123.txt
2
3

sent 223 bytes received 91 bytes 628.00 bytes/sec
total size is 0 speedup is 0.00
[root@ask-02 rsync-Z]# ls -l /tmp/test2/
總用量 0
-rw-r--r--. 1 root root 0 1月 30 16:33 1
-rw-r--r--. 1 root root 0 1月 30 16:33 123.txt
-rw-r--r--. 1 root root 0 1月 30 16:33 2
-rw-r--r--. 1 root root 0 1月 30 16:33 3

上列加上-L選項便把SRC中軟鏈接的目標文件複製到DST

使用--delete選項;

[root@ask-02 rsync-Z]# touch /tmp/test2/new.txt
[root@ask-02 rsync-Z]# ls /tmp/test2/
1 123.txt 2 3 new.txt
[root@ask-02 rsync-Z]# rsync -avL --delete test1/ /tmp/test2/
sending incremental file list
./
deleting new.txt

sent 67 bytes received 15 bytes 164.00 bytes/sec
total size is 0 speedup is 0.00
[root@ask-02 rsync-Z]# ls !$
ls /tmp/test2/
1 123.txt 2 3

若是在DST增長文件,而SRC當中沒有這些文件,同步時加上--delete選項後就會刪除新增的文件

使用--exclude選項;

[root@ask-02 rsync-Z]# touch test1/4
[root@ask-02 rsync-Z]# ls test1/
1 123.txt 2 3 4
[root@ask-02 rsync-Z]# rsync -avL --exclude="4" test1/ test2/
sending incremental file list
created directory test2
./
1
123.txt
2
3

sent 223 bytes received 91 bytes 628.00 bytes/sec
total size is 0 speedup is 0.00
[root@ask-02 rsync-Z]# ls test2
1 123.txt 2 3**

該選項還支持匹配字符(*)使用;

[root@ask-02 rsync-Z]# ls test1
1 123.txt 2 3 345.txt 4 567.txt
[root@ask-02 rsync-Z]# rsync -avL --exclude="*.txt" test1/ /tmp/test2/
sending incremental file list
created directory /tmp/test2
./
1
2
3
4

sent 221 bytes received 91 bytes 624.00 bytes/sec
total size is 0 speedup is 0.00
[root@ask-02 rsync-Z]# ls /tmp/test2/
1 2 3 4

使用-P(--progress)選項;

[root@ask-02 rsync-Z]# rm -rf /tmp/test2/
[root@ask-02 rsync-Z]# rsync -avP test1/ /tmp/test2/
sending incremental file list
created directory /tmp/test2
./
1
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=6/8)
123.txt -> /root/123.txt
2
0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=4/8)
3
0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=3/8)
345.txt
0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=2/8)
4
0 100% 0.00kB/s 0:00:00 (xfer#5, to-check=1/8)
567.txt
0 100% 0.00kB/s 0:00:00 (xfer#6, to-check=0/8)

sent 372 bytes received 132 bytes 1008.00 bytes/sec
total size is 13 speedup is 0.03

加上-P觀察同步過程狀態。

使用-u選項;

[root@ask-02 rsync-Z]# vim /tmp/test2/567.txt
[root@ask-02 rsync-Z]# cat /tmp/test2/567.txt
hello world
[root@ask-02 rsync-Z]# rsync -avPu test1/ /tmp/test2/
sending incremental file list
./

sent 119 bytes received 15 bytes 268.00 bytes/sec
total size is 14 speedup is 0.10
[root@ask-02 rsync-Z]# cat test1/567.txt

[root@ask-02 rsync-Z]# cat /tmp/test2/567.txt
hello world

加上-u選項後,能夠看到目標文件的心內容不會被源目錄文件的覆蓋。

rsync經過ssh同步

前面介紹到的rsync5中命令格式中,第二種和三種(使用一個冒號)就屬於經過ssh的方式備份數據,這種方式其實就是讓用戶登陸到遠程服務器上執行rsync的任務。

[root@ask-02 rsync-Z]# rsync -av /etc/passwd 192.168.74.128:/tmp/test1.txt
root@192.168.74.128's password:
sending incremental file list
passwd

sent 1428 bytes received 31 bytes 224.46 bytes/sec
total size is 1354 speedup is 0.93

若是你的另一臺服務器尚未安裝rsync工具須要先進行安裝

[root@ask-01 ~]# yum install -y rsync

這時你一個cat如下目標服務器上/tmp/下的test1.txt文件,這裏名字是自定義的,你能夠命名爲你熟知的名字

[root@ask-01 ~]# cat /tmp/test1.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

固然還能夠從遠程將文件發送到本機上

[root@ask-02 rsync-Z]# rsync -avP 192.168.74.128:/tmp/test1.txt /tmp/cs1.txt
root@192.168.74.128's password:
receiving incremental file list
test1.txt
1354 100% 1.29MB/s 0:00:00 (xfer#1, to-check=0/1)

sent 30 bytes received 1436 bytes 101.10 bytes/sec
total size is 1354 speedup is 0.92

一樣的你能夠cat如下本機/tmp/目錄下的cs1.txt文件檢查一下。
若是你的另一臺服務器端口並非22,那麼你能夠加個選項來操做

[root@ask-02 rsync-Z]# rsync -avP -e "ssh -p 22" /etc/passwd/ 192.168.74.128:/tmp/cs1.txt

加上-e選項後面跟遠程命令設定端口。

相關文章
相關標籤/搜索