第六章 NFS、rsync等統一使用者相關操作
阿新 • • 發佈:2020-08-15
一、統一使用者
1.httpd
2.NFS掛載目錄
3.rsync
1.所有伺服器統一建立使用者
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666
2.修改httpd使用者
[root@web01 ~]# vim /etc/httpd/conf/httpd.conf
User www
Group www
#重啟服務
[root@web01 ~]# systemctl restart httpd
[root@web01 ~]# ps -ef | grep httpd
root 31537 1 1 21:57 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
www 31538 31537 0 21:57 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
www 31539 31537 0 21:57 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
www 31540 31537 0 21:57 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
www 31541 31537 0 21:57 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
www 31542 31537 0 21:57 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 31544 18601 0 21:57 pts/1 00:00:00 grep --color=auto httpd
3.修改NFS服務端許可權
[root@NFS /data]# vim /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
#重啟
[root@NFS /data]# systemctl restart NFS
4.修改rsync使用者
[root@backup ~]#vim /etc/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[backup]
comment = welcome to oldboyedu backup!
path = /backup
5.NFS服務端重新授權目錄
[root@NFS ~]# chown -R www.www /data/
6.web端重新掛載
[root@web01 /var/www/html]# umount /var/www/html/upload
[root@web01 /var/www/html]# mount -t NFS 172.16.1.31:/data ./upload
7.rsync服務端重新授權目錄
[root@backup ~]#chown -R www:www /backup
二、NFS總結
1.NFS儲存優點
1)簡單易用,部署方便
2)資料可查,服務穩定
2.NFS的服務缺點
1)存在單點故障, 如果構建高可用維護麻煩web->NFS()->backup
2)NFS資料明文, 並不對資料做任何校驗。
3)客戶端掛載NFS服務沒有密碼驗證, 安全性一般(內網使用)
3.NFS應用建議
1)開機掛載
如果希望NFS檔案共享服務能一直有效,則需要將其寫入到fstab檔案中
#編輯fstab檔案
[root@NFS-client ~]# vim /etc/fstab
172.16.1.31:/data /NFSdir NFS defaults 0 0
#驗證fstab是否寫正確
[root@NFS-client ~]# mount -a
2)生產場景應將靜態資料儘可能往前端推,減少後端儲存壓力
3)必須將儲存裡的靜態資源通過CDN快取,jpg\png\mp4\avi\css\js
4)如果沒有快取或架構本身歷史遺留問題太大, 在多儲存也無用
三、rsync+NFS解決單點故障
1.環境準備
主機 | 角色 | IP |
---|---|---|
backup | rsync服務端,NFS服務端 | 172.16.1.41 |
NFS | rsync客戶端,NFS服務端 | 172.16.1.31 |
web01 | rsync客戶端,NFS客戶端 | 172.16.1.7,10.0.0.7 |
2.web01搭載上傳作業平臺
1.安裝httpd和php
[root@web01 ~]# yum install -y httpd php
2.上傳程式碼
[root@web01 ~]# rz kaoshi.zip
[root@web01 ~]# yum unzip
[root@web01 ~]# unzip kaoshi.zip -d /var/www/html/
3.建立httpd使用者
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666
4.配置httpd服務
[root@web01 ~]# vim /etc/httpd/conf/httpd.conf
User www
Group www
#開啟服務
[root@web01 ~]# systemctl start httpd
5.修改許可權
[root@web01 ~]# chown -R www.www /var/www/html/
3.NFS伺服器搭建NFS服務端
1.安裝NFS
[root@NFS ~]# yum install -y NFS-utils rpcbind
2.配置NFS
[root@backup ~]# vim /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
3.檢視配置生效
[root@backup ~]# cat /var/lib/nfs/etab
/data 172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=666,anongid=666,sec=sys,rw,secure,root_squash,all_squash)
4.建立使用者
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666
5.建立目錄
[root@web01 ~]# mkdir /data
[root@web01 ~]# chown -R www.www /data
4.掛載web01資料目錄
[root@web01 ~]# showmount -e 172.16.1.31
[root@web01 ~]# mount -t nfs 172.16.1.31:/data /var/www/html/upload
5.backup搭建rsync服務端
1.安裝rsync
[root@backup ~]# yum -y install rsyncd
2.配置rsync
[root@backup ~]# cat /etc/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = true
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[backup]
comment = "檔案備份目錄"
path = /backup
[data]
comment = "資料備份目錄"
path = /data
3.重啟rsync
[root@backup ~]# systemctl restart rsyncd
6.NFS資料實時備份到backup伺服器
1.安裝inotify
[root@nfs ~]# yum -y install inotify-tools
2.編寫指令碼實時備份data目錄
[root@nfs /data]# vim /scripts/backup_data.sh
7.測試
1.訪問交作業頁面,上傳圖片
2.檢視web伺服器
[root@web01 ~]# ll /var/www/html/upload
3.檢視nfs伺服器data目錄
[root@nfs ~]# ll /data/
4.檢視backup伺服器data目錄
[root@backup ~]# ll /data
8.backup搭建NFS服務端
1.安裝NFS
[root@backup ~]# yum install -y nfs-utils rpcbind
2.配種NFS
[root@backup ~]# vim /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
3.建立使用者
[root@backup ~]# groupadd www -g 666
[root@backup ~]# useradd www -u 666 -g 666
4.啟動服務
[root@backup ~]# systemctl start nfs
9.測試
1.NFS伺服器出現故障
[root@nfs ~]# systemctl stop nfs
2.切換掛載機器
[root@web01 ~]# umount -lf /var/www/html/upload
[root@web01 ~]# mount -t nfs 172.16.1.41:/data /var/www/html/upload
四、實時同步
1.實時同步簡單介紹
1.概述
實時同步是一種只要當前目錄發生變化則會觸發一個事件,事件觸發後會將變化的目錄同步至遠端伺服器
2.為什麼要實時同步
保證資料的連續性, 減少人力維護成本,解決nfs單點故障
3.實時同步資料選擇
sersync+RSYNC(√)、inotify+rsync
Inotify是一個通知介面,用來監控檔案系統的各種變化,如果檔案存取,刪除,移動。可以非常方便地實現檔案異動告警,增量備份,並針對目錄或檔案的變化及時作出響應。rsync+inotify可以實觸發式實時同步增量備份
sersync是國人基於rsync+inotify-tools開發的工具,不僅保留了優點同時還強化了實時監控,檔案過濾,簡化配置等功能,幫助使用者提高執行效率,節省時間和網路資源。
2.安裝sersync(NFS伺服器)
1.安裝sersync和inotify
[root@nfs01 ~]# yum install rsync inotify-tools -y
2.下載serdync軟體包
[root@nfs01 ~]# wget https://raw.githubusercontent.com/wsgzao/sersync/master/sersync2.5.4_64bit_binary_stable_final.tar.gz
3.解壓安裝包
[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
4.移動並改名
[root@nfs ~]# mv GNU-Linux-x86 /usr/local/sersync
5.修改配置檔案
[root@nfs ~]# cat /usr/local/sersync/confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
#主機IP
<host hostip="localhost" port="8008"></host>
#除錯模式
<debug start="false"/>
<fileSystem xfs="false"/>
#檔案過濾
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
#inotify監控配置
<inotify>
#inotify監控的行為
<delete start="true"/>
<createFolder start="true"/>
<createFile start="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/>
<modify start="true"/>
</inotify>
#推送部分
<sersync>
#本地監控的目錄
<localpath watch="/data">
#遠端IP及模組名字
<remote ip="172.16.1.41" name="data"/>
</localpath>
<rsync>
#rsync同步時的引數
<commonParams params="-artuz"/>
#開啟認證
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
#如果rsync服務不是873埠,需要開啟
<userDefinedPort start="false" port="874"/><!-- port=874 -->
#超時時間
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
#指定錯誤日誌
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
#定時任務,預設600分鐘進行一次全備
<crontab start="false" schedule="600"><!--600mins-->
#定時任務檔案過濾
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>
<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name=