sersync之不洗澡

inotiry圖片參考

sersync圖片參考

 

 

 

 

 

inotify文字教程

該軟件對系統有要求,內核2.6以上,而且有以下目錄,後面會講解三個文件用途web

  [root@jokerpro ~]# uname -r
  3.10.0-693.2.2.el7.x86_64shell

[root@jokerpro ~]# ls -l /proc/sys/fs/inotify/
-rw-r--r-- 1 root root 0 May 31 10:32 max_queued_events
-rw-r--r-- 1 root root 0 May 31 10:32 max_user_instances
-rw-r--r-- 1 root root 0 May 31 10:32 max_user_watches

安裝該軟件express

1,安裝inotify-tools
yum install inotify-tools -y
2,命令集工具,即inotifywait和inotifywatch
inotifywait:在被監控的文件或目錄上等待特定文件系統事件 (open,close,delete等)發生,執行後處於阻塞狀態,適合在shell腳本中使用
inotifywatch:收集被監控的文件系統使用度統計數據,指文件系統事件發生的次數統計
3,簡單測試
inotifywait -mqr --excludei=a.txt --timefmt '%d/%m/%y%H:%M' --format '%T %w%f' -e create,close_write /backup3

附贈參數說明bash

inotifywait 參數說明服務器

參數名稱 參數說明
-m,–monitor 始終保持事件監聽狀態
-r,–recursive 遞歸查詢目錄
-q,–quiet 只打印監控事件的信息
–excludei 排除文件或目錄時,不區分大小寫
-t,–timeout 超時時間
–timefmt 指定時間輸出格式
–format 指定時間輸出格式
-e,–event 後面指定刪、增、改等事件
--format : 自定義inotifywait的輸出格式,如--format '%T %w%f',格式解釋以下
%w :顯示被監控目錄的名字
%f : 發生變化的文件名
%T: 使用--timefmt選項中自定義的時間格式

inotifywait events事件說明app

事件名稱 事件說明
access 讀取文件或目錄內容
modify 修改文件或目錄內容
attrib 文件或目錄的屬性改變
close_write 修改真實文件內容
close_nowrite  
close  
open 文件或目錄被打開
moved_to 文件或目錄移動到
moved_from 文件或目錄從移動
move 移動文件或目錄移動到監視目錄
create 在監視目錄下建立文件或目錄
delete 刪除監視目錄下的文件或目錄
delete_self  
unmount 卸載文件系統

優化 Inotify

#默認狀況下不須要優化ssh

在/proc/sys/fs/inotify目錄下有三個文件,對inotify機制有必定的限制socket

 

#文件說明
max_user_watches #設置inotifywait或inotifywatch命令能夠監視的文件數量(單進程)
max_user_instances #設置每一個用戶能夠運行的inotifywait或inotifywatch命令的進程數
max_queued_events #設置inotify實例事件(event)隊列可容納的事件數量

 

附贈一個監控腳本,rsync+inotify都應該裝在邏輯服上,推送到備份服務器上ide

#!/bin/bash
# The author is joker, which is used to listen to directories and synchronize files.
# 判軟件安裝
inotify_order=`which inotifywait 1>/dev/null 2>&1`
inotify_state=`echo $?`
rsync_order=`which rsync 1>/dev/null 2>&1`
rsync_state=`echo $?`
if [ $inotify_state -eq 1 -a $rsync_state -eq 1 ];then
    yum install inotify-tools rsync -y
    sleep1
    sh $0
else
# 腳本是時刻監控path目錄發生的變化,應該存放於rsync的客戶端,即邏輯服務器
# 被監控目錄
localpath=/joker/
# rsync驗證用戶,密碼
auth_user=rsync_backup
cat >/etc/rsync.passwd<<EOF
woshimima
EOF
passwd_file=/etc/rsync.passwd
chmod 600 /etc/rsync.passwd
# 備份服務器IP
ip="60.205.188.107"
# 備份服務器備份目錄,匹配rsync模塊
backupdir=gameserver1
# m保持監聽狀態,r遞歸查詢目錄,q打印監控事件信息,w顯示被監控目錄名字,f發生變化的文件名
inotifywait -mrq --format '%w%f' -e close_write,delete $localpath \
|while read file
do
    if [ -f $file ];then
        rsync -az $file --delete $auth_user@$ip::$backupdir --password-file=$passwd_file
    else
        cd $localpath
        rsync -az ./ --delete $auth_user@$ip::$backupdir --password-file=$passwd_file
    fi
done
fi

 

sersync文字教程

包在本身的雲服務器目錄上/joker,讀者若是沒有該目錄,請從網上查找svn

1,想同步什麼文件 6-11
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>
2,inotify監控屬性 12-21
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>
3,sersync配置 23-44
其中
24-28 目錄
24          <localpath watch="/opt/tongbu">  # 本地推送目錄
25              <remote ip="127.0.0.1" name="tongbu1"/> #rsync服務器ip和模塊
26              <!--<remote ip="192.168.8.39" name="tongbu"/>-->
27              <!--<remote ip="192.168.8.40" name="tongbu"/>-->
28          </localpath>
29-35 rsync命令
29          <rsync>
30              <commonParams params="-artuz"/> # 默認avz
31              <auth start="true" users="root" passwordfile="/etc/rsync.pas"/> # 開啓驗證,用戶,密碼
32              <userDefinedPort start="false" port="874"/<!port=874 --> # 端口
33              <timeout start="true" time="100"/><!-- timeout=100 --> # 客戶端超時
34              <ssh start="false"/>
35          </rsync>
4,日誌 36
36    <failLogpath="/tmp/rsync_fail_log.sh"timeToExecute="60"/><!--default every 60mins execute once-->

啓動執行

 /mnt/sersync/bin/sersync -d -r -o /mnt/sersync/conf/confxml.xml

附贈啓動參數

[root@jokerpro bin]# /service/script/sersync/bin/sersync -h
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
_______________________________________________________
參數-d:啓用守護進程模式
參數-r:在監控前,將監控目錄與遠程主機用rsync命令推送一遍
參數-n: 指定開啓守護線程的數量,默認爲10個
參數-o:指定配置文件,默認使用confxml.xml文件
參數-m:單獨啓用其餘模塊,使用 -m refreshCDN 開啓刷新CDN模塊
參數-m:單獨啓用其餘模塊,使用 -m socket 開啓socket模塊
參數-m:單獨啓用其餘模塊,使用 -m http 開啓http模塊
不加-m參數,則默認執行同步程序

注意與事項

邏輯服務器安裝inotify+sersync+rsync命令

備份服務器安裝rsync的daemon模式

邏輯服務器數據產生目錄發生數據變化,就會推送到備份服務器上

通常錯誤仍是發生在rsync上,請看上一篇文章,搞懂rsync,虛擬帳號,rsync帳號,目錄權限

附贈sersync腳本,執行於邏輯服上

#!/bin/bash
# The author is joker, applied to file synchronization.
# rsync 驗證用戶,密碼
author=rsync_backup
password=woshimima
# 判軟件安裝
inotify_order=`which inotifywait 1>/dev/null 2>&1`
inotify_state=`echo $?`
rsync_order=`which rsync 1>/dev/null 2>&1`
rsync_state=`echo $?`
if [ $inotify_state -eq 1 -a $rsync_state -eq 1 ];then
    echo -e "\033[31m 紅色字,正在安裝inotify,rsync \033[0m" 
    yum install inotify-tools rsync -y
    sleep1
    sh $0
else
    # 只須要密碼
cat>/etc/rsync.passwd<<EOF    
$password
EOF
    chmod 600 /etc/rsync.passwd
    # 查找xml文件位置
    sersync_path=`pwd`
    confxml_path=`cd ..&&pwd`
    # -d 守護進程模式,-r若是是第一次監控,將全量推送一次,後續增量,-o,指定配置文件
    $sersync_path/sersync -d -r -o $confxml_path/conf/confxml.xml
    sersync_process=`ps -ef|grep sersync|grep -v grep|wc -l`
    if [ $sersync_process -ge 1 ];then
        echo -e "\033[32m 綠色字,sersync 啓動成功 \033[0m"
    else
        echo -e "\033[31m 紅色字,啓動失敗,檢查inotify,rsync是否安裝與正常啓動 \033[0m" 
    fi
fi
相關文章
相關標籤/搜索