Ubuntu服务器之间进行Sersync+Rsync同步

对于服务器之间的文件,如果想做同步,可以采用sersync+rsync+inotify组合的方式来做到主从方式的自动同步。最近恰好也是用到这个,所以在此简单记录一下。

模型

服务器:数据从83—>84 和85 
10.10.10.84(从) 
10.10.10.85(从) 
10.10.10.83(主)

安装与配置sersync和rsync(从)

在Ubuntu server上,rsync和inotify默认都是安装了的。配置以xinetd方式运行。 
(1)安装命令 
apt-get install rsync xinetd 
(2)创建存放文件目录,命令如下: 
mkdir /home/tfxiaozi/webapps  也可以不创建用已有目录

(3)修改rsync启动方式 
vim /etc/default/rsync 
(4)修改下面这行 
RSYNC_ENABLE=inetd 
(5)rsync启动方式的配置文件 
vim /etc/xinetd.d/rsync,加入下面内容

service rsync
{
        disable         = no
        wait            = no
        socket_type     = stream
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure += USERID
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

(6)rsync的配置文件 
vim /etc/rsyncd.conf加入下面内容

max connections = 65535
log file = /var/log/rsync.log
timeout = 300
[webapps]
path = /var/www
read only = no
list = yes
uid = root
gid = root
auth users = test
secrets file = /etc/rsyncd.secrets
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

用户组最好设置为root,不然文件权限同步过去的时候会变化。 
(7)创建rsync传输过程中的认证文件 
vim /etc/rsyncd.secrets,内容如下,格式:用户名:密码

test:123xcvz
 
 
  • 1

然后,将认证文件权限修改成

 chmod 600 /etc/rsyncd.secrets
 
 
  • 1

(8)重启xinetd

/etc/init.d/xinetd restart
 
 
  • 1

查看rsyncserver是否启动

netstat -ntpl|grep 873
tcp  0   0 0.0.0.0:873    0.0.0.0:*    LISTEN  24651/xinetd
 
 
  • 1
  • 2

(9)测试:在83机器上执行

root@tfxiaozi-server:~# rsync test@10.10.10.85::webapps
pssword: 
drwxrwxr-x          4,096 2016/06/14 09:45:57 .
 
 
  • 1
  • 2
  • 3

【注】84和85两台机器的配置相同。

配置主服务器(源)

10.10.10.83服务器 
sersync下载地址 
https://code.google.com/archive/p/sersync/downloads 
http://sersync.sourceforge.net/ 
解压: 
tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz 
解压后两个文件直接放在/usr/local/sersync2.5/目录下即可。

 
 
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

修改sersync的配置文件

vi 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="true"/>
        <modify start="true"/>
    </inotify>
    <sersync>
        <localpath watch="/var/www">
            <remote ip="10.10.10.84" name="webapps"/>
            <remote ip="10.10.10.85" name="webapps"/>
        </localpath>
        <rsync>
            <commonParams params="-auvPz"/>
            <auth start="true" users="test" passwordfile="/etc/password.rsync"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="true" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
        <failLog path="/usr/local/sersync2.5.4/rsync_fail_log.sh" 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>
    <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>
 
 
  • 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
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

创建密码文件: 
vi /etc/password.rsync,输入123xcvz。这个密码和上面第(7)步是一样的,但是不加用户名,密码文件权限要设置为600。 
到这里就配置完毕了。

测试

/usr/local/sersync2.5.4/sersync2 -r -d -o /usr/local/sersync2.5.4/confxml.xml
 
 
  • 1

在10.10.10.83的/home/tfxiaozi/webapps目录下新建个文件1.txt,则在10.10.10.84和10.10.10.85的/home/tfxiaozi/webapps下会自动同步刚才的1.txt到本地。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值