天天看點

rsync+nfs+inotify

rsync:備份服務和scp差不多但是不同的是rsync是增量備份而scp是全量備份,更加節省磁盤。(備份伺服器(資料庫檔案,全網重要檔案

))

好處:增量備份

缺點:大檔案傳輸存在瓶頸

------------------------------------------------------------------------------------------------

nfs:網絡檔案系統(檔案伺服器(jsp,jpg,gif等檔案))

優點:穩定性較好。

缺點:隻能處理tb級檔案。容易出現單點故障問題(可以用DRBD+HEARTBEAT來調整解決),資料不安全(明文傳輸)

-------------------------------------------------------------------------------------------------

inotify:将監控的檔案顯示出來

好處:rsync隻能手動找檔案進行傳輸,有了inotify之後就可以通過監控一個目錄,然後通過腳本進行自動化的備份。

缺點:隻有單線程,有檔案限制200個檔案之後就會出現延遲。

部署過程:建立兩台機器A,B(A:rsync備份機器;B:nfs+inoti+test測試機器)

-----------------------------------------------------------伺服器A-------------------------------------------------------

1.關閉防火牆(這裡我做了開機的啟動優化)

awk': chkconfig --list | grep 3:on | grep -vE "sshd|rsyslog|sysstat|network|sshd" | awk '{print "chkconfig",$1,"off"}' | 

bash

2.安裝rsync

yum install rsync -y

3.建立rsync配置檔案目錄和配置檔案

mkdir /etc/rsync && cd /etc/rsync && touch rsync.conf

4.配置rsync配置檔案 

uid = rsync

gid = rsync

use chroot = no

max connections = 5

pid file = /var/run/rsyncd.pid

log file = /var/log/rsyncd.log

[rong]

path=/data

ignore errors

read only = no

auth users = ls

secrets file = /etc/rsync/passwd

5.建立使用者和備份目錄

useradd rsync

mkdir /data

chown rsync.rsync /data

chmod 777 /data

6.建立ls(前面配置檔案定義的虛拟使用者)使用者的密碼檔案并配置權限

echo "ls:123456" > /etc/rsync/passwd

chmod 600 /etc/rsync/passwd

7.啟動rsync(這裡需要注意rsync是背景啟動的)

rsync --daemon --config=/etc/rsync/rsync.conf

8檢視是否啟動

lsof -i:873

到這裡我們A(rsync)就配置完成了

---------------------------------------------------------伺服器B-------------------------------------------------------

2.1配置rsync的用戶端密碼檔案并賦予權限

echo "123456" > /root/passwd

chmod 600 /root/passwd

2.2測試用戶端的rsync是否好用

rsync -r /data/ [email protected]::rong --password-file=/root/passwd

3.安裝nfs伺服器(一般預設是安裝的)

yum install nfs

3.1修改nfs伺服器配置檔案

echo "/data *(rw,all_squash)" >> /etc/exports

3.2建立nfs共享檔案

3.3.啟動rpcbind

/etc/init.d/rpcbind start

3.4.啟動nfs

/etc/init.d/nfs start

3.5檢視nfs是否成功并挂載

showmount -e localhost

mount 10.0.0.101:/data/ /mnt/

4.安裝inotify

4.1安裝epel源

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

4.2安裝inotify

 yum install inotify-tools -y

4.3檢視inotify是否好用

A:inotifywait -rmq --format "%w%f" -e create,delete,close_write /data/

B:另建立視窗,往/data中建立檔案看A視窗是否有顯示

4.4編寫inotify監控腳本

#!/bin/bash

inotifywait -rmq --format "%w%f" -e create,delete,close_write /data/ | while read line

do

        if [ -f $line ]

        then

                rsync -az --delete $line [email protected]::rong --password-file=/root/passwd

        else

                rsync -za --delete -r /data/ [email protected]::rong --password-file=/root/passwd

        fi

done

4.5安裝screen

yum install screen -y

4.6建立screen背景視窗運作腳本

screen -S jk

sh jiankong.sh

Ctrl +A+D 退出

4.7檢視是否能夠實作nfs挂載目錄的備份

cd /mnt

touch aa