记录日常工作关于系统运维,虚拟化云计算,数据库,网络安全等各方面问题。

Sersync + Rsync 代码分发


简介:

Sersync 是基于 inotify 来编写的 Linux 系统文件监控工具,当监控到文件发生变化时,调用 rsync 同步文件。

类似的功能,以前有用 rsync + inotify 实现过,这次来使用一下这个同步更迅速、功能更完善的 Sersync 。

一、代码分发服务器上安装 Sersync 、Rsync

shell > cd /usr/local/src
shell > wget https://github.com/wsgzao/sersync/archive/master.zip
shell > unzip master.zip
shell > cd sersync-master && ls
inotify-tools-3.14.tar.gz  README.md  rsync-3.1.1.tar.gz  sersync2.5.4_64bit_binary_stable_final.tar.gz

# 来到这里,你只要看一眼 README.md 你就啥都懂了

> Rsync

shell > tar zxf rsync-3.1.1.tar.gz
shell > cd rsync-3.1.1
shell > ./configure; make; make install

> Inotify-tools

shell > tar zxf inotify-tools-3.14.tar.gz
shell > cd inotify-tools-3.14
shell > ./configure; make; make install

> Sersync ( 监控同步目录变化,调用 Rsync 同步数据 )

shell > tar zxf sersync2.5.4_64bit_binary_stable_final.tar.gz
shell > mv GNU-Linux-x86 /usr/local/sersync

# 安装完毕

二、节点服务器安装、配置 Rsync

复制代码
shell > cd /usr/local/src
shell > wget  http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz
shell > tar zxf rsync-3.1.1.tar.gz
shell > cd rsync-3.1.1
shell > ./configure; make; make install

shell > vim /etc/rsyncd.conf

uid = www-data
gid = www-data

log file = /var/log/rsyncd.log

list = false
read only = no
use chroot = no
ignore errors = yes
max connections = 36000

auth users = rsync
secrets file = /etc/rsync.pass

# hosts allow = 192.168.1.80
# hosts deny = *

# 认证的模块名
[web1]
comment = web1
path = /data/webroot/web1

[web2]
comment = web2
path = /data/webroot/web2

shell > useradd -r -s /sbin/nologin www-data
shell > mkdir -p /data/webroot/{web1,web2}
shell > chown -R www-data.www-data /data/webroot

shell > echo "rsync:123456" > /etc/rsync.pass
shell > chmod 600 /etc/rsync.pass

shell > setenforce 0             # 关闭 SELinux
shell > vim /etc/selinux/config  # 永久
SELINUX=disabled  # 原 enforcing
SELINUXTYPE=targeted

# 另外防火墙开放 TCP 873

shell > rsync -4 --daemon  # 以守护进程方式启动,-4 只监听 IPV4
shell > echo "/usr/local/bin/rsync -4 --daemon" >> /etc/rc.local  # 加入开机启动
复制代码

三、代码同步服务器测试 Rsync,配置 Sersync

1、测试 Rsync 数据同步

shell > rsync -av --delete /data/webroot/web1/ rsync@192.168.1.30::web1  # 将 web1 目录下的文件同步到 1.30 web1 模块指定的目录下
password:

# 输入密码:123456,采用下面的方法,不需要手动输入密码

shell > echo "123456" > /etc/rsyncd.pass
shell > chmod 600 /etc/rsyncd.pass

shell > rsync -av --delete --password-file=/etc/rsyncd.pass /data/webroot/web1/ rsync@192.168.1.30::web1 

# 注意同步目录及子目录、文件权限,设为 www-data、注意认证密码文件。

2、配置 Sersync

复制代码
shell > cd /usr/local/sersync
shell > cp confxml.xml web1.xml

shell > vim web1.xml  # 编辑配置文件

    <sersync>
        <localpath watch="/data/webroot/web1">
            <remote ip="192.168.1.30" name="web1"/>
        </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="/tmp/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>
复制代码

# <localpath watch="/data/webroot/web1"> # 需要同步的目录
# <remote ip="192.168.1.30" name="web1"/> # 同步主机,认证模块名 ( 可以写多条 )
# <commonParams params="-artuz"/> # Rsync 同步参数
# <auth start="false" users="root" passwordfile="/etc/rsync.pas"/> # 是否开启认证,客户端开启认证,需设为 true,填写认证用户、密码文件路径
# <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/> # 失败日志记录
# <crontab start="false" schedule="600"> # 自带任务计划,多长时间自动同步数据,默认关闭

3、运行 Sersync

shell > nohup /usr/local/sersync/sersync2 -r -d -n 12 -o /usr/local/sersync/web1.xml > /usr/local/sersync/logs/rsync_web1.log 2>&1 &

# -r 启动监控前,将监控目录与远程主机同步一次
# -d 启用守护进程模式
# -n 开启同步进程的数量,默认 10 个
# -o 指定配置文件,默认 confxml.xml

# 多个监控目录,指定不同的配置文件,开启多个进程。

四、测试 Sersync + Rsync

# 代码分发服务器上,监控目录内创建文件(修改权限),增加文件内容,查看节点服务器有没有同步数据。
# 删除文件、创建目录、删除目录

# 测试发现,如果同步目录内文件权限不为 www-data,该文件可以同步到节点服务器,但是追加文件内容,则无法同步。
# 注意文件权限。

# 一个完整的流程:开发同事将代码提交到版本库-->点击上线-->数据同步到线上代码分发服务器-->通过 Sersync + Rsync 分发到-->节点服务器



转载请标明出处【Sersync + Rsync 代码分发,自动实时同步】。

《www.micoder.cc》 虚拟化云计算,系统运维,安全技术服务.

网站已经关闭评论