實時同步 && 全網備份

一. 實時同步:

1. 實時同步服務概述:
原因:實時同步是一種只要當前目錄發生變化則會觸發一個事件,事件觸發後會將變化的目錄同步至遠程服務器。
意義:保證數據的連續性 ,減少人力維護成本
如何實現數據實時同步:
	1)發現數據產生變化:inotify(監控軟件)
	   inotifywait   參數   事件   監控目錄
	2)進行數據傳輸備份:rsync
2 實時同步實現方式(命令方式):
    存儲服務器部署
    第一個歷程:按照監控數據變化軟件
	條件:確認epel源可以正常使用
	yum install -y inotify-tools
	
	第二個歷程:監控命令使用
	/usr/bin/inotifywait    --- 監控數據變化命令
    /usr/bin/inotifywatch   --- 統計數據變化次數  添加 刪除 修改
	
	語法結構:inotifywait  --help
	inotifywait  參數  事件  監控目錄
	· 永久監控目錄中數據變化:  inotifywait -m  目錄
      -m|--monitor  	Keep listening for events forever. 
                        保持永久監控
      -d|--daemon   	Same as --monitor
	                    類似-m參數
						
    · 實現目錄中數據遞歸監控:  inotifywait -rm  目錄
	  -r|--recursive	Watch directories recursively.
	                    監控目錄中子目錄數據變化
						
	
    · 實時目錄中數據排除監控功能:	
	  --exclude <pattern>   Exclude all events on files matching the extended regular expression <pattern>
	                        排除指定數據信息不要進行監控 (區分大小寫識別信息)
      --excludei <pattern>  Like --exclude but case insensitive.
	                        排除指定數據信息不要進行監控 (忽略大小寫識別信息)  
					
    · 將無用的信息進制輸出
      -q|--quiet    	    Print less (only print events).	
	                        輸出少量信息(只輸出事件信息)
	
	· 知識輸出信息格式
	  --format <fmt>	Print using a specified printf-like format string; read the man page for more details.
	                    指定輸出信息格式
						%e  顯示觸發事件信息 
						%w  顯示監控目錄信息
						%f  觸發事件數據信息
	  --timefmt <fmt>	strftime-compatible format string for use with %T in --format string.
                        定義顯示的時間格式信息(時間格式的定義和 date命令類似)
	  -c|--csv      	Print events in CSV format.   (MySQL)
	  -e|--event <event1> [ -e|--event <event2> ... ]         Listen for specific event(s)
	                                                          指定監視事件信息
															  
	  														  
	  事件信息:inotify軟件採用觸發機制進行監控
      access		file or directory contents were read
	                文件或目錄內容被讀取時
	  modify		file or directory contents were written
	                文件或目錄內容被寫入
	  attrib		file or directory attributes changed
	                文件或目錄屬性信息被改變
	  close_write	file or directory closed, after being opened in writeable mode    ******
	                文件或目錄關閉,寫入新的信息之後
	  close_nowrite	file or directory closed, after being opened in read-only mode
	                文件或目錄關閉,只讀模式進行關閉
	  close		    file or directory closed, regardless of read/write mode
	                文件或目錄關閉,無論文件數據是否進行讀或者寫入
	  open		    file or directory opened
	                文件或目錄被打開了
	  moved_to	    file or directory moved to watched directory
	                文件或目錄移動到監控目錄中
	  moved_from	file or directory moved from watched directory
	                文件或目錄從監控目錄移除
	  move		    file or directory moved to or from watched directory             *****
	                文件或數據不管是從目錄中移除或是移入
	  create		file or directory created within watched directory               *******
	                文件或目錄被創建出來在監控目錄中
	  delete		file or directory deleted within watched directory               ******         
	                在監控目錄中文件或目錄被刪除
	  delete_self	file or directory was deleted
	                在監控目錄中文件或目錄被刪除
	  unmount		file system containing file or directory unmounted
	                文件系統中包含文件或目錄被卸載

3. 實時同步實現方式(腳本實現):
    編寫腳本思路:
	1)發現變化數據信息    inotifywait
       inotifywait -mrq --format "%w%f" /data -e "close_write,move,create,delete"
       數據信息  

	2)將變化數據進行傳輸  rsync
	   rsync -az  數據信息  rsync_backup@172.16.1.41::backup  --password-file=/etc/rsync.password 
	
    3) 監控操作和同步操作建立聯繫
       shell腳本循環語句:
	   · for    循環語句     --- 有限循環
	   · while  循環語句     --- 無限循環
	     條件爲真就會一直循環
	   · until  循環語句     --- 無限循環 
	     條件爲假就會一直循環
	   
	   腳本解釋過程:
	   inotifywait -mrq --format "%w%f" /data -e "close_write,move,create,delete"|while read data_info
	       信息   --- 賦值 買一 --- 循環動作 增一
	   do
	     rsync -az  $data_info --delete  rsync_backup@172.16.1.41::backup  --password-file=/etc/rsync.password 
       done

       腳本內容:
	   # cat /server/scripts/sync_data.sh 
       #!/bin/bash
       
       inotifywait -mrq --format "%w%f" /data -e "close_write,move,create,delete"|while read data_info
       do
          rsync -az  /data/ --delete  rsync_backup@172.16.1.41::backup  --password-file=/etc/rsync.password 
       done
4. 實時同步實現方式(軟件部署):
    sersync  --- 實時同步軟件  部署前提(inotify-tools  rsync守護進程模式)
    誕生過程:金山公司開發人員 周洋  --- 數據實時同步 (inotify+rsync 腳本)--- 開發實時同步程序(inotify+rsync)
	https://github.com/wsgzao/sersync          


    sersync軟件部署過程:
	第一個歷程:下載軟件二進制包
	https://github.com/wsgzao/sersync  
	
	第二個歷程:解壓軟件,並保存到相應目錄中
	cd /server/tools
	unzip sersync-master.zip
	cd sersync-master/
    tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
	tree GNU-Linux-x86/
	mkdir /usr/local/sersync
	mv GNU-Linux-x86/* /usr/local/sersync/
	
	第三個歷程:修改sersync配置文件
	vim confxml.xml
     6     <filter start="false">    --- 排除指定數據不要進行同步(默認關閉)
     7         <exclude expression="(.*)\.svn"></exclude>
     8         <exclude expression="(.*)\.gz"></exclude>
     9         <exclude expression="^info/*"></exclude>
    10         <exclude expression="^static/*"></exclude>
    11     </filter>
	
	12     <inotify>                 --- 監控事件信息
    13         <delete start="true"/>
    14         <createFolder start="true"/>
    15         <createFile start="false"/>
    16         <closeWrite start="true"/>
    17         <moveFrom start="true"/>
    18         <moveTo start="true"/>
    19         <attrib start="false"/>
    20         <modify start="false"/>
    21     </inotify>
	
	24         <localpath watch="/opt/tongbu">    --- 實現實時同步配置
    25             <remote ip="127.0.0.1" name="tongbu1"/>
    26             <!--<remote ip="192.168.8.39" name="tongbu"/>-->
    27             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
    28         </localpath>
    29         <rsync>
    30             <commonParams params="-artuz"/>
    31             <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
    32             <userDefinedPort start="false" port="874"/><!-- port=874 -->
    33             <timeout start="false" time="100"/><!-- timeout=100 -->
    34             <ssh start="false"/>
    35         </rsync>

    第四個歷程:啓動服務程序
	sersync -h
	參數-d:  啓用守護進程模式
	參數-r:  在監控前,將監控目錄與遠程主機用rsync命令推送一遍
	參數-o:  指定配置文件,默認使用confxml.xml文件  此時存放(/opt/sersync/confxml.xml)
	
	sersync  -dro  /usr/local/sersync/confxml.xml 
	
PPS:因爲sersync不屬於環境變量,所以在使用命令sersync時,應該在環境變量中添加變量


	實時同步服務出現異常:
	修改配置文件參數,進行調試
	<debug start="true"/>

在這裏插入圖片描述

二. 全網備份:

1. 全網備份需求說明:
基本要求:
要求:每天晚上 00 點整在 Web 服務器上打包備份系統配置文件、網站程序目錄及訪問日誌並通過rsync命令推送備份服務器backup上備份保留;
(備份思路可以是先在本地按日期打包,然後再推到備份服務器 backup 上),NFS 存儲服務器同 Web 服務器,實際工作中就是全部的服務器。


具體要求:
1)所有服務器的備份目錄必須都爲/backup。
2)要備份的系統配置文件包括但不限於:
  a.定時任務服務的配置文件(/var/spool/cron/root)(適合 web 和 nfs 服務器)。
  b.開機自啓動的配置文件(/etc/rc.local)(適合 web 和 nfs 服務器)。
  c.日常腳本的目錄 (/server/scripts)。
  d.防火牆 iptables 的配置文件(/etc/sysconfig/iptables)。
  e.自己思考下還有什麼需要備份呢?
3)Web 服務器站點目錄假定爲(/var/html/www)4)Web 服務器 A 訪問日誌路徑假定爲(/app/logs)
5)Web 服務器保留打包後的 7 天的備份數據即可(本地留存不能多於7天,因爲太多硬盤會滿)
6)備份服務器上,保留每週一的所有數據副本,其它要保留6個月的數據副本。
7)備份服務器上要按照備份數據服務器的內網IP爲目錄保存備份,備份的文件按照時間名字保存。
8)需要確保備份的數據儘量完整正確,在備份服務器上對備份的數據進行檢查,把備份的成功及失敗結果信息發給系統管理員郵箱中。
2. 第一步:先將每一步需求進行完成:
1)所有服務器的備份目錄必須都爲/backup
mkdir  /backup

2)要備份的系統配置文件包括但不限於:
   a.定時任務服務的配置文件(/var/spool/cron/root)(適合 web 和 nfs 服務器)。
   b.開機自啓動的配置文件(/etc/rc.local)(適合 web 和 nfs 服務器)。
   c.日常腳本的目錄 (/server/scripts)。
   d.防火牆 iptables 的配置文件(/etc/sysconfig/iptables)。
   e.自己思考下還有什麼需要備份呢?
tar zcvf /backup/sys_backup_$(date +%F_星期%w -d "-1 day").tar.gz /var/spool/cron/root /etc/rc.local /server/scripts /etc/sysconfig/iptables

3)Web 服務器站點目錄假定爲(/var/html/www)。
tar zcvf  /backup/www_data_$(date +%F_星期%w -d "-1 day").tar.gz   /var/html/www

4)Web 服務器 A 訪問日誌路徑假定爲(/app/logs)
tar zcvf  /backup/log_data_$(date +%F_星期%w -d "-1 day").tar.gz   /app/logs

5)Web 服務器保留打包後的 7 天的備份數據即可(本地留存不能多於 7 天,因爲太多硬盤會滿)
find /backup -type f -mtime +7 -delete

6)備份服務器上,保留每週一的所有數據副本,其它要保留6個月的數據副本。
find /backup -type f -mtime +180 -delete
方法一:排除每週一數據不要刪除
find /backup -type f ! -name "*星期1*" -a -mtime +180 -delete
方法二:識別每週一數據單獨保存
週日:存儲服務器 (/backup/週日備份)  --->  備份服務器(/backup)
週一:存儲服務器 (/backup/週一備份)  --->  備份服務器(/backup) 將當天的數據移動到 /week01目錄  
00:00   數據進行備份   週一    週日數據信息
00:00   數據進行備份   週二    週一數據信息    
00:30   備份服務器   find /backup/ -type f -name "*.tar.gz" -mmin -30 | xargs mv -t /week01 --> mv_data.sh
0 30 * * 2  /bin/sh mv_data.sh &>/dev/null

7)備份服務器上要按照備份數據服務器的內網 IP 爲目錄保存備份,備份的文件按照時間名字保存。(優選)
方法一:存儲服務傳輸備份數據時,創建相應子目錄
NFS:172.16.1.31   rsync -avz /backup/ rsync_backup@172.16.1.41::backup/172.16.1.31 --password-file=/etc/password
web:172.16.1.7    rsync -avz /backup/ rsync_backup@172.16.1.41::backup/172.16.1.7  --password-file=/etc/password
方法二:存儲服務本地創建相應子目錄

8)需要確保備份的數據儘量完整正確,在備份服務器上對備份的數據進行檢查,把備份的成功及失敗結果信息發給系統管理員郵箱中

驗證數據完整性能:利用數據指紋驗證數據完整性
存儲服務端:         -數據與指紋信息->     備份服務器:
對數據生成指紋信息                        識別數據信息,生成新的指紋 vs 收到指紋信息 對比
利用md6sum命令 生成指紋信息/對比指紋信息進行驗證
1. 生成指紋信息			find /backup/ -type f -name "*.tar.gz"|xargs md5sum >/backup/finger.txt
2. 將數據和指紋文件進行傳輸	rsync -avz /backup/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
3. 對指紋信息進行驗證		md5sum -c finger.txt

進行郵件發送告知:
1. 申請郵箱,開啓smtp功能/進行授權碼設置
2. 編寫郵件發送配置信息
	163郵箱:
	vim /etc/mail.rc
	set from=郵箱地址@163.com 
	smtp=smtp.163.com <- 郵件發送郵件服務器域名,此處爲163郵箱的發送服務器域名
	set smtp-auth-user=郵箱賬號 
	smtp-auth-password=登錄郵箱密碼 smtp-auth=login
	例如:
	set from=17778058507@163.com
	set smtp=smtp.163.com
	set smtp-auth-user=17778058507@163.com
	set smtp-auth-password=oldboy123(隨意寫的,不要借用)
	QQ郵箱:
	vim /etc/mail.rc
	set from=郵箱地址@qq.com 
	smtp=smtp.qq.com <- 郵件發送郵件服務器域名,此處爲163郵箱的發送服務器域名
	set smtp-auth-user=郵箱賬號 
	smtp-auth-password=登錄郵箱驗證碼之類的 smtp-auth=login
	例如:
	set from=3034698155@qq.com
	set smtp=smtp.qq.com
	set smtp-auth-user=3034698155@qq.com
	set smtp-auth-password=xxxxxxxxxx(隨意寫的,不要借用)
3. 重啓郵件服務
	systemctl restart postfix
4. 郵件發送測試
3. 第二步:將所有完成項目進行整合:

網站服務器(web服務器)上腳本編寫

#!/bin/bash
host_IP=$(hostname -i)
backup_data="./var/spool/cron/root ./etc/rc.local"
    
#01. 創建存儲數據目錄
mkdir -p /backup/$host_IP/
    
#02. 將數據本地保存壓縮備份
mkdir -p /server/scripts /var/html/www /app/logs 
touch /etc/sysconfig/iptables
cd /
tar zchf /backup/$host_IP/sys_backup_$(date +%F_星期%w -d "-1 day").tar.gz $backup_data
    
#03. 將當天生成備份文件進行指紋驗證,生成指紋文件
find /backup -type f -name "*.tar.gz" -mtime -1 | xargs md5sum >/backup/$host_IP/finger.txt 
    
#04. 實現數據遠程備份
rsync -az /backup/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
    
#05. 將歷史數據信息進行清理
find /backup/ -type f -name "*.tar.gz" -mtime +7 -delete

存儲服務器(nfs服務器)上腳本編寫

#!/bin/bash
host_IP=$(hostname -i)
backup_data="./var/spool/cron/root ./etc/rc.local ./server/scripts ./etc/sysconfig/iptables"
    
#01. 創建存儲數據目錄
mkdir -p /backup/$host_IP/
    
#02. 將數據本地保存壓縮備份
mkdir -p /server/scripts /var/html/www /app/logs 
touch /etc/sysconfig/iptables
cd /
tar zchf /backup/$host_IP/sys_backup_$(date +%F_星期%w -d "-1 day").tar.gz $backup_data
tar zcf /backup/$host_IP/www_data_$(date +%F_星期%w -d "-1 day").tar.gz ./var/html/www
tar zcf /backup/$host_IP/log_data_$(date +%F_星期%w -d "-1 day").tar.gz ./app/logs
    
#03. 將當天生成備份文件進行指紋驗證,生成指紋文件
find /backup -type f -name "*.tar.gz" -mtime -1 | xargs md5sum >/backup/$host_IP/finger.txt 
    
#04. 實現數據遠程備份
rsync -az /backup/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
    
#05. 將歷史數據信息進行清理
find /backup/ -type f -name "*.tar.gz" -mtime +7 -delete

備份服務器(backup服務器)上腳本編寫

#!/bin/bash

#01. 驗證數據完整性
find /backup -type f -name "finger.txt"|xargs md5sum -c >/backup/check_log.txt
#02. 將驗證結果進行郵件發送
mail -s "data_check_$(date +%F_%T)" 3034698155@qq.com </backup/check_log.txt
#03. 將數據進行清理操作
find /backup -type f ! -name "*星期1*" -a -mtime +180 -delete
4. 第三步:將全網備份進行最終測試(編寫腳本的定時任務):

web服務器上定時任務

00 00 * * *  /bin/sh /server/scripts/backup_data_sh

存儲服務器上定時任務

00 00 * * *  /bin/sh /server/scripts/backup_data_sh

備份服務器上定時任務

#因爲不知道上兩個定時任務什麼時候能結束,所以這個定時任務可以時間定晚一些
00 05 * * *  /bin/sh /server/scripts/backup_mail_sh
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章