Rsync+sersync实现服务器自动同步

567次阅读
没有评论
Rsync+sersync 实现服务器自动同步

Rsync 安装:

yum install -y rsync

SeRsync 安装:

wget –no-check-certificate https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz

服务器:

192.168.80.151 (主服务器): 需安装 rsync + sersync

192.168.80.152(备用服务器): 需要安装 rsync

备用服务器(192.168.80.152)


安装 rsync 并修改配置

[root@node02 ~]# yum install -y rsync
[root@node02 html]# cat /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area
uid = root #rsync 程序运行用户(权限,默认 nobody)gid = root
use chroot = yes #默认为 true,修改为 no,增加对目录文件软连接的备份
max connections = 4 #最大连接数
pid file = /var/run/rsyncd.pid #pid 文件存放位置
log file = /var/log/rsyncd.log #修改日志文件存放位置
exclude = lost+found/ 
transfer logging = yes
timeout = 900
ignore nonreadable = yes
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

[html] #moudle 自定义名称
        path = /home/jby/html #存放路径
        comment = html backup #说明
        secrets file = /etc/rsyncd.secrets #用户认证配置文件,里面保存用户名称和密码,需要自己创建
        read only = no #默认是 true,需要修改为 no,否则没有写权限无法保存文件


[root@node02 html]# cat /etc/rsyncd.secrets
# 用户: 密码(这里的用户可以不是系统用户)rsync:rsync
[root@node02 ~]# systemctl start rsyncd
[root@node02 ~]# systemctl enable rsyncd

主服务器(192.168.80.151)


1、安装 rsync(使用默认配置就行)

[root@node01 ~]# yum install -y rsync
[root@node01 ~]# systemctl start rsyncd
[root@node01 ~]# systemctl enable rsyncd

2、安装 sersync

[root@node01 ~]# wget --no-check-certificate https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@node01 ~]# mv GNU-Linux-x86 /usr/local/sersync
[root@node01 ~]# cd /usr/local/sersync/
[root@node01 sersync]# cp confxml.xml confxml.xmlbak
[root@node01 sersync]# vim /usr/local/sersync/confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host>
    <debug start="false"/>
    <fileSystem xfs="false"/>
    <filter start="false">
        <exclude expression="(.*)\.svn"></exclude>
        <exclude expression="(.*)\.gz"></exclude>
        <exclude expression="^info/*"></exclude>
        <exclude expression="^static/*"></exclude>
    </filter>
    <inotify>
        <delete start="true"/>
        <createFolder start="true"/>
        <createFile start="false"/>
        <closeWrite start="true"/>
        <moveFrom start="true"/>
        <moveTo start="true"/>
        <attrib start="false"/>
        <modify start="false"/>
    </inotify>

    <sersync>
        <localpath watch="/home/html"> #修改路径为需要同步的文件夹
            <remote ip="192.168.80.152" name="html"/> #修改 ip 为备用服务器 ip,name 为备用服务器定义好的 moudle
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="rsync" passwordfile="/root/.rsync.secrets"/> #修改 start=true 启用验证,users 为备用服务器定义的用户,指定存有密码文件的路径
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="false" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> #path 是同步失败存放日志的路径,根据自己情况确定是否修改
        <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>

    <plugin name="command">
        <param prefix="/bin/sh" suffix=""ignoreError="true"/>  <!--prefix /opt/tongbu/mmm.sh suffix-->
        <filter start="false">
            <include expression="(.*)\.php"/>
            <include expression="(.*)\.sh"/>
        </filter>
    </plugin>

    <plugin name="socket">
        <localpath watch="/opt/tongbu">
            <deshost ip="192.168.138.20" port="8009"/>
        </localpath>
    </plugin>
    <plugin name="refreshCDN">
        <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
            <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
            <sendurl base="http://pic.xoyo.com/cms"/>
            <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
        </localpath>
    </plugin>
</head>

[root@node01 sersync]# echo rsync > /root/.rsync.secrets
# 启动 sersync,更多命令可使用 -h 参数进行查看
[root@node01 html]# /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
# 增加开机自启
[root@node01 html]# chmod +x /etc/rc.d/rc.local
[root@node01 html]# vim /etc/rc.d/rc.local
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
正文完
 
评论(没有评论)