zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    优先队列
    Problem W UVA 662 二十三 Fast Food
    UVA 607 二十二 Scheduling Lectures
    UVA 590 二十一 Always on the run
    UVA 442 二十 Matrix Chain Multiplication
    UVA 437 十九 The Tower of Babylon
    UVA 10254 十八 The Priest Mathematician
    UVA 10453 十七 Make Palindrome
    UVA 10163 十六 Storage Keepers
    UVA 1252 十五 Twenty Questions
  • 原文地址:https://www.cnblogs.com/ccw869476711/p/9006907.html
Copyright © 2011-2022 走看看