inotify+rsync+sersync實時數據備份

第1章 rsync備份服務

1.1 rsync軟件介紹

rsync軟件官方鏈接地址:http://www.samba.org/ftp/rsync/rsync.html 

提示信息:

man rsync查看客戶端說明信息

man rsyncd.conf查看服務端配置

 

Rsync是一款開源的、快速的、多功能的、可實現全量及增量的本地或遠程數據同步備份的優秀工具

全量:將全部數據,進行傳輸覆蓋

          cp mv scp

增量:只傳輸差異部分的數據

      rsync

rstync適用於unix/linux/windows等多種操作系統平臺

 

                                             

nfs(客戶端)   (拉)<—— rsync (軟件)——>(推)     backup(服務端)

1.2 rsync軟件功能

1.2.1 類似於cp命令---(本地備份傳輸數據)

[root@backup ~]# rsync /etc/hosts /tmp/

[root@backup ~]# ls /tmp/hosts

/tmp/hosts

總結說明:利用rsync命令在備份數據目錄時

備份數據的目錄後面有/       類似這樣形式old_dir/      表示old_dir目錄下的內容進行復制

備份數據的目錄後面沒有/     類似這樣形式old_dir       表示old_dir目錄下的內容以及目錄本身進行復制

1.2.2 類似於scp命令---(遠程備份傳輸數據)

[root@backup ~]# rsync -rv /old_dir/ 10.0.0.31:/tmp/

[email protected]'s password:

sending incremental file list

a

b

c

d

e

 

sent 253 bytes  received 107 bytes  102.86 bytes/sec

total size is 0  speedup is 0.00

[root@backup ~]# rsync -rv /old_dir 10.0.0.31:/tmp/

[email protected]'s password:

sending incremental file list

old_dir/

old_dir/a

old_dir/b

old_dir/c

old_dir/d

old_dir/e

 

sent 279 bytes  received 111 bytes  111.43 bytes/sec

total size is 0  speedup is 0.00

1.2.3 類似於rm命令--- (實現無差異同步備份)

[root@backup tmp]# rsync -r --delete /null/  /old01/

[root@backup tmp]# ll /old01/

total 0

說明:rsync命令清空目錄等內容或者清空文件內容效率比rm命令要高

 

1.2.4 類似於ls命令 --- (本地文件信息查看)

[root@backup tmp]# rsync /etc/hosts

-rw-r--r--         371 2017/10/10 15:45:09 hosts

1.3 rsync實現增量複製的原理

Rsync通過其獨特的“quick check”算法,實現增量傳輸數據

官方增量傳輸算法說明:

Rsync finds files that need to be transferred using a “quick check” algorithm (by default) that looks

for files that have changed in size or in last-modified time.  Any changes  in  the  other  preserved

attributes  (as  requested by options) are made on the destination file directly when the quick check

indicates that the file’s data does not need to be updated.

在同步備份數據時,默認情況下,Rsync通過其獨特的“quick check”算法,它僅同步大小或者最後修改時間發生變化的文件或目錄,當然也可根據權限,屬主等屬性的變化同步,但需要指定相應的參數,甚至可以實現只同步一個文件裏有變化的內容部分,所以,可以實現快速的同步備份數據。

 

1.4 rsync7個特性說明

01. 支持拷貝普通文件與特殊文件如鏈接文件,設備等。

02. 可以有排除指定文件或目錄同步的功能,相當於打包命令tar的排除功能。

    #tar zcvf backup_1.tar.gz  /opt/data  -exclude=old  

    說明:在打包/opt/data時就排除了old命名的目錄和文件。

03. 可以做到保持原文件或目錄的權限、時間、軟硬鏈接、屬主、組等所有屬性均不改變-p

04. 可實現增量同步,既只同步發生變化的數據,因此數據傳輸效率很高(tar -N)。

    # 將備份/home 目錄自 2008-01-29 以來修改過的文件

    # tar -N 2008-01-29 -zcvf /backups/inc-backup_$(date +%F).tar.gz /home

    # 將備份 /home 目錄昨天以來修改過的文件

    # tar -N $(date -d yesterday "+%F") -zcvf /backups/inc-backup_$(date +%F).tar.gz /home

# 添加文件到已經打包的文件

    # tar -rf all.tar *.gif    ###此命令目前實測效果不理想

    說明:這條命令是將所有.gif的文件增加到all.tar的包裏面去。-r是表示增加文件的意思。

05. 可以使用rcp,rsh,ssh等方式來配合進行隧道加密傳輸文件(rsync本身不對數據加密)

06. 可以通過socket(進程方式)傳輸文件和數據(服務端和客戶端)*****。重點掌握

07. 支持匿名的或認證(無需系統用戶)的進程模式傳輸,可實現方便安全的進行數據備份及鏡像。

1.5 rsync工作原理

   

1.6 rsync軟件工作方式

SYNOPSIS

本地數據同步方式

       Local:  rsync [OPTION...] SRC... [DEST]

              

遠程數據同步方式:

       Access via remote shell:

         Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]

         Push: rsync [OPTION...] SRC... [USER@]HOST:DEST

              

守護進程方式數據同步:

       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

              

1.6.1 本地數據同步方式(類似於cp

Local:  rsync [OPTION...] SRC... [DEST]

rsync         -- 數據同步命令

[OPTION...]   -- rsync命令參數信息

SRC           -- 要同不得數據信息(文件或目錄)

[DEST]        -- 將數據傳輸到什麼位置

 

實例演示命令:

[root@backup test]# ll

total 4

drwxr-xr-x 2 root root 4096 Jun 29 23:21 ceshi

-rw-r--r-- 1 root root    0 Jun 29 23:21 test.txt

[root@backup test]# rsync test.txt  ceshi/

[root@backup test]# ll ceshi

total 0

-rw-r--r-- 1 root root 0 Jun 29 23:22 test.txt

1.6.2 遠程數據同步方式(類似scp---又稱爲隧道傳輸

Access via remote shell:

Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]

Push: rsync [OPTION...] SRC... [USER@]HOST:DEST

說明:需要進行交互傳輸數據。如果想實現免交互傳輸數據,需要藉助ssh+key方式實現:

1.6.2.1 push:推

SRC:        本地要懟過去的數據信息

DEST         懟到遠端什麼位置

 

實踐操作:push

Push: rsync [OPTION...] SRC... [USER@]HOST:DEST

服務端推:

[root@backup ~]# ll /tmp

total 0

-rw-r--r-- 1 root root 0 Jun 29 23:39 test.txt

[root@backup ~]# rsync -r /tmp 10.0.0.31:/test/

[email protected]'s password:##123456

[root@backup ~]#

客戶端查看:

[root@nfs01 ~]# ll /test

total 4

drwxr-xr-x 3 root root 4096 Jun 29 23:52 tmp

 

 

服務端推:

[root@backup ~]# rsync -r /tmp/ 10.0.0.31:/test/

[email protected]'s password:

客戶端查看:

[root@nfs01 ~]# ll /test

total 0

-rw-r--r-- 1 root root 0 Jun 29 23:53 test.txt

說明:/tmp   -- 表示將tmp目錄下面數據內容及目錄本身都進行傳輸

      /tmp/  -- 表示只傳輸tmp目錄下面的內容信息

 

 

遠程隧道加密傳輸方式

[root@backup tmp]# rsync -vzrtopgP  -e 'ssh -p 22' [email protected]:/tmp/ /tmp/

[root@backup tmp]# rsync -vzrtopgP  -e 'ssh -p 22' /tmp/  [email protected]:/tmp

1.6.2.2 pull:拉

Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]

[USER@] :  以什麼用戶身份傳輸數據信息

HOST:        遠程主機信息(IP地址信息 主機名稱信息)

SRC:          遠端要恏過來的數據信息

[dest]        恏到本地什麼位置

nfs01服務器作爲本地,要獲取rsync服務器上的數據,自然就成拉的概念了

[old@nfs01 ~]$ rsync -rv [email protected]:/tmp /tmp/

[email protected]'s password:

receiving incremental file list

tmp/

tmp/a

tmp/b

tmp/c

tmp/d

tmp/e

rsync: send_files failed to open "/tmp/yum.log": Permission denied (13)

tmp/.ICE-unix/

tmp/

 

sent 137 bytes  received 459 bytes  170.29 bytes/sec

total size is 0  speedup is 0.00

rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1505) [generator=3.0.6]

[old@nfs01 ~]$ ls  /tmp/

tmp

 

1.6.3 守護進程方式數據同步:(面向交互傳輸同步傳輸數據)

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

1.7 rsync軟件在企業中的應用場景

01. 兩臺服務器之間數據同步(定時任務cron+rsync

    同步網站內部人員數據信息(定時任務最小週期爲1分鐘)

一般是網站內部人員存儲的數據信息,用定時同步

 

02. 兩臺服務器之間數據同步(實時任務inotify/sersync/lrsyncd+rsync

    同步網站用戶人員數據信息

一般是網站外部人員存儲的數據信息,用實時同步

 

 

1.8 rsync守護進程模式部署

規劃:#配置rsync守護進程方式(需要有服務端與客戶端)

01. backup服務器作爲rsync服務端

02.rsync客戶端服務器作爲參照服務器,將數據推到rsync服務端

     

1.8.1 第一部分:配置rsync服務端(將服務端配置到backup服務器上)

第一個里程碑:軟件是否存在

[root@backup ~]# rpm -qa|grep rsync

rsync-3.0.6-12.el6.x86_64

第二個里程碑:進行軟件服務配置

vim /etc/rsyncd.conf  (默認沒有這個配置文件)

cat >/etc/rsyncd.conf<<EOF

#rsync_config

#created by baoge at 2017

##rsyncd.conf start##

uid = rsync

gid = rsync

use chroot = no

max connections = 200

timeout = 300

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log

[backup]

comment = "backup dir by baoge"

path = /backup

ignore errors

read only = false

list = false

hosts allow = 172.16.1.0/24

hosts deny = 0.0.0.0/32

auth users = rsync_backup

secrets file = /etc/rsync.password

EOF

配置文件說明

###以上爲配置文件的描述信息

#rsync_config

#created by baoge at 2017

##rsyncd.conf start##

 

###以上爲配置文件的全局配置

uid = rsync

##用戶 遠端的命令使用rsync訪問共享目錄

gid = rsync

##用戶組

use chroot = no

##安全相關

max connections = 200

##最大連接數

timeout = 300

##超時時間

pid file = /var/run/rsyncd.pid

##進程對應的進程號文件

lock file = /var/run/rsync.lock

##鎖文件

log file = /var/log/rsyncd.log

##日誌文件 顯示出錯信息等

 

###配置文件模塊配置

[backup]                             ##模塊名稱

comment = "backup dir by baoge"  ##描述信息

path = /backup                      ##模塊對應的位置  

ignore errors                        ##忽略錯誤程序

read only = false                    ##是否只讀

list = false                           ##是否可以列表

hosts allow = 172.16.1.0/24         ##准許訪問rsync服務器的範圍(白名單)

hosts deny = 0.0.0.0/32             ##禁止訪問rsync服務器的範圍(黑名單)

auth users = rsync_backup          ##不存在的用戶,只用於認證(開門)

secrets file = /etc/rsync.password   ##不存在的用戶進行認證時的密鑰文件

 

第三個里程碑:創建rsync服務管理用戶

[root@backup ~]# useradd -s /sbin/nologin -M rsync

[root@backup ~]# id  rsync

uid=501(rsync) gid=501(rsync) groups=501(rsync)

第四個里程碑:創建數據備份存儲目錄

mkdir -p /backup

chown -R rsync.rsync /backup/

第五個里程碑:創建認證用戶密碼文件

[root@backup ~]# echo "rsync_backup:test123"  >/etc/rsync.password

[root@backup ~]# chmod 600 /etc/rsync.password

[root@backup ~]# cat  /etc/rsync.password

rsync_backup:test123

[root@backup ~]# ll /etc/rsync.password

-rw------- 1 root root 23 Nov 28 11:24 /etc/rsync.password

第六個里程碑:啓動rsync服務

[root@backup ~]# rsync --daemon

[root@backup ~]# ps -ef|grep rsync

root      25120      1  0 11:27 ?        00:00:00 rsync --daemon

root      25122  24773  0 11:27 pts/1    00:00:00 grep rsync

[root@backup ~]# netstat -lntup|grep rsync  ##查看服務端口

tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      25120/rsync        

tcp        0      0 :::873                      :::*                        LISTEN      25120/rsync        

至此:服務端配置完成

 

客戶端進行推送數據測試

[root@nfs01 ~]# rsync -avz /etc/hosts [email protected]::backup

Password:

sending incremental file list

hosts

 

sent 201 bytes  received 27 bytes  11.69 bytes/sec

total size is 371  speedup is 1.63

 

服務端查看:

[root@backup ~]# ls /backup

hosts

1.8.2 第二部分:配置rsync客戶端

架構中其他服務器稱爲rsync客戶端

第一個里程碑:軟件是否存在

[root@nfs01 ~]# rpm -qa|grep rsync

rsync-3.0.6-12.el6.x86_64

 

第二個里程碑:建立認證文件

[root@nfs01 ~]# echo "test123"  >/etc/rsync.password

[root@nfs01 ~]# chmod 600 /etc/rsync.password

 

測試:

[root@nfs01 baoge_rsync]# touch {a,b,c}.txt

[root@nfs01 baoge_rsync]# ll

total 0

-rw-r--r-- 1 root root 0 Jun 29 17:39 a.txt

-rw-r--r-- 1 root root 0 Jun 29 17:39 b.txt

-rw-r--r-- 1 root root 0 Jun 29 17:39 c.txt

 

[root@nfs01 baoge_rsync]# rsync -avz /baoge_rsync/{a,b,c}.txt  [email protected]::backup --password-file=/etc/rsync.password

password file must not be other-accessible

continuing without password file

Password:

sending incremental file list

a.txt

b.txt

c.txt

 

sent 154 bytes  received 65 bytes  16.22 bytes/sec

total size is 0  speedup is 0.00

 

[root@backup backup]# ll

total 0

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:39 a.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:39 b.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:39 c.txt

數據推送正常

 

免交互式推送數據(設置環境變量)

[root@nfs01 ~]# export RSYNC_PASSWORD=test123       ###臨時

echo  'export RSYNC_PASSWORD=test123' >>/etc/profile   ##永久

[root@backup backup]# ll

total 0

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:47 1.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:47 2.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:47 3.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:39 a.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:39 b.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:39 c.txt

第三個里程碑:測試傳輸

守護進程方式推拉

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

 

交互式:rsync -avz /etc/hosts  [email protected]::backup

非交互式:rsync -avz /etc/hosts  [email protected]::backup --password-file=/etc/rsync.password

 

實踐測試:

推:

[root@nfs01 ~]# rsync -avz /tmp/push.txt [email protected]::backup

sending incremental file list

push.txt

 

sent 65 bytes  received 27 bytes  184.00 bytes/sec

total size is 0  speedup is 0.00

對端查看:

[root@backup backup]# ll

total 0

-rw-r--r-- 1 rsync rsync 0 Jun 30 12:39 push.txt

-rw-r--r-- 1 root  root  0 Jun 30 12:22 rsynctest.txt

 

拉:

[root@nfs01 ~]# rsync -avz [email protected]::backup/rsynctest.txt /tmp

receiving incremental file list

rsynctest.txt

 

sent 83 bytes  received 142 bytes  450.00 bytes/sec

total size is 0  speedup is 0.00

[root@nfs01 ~]# ll /tmp

total 0

-rw-r--r-- 1 root root 0 Jun 30 12:22 rsynctest.txt

 

##常見問題:

[root@nfs01 tmp]# rsync -avz /etc/hosts  [email protected]::backup

Password:

sending incremental file list

hosts

rsync: mkstemp ".hosts.U5OCyR" (in backup) failed: Permission denied (13)

 

sent 200 bytes  received 27 bytes  13.76 bytes/sec

total size is 371  speedup is 1.63

rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]

說明:備份目錄權限設置不正確

1.8.3 第三部分:rsync服務的重啓—kill

1.     kill 進程pid

kill -9 強制殺死

說明:需要知道進程號信息,且進程殺死後會有說明信息

2.killall 進程名

說明:進程殺死後有提示說明

3.pkill 進程名(模糊殺死)

說明:進程殺死後沒有提示說明

1.9 rsync參數

命令參數

參數說明

-v,--verbose

詳細模式輸出,傳輸時的進度等信息

-z,--compress

傳輸時進行壓縮以提高傳輸效率,--compress可以按級別壓縮。局域網可以不用壓縮

-a,--archive重要

歸檔模式,表示以遞歸方式傳輸文件,並保持所有文靜屬性,等於-rtopgDl

-r,--recursive   歸類於-a

對子目錄以遞歸模式,即目錄下的所有目錄都同樣傳輸

-t,--times    歸類於-a

保持文件時間信息

-o,--owner   歸類於-a

保持文件屬主信息

-p,--perms   歸類於-a

保持文件權限

-g,--group   歸類於-a

保持文件屬組信息

-D,--devices 歸類於-a

保持設備文件信息

-l,--links    歸類於-a

保留軟鏈接(小寫L

-P,--progress

顯示等信息過同步的過程及傳輸時的進度

-e

使用的信道協議(remote   shell),指定替代rshshell程序

例如:ssh

-exclude=PATTENRN

指定排除不需要傳輸的文件信息(類似於tar中)

--exclude-from=file

文件所在的目錄,即可以實現排除多個文件(類似於tar中)

--bwlimit=RATE

傳輸文件時限速

limit I/O   bandwidth;KBbytes per cecond

limit socket I/O   bandwidth限速功能

案例:某DBA做數據同步,帶寬佔滿,導致用戶無法訪問網站

--delete

讓目標目論SRC和源目錄DST一致,即無差異同步數據

注:

保持同步目錄以及文件屬性:

這裏的-avzP相當於-vzrtopgDlP(還多了Dl功能),生產環境常用的參數選項爲-avzP-vzrtopgP

如果放入腳本中,也可以把-v-P去掉。這裏的--progress可以用 -P代替

提示信息:

以上參數還可以使用man rsync或者參考資料地址。

thhp://www.samba.org/ftp/rsync/rsync.html OPTIONS SUMMARY

生產參數:-avz或者-vzrtopg

更多參數:http://www.samba.org/ftp/rsync/rsync.html

 

1.9.1 案例:某DBA做數據同步,帶寬佔滿,導致用戶無法訪問網站

 

1.10 利用xinetd超級守護進程啓動rsync服務

第一個里程碑:安裝xinetd服務

       yum install -y xinetd

第二個里程碑:讓rsync服務可以被xinetd服務所管理

       [root@backup xinetd.d]# vim /etc/xinetd.d/rsync

    # default: off

    # description: The rsync server is a good addition to an ftp server, as it \

    #     allows crc checksumming etc.

    service rsync

    {

           disable  = no                 <-- 把默認yes修改爲no

           flags             = IPv6

           socket_type     = stream

           wait            = no

           user            = root

           server          = /usr/bin/rsync

           server_args     = --daemon

           log_on_failure  += USERID

    }

第三個里程碑:停止rsync服務,利用xinetd服務重啓啓動

       [root@backup ~]# killall rsync

       [root@backup ~]# /etc/init.d/xinetd start

    Starting xinetd:                                           [  OK  ]

       [root@backup ~]# netstat -lntup|grep 873

    tcp        0      0 :::873                      :::*                        LISTEN      4833/xinetd

第四個里程碑:客戶端進行測試

[root@nfs01 ~]# rsync -av /etc/hosts [email protected]::backup

sending incremental file list

   

sent 26 bytes  received 8 bytes  68.00 bytes/sec

total size is 378  speedup is 11.12

1.11 rsync服務的擴展功能

1.11.1 設置rsync軟件開機啓動(僅服務端)

1.     配置/etc/rc.local文件

echo 'rsync --daemon' >>/etc/rc.local

2.     配置/etc/init.d/目錄(需要自己編寫啓動腳本)

 

3.     xinetd服務啓動rsync

1.11.2 守護進程多模塊功能配置

第一個里程碑:編寫配置文件添加多模塊

[root@backup ~]# cat  /etc/rsyncd.conf

#rsync_config

#created by baoge at 2017

##rsyncd.conf start##

uid = rsync

gid = rsync

use chroot = no

max connections = 200

timeout = 300

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log

ignore errors

read only = false

list = false

hosts allow = 172.16.1.0/24

#hosts deny = 0.0.0.0/32

auth users = rsync_backup

secrets file = /etc/rsync.password

[backup]

comment = "backup dir by baoge"

path = /backup

[backup_dev]

comment = "backup dir by Baohong"

path = /backup_dev

 

第二個里程碑:重啓rsync服務

killall rsync

rsync --daemon

創建新模塊的備份目錄

mkdir -p /backup_dev

chown -R rsync.rsync /backup_dev/

實踐測試

[root@nfs01 ~]# rsync -avz /etc/hosts  [email protected]::backup_dev --password-file=/etc/rsync.password

sending incremental file list

hosts

 

sent 201 bytes  received 27 bytes  456.00 bytes/sec

total size is 371  speedup is 1.63

 

[root@backup ~]# ls /backup_dev

hosts

 

 

[root@nfs01 ~]# rsync -avz /etc  [email protected]::backup_dev

[root@backup ~]# ll /backup_dev/

total 8

drwxr-xr-x 79 rsync rsync 4096 Jun 29 17:25 etc

-rw-r--r--  1 rsync rsync  337 Jun 28 01:38 hosts

 

1.11.3 rsync排除功能實踐

創建模擬環境

[root@nfs01 ~]# mkdir -p /paichu

[root@nfs01 ~]# mkdir  /paichu/{a..d}

[root@nfs01 ~]# touch  /paichu/{a..d}/{1..4}

[root@nfs01 ~]# tree  /paichu

/paichu

├── a

   ├── 1

   ├── 2

   ├── 3

   └── 4

├── b

   ├── 1

   ├── 2

   ├── 3

   └── 4

├── c

   ├── 1

   ├── 2

   ├── 3

   └── 4

└── d

    ├── 1

    ├── 2

    ├── 3

    └── 4

 

4 directories, 16 files

 

法一:

[root@nfs01 ~]# cd   /paichu

[root@nfs01 paichu]#

[root@nfs01 paichu]# ll

total 16

drwxr-xr-x 2 root root 4096 Jun 30 14:57 a

drwxr-xr-x 2 root root 4096 Jun 30 14:57 b

drwxr-xr-x 2 root root 4096 Jun 30 14:57 c

drwxr-xr-x 2 root root 4096 Jun 30 14:57 d

 

rsync -av --exclude=a --exclude=b --exclude=d/4 /paichu/ [email protected]::backup_dev

[root@nfs01 paichu]# rsync -av --exclude=a --exclude=b --exclude=d/4 /paichu/ [email protected]::backup_dev

sending incremental file list

./

c/

c/1

c/2

c/3

c/4

d/

d/1

d/2

d/3

 

sent 387 bytes  received 152 bytes  1078.00 bytes/sec

total size is 0  speedup is 0.00

 

服務端查看:

[root@backup ~]# ll /backup_dev

total 8

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 c

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 d

[root@backup ~]# tree /backup_dev

/backup_dev

├── c

   ├── 1

   ├── 2

   ├── 3

   └── 4

└── d

    ├── 1

    ├── 2

    └── 3

 

2 directories, 7 files

 

法二:

事先編寫排除文件

[root@nfs01 paichu]# cat exclude.txt  -A

c$

d$

a/1$

exclude.txt$

注意:編輯排除文件的時候,只能一行寫一個文件或目錄,且每行行尾不能有空格

推送數據測試:

[root@nfs01 paichu]# rsync -av  --exclude-from=exclude.txt /paichu/ [email protected]::backup_dev

sending incremental file list

./

a/

a/2

a/3

a/4

b/

b/1

b/2

b/3

b/4

 

sent 387 bytes  received 152 bytes  1078.00 bytes/sec

total size is 0  speedup is 0.00

 

 

服務端查看

[root@backup ~]# rm -rf /backup_dev/*

[root@backup ~]# ll /backup_dev

total 0

[root@backup ~]# ll /backup_dev

total 8

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 a

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 b

[root@backup ~]# tree /backup_dev

/backup_dev

├── a

   ├── 2

   ├── 3

   └── 4

└── b

    ├── 1

    ├── 2

    ├── 3

    └── 4

 

2 directories, 7 files

 

##可以不用事先在服務端創建備份目錄 但是不可以創建多級目錄   

服務端也可以/etc/rsyncd.conf裏配置排除

 

 

1.11.4 分類備份

o  備份時針對不同的人員將數據分別備份至期對應目錄下的實踐:

人員身份:            分類備份目錄

運維                   sa

開發                   dev

DBA                    dba

要求說明:

1./fenlei/a目錄備份至運維備份目錄/backup/sa

2./fenlei/b目錄備份至開發備份目錄/backup/dev

3./fenlei/c目錄備份至DBA備份目錄/backup/dba

 

實踐操作:

服務端:

[root@backup backup]# ll

total 0

[root@backup backup]#

 

客戶端:

[root@nfs01 fenlei]# ll

total 12

drwxr-xr-x 2 root root 4096 Jun 30 14:57 a

drwxr-xr-x 2 root root 4096 Jun 30 14:57 b

drwxr-xr-x 2 root root 4096 Jun 30 14:57 c

 

 

運維人員數據備份

[root@nfs01 fenlei]# rsync -av /fenlei/a [email protected]::backup/sa

sending incremental file list

created directory sa

a/

a/1

a/2

a/3

a/4

 

sent 222 bytes  received 88 bytes  620.00 bytes/sec

total size is 0  speedup is 0.00

 

 

開發人員數據備份

[root@nfs01 fenlei]# rsync -av /fenlei/b [email protected]::backup/dev

sending incremental file list

created directory dev

b/

b/1

b/2

b/3

b/4

 

sent 222 bytes  received 88 bytes  620.00 bytes/sec

total size is 0  speedup is 0.00

 

 

DBA人員數據備份

[root@nfs01 fenlei]# rsync -av /fenlei/c [email protected]::backup/dba

sending incremental file list

created directory dba

c/

c/1

c/2

c/3

c/4

 

sent 222 bytes  received 88 bytes  620.00 bytes/sec

total size is 0  speedup is 0.00

 

 

服務端查看備份數據:

[root@backup backup]# ll

total 12

drwxr-xr-x 3 rsync rsync 4096 Jun 30 16:37 dba

drwxr-xr-x 3 rsync rsync 4096 Jun 30 16:36 dev

drwxr-xr-x 3 rsync rsync 4096 Jun 30 16:36 sa

[root@backup backup]# tree ./

./

├── dba

   └── c

       ├── 1

       ├── 2

       ├── 3

       └── 4

├── dev

   └── b

       ├── 1

       ├── 2

       ├── 3

       └── 4

└── sa

    └── a

        ├── 1

        ├── 2

        ├── 3

        └── 4

 

6 directories, 12 files

說明:在客戶端做備份時,不必先在服務端創建對應下的目錄,只需要在客戶端備份命令的相應模塊後面加上相應的目錄名就行了

rsync -av /fenlei/b [email protected]::backup/dev

    注意:這種方式只能創建一級目錄,不能創建多級目錄

 

1.11.5 守護進程的訪問權限控制配置

hosts allow = 172.16.1.0/24  (只允許172.16.1.0/24這個網段的主機訪問)

 

 

hosts deny = 0.0.0.0/32      (禁止0.0.0.0/32這個網段的主機訪問)

因爲0.0.0.0/32這個地址不存在,那麼把黑名單轉換成白名單,就是沒有禁止訪問的網段,所以它的權限大於白名單的權限

說明:白名單和黑名單同時存在是,權限爲大着優先使用

 

實踐證明:將nfs服務器/fenlei目錄下的數據備份至rsync服務器的/backup目錄下,走公網

設想:不能備份

###服務端:

###[root@backup backup]# ll

###total 0

 

客戶端備份

[root@nfs01 fenlei]# rsync -av /fenlei/ [email protected]::backup

sending incremental file list

./

a/

a/1

a/2

a/3

a/4

b/

b/1

b/2

b/3

b/4

c/

c/1

c/2

c/3

c/4

 

sent 631 bytes  received 251 bytes  1764.00 bytes/sec

total size is 0  speedup is 0.00

 

 

服務端查看:

[root@backup backup]# ll

total 12

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 a

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 b

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 c

結論:白名單和黑名單同時存在是,權限爲大着優先使用

 

 

o  設計只讓內網地址爲172.16.1.0/24這個網段的主機備份

hosts allow = 172.16.1.0/24

#hosts deny = 0.0.0.0/32

 

[root@backup backup]# killall rsync

[root@backup backup]# killall rsync

rsync: no process killed

[root@backup backup]# rsync --daemon

[root@backup backup]# ps -ef|grep [r]sync

root       1710      1  0 17:11 ?        00:00:00 rsync --daemon

[root@backup backup]# netstat -lnupt|grep [r]sync

tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      1710/rsync         

tcp        0      0 :::873                      :::*                        LISTEN      1710/rsync         

再次測試:

刪除之前向服務端備份的數據:

[root@backup backup]# rm -rf *

[root@backup backup]# ll

total 0

 

客戶端備份數據:

[root@nfs01 fenlei]# rsync -av /fenlei/ [email protected]::backup

@ERROR: Unknown module 'backup'

rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]

###可以發現已經有權限限制

客戶端也沒有新的備份數據

[root@backup backup]# ll

total 0

1.11.6 守護進程無差異同步配置

首先確保,服務端與客戶端數據信息一致

1.11.6.1          推方式

說明:客戶端/fenlei目錄與服務端/backup無差異同步

服務端數據查看

[root@backup backup]# ll

total 4

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 a

 

客戶端數據查看

[root@nfs01 fenlei]# ll

total 12

drwxr-xr-x 2 root root 4096 Jun 30 14:57 a

drwxr-xr-x 2 root root 4096 Jun 30 14:57 b

drwxr-xr-x 2 root root 4096 Jun 30 14:57 c

 

客戶端執行無差異同步命令

[root@nfs01 fenlei]# rsync -avz --delete /fenlei/ [email protected]::backup

sending incremental file list

./

b/

b/1

b/2

b/3

b/4

c/

c/1

c/2

c/3

c/4

 

sent 452 bytes  received 172 bytes  1248.00 bytes/sec

total size is 0  speedup is 0.00

 

服務端再次查看數據,是否與客戶端數據一致

[root@backup backup]# ll

total 12

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 a

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 b

drwxr-xr-x 2 rsync rsync 4096 Jun 30 14:57 c

        推方式數據無差異同步:本地沒有的數據信息,遠程服務端也不能有/ 本地有的數據信息,遠程服務端也必須有

 

 

1.11.6.2          拉方式

服務端/backup_dev目錄客戶端/baoge_rsync目錄與無差異同步

服務端數據查看

[root@backup backup_dev]# ll

total 0

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:47 1.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:47 2.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:47 3.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:39 a.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:39 b.txt

-rw-r--r-- 1 rsync rsync 0 Jun 29 17:39 c.txt

 

客戶端數據查看

[root@nfs01 baoge_rsync]# ll

total 0

-rw-r--r-- 1 root root 0 Jun 29 17:47 1.txt

-rw-r--r-- 1 root root 0 Jun 29 17:47 2.txt

-rw-r--r-- 1 root root 0 Jun 29 17:47 3.txt

 

客戶端執行無差異同步命令

[root@nfs01 baoge_rsync]# rsync -avz --delete  [email protected]::backup_dev /baoge_rsync/

receiving incremental file list

./

a.txt

b.txt

c.txt

 

sent 137 bytes  received 309 bytes  892.00 bytes/sec

total size is 0  speedup is 0.00

 

客戶端查看數據有沒有同步

[root@nfs01 baoge_rsync]# ll

total 0

-rw-r--r-- 1 501 501 0 Jun 29 17:47 1.txt

-rw-r--r-- 1 501 501 0 Jun 29 17:47 2.txt

-rw-r--r-- 1 501 501 0 Jun 29 17:47 3.txt

-rw-r--r-- 1 501 501 0 Jun 29 17:39 a.txt

-rw-r--r-- 1 501 501 0 Jun 29 17:39 b.txt

-rw-r--r-- 1 501 501 0 Jun 29 17:39 c.txt

拉方式數據無差異同步:遠端沒有的數據信息,本地客戶端也不能有/ 遠端有的數據信息,本地客戶端也必須有

   注意:無差異同步數據備份功能慎用

 

此命令還有一個特殊用途:快速清空大文件

服務端大文件

[root@backup backup_dev]# ll

total 4

-rw-r--r-- 1 root root 1275 Jun 30 20:59 big.txt

 

客戶端執行無差異同步數據命令

客戶端的空文件

[root@nfs01 baoge_rsync]# ll

total 0

-rw-r--r-- 1 root root 0 Jun 30 21:00 null.txt

[root@nfs01 baoge_rsync]# rsync -avz --delete /baoge_rsync/null.txt [email protected]::backup_dev/big.txt --password-file=/etc/rsync.passwordsending incremental file list

null.txt

 

sent 69 bytes  received 39 bytes  216.00 bytes/sec

total size is 0  speedup is 0.00

 

查看服務端大文件的大小

[root@backup backup_dev]# ll

total 0

-rw-r--r-- 1 rsync rsync 0 Jun 30 21:00 big.txt

結論:已經快速清空了服務端大文件中的數據

 

1.11.7 守護進程的列表功能配置

  rsync服務端配置文件中

  list = false    --- 表示關閉顯示模塊信息列表功能

  list = true     --- 表示開啓顯示模塊信息列表功能

  

客戶端查看模塊信息命令

  rsync [email protected]::

參數功能說明:再執行同步數據命令的時候會把服務端配置文件裏面的所有模塊信息顯示出來,再無它用。

 

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章