rsync同步工具學習筆記

rsync同步工具

1、rsync介紹

rsync是一款開源的、快速的、多功能的、可實現全量及增量的本地或遠程數據同步備份的優秀工具。rsync軟件適用於unix/linux/windows等多種操作系統平臺。

rsync和ssh帶的scp命令比較相似,但又優於scp命令的功能,scp每次都是全量拷貝,而rsync可以進行增量拷貝。當然,rsync還可以在本地主機的不同分區或目錄之間全量及增量的複製數據,這又類似cp命令,但同樣也優於cp命令,cp每次都是全量拷貝,而rsync可以增量拷貝.利用rsync還可以實現刪除文件和目錄功能,這又相當於rm命令

2、rsync特性

  • 支持拷貝特殊文件如鏈接文件,設備等。
  • 可以有排除指定文件或目錄同步的功能,相當於打包命令tar的排除功能。
  • 可以做到保持原文件或目錄的權限、時間、軟硬鏈接、屬主、組等所有屬性均不改變。
  • 可實現增量同步,即可同步發生變化的數據,因此數據傳輸效率很高。
  • 可以使用rcp,rsh,ssh等方式來配合傳輸文件(rsync本身不對數據加密)。
  • 可以通過socket傳輸文件和數據。
  • 支持匿名的或認證(無需系統用戶)的進程模式傳輸,可實現方便安全的進行數據備份及鏡像。

3、rsync工作場景

  • 兩臺服務器之間數據同步。
  • 把所有客戶服務器數據同步到備份服務器,生產場景集羣架構服務器備份方案。
  • rsync結合inotify的功能做實時的數據同步。

4、rsync命令同步參數選項

常用參數選項說明:

參數 完整參數 說明
-v --verbose 詳細模式輸出,傳輸時的進度信息
-z --compress 傳輸時進行壓縮以提高傳輸效率, --compress-level=NUM可按級別壓縮
-a --archive 歸檔模式,表示以遞歸方式傳輸文件,並保持所有文件屬性,等於-rtopgDl
-r --recursive 對子目錄以遞歸模式,即目錄下的所有目錄都同樣傳輸,注意是小寫r
-t --times 保持文件時間信息
-o --owner 保持文件屬主信息
-p --perms 保持文件權限
-g --group 保持文件屬組信息
-P --progress 顯示同步的過程及傳輸時的進度等信息
-D --devices 保持設備文件信息
-l --links 保持軟鏈接
-e --rsh=COMMAND 使用的信道協議,指定替代rsh的shell程序,例如ssh
--exclude=PATTERN 指定排除不需要傳輸的文件模式
--exclude-from=FILE 從文件中讀取指定排除不需要傳輸的文件模式

以上參數爲在生產環境中的常用參數,基本使用已足夠,相關的參數還有非常多,瞭解更多可以man rsync。最常用的參數avz相當於vzrtopgDl,生產參數-avz或者用-vzrtopg

5、rsync工作方式

一般來說,rsync大致使用三種主要的傳輸數據的方式,分別爲:

5.1 本地數據傳輸模式(local-only mode),單個主機本地之間的數據傳輸(此時類似於cp命令的功能)。

rsync本地傳輸模式的語法爲:

rsync [OPTION...] SRC... [DEST]

語法說明:

  • rsync爲同步的命令
  • [OPTION]爲同步時的參數選項
  • SRC爲源,即待同步的分區、文件或目錄等
  • [DEST]爲目的分區、文件或目錄

實例,將/etc/hosts 同步至/tmp

MacBook-Pro:tmp maxincai$ rsync /etc/hosts /tmp
MacBook-Pro:tmp maxincai$ ll
total 4
drwxrwxrwt  9 root      wheel  306  1 14 15:04 ./
drwxr-xr-x@ 6 root      wheel  204  6 26  2015 ../
drwxrwxrwt  3 maxincai  wheel  102 12 31 10:46 .pd/
-rw-r--r--  1 maxincai  wheel  737  1 14 15:04 hosts
5.2 遠程傳輸,藉助rcp,ssh等通道來傳輸數據(此時類似於scp命令的功能)。

遠程傳輸模式的語法爲:

拉取: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
推送: rsync [OPTION...] SRC... [USER@]HOST:DEST

語法說明:

  • rsync爲同步的命令
  • [OPTION]爲同步時的參數選項
  • [USER@]HOST: 遠程用戶名及host
  • SRC爲源,即待同步的分區、文件或目錄等
  • [DEST]爲目的分區、文件或目錄

推送實例

[root@vagrant-centos65 ~]# mkdir data
[root@vagrant-centos65 ~]# ll
total 8
drwxr-xr-x 2 root root 4096 Jan 14 07:26 data
drwxr-xr-x 3 root root 4096 Jan 11 11:08 workspace
[root@vagrant-centos65 ~]# cd data
[root@vagrant-centos65 data]# ll
total 0
[root@vagrant-centos65 data]# ll
total 0
[root@vagrant-centos65 data]# vim 1.txt
[root@vagrant-centos65 data]# rsync -avzP -e 'ssh -p 22' /root/data [email protected]:/root
The authenticity of host '192.168.35.2 (192.168.35.2)' can't be established.
RSA key fingerprint is 0c:5f:f6:c7:a6:27:4e:a0:e6:7c:99:8a:db:2e:41:df.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.35.2' (RSA) to the list of known hosts.
[email protected]'s password:
sending incremental file list
data/
data/1.txt
           4 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/2)

sent 104 bytes  received 35 bytes  25.27 bytes/sec
total size is 4  speedup is 0.03
[root@vagrant-centos65 data]#

拉取實例

[root@vagrant-centos65 data]# rsync -avzP -e 'ssh -p 22'  [email protected]:/root/data1 /root
[email protected]'s password:
receiving incremental file list
data1/
data1/2.txt
           4 100%    3.91kB/s    0:00:00 (xfer#1, to-check=0/2)

sent 34 bytes  received 106 bytes  56.00 bytes/sec
total size is 4  speedup is 0.03
[root@vagrant-centos65 data]# ll
total 4
-rw-r--r-- 1 root root 4 Jan 14 07:26 1.txt
[root@vagrant-centos65 data]# cd ..
[root@vagrant-centos65 ~]# ll
total 12
drwxr-xr-x 2 root root 4096 Jan 14 07:27 data
drwxr-xr-x 2 root root 4096 Jan 14 07:30 data1
drwxr-xr-x 3 root root 4096 Jan 11 11:08 workspace
[root@vagrant-centos65 ~]#
5.3 以守護進程(socket)的方式傳輸數據,這個是rsync自身的重要功能。

我們實驗的機器如下:

server: 192.168.35.2

client: 192.168.36.2

先新建配置文件,請注意rsyncd.conf配置文件只用在服務端新建就好。

vim /etc/rsyncd.conf

以下只是配置文件中最常用的部份,更多的請使用命令man rsyncd.conf查看。

# 運行rsync的用戶和組id
uid = rsync
gid = rsync
# bug信息的處理,一種安全方式
use chroot = no
# 最大的連接數
max connections = 200
# 超時時間
timeout = 300
# pid文件
pid file = /var/run/rsyncd.pid
# 鎖文件
lock file = /var/run/rsync.lock
# 日誌文件
log file = /var/log/rsyncd.log
# 需要同步的模塊,這是其中一個,可以有多個
[data1]
# 同步的根目錄
path = /data1/
# 忽略錯誤
ignore errors
# 只讀falsh 表示可讀可寫
read only = false
# 不可列表
list = false
# 允許訪問的網段
hosts allow = 10.0.0.0/24
# 拒絕訪問的網段
hosts deny = 0.0.0.0/32
# 用戶名
auth users = rsync_backup
# 密碼文件路徑
secrets file = /etc/rsync.password

以守護進程的方式啓動rsync

[root@vagrant-centos65 data1]# rsync --daemon

如果需要了解啓動更詳情的信息,建議查看幫助,這裏就不具體列出來了

[root@vagrant-centos65 data1]# rsync --daemon --help

查看是否啓動成功,rsyncd的默認端口是873

[root@vagrant-centos65 data1]# netstat -lntup | grep 873
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      2580/rsync
tcp        0      0 :::873                      :::*                        LISTEN      2580/rsync
[root@vagrant-centos65 data1]# ps -ef | grep rsync
root      2580     1  0 08:19 ?        00:00:00 rsync --daemon
root      2585  2116  0 08:30 pts/0    00:00:00 grep rsync

如果啓動出錯,我們就需要查看一下系統日誌,我們這裏日誌顯示正常啓動

[root@vagrant-centos65 data1]# cat /var/log/rsyncd.log
2016/01/15 08:19:12 [2580] rsyncd version 3.0.6 starting, listening on port 873

下面我們創建需要同步的目錄,並給予相應的權限

[root@vagrant-centos65 /]# mkdir -p /data1
[root@vagrant-centos65 /]# chown -R rsync.rsync /data1
# 這裏報錯了,因爲沒有相應的用戶,我們需要創建
chown: invalid user: `rsync.rsync'
# 創建一個不需要登錄的系統用戶
[root@vagrant-centos65 /]# useradd rsync -s /sbin/nologin
[root@vagrant-centos65 /]# chown -R rsync.rsync /data1

將用戶名和密碼重定義輸出到我們的密碼存放文件

# rsync_backup是用戶名,maxincai是密碼
[root@vagrant-centos65 /]# echo "rsync_backup:maxincai" >/etc/rsync.password
[root@vagrant-centos65 /]# cat /etc/rsync.password
rsync_backup:maxincai
# 由於我們存放的是密碼文件,爲了安全,修改權限爲600,同時這也是rsync本身的要求
root@vagrant-centos65 /]# chmod 600 /etc/rsync.password
# 同時我們需要關閉防火檣
[root@vagrant-centos65 /]# /etc/init.d/iptabls stop
-bash: /etc/init.d/iptabls: No such file or directory
[root@vagrant-centos65 /]# getenforce
Disabled

下面配置客戶端,將密碼保存在密碼配置文件,同是爲了與服務端統一,我們使用相當的文件名,注意這裏我們只需要放入密碼即可

[root@vagrant-centos65 ~]# echo "maxincai" >/etc/rsync.password
[root@vagrant-centos65 ~]# chmod 600 /etc/rsync.password
[root@vagrant-centos65 ~]# cat /etc/rsync.password
maxincai

通過daemon方式遠程傳輸的語法爲:

Access via rsync daemon:
        Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
              rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
        Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
              rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

在客戶端進行拉取實例:

先看看server端的目錄結構

[root@vagrant-centos65 data1]# pwd
/data1
[root@vagrant-centos65 data1]# tree
.
├── dir1
│   └── test3
├── dir2
├── test1
└── test2

3 directories, 2 files
[root@vagrant-centos65 data1]#

開始拉取

[root@vagrant-centos65 ~]# rsync -avz [email protected]::data1 /data1
Password:
# 需要輸入密碼,輸入之前的定義的密碼maxincai
receiving incremental file list
created directory /data1
./
test1
test2
dir1/
dir1/test3/
dir2/

sent 116 bytes  received 289 bytes  90.00 bytes/sec
total size is 0  speedup is 0.00
# 可以看到通過成功,看看同步之後的目錄結構
[root@vagrant-centos65 ~]# cd /data1
[root@vagrant-centos65 data1]# ll
total 8
drwxr-xr-x 3 root root 4096 Jan 15 09:11 dir1
drwxr-xr-x 2 root root 4096 Jan 15 09:11 dir2
-rw-r--r-- 1 root root    0 Jan 15 09:10 test1
-rw-r--r-- 1 root root    0 Jan 15 09:11 test2
[root@vagrant-centos65 data1]# tree
.
├── dir1
│   └── test3
├── dir2
├── test1
└── test2

3 directories, 2 files

在看看拉取的時候需要輸入密碼,不方便我們在rcontab中進行定義,所以我們需要讓他自動讀取密碼,參數--password-file=/etc/rsync.password

[root@vagrant-centos65 data1]# rsync -avz [email protected]::data1 /data1 --password-file=/etc/rsync.password
receiving incremental file list

sent 66 bytes  received 205 bytes  108.40 bytes/sec
total size is 0  speedup is 0.00

在客戶端進行推送實例

# 創建10個文件
[root@vagrant-centos65 data1]# touch {1..10}
[root@vagrant-centos65 data1]# ll
total 8
-rw-r--r-- 1 root root    0 Jan 15 09:25 1
-rw-r--r-- 1 root root    0 Jan 15 09:25 10
-rw-r--r-- 1 root root    0 Jan 15 09:25 2
-rw-r--r-- 1 root root    0 Jan 15 09:25 3
-rw-r--r-- 1 root root    0 Jan 15 09:25 4
-rw-r--r-- 1 root root    0 Jan 15 09:25 5
-rw-r--r-- 1 root root    0 Jan 15 09:25 6
-rw-r--r-- 1 root root    0 Jan 15 09:25 7
-rw-r--r-- 1 root root    0 Jan 15 09:25 8
-rw-r--r-- 1 root root    0 Jan 15 09:25 9
drwxr-xr-x 3 root root 4096 Jan 15 09:11 dir1
drwxr-xr-x 2 root root 4096 Jan 15 09:11 dir2
-rw-r--r-- 1 root root    0 Jan 15 09:10 test1
-rw-r--r-- 1 root root    0 Jan 15 09:11 test2

# 開始推送
[root@vagrant-centos65 data1]#  rsync -avz /data1/ [email protected]::data1 --password-file=/etc/rsync.password
sending incremental file list
./
1
10
2
3
4
5
6
7
8
9

sent 558 bytes  received 204 bytes  1524.00 bytes/sec
total size is 0  speedup is 0.00

# 查看服務端的同步情況
[root@vagrant-centos65 data1]# tree
.
├── 1
├── 10
├── 2
├── 3
├── 4
├── 5
├── 6
├── 7
├── 8
├── 9
├── dir1
│   └── test3
├── dir2
├── test1
└── test2

3 directories, 12 files

其他的語法

# 推送
[root@vagrant-centos65 data1]#  rsync -avz /data1/ rsync://[email protected]/data1 --password-file=/etc/rsync.password
sending incremental file list

sent 195 bytes  received 11 bytes  412.00 bytes/sec
total size is 0  speedup is 0.00
# 拉取][]
[root@vagrant-centos65 data1]#  rsync -avz  rsync://[email protected]/data1 /data1/ --password-file=/etc/rsync.password
receiving incremental file list

sent 96 bytes  received 317 bytes  826.00 bytes/sec
total size is 0  speedup is 0.00

小結:

rsync server:

  • vim /etc/rsyncd.conf(用戶,目錄,模塊,虛擬用戶及密碼文件)
  • 創建共享目錄 /data1
  • 創建rsync用戶,並且授權訪問/data1
  • 創建密碼文件,複製配置文件裏的路徑,然後添加密碼內容,內容虛擬用戶名:密碼
  • 密碼文件權限600
  • rsync --daemon然後放入/etc/rc.local
  • tail /var/log/rsyncd.log

rsync client(多個)

  • 密碼文件和服務端沒任何關係,命令時 --password-file=/etc/rsync.password 內容:密碼
  • /etc/rsync.password 600
  • 同步:
    推、拉

排錯:

  • 防火牆和selinux
  • /var/log/rsyncd.log
  • 整個部署流程整體考慮排查
  • 操作習慣當作一個大事

排除單個文件

[root@vagrant-centos65 data1]#  rsync -avz  rsync://[email protected]/data1 /data1/ --exclude=1 --password-file=/etc/rsync.password

排除多個文件

rsync -avz  rsync://[email protected]/data1 /data1/ --exclude={1,2} --password-file=/etc/rsync.password

rsync -avz  rsync://[email protected]/data1 /data1/ --exclude=a --exclude=b --password-file=/etc/rsync.password

# 排除連續的
rsync -avz  rsync://[email protected]/data1 /data1/ --exclude={a..g} --password-file=/etc/rsync.password

# 從文件中讀取排除規則
rsync -avz  rsync://[email protected]/data1 /data1/ --exclude-from=paichu.log --password-file=/etc/rsync.password

服務端排除參數,在/etc/rsyncd.conf中修改:

# 注意是用空格去分隔
exclude=a b test/1.txt
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章