设为首页 收藏本站
查看: 746|回复: 0

[经验分享] csync+lsync 实时双向批量同步小文件

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-7-31 09:13:04 | 显示全部楼层 |阅读模式
环境 ubuntu 12.04
1) 两台机器安装配置:

    apt-get install xinetd csync2 lsyncd
1
2
3
4
5
6
7
8
9
10
11
12
13
    cat /etc/xinetd.d/csync2         
        service csync2
            {
                disable = no
                port = 30865
                socket_type = stream
                protocol = tcp
                wait = no
                user = root
                passenv = PATH
                server = /usr/sbin/csync2
                server_args = -i
            }



2) node2配置:

在主机node2上面执行csync2 -k/etc/csync2.key (时间长, 耐心等待)  
在主机node2上面csync配置下面三个文件:

1
2
3
4
5
6
7
8
9
10
11
cat /etc/csync2.cfg
    nossl * *;
    group WEB
    {
        host node2.wenjun.com;
        host node3.wenjun.com;
        key /etc/csync2.key;
        include /home/ubuntu;
        exclude *~ .*;
        auto none;
    }



1
2
3
4
5
6
7
8
9
10
11
cat /etc/csync2_web1.cfg
    nossl * *;
    group web1
    {
            host node2.wenjun.com;
            host (node3.wenjun.com);    #()向这台推送数据
            key /etc/csync2.key;
            include /home/ubuntu;
            exclude *~ .*;
            auto none;
    }



1
2
3
4
5
6
7
8
9
10
11
cat /etc/csync2_web2.cfg
    nossl * *;
    group web2
    {
            host (node2.wenjun.com);
            host node3.wenjun.com;
            key /etc/csync2.key;
            include /home/ubuntu;
            exclude *~ .*;
            auto none;
    }



lsyncd node2配置

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
cat /etc/lsyncd/lsyncd.conf.lua
    settings = {
            logident        = "lsyncd",
            logfacility     = "user",
        logfile = "/var/log/lsyncd.log",
        statusFile = "/var/log/lsyncd.stat",
        statusInterval = 1,
    }
     
    initSync = {
            delay = 1,
            maxProcesses = 1,
            action = function(inlet)
                    local config = inlet.getConfig()
                    local elist = inlet.getEvents(function(event)
                            return event.etype ~= "Blanket"
                    end)
                    local directory = string.sub(config.source, 1, -2)
                    local paths = elist.getPaths(function(etype, path)
                            return "\t" .. config.syncid .. ":" .. directory .. path
                    end)
                    log("Normal", "Processing syncing list:\n", table.concat(paths, "\n"))
                    spawn(elist, "/usr/sbin/csync2", "-C", config.syncid, "-x")
            end,
            collect = function(agent, exitcode)
                    local config = agent.config
                    if not agent.isList and agent.etype == "Blanket" then
                            if exitcode == 0 then
                                    log("Normal", "Startup of '", config.syncid, "' instance finished.")
                            elseif config.exitcodes and config.exitcodes[exitcode] == "again" then
                                    log("Normal", "Retrying startup of '", config.syncid, "' instance.")
                                    return "again"
                            else
                                    log("Error", "Failure on startup of '", config.syncid, "' instance.")
                                    terminate(-1)
                            end
                            return
                    end
                    local rc = config.exitcodes and config.exitcodes[exitcode]
                    if rc == "die" then
                            return rc
                    end
                    if agent.isList then
                            if rc == "again" then
                                    log("Normal", "Retrying events list on exitcode = ", exitcode)
                            else
                                    log("Normal", "Finished events list = ", exitcode)
                            end
                    else
                            if rc == "again" then
                                    log("Normal", "Retrying ", agent.etype, " on ", agent.sourcePath, " = ", exitcode)
                            else
                                    log("Normal", "Finished ", agent.etype, " on ", agent.sourcePath, " = ", exitcode)
                            end
                    end
                    return rc
            end,
            init = function(inlet)
                    local config = inlet.getConfig()
                    local event = inlet.createBlanketEvent()
                    log("Normal", "Recursive startup sync: ", config.syncid, ":", config.source)
                    spawn(event, "/usr/sbin/csync2", "-C", config.syncid, "-x")
            end,
            prepare = function(config)
                    if not config.syncid then
                            error("Missing 'syncid' parameter.", 4)
                    end
                    local c = "csync2_" .. config.syncid .. ".cfg"
                    local f, err = io.open("/etc/" .. c, "r")
                    if not f then
                            error("Invalid 'syncid' parameter: " .. err, 4)
                    end
                    f:close()
            end
    }
    local sources = {
            ["/home/ubuntu"] = "web1"   
    }
    for key, value in pairs(sources) do
            sync {initSync, source=key, syncid=value}
    end



3) node3配置
同步node2 csync配置到node3

    scp node2:/etc/csync.* /etc/
同步node2 lsyncd 配置到node3
    scp -r node2:/etc/lsyncd  /etc/
    修改下面部分:

1
2
3
        local sources = {        
                ["/home/ubuntu"] = "web2"   
        }



4) 两台机器都执行
1
2
3
4
    csync2 -xv            # 初始化csync2 的sqlite数据库
    csync2 -vvv -T        # 测试csync2
     
    /etc/init.d/lsyncd start        #启动lsyncd



1
    tail -f /var/log/lsyncd.log     #查看日志输出显示如下为正常



1
2
3
        Tue Jul 29 17:25:23 2014 Normal: Recursive startup sync: web1:/home/ubuntu/wenjun1/        
        Tue Jul 29 17:25:23 2014 Normal: Startup of 'web1' instance finished.
        Tue Jul 29 17:25:33 2014 Normal: Processing syncing list:



        Wed Jul 30 10:56:28 2014 Normal: Recursive startup sync: web2:/home/ubuntu/wenjun1/
        Wed Jul 30 10:56:28 2014 Normal: Startup of 'web2' instance finished.
        Wed Jul 30 10:56:39 2014 Normal: Processing syncing list:

5) 测试同步
    测试1:

1
2
3
4
    #!/bin/sh
    for ((i=0; i<100; i++)); do
            touch /home/ubuntu/${i}.html
    done



    测试2:
1
2
3
4
5
6
7
8
9
10
    #!/bin/bash   
    for ((i=0;i<10;i++))
        do
            number=$((1000000 + ($(od -An -N2 -i /dev/random)) % (10000 + 1000)))
            for ((j=0; j<1000; j++))
        do
                    touch /home/ubuntu/${i}-${j}.html
            done
            sleep ${number}  
    done




6) 适用环境
    此方案也可做成链式同步;N台相同资源机器同步


参考: http://oss.linbit.com/csync2/paper.pdf

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-22972-1-1.html 上篇帖子: CentOS install Desktop and Remotely access 远程管理Centos桌面 TigerVNC 下篇帖子: RHEL6.5服务器本地yum源配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表