rsync同步部署

作者:Chris

数据镜像备份工具 —— remote sync 实时同步部署

部署服务器ip

Client 172.16.0.10 
Server 172.16.0.20

一. Client 配置

卸载系统自带安装的rsync。

rpm -qa |grep rsync
rpm -e rsync-2.6.8-3.1

安装3.0版的rsync

tar zxvf rsync-3.0.9.tar.gz
cd rsync-3.0.9
./configure --prefix=/usr/local 
make
make install

添加环境变量和重启脚本

cp /usr/local/bin/rsync /usr/bin
cp rsyncd /etc/init.d/
chkconfig —add rsyncd 
chkconfig rsyncd on

以下是重启脚本rsyncd代码:

#!/bin/bash
#chkconfig: - 85 15
#this script for start|stop rsync daemon service  

status=$(netstat -lnp | grep -w 873 | grep -v 'grep') 
pidfile="/var/run/rsyncd.pid" 
start_rsync="rsync --daemon"
pid=$(ps -ef | egrep "rsync --daemon"  | grep -v 'grep' | gawk -F' '  '{print $2}')

function rsyncstart() { 

    if [ "${status}X" == "X" ];then 

       ${start_rsync}   

        status=$(netstat -lnp | grep -w 873 | grep -v 'grep') 

        if [  "${status}X" != "X"  ];then 

            echo "rsync service start.......OK" 

        fi 

    else 

        echo "rsync service is running !"    

    fi 
} 

function rsyncstop() { 

    if [ "${status}X" != "X" ];then 

        kill -9 ${pid}
        rm -f $pidfile     

        status=$(netstat -lnp | grep -w 873 | grep -v 'grep') 

        if [ "${status}X" == "X" ];then 

            echo "rsync service stop.......OK" 
        fi 
    else 

        echo "rsync service is not running !"    

    fi 
} 

function rsyncstatus() { 

    if [ "${status}X" != "X" ];then 

        echo "rsync service is running !"   

    else 

         echo "rsync service is not running !"  

    fi 

} 

function rsyncrestart() { 

    if [ "${status}X" == "X" ];then 

               echo "rsync service is not running..." 

               rsyncstart 
        else 

               rsyncstop 

               rsyncstart    

        fi       
}  

case $1 in 

        "start") 
               rsyncstart 
                ;; 

        "stop") 
               rsyncstop 
                ;; 

        "status") 
               rsyncstatus 
               ;; 

        "restart") 
               rsyncrestart 
               ;; 

        *) 
          echo 
                echo  "Usage: $0 start|stop|restart|status" 
          echo 
esac 

添加rsync配置文件

vi /etc/rsyncd.conf 

添加下面的代码到rsyncd.conf中

uid = nobody 
gid = nobody 
use chroot = no strict 
modes = yes 
max connections = 200 
pid file = /var/run/rsyncd.pid 
lock file = /var/run/rsync.lock 
log file = /var/log/rsyncd.log

#同步配置test
[test]  #必须和server端name一样
path = /home/client
comment = test
read only = no
write only = no
ignore errors = yes
list = yes
uid = root
gid = root
hosts allow =  172.16.0.20  #server端ip
hosts deny = *

注:如果要添加多个同步server端,可以增加[test]这块配置。

启动rsync服务

service rsyncd start

iptables设置,开放rsync端口

-A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT

二. server 配置

配置rsync服务端程序sersync2

tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
cd GNU-Linux-x86
mkdir /etc/sersync2
cp confxml.xml /etc/sersync2
cp sersync2 /usr/bin

配置sersync启动文件

vi /etc/sersync2/confxml.xml

修改以下部分

<sersync>
        <localpath watch="/home/server">
            <remote ip="172.16.0.20" name="test"/>
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="false" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
        <failLog path="/var/log/rsync_fail.log" timeToExecute="60"/><!--default every 60mins execute once-->
        <crontab start="false" schedule="600"><!--600mins-->
            <crontabfilter start="false">
                <exclude expression="*.php"></exclude>
                <exclude expression="info/*"></exclude>
            </crontabfilter>
        </crontab>
        <plugin start="false" name="command"/>
    </sersync>

运行sersync2 后台运行

sersync2 -r -d -o /etc/sersync2/confxml.xml 

添加开机启动项:

vi /etc/rc.d/rc.local
sersync2 -d -o /etc/sersync2/confxml.xml >> /bakup/rsync.log 2>&1


sersync2常用参数 
参数-d:启用守护进程模式 
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍 
参数-n: 指定开启守护线程的数量,默认为10个 
参数-o:指定配置文件,默认使用confxml.xml文件 
其他参数可以使用 sersync2 -help 查看

rsync server端172.16.0.20实时同步到client端172.16.0.10部署配置完成。

服务验收: 在server创建文件 touch /home/server/test

在client查看同步情况 ll /home/client

在server删除clinet也删除


Question:

重启client端服务器后,发现同步报没有权限,但再手动重启service rsyncd restart后,同步又恢复正常。

2014/05/29 14:46:24 [2971] rsync: failed to set times on "." (in test1): Permission denied (13)
2014/05/29 14:46:24 [2971] rsync: recv_generator: mkdir "www" (in test1) failed: Permission denied(13)
2014/05/29 14:46:24 [2971] *** Skipping any contents from this failed directory ***

检查了服务器的同步的目录权限都没有问题,原因是开启了SELinux 的enforce模式,改成disable后,同步恢复正常。

注:如果发现无法同步等问题,大部分可以查看两边日志来针对解决

client日志在rsync.conf文件配置

server日志在confxml.xml里failLog path=”/var/log/rsync_fail.log”

总结:rsync对于服务器不多得情况下还是非常好用得,程序同步也非常快,以前在uniqlo的app服务器上用,每天几w用户安装登录,lnmp架构rsync同步也没有出过问题,不过当时配置不完善,现在重写了启动脚本,服务器宕机再启的话也能继续实时同步。 ok,出门吃拉面!!!

您的评论

Build by Loppo 0.6.14