linux實現多台服務器文件同步


inotify-tools+rsync實時同步文件安裝和配置

Linux+Nginx+PHP+MySQL+MemCached+eaccelerator安裝優化記錄(見 http://www.linuxidc.com/Linux/2012-06/63622.htm ).服務器A:論壇的主服務器,運行DZ X2論壇程序;服務器B:論壇從服務器,需要把X2的圖片附件和MySQL數據實時從A主服務器實時同步到B服務器.MySQL同步設置會在下一編中說到.以下是用於實時同步兩台服務器的圖片.

因為一般的RSYNC需要CRON來定期運行SH腳本來實現同步,這樣會帶來一些問題.比如用戶從主服務器上傳上一個圖片,需要最少一分鍾才能從從服務器顯示出來.自從Linux 2.6內核后,支持了inotify機制,當某些文件或文件夾有改變時,發出相應的事件,這樣,第三方程序只要訂閱這些事件,就可以處理相應的操作了.這時,只要有文件被修改,就執行一次RSNYN,把修改的文件主動地上傳到另一台服務器上就可以了.

我使用的是google的inotify-tools,比較簡單.國內有功能很強大的類似的程序,但是好復雜.另外需要注意的是:如果使用inotify-tools來實現實時同步,我們的主服務器--源文件服務器(也就是服務器A)實現是RSYNC的從服務器,我們的從服務器--目標同步的服務器(服務器B)才是RSYNC的主服務器.不要搞混了哦.

好了,開始吧!

首先從主服務器A開始,

需要確定你的系統是否支持inotify:

1 ll /proc/sys/fs/inotify
2 total 0
3 -rw-r--r-- 1 root root 0 Jan 4 17:56 max_queued_events
4 -rw-r--r-- 1 root root 0 Jan 4 17:56 max_user_instances
5 -rw-r--r-- 1 root root 0 Jan 4 17:56 max_user_watches

能輸出這樣的結果表示支持.

 

下載並安裝inotify-tools:

 

這樣就完成了inotify-tools的當然.

接下來需要寫兩個SH腳本,inotify_init.sh和inotify_monitor.sh:

inotify_init.sh 用於第一次初始化,也就是運行一次完整的RSYNC同步.

1 vi /root/inotify_init.sh

 

1 vi /root/inotify_init.sh

內容如下:

1 #!/bin/sh
2 SRC=/主服務器A需要同步的目錄/ #記得在最后面加/不然RYNC會自動增加一層目錄
3  
4 DES=bbsatt
5 IP=從服務器B的IP
6 USER=rsync
7 #DST=/etc/rsyncd 遠程rsync模塊下的目錄
8 INWT=/usr/bin/inotifywait
9 RSYNC=/usr/bin/rsync
10  
11 $RSYNC -zahqt --password-file=/root/rsync.pwd $SRC $USER@$IP::$DES

 

1 #!/bin/sh
2 SRC=/主服務器A需要同步的目錄/ #記得在最后面加/不然RYNC會自動增加一層目錄
3  
4 DES=bbsatt
5 IP=從服務器B的IP
6 USER=rsync
7 #DST=/etc/rsyncd 遠程rsync模塊下的目錄
8 INWT=/usr/bin/inotifywait
9 RSYNC=/usr/bin/rsync
10  
11 $RSYNC -zahqt --password-file=/root/rsync.pwd $SRC $USER@$IP::$DES

保存退出.

設置可執行權限:

1 chmod +x /root/inotify_init.sh

 

1 chmod +x /root/inotify_init.sh

 

接下是inotify_monitor.sh,用於訂閱文件修改事件.注意,因為特別原因,我在這里做的是增量備份+實時同步,也就是說,當主服務器A上的圖片被刪除是,從服務器B是不會刪除圖片的.

 
1 vi /root/inotify_monitor.sh

 

1 #!/bin/bash
2   
3 ###########################
4 sync[0]='/主服務器需要同步的目錄/,從服務器B的IP,bbsatt,rsync' # localdir,host,rsync_module,auth_user
5  
6 INWT=/usr/bin/inotifywait
7 RSYNC=/usr/bin/rsync
8 PASS=/root/rsync.pwd
9 ###########################
10   
11 for item in ${sync[@]}; do
12  
13 dir=`echo $item | awk -F"," '{print $1}'`
14 host=`echo $item | awk -F"," '{print $2}'`
15 module=`echo $item | awk -F"," '{print $3}'`
16 user=`echo $item | awk -F"," '{print $4}'`
17   
18 $INWT -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' \
19 --event CLOSE_WRITE,create,move $dir | while read date time file 21 event
20 do
21 #echo $event'-'$file
22 case $event in
23 MODIFY|CREATE|MOVE|MODIFY,ISDIR|CREATE,ISDIR|MODIFY,ISDIR)
24 if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then
25 cmd="$RSYNC -zahqzt --exclude='*' --password-file=$PASS \
26 --include=$file $dir $user@$host::$module > /dev/null 2>1&"
27 echo $cmd
28 $cmd
29 fi
30 ;;
31  
32 MOVED_FROM|MOVED_FROM,ISDIR|DELETE,ISDIR)
33 if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then
34 cmd="$RSYNC -zahqzt --password-file=$PASS --exclude=$file \
35 $dir $user@$host::$module > /dev/null 2>1&"
36 echo $cmd
37 $cmd
38 fi
39 ;;
40 esac
41 done &
42 done

 

1 chmod +x /root/inotify_monitor.sh

 

 

 

 

 

 
 
1 vi /root/inotify_monitor.sh
1 #!/bin/bash
2   
3 ###########################
4 sync[0]='/主服務器需要同步的目錄/,從服務器B的IP,bbsatt,rsync' # localdir,host,rsync_module,auth_user
5  
6 INWT=/usr/bin/inotifywait
7 RSYNC=/usr/bin/rsync
8 PASS=/root/rsync.pwd
9 ###########################
10   
11 for item in ${sync[@]}; do
12  
13 dir=`echo $item | awk -F"," '{print $1}'`
14 host=`echo $item | awk -F"," '{print $2}'`
15 module=`echo $item | awk -F"," '{print $3}'`
16 user=`echo $item | awk -F"," '{print $4}'`
17   
18 $INWT -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' \
19 --event CLOSE_WRITE,create,move $dir | while read date time file 21 event
20 do
21 #echo $event'-'$file
22 case $event in
23 MODIFY|CREATE|MOVE|MODIFY,ISDIR|CREATE,ISDIR|MODIFY,ISDIR)
24 if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then
25 cmd="$RSYNC -zahqzt --exclude='*' --password-file=$PASS \
26 --include=$file $dir $user@$host::$module > /dev/null 2>1&"
27 echo $cmd
28 $cmd
29 fi
30 ;;
31  
32 MOVED_FROM|MOVED_FROM,ISDIR|DELETE,ISDIR)
33 if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then
34 cmd="$RSYNC -zahqzt --password-file=$PASS --exclude=$file \
35 $dir $user@$host::$module > /dev/null 2>1&"
36 echo $cmd
37 $cmd
38 fi
39 ;;
40 esac
41 done &
42 done
 
1 chmod +x /root/inotify_monitor.sh

 

 

設置RSYNC自動登錄驗證密碼

1 vi /root/rsync.pwd
2 xxxxxx

 

 

1 vi /root/rsync.pwd
2 xxxxxx

保存,退出

設置只有ROOT才可以查看的權限.

1 chmod 0600 /root/rsync.pwd

 

 

1 chmod 0600 /root/rsync.pwd

 

以下是備從務器B的配置:

安裝RSYNC

1 yum rsync -y

 

配置RSNYD服務:

1 vi /etc/rsyncd.conf

 

內容如下,需要把Apache修改成你運行網站的用戶名,我的是因為原來使用apache,雖然現在用Nginx,也一直沒改用戶名:

1 uid = apache
2 gid = apache
3 use chroot = no
4 max connections = 4
5 pid file = /var/run/rsyncd.pid
6 lock file = /var/run/rsync.lock
7 log file = /var/log/rsyncd.log
8  
9 [bbsatt]
10 path = /從服務器B本地用於存放備份的目錄
11 ignore errors
12 read only = no
13 list = false
14 hosts allow = 主服務器A的IP
15 auth users = rsync
16 secrets file = /etc/rsync.pas

 

1 vi /etc/rsync.pas
2 rsync:xxxxxx

 

1 chmod 0600 /etc/rsync.pas

 

 

 

 

 

 

 

1 uid = apache
2 gid = apache
3 use chroot = no
4 max connections = 4
5 pid file = /var/run/rsyncd.pid
6 lock file = /var/run/rsync.lock
7 log file = /var/log/rsyncd.log
8  
9 [bbsatt]
10 path = /從服務器B本地用於存放備份的目錄
11 ignore errors
12 read only = no
13 list = false
14 hosts allow = 主服務器A的IP
15 auth users = rsync
16 secrets file = /etc/rsync.pas

 

1 vi /etc/rsync.pas
2 rsync:xxxxxx

 

1 chmod 0600 /etc/rsync.pas

 

 

 

啟動RSYNCD

1 rsync --daemon

 

 

1 rsync --daemon

 

 

添加開機自動啟動服務:

1 vi /etc/rc.local

 

 

1 vi /etc/rc.local

 

 

添加以下內容:

 
1 rsync --daemon

 

 
1 rsync --daemon

 

 

回到主服務器A,

1 vi /etc/rc.local

 

 

1 vi /etc/rc.local

 

 

添加以下內容,實時開機自動同步:

1 /root/inotify_init.sh
2 /root/inotify_monitor.sh

 

 

 

1 /root/inotify_init.sh
2 /root/inotify_monitor.sh

 

 

保存退出

運行

/root/inotify_init.sh

1 /root/inotify_monitor.sh

 



1 /root/inotify_monitor.sh

 

好了,這樣就能實現實時同步圖片文件了.隨便在主服務器A的同步目錄下新建一個文件試試吧.

 

 

注:轉載https://www.linuxidc.com/Linux/2012-06/63624.htm


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM