服務器A:論壇的主服務器,運行DZ X2論壇程序;服務器B:論壇從服務器,須要把X2的圖片附件和MySQL數據實時從A主服務器實時同步到B服務器.MySQL同步設置會在下一編中說到.如下是用於實時同步兩臺服務器的圖片.git
由於通常的RSYNC須要CRON來按期運行SH腳原本實現同步,這樣會帶來一些問題.好比用戶從主服務器上傳上一個圖片,須要最少一分鐘才能從從服務器顯示出來.自從Linux 2.6內核後,支持了inotify機制,當某些文件或文件夾有改變時,發出相應的事件,這樣,第三方程序只要訂閱這些事件,就能夠處理相應的操做了.這時,只要有文件被修改,就執行一次RSNYN,把修改的文件主動地上傳到另外一臺服務器上就能夠了.github
我使用的是google的inotify-tools,比較簡單.國內有功能很強大的相似的程序,可是好複雜.另外須要注意的是:若是使用inotify-tools來實現實時同步,咱們的主服務器--源文件服務器(也就是服務器A)實現是RSYNC的從服務器,咱們的從服務器--目標同步的服務器(服務器B)纔是RSYNC的主服務器.不要搞混了哦.shell
好了,開始吧!apache
首先從主服務器A開始,bash
須要肯定你的系統是否支持inotify:服務器
1
2
3
4
5
|
ll
/proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Jan 4 17:56 max_queued_events
-rw-r--r-- 1 root root 0 Jan 4 17:56 max_user_instances
-rw-r--r-- 1 root root 0 Jan 4 17:56 max_user_watches
|
能輸出這樣的結果表示支持.網站
下載並安裝inotify-tools:ui
1
2
3
4
5
6
|
wget --no-check-certificate http:
//github
.com
/downloads/rvoicilas/inotify-tools/inotify-tools-3
.14.
tar
.gz
tar
xzvf inotify-tools-3.14.
tar
.gz
cd
inotify-tools-3.14
.
/configure
--prefix=
/usr
make
make
install
|
這樣就完成了inotify-tools的固然.google
接下來須要寫兩個SH腳本,inotify_init.sh和inotify_monitor.sh:spa
inotify_init.sh 用於第一次初始化,也就是運行一次完整的RSYNC同步.
1
|
vi
/root/inotify_init
.sh
|
內容以下:
1
2
3
4
5
6
7
8
9
10
11
|
#!/bin/sh
SRC=/主服務器A須要同步的目錄/
#記得在最後面加/否則RYNC會自動增長一層目錄
DES=bbsatt
IP=從服務器B的IP
USER=
rsync
#DST=/etc/rsyncd 遠程rsync模塊下的目錄
INWT=
/usr/bin/inotifywait
RSYNC=
/usr/bin/rsync
$RSYNC -zahqt --password-
file
=
/root/rsync
.
pwd
$SRC $USER@$IP::$DES
|
保存退出.
設置可執行權限:
1
|
chmod
+x
/root/inotify_init
.sh
|
接下是inotify_monitor.sh,用於訂閱文件修改事件.注意,由於特別緣由,我在這裏作的是增量備份+實時同步,也就是說,當主服務器A上的圖片被刪除是,從服務器B是不會刪除圖片的.
1
|
vi
/root/inotify_monitor
.sh
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
#!/bin/bash
###########################
sync
[0]=
'/主服務器須要同步的目錄/,從服務器B的IP,bbsatt,rsync'
# localdir,host,rsync_module,auth_user
INWT=
/usr/bin/inotifywait
RSYNC=
/usr/bin/rsync
PASS=
/root/rsync
.
pwd
###########################
for
item
in
${
sync
[@]};
do
dir
=`
echo
$item |
awk
-F
","
'{print $1}'
`
host=`
echo
$item |
awk
-F
","
'{print $2}'
`
module=`
echo
$item |
awk
-F
","
'{print $3}'
`
user=`
echo
$item |
awk
-F
","
'{print $4}'
`
$INWT -mrq --timefmt
'%d/%m/%y %H:%M'
--
format
'%T %w%f %e'
--event CLOSE_WRITE,create,move $
dir
|
while
read
date
time
file
event
do
#echo $event'-'$file
case
$event
in
MODIFY|CREATE|MOVE|MODIFY,ISDIR|CREATE,ISDIR|MODIFY,ISDIR)
if
[
"${file: -4}"
!=
'4913'
] && [
"${file: -1}"
!=
'~'
];
then
cmd="$RSYNC -zahqzt --exclude=
'*'
--password-
file
=$PASS
--include=$
file
$
dir
$user@$host::$module >
/dev/null
2>1&"
echo
$cmd
$cmd
fi
;;
MOVED_FROM|MOVED_FROM,ISDIR|DELETE,ISDIR)
if
[
"${file: -4}"
!=
'4913'
] && [
"${file: -1}"
!=
'~'
];
then
cmd="$RSYNC -zahqzt --password-
file
=$PASS --exclude=$
file
$
dir
$user@$host::$module >
/dev/null
2>1&"
echo
$cmd
$cmd
fi
;;
esac
done
&
done
|
1
|
chmod
+x
/root/inotify_monitor
.sh
|
設置RSYNC自動登陸驗證密碼
1
2
|
vi
/root/rsync
.
pwd
xxxxxx
|
保存,退出
設置只有ROOT才能夠查看的權限.
1
|
chmod
0600
/root/rsync
.
pwd
|
如下是備從務器B的配置:
安裝RSYNC
1
|
yum
rsync
-y
|
配置RSNYD服務:
1
|
vi
/etc/rsyncd
.conf
|
內容以下,須要把Apache修改爲你運行網站的用戶名,個人是由於原來使用apache,雖然如今用Nginx,也一直沒改用戶名:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
uid = apache
gid = apache
use chroot = no
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[bbsatt]
path = /從服務器B本地用於存放備份的目錄
ignore errors
read only = no
list = false
hosts allow = 主服務器A的IP
auth users = rsync
secrets file = /etc/rsync.pas
|
1
2
|
vi
/etc/rsync
.pas
rsync
:xxxxxx
|
1
|
chmod
0600
/etc/rsync
.pas
|
啓動RSYNCD
1
|
rsync
--daemon
|
添加開機自動啓動服務:
1
|
vi
/etc/rc
.
local
|
添加如下內容:
1
|
rsync
--daemon
|
回到主服務器A,
1
|
vi
/etc/rc
.
local
|
添加如下內容,實時開機自動同步:
1
2
|
/root/inotify_init
.sh
/root/inotify_monitor
.sh
|
保存退出
運行
/root/inotify_init.sh
1
|
/root/inotify_monitor.sh
|
好了,這樣就能實現實時同步圖片文件了.隨便在主服務器A的同步目錄下新建一個文件試試吧.