1. nfs存儲的單點問題
如果nfs服務器宕機了,則所有的nfs客戶機都會受到影響。一旦宕機,會丟失部分用戶的數據。為了解決單點問題,需要實現共享存儲的實時備份,即:將nfs服務端共享目錄下的數據實時備份到備份服務器(或其它存儲設備),以保證數據的完整性。
2. NFS共享數據的實時同步推送備份
公司有兩台web服務器一直在對外提供服務,但隨着業務的發展用戶越來越多,網站的功能也越來越強大,各種圖片,視頻等占用硬盤空間越來越大。
於是,領導將web服務器的數據直接存儲到NFS服務器上作為存儲使用;並且為了防止NFS服務器發生單點故障,領導希望將web服務器存儲的內容實時同步到Rsync備份服務器上。現在由你來計划完成領導的需求。
具體要求如下:
- NFS服務器的要求如下:
- 服務器的共享目錄名為/data目錄;
- 權限要求只能內網網段訪問且可讀可寫,時時同步;
- 為了方便管理人員管理,需要指定NFS虛擬賬戶為zuma,uid=888,gid=888
- 所有訪問者的身份都壓縮為最低身份
- 將/data目錄里的內容同步時時推送到備份服務器的/data目錄里(inotify+rsync)
- web服務器將NFS共享目錄統一掛載到/var/html/www目錄下
思路:
1. NFS存儲服務器和Rsync備份服務器,Rsync服務器部署運行rsync --daemon服務,NFS服務器作為Rsync的客戶端,可以通過rsync -avz /data rsync_backup@192.168.0.41::nfsbackup/ --password-file=/etc/rsync.password命令,將/data目錄的文件備份到Rsync備份服務器。
2. NFS存儲服務器部署完成,正常運行情況下。
3. NFS存儲服務器和Rsync備份服務器之間,通過crond+rsync服務,通過定時任務的備份服務,將數據備份到Rsync備份服務器
4. 想要實現實時備份,則在NFS存儲服務器上通過inotify,sersync或irsync服務,監控NFS存儲服務器共享目錄的磁盤的block的變化,觸發推動執行同步。
2.1 環境准備
操作系統和內核版本
[root@web01-8 ~]# cat /etc/redhat-release CentOS release 6.7 (Final) [root@web01-8 ~]# uname -r 2.6.32-573.el6.x86_64
角色-ip
角色 | 主機名 | eth0(外網) | eth1(內網) |
---|---|---|---|
C1-NFS服務器 | nfs01 | 10.0.0.31 | 192.168.0.31 |
C2-Rsync存儲服務器 | backup | 10.0.0.41 | 192.168.0.41 |
2.2 NFS服務的部署
部署過程詳情見:https://www.cnblogs.com/zoe233/p/11973710.html
【NFS服務端】
# 系統環境 [root@nfs-31 mnt]# cat /etc/redhat-release CentOS release 6.10 (Final) [root@nfs-31 mnt]# uname -r 2.6.32-573.el6.x86_64 [root@nfs-31 mnt]# uname -m x86_64 # 查看rpcbind和nfs服務,並設置開機自啟動 [root@nfs-31 mnt]# rpm -qa nfs-utils rpcbind rpcbind-0.2.0-16.el6.x86_64 nfs-utils-1.2.3-78.el6_10.1.x86_64 [root@nfs-31 mnt]# /etc/init.d/rpcbind status rpcbind 已停 [root@nfs-31 mnt]# /etc/init.d/rpcbind start 正在啟動 rpcbind: [確定] [root@nfs-31 mnt]# /etc/init.d/nfs status rpc.svcgssd 已停 rpc.mountd 已停 nfsd 已停 rpc.rquotad 已停 [root@nfs-31 mnt]# /etc/init.d/nfs start 啟動 NFS 服務: [確定] 關掉 NFS 配額: [確定] 啟動 NFS mountd: [確定] 啟動 NFS 守護進程: [確定] 正在啟動 RPC idmapd: [確定] [root@nfs-31 mnt]# chkconfig --list rpcbind rpcbind 0:關閉 1:關閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關閉 [root@nfs-31 mnt]# chkconfig --list nfs nfs 0:關閉 1:關閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關閉 [root@nfs-31 mnt]# tail -3 /etc/rc.local # chkconfig和/etc/rc.local的配置二選一即可。 # start up nfs service /etc/init.d/rpcbind start /etc/init.d/nfs start # 創建需要共享的目錄並授權 mkdir /data -p grep nfsnobody /etc/passwd chown -R nfsnobody.nfsnobody /data ls -ld /data # 配置NFS服務配置文件,並且在本地查看掛載信息 [root@nfs-31 mnt]# cat /etc/exports # shared /data by zoe for test at 20191205 /data 192.168.0.0/24(rw,sync) [root@nfs-31 mnt]# exportfs -rv # 加載配置,可以用來檢查配置文件是否合法 exporting 192.168.0.0/24:/data # 在NFS服務器本地查看掛載情況 showmount -e 192.168.0.31 showmount -e localhost # 通過查看nfs服務器配置文件的參數(包括默認加載的參數) [root@nfs-31 mnt]# cat /var/lib/nfs/etab /data 192.168.0.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,anonuid=65534,anongid=65534,sec=sys,rw,root_squash,no_all_squash)
在本地服務器端,同時又作為客戶端進行掛載測試:
[root@nfs-31 mnt]# mount -t nfs 192.168.0.31:/data /mnt [root@nfs-31 mnt]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 6.9G 1.9G 4.7G 28% / tmpfs 499M 0 499M 0% /dev/shm /dev/sda1 190M 67M 114M 37% /boot 192.168.0.31:/data 6.9G 1.9G 4.7G 28% /mnt
成功將nfs的共享目錄掛載在/mnt目錄下。
【NFS客戶端】
在所有的NFS客戶端上執行的操作都是相同的。
# 系統環境 [root@backup-41 ~]# cat /etc/redhat-release CentOS release 6.10 (Final) [root@backup-41 ~]# uname -r 2.6.32-573.el6.x86_64 [root@backup-41 ~]# uname -m x86_64 # 檢查安裝包 [root@backup-41 ~]# rpm -qa rpcbind rpcbind-0.2.0-16.el6.x86_64 # 為了使用showmount等功能,所有客戶端最好也安裝NFS軟件,但不啟動NFS服務 rpm -qa nfs-utils # 啟動rpc服務(不需要啟動NFS服務) [root@backup-41 ~]# /etc/init.d/rpcbind status rpcbind is stopped [root@backup-41 ~]# /etc/init.d/rpcbind start Starting rpcbind: [ OK ] [root@backup-41 ~]# showmount -e 192.168.0.31 Export list for 192.168.0.31: /data 192.168.0.0/24 # 掛載NFS共享目錄/data [root@backup-41 ~]# mount -t nfs 192.168.0.31:/data /mnt [root@backup-41 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 6.9G 1.9G 4.7G 28% / tmpfs 499M 0 499M 0% /dev/shm /dev/sda1 190M 67M 114M 37% /boot 192.168.0.31:/data 6.9G 1.9G 4.7G 28% /mnt # 測試 [root@backup-41 mnt]# cd /mnt [root@backup-41 mnt]# ls [root@backup-41 mnt]# mkdir /mnt/backup/rpcbind/test -p [root@backup-41 mnt]# ls backup file # 在nfs服務端查看共享目錄/data [root@nfs-31 mnt]# ls /data backup file
將rpcbind服務和掛載加入開機自啟動:
[root@backup-41 mnt]# tail -3 /etc/rc.local # rpcbind start and mount shared directory ip:/data /etc/init.d/rpcbind start /bin/mount -t nfs 192.168.0.31:/data /mnt
2.3 Rsync服務的部署
部署過程詳情參見:https://www.cnblogs.com/zoe233/p/11962110.html
本節可以將nfs的備份數據統一放在某個模塊下,所以可以通過修改配置文件/etc/rsyncd.conf添加模塊
[root@backup-41 192.168.0.8]# tail -20 /etc/rsyncd.conf ignore errors read only = false list = false hosts allow = 192.168.0.31/24 hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password ###### [backup] path = /backup/ [multi_module_1] path = /multi_module_1/ [nfsbackup] path = /nfsbackup/ ## rsync_config____end ##
在配置文件里添加模塊[nfsbackup]。
注意:多個模塊的信息相同的,可以統一放在模塊上方,如ignore errors等參數。
重啟rsync --daemon服務。
[root@backup-41 192.168.0.8]# pkill rsync [root@backup-41 192.168.0.8]# lsof -i tcp:873 [root@backup-41 192.168.0.8]# rsync --daemon [root@backup-41 192.168.0.8]# lsof -i tcp:873 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rsync 16317 root 3u IPv4 211440 0t0 TCP *:rsync (LISTEN) rsync 16317 root 5u IPv6 211441 0t0 TCP *:rsync (LISTEN)
根據新模塊nfsbackup的配置要求,創建/nfsbackup目錄,以及設置目錄的屬主和屬組。
[root@backup-41 192.168.0.8]# mkdir /nfsbackup -p [root@backup-41 192.168.0.8]# chown -R rsync.rsync /nfsbackup [root@backup-41 192.168.0.8]# ll -d /nfsbackup/ drwxr-xr-x 2 rsync rsync 4096 Dec 12 13:00 /nfsbackup/
2.4 inotify
inotify的具體內容查看:https://www.cnblogs.com/zoe233/p/12035383.html
2.4.1 inotify安裝
查看當前系統是否支持inotify
[root@nfs-31 data]# uname -r 2.6.32-573.el6.x86_64 [root@nfs-31 data]# ls -l /proc/sys/fs/inotify/ total 0 -rw-r--r-- 1 root root 0 Dec 12 18:59 max_queued_events -rw-r--r-- 1 root root 0 Dec 12 18:59 max_user_instances -rw-r--r-- 1 root root 0 Dec 12 18:59 max_user_watches # 顯示這三個文件則證明支持inotify
安裝inotify軟件:
[root@nfs-31 inotify]# rpm -qa inotify-tools [root@nfs-31 inotify]# yum install inotify-tools -y # Error: Nothing to do
安裝失敗,用wget獲取源,再用yum安裝:
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo yum -y install inotify-tools
[root@nfs-31 inotify]# rpm -qa inotify-tools
inotify-tools-3.14-2.el6.x86_64
一共安裝了2個工具,即inotifywait和inotifywatch。
- inotifywait:在被監控的文件或目錄上等待特定文件系統事件(open,close,delete等)發生,執行后處於阻塞狀態,適合shell腳本中使用。
- inotifywatch:收集被監視的文件系統使用度統計數據,指文件系統事件發生的次數統計。
2.4.2 inotifywatch命令
比較重要的參數的含義:
inotifywait參數 | 含義說明 |
---|---|
-r --recursive | 遞歸查詢目錄 |
-q --quiet | 打印很少的信息,僅僅打印監控事件的信息 |
-m,--monitor | 始終保持事件監聽狀態 |
--exclude | 排除文件或目錄時,不區分大小寫。 |
--timefmt | 指定時間輸出的格式 |
--format | 打印使用指定的輸出類似格式字符串 |
-e,--event | 通過此參數可以指定需要監控的事件,如下一個列表所示 |
-e,--event 事件的各種事件含義:
Events | 含義 |
---|---|
access | 文件或目錄被讀取 |
modify | 文件或目錄內容被修改 |
attrib | 文件或目錄屬性被改變 |
close | 文件或目錄封閉,無論讀/寫模式 |
open | 文件或目錄被打開 |
moved_to | 文件或目錄被移動至另外一個目錄 |
move | 文件或目錄被移動到另一個目錄或從另一個目錄移動至當前目錄 |
create | 文件或目錄被創建在當前目錄 |
delete | 文件或目錄被刪除 |
umount | 文件系統被卸載 |
--format 的格式意義:
- %w 發生事件的監視文件的名稱
- This will be replaced with the name of the Watched file on which an event occurred.
- %f 當一個事件發生在一個目錄中時,它將被替換為導致該事件發生的文件名。否則,將替換為空字符串。
- When an event occurs within a directory, this will be replaced with the name of the File which caused the event to occur. Otherwise, this will be replaced with an empty string.
- %e 發生的事件,以逗號分隔。
- Replaced with the Event(s) which occurred, comma-separated.
- %Xe 發生的事件,用“X”中的任何字符分隔。
- Replaced with the Event(s) which occurred, separated by whichever character is in the place of ‘X’.
- %T 替換為--timefmt選項指定格式的當前時間,該格式字符串應適合傳遞給strftime(3)。
- Replaced with the current Time in the format specified by the --timefmt option, which should be a format string suitable for passing to strftime(3).
2.4.3 人工測試同步
開啟兩個窗口
測試create事件
在第一個窗口開啟inotifywait,監聽/backup目錄:
[root@nfs-31 inotify]# inotifywait -mrq --timefmt '%y/%m/%d %H:%M' --format '%T %w%f' -e create /backup
# 命令說明:
# -mrq:-m 實時監聽,-r遞歸監控整個目錄,包括子目錄,-q 只輸出簡短信息
# --timefmt:指定輸出的時間格式
# --format:輸出輸出的格式
# -e create:指定監控的事件類型,監控創建create事件
第二個窗口,進入/backup目錄,創建兩個文件,觸發create事件
[root@nfs-31 backup]# cd /backup [root@nfs-31 backup]# touch inotifywait_create_event_1 [root@nfs-31 backup]# touch inotifywait_create_event_2
觸發事件后,查看第一個窗口會發現,屏幕輸出了創建事件的內容(時間和創建的文件路徑加名稱)
[root@nfs-31 inotify]# inotifywait -mrq --timefmt '%y/%m/%d %H:%M' --format '%T %w%f' -e create /backup 19/12/12 19:26 /backup/inotifywait_create_event_1 19/12/12 19:41 /backup/inotifywait_create_event_2
2.4.4 編寫inotify實時監控腳本
[root@nfs-31 /]# cd /server/scripts [root@nfs-31 scripts]# ls backup.sh [root@nfs-31 scripts]# vi inotifywait_nfs_to_backup.sh [root@nfs-31 scripts]# cat inotifywait_nfs_to_backup.sh #!/bin/bash
Path=/data backup_Server=192.168.0.41 /usr/bin/inotifywait -mrq --format '%w%f' -e close_write,delete $Path|while read line do if [ -f $line ];then rsync -az $line --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password else cd $Path &&\ rsync -az ./ --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password fi done
腳本可以加入開機啟動:
echo "/bin/sh /server/scripts/inotifywait_nfs_to_backup.sh &" >> /etc/rc.local
提示:
-
一個& 代表從后台開始運行該條命令