Linux文件实时同步,可实现一对多

说明:该功能服务端安装sersync2,客户端安装rsync,原理就是服务端主动推送设定目录下的所有更新的文件到各个客户端rsync接收

rsync大家都知道,是Linux自带的数据同步工具,而sersync2是google大神的开源项目http://code.google.com/p/sersync/

下面给出具体的实现步骤,实现的详细原理大家可以去上面的开源网址,上面说的很详细

 

  1. 客户端配置,首先系统安装rsync工具,
    [root@yo57 ~]# vi /etc/rsyncd.conf
                     
    uid=www
    gid=www
    max connections=36000
    use chroot=no
    log file=/var/log/rsyncd.log
    pid file=/var/run/rsyncd.pid
    lock file=/var/run/rsyncd.lock
                     
                     
    [yowebtongbu]
    path=/Data/code/adserver
    comment = yo web files
    ignore errors = yes
    read only = no
    hosts allow = 192.168.0.0/24
    hosts deny = *
    [root@yo57 ~]# /usr/bin/rsync --daemon --config=/etc/rsyncd.conf
    [root@yo57 ~]# ps -ef|grep rsyn
    root      1070 29923  0 17:04 pts/4    00:00:00 grep rsyn
    root     32069     1  0 16:54 ?        00:00:00 /usr/bin/rsync --daemon --config=/etc/rsyncd.conf
  2. 写入开机启动项
    [root@yo57 ~]# vi /etc/rc.local
                  
    #!/bin/sh
    #
    # This script will be executed *after* all the other init scripts.
    # You can put your own initialization stuff in here if you don't
    # want to do the full Sys V style init stuff.
                  
    touch /var/lock/subsys/local
                  
    ulimit -SHn 51200
    /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
    /usr/local/nginx/sbin/nginx
    /etc/init.d/php_fpm start
    /usr/bin/rsync --daemon --config=/etc/rsyncd.conf


  3. 服务器端
    [root@10  local ] # tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
    [root@10  local ] # cd sersync2.5.4_64
    [root@10 sersync2.5.4_64] # ls
    confxml.xml  sersync2
    [root@10 sersync2.5.4_64] # vi confxml.xml
    修改这一段即可
    <localpath  watch = "/Data/code/adserver" >
           <remote ip= "192.168.0.27"  name= "yowebtongbu" />
           <!--<remote ip= "192.168.8.39"  name= "tongbu" />-->
           <!--<remote ip= "192.168.8.40"  name= "tongbu" />-->
       < /localpath >
    进行一次完整同步
    [root@10 sersync2.5.4_64]# ./sersync2 -r
    写入脚本并放入开机启动项
    [root@10 sersync2.5.4_64] # cat /usr/local/sbin/sersync.sh    
    #!/bin/bash
    PATH= /bin : /sbin : /usr/bin : /usr/sbin : /usr/local/bin : /usr/local/sbin :~ /bin
    export  PATH
       
    SDATH= "/usr/local/sersync2.5.4_64"
    SSTART= "./sersync2 -r -d"
    SPID=` ps  -ef| grep  'sersync2' | grep  - v  'grep' | awk  '{print $2}' `
       
    function_start()
    {
         echo  -en  "\033[32;49;1mStarting sersync2......\n"
         echo  -en  "\033[39;49;0m"
         if  [ -t ${SPID} ];  then
             cd  ${SDATH}
             ${SSTART} >  /dev/null  2>&1
             printf  "Serync2 is the successful start!\n"
         else
         printf  "Sersync2 is runing!\n"
         exit  1
         fi
    }
       
    function_stop()
    {
         echo  -en  "\033[32;49;1mStoping sersync2......\n"
         echo  -en  "\033[39;49;0m"
         if   [ -t ${SPID} ];  then
             printf   "Sersync2 program is not runing!\n"
         else
             kill  ${SPID}
             printf  "Sersync2 program is stoped\n"
         fi
    }
       
    function_restart()
    {
         echo  -en  "\033[32;49;1mRestart sersync2......\n"
         echo  -en  "\033[39;49;0m"
         if   [ -t ${SPID} ];  then
             cd  ${SDATH}
             ${SSTART} >  /dev/null  2>&1
         else
             kill  ${SPID}
             sleep  1
             cd  ${SDATH}
             ${SSTART} >  /dev/null  2>&1
         fi
         printf  "Sersync2 is the successful restart!\n"
    }
       
    function_kill()
    {
         killall sersync2
    }
       
    function_status()
    {
         if  ps  -ef| grep  'sersync2' | grep  - v  'grep'  /dev/null  2>&1
         then
             printf  "Sersync2 is down!!!\n"
         else
             printf  "Sersync2 is running now!\n"
         fi
    }
       
    if  "$1"  "start"  ];  then
         function_start
    elif  "$1"  "stop"  ];  then
         function_stop
    elif  "$1"  "restart"  ];  then
         function_restart
    elif  "$1"  "kill"  ];  then
         function_kill
    elif  "$1"  "status"  ];  then
         function_status
    else
         echo  -en  "\033[32;49;1m Usage: sersync2 {start|stop|restart|kill|status}\n"
         echo  -en  "\033[39;49;0m"
    fi


    [root@10 sersync2.5.4_64] # vi /etc/rc.local
        
    #!/bin/sh
    #
    # This script will be executed *after* all the other init scripts.
    # You can put your own initialization stuff in here if you don't
    # want to do the full Sys V style init stuff.
        
    touch  /var/lock/subsys/local
    #/etc/init.d/lemp start
    /usr/local/nginx/sbin/nginx
    /etc/init .d /php_fpm  start
    /usr/local/zabbix/sbin/zabbix_agentd
    /usr/local/sbin/sersync .sh start

 

整个实现就这么简单,以后主服务器上面/Data/code/adserver目录下有新建、删除、修改文件或文件夹(包括下层递归)的的数据会自动推送到下面的各个服务端对应目录下,如果临时不需要该功能,kill掉服务端即可,操作完以后在手动开启服务端(此时客户端不用动)

 

posted @ 2013-10-31 22:25  pangbangb  阅读(981)  评论(0编辑  收藏  举报