使用三台服务器:
系统:CentOS 6.8

192.168.8.169 开发服务器

192.168.8.167 线上服务器1
192.168.8.168 线上服务器2

实现思路:
在开发服务器上制定一个规则,
即只要rsync.txt存在,
线上服务器就开始进行文件同步,同步完删除该文件。

实现步骤:
(1)安装Rsync。

1、Rsync简介:
Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。
Rsync使用所谓的“Rsync算法”来使本地和远 程两个主机之间的文件达到同步,
这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。

2、Rsync安装:
wget https://download.samba.org/pub/rsync/rsync-3.1.3.tar.gz
tar -zxvf rsync-3.1.3.tar.gz
cd rsync-3.1.3

./configure --prefix=/usr/local/rsync
make
make install

第二步:
安装inotify和inotify-tools

Centos 6和CentOS 7,已经默认安装了inotify,
如果要查看是否安装,可以使用如下命令
:

ll /proc/sys/fs/inotify

如果列出如下三项,则证明已经安装
max_queued_events

max_user_instances

max_user_watches

第三步:
安装inotify-tools工具。
wget https://jaist.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
tar -zxvf inotify-tools-3.13.tar.gz
cd inotify-tools-3.13
./configure
make && make install

第四步:
线上服务器的配置:
touch /etc/rsyncd.conf
vim /etc/rsyncd.conf

输入以下内容:
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict mode = no
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /usr/data/rsync/rsyncd.log

[source-code]
path = /usr/local/nginx/html/hello/
comment = this is a comment message
ignore errors
read only = no
write noly = no
hosts allow = 192.168.8.169
hosts deny = *
list = false
uid = root
gid = root
auth users = root
secrets file = /usr/local/rsync/conf/server.pass

[source-code-update]
path = /usr/local/nginx/html/hello-update/
comment = this is a comment message
ignore errors
read only = no
write noly = no
hosts allow = 192.168.8.169
hosts deny = *
list = false
uid = root
gid = root
auth users = root
secrets file = /usr/local/rsync/conf/server.pass

说明:
source-code是源代码目录。
source-code-update 是控制是否更新的目录。

然后继续在线上服务器操作:
因为nginx部署的web位置一样,
cd /usr/local/nginx/html/
mkdir -p hello/
cd /usr/local/rsync(rsync的安装位置)
mkdir -p conf/
cd conf
touch server.pass
vim server.pass
root:123abc+-
给密码文件设置访问权限:
chmod 600 server.pass

第五步:
然后对开发服务器进行配置:
touch /etc/rsyncd.conf
vim /etc/rsyncd.conf

输入以下内容:
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict mode = no
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /usr/data/rsync/rsyncd.log

[source-code]
path = /usr/local/nginx/html/hello/
comment = this is a comment message
ignore errors
read only = no
write noly = no
hosts allow = 192.168.8.169,192.168.8.167
hosts deny = *
list = false
uid = root
gid = root
auth users = root
secrets file = /usr/local/rsync/conf/server.pass

建立项目目录
cd /usr/local/nginx/html/
mkdir hello
cd /usr/local/rsync(rsync的安装位置)
mkdir -p conf/

然后还要建立一个密码文件'/usr/local/rsync/conf/server.pass' 由于1是客户端,因此密码没有前缀
123abc+-
给密码文件设置访问权限:
chmod 600 server.pass

cp /usr/local/rsync/bin/rsync /usr/bin/
/usr/local/rsync/bin/rsync --daemon

sh datarsync.sh

第一次测试
/usr/local/bin/inotifywait \
-mrq --timefmt '%d/%m/%y' \
--format '%T %w%f%e' \
-e modify,delete,create,attrib \
/usr/local/nginx/html/hello/index.html \
结果:有改变时,有输出

第二次测试,输出到log:
/usr/local/bin/inotifywait \
-mrq --timefmt '%d/%m/%y' \
--format '%T %w%f%e' \
-e modify,delete,create,attrib \
/usr/local/nginx/html/hello/ | while read files
do
echo "${files} was rsyncd" >>/tmp/rsync.log 2>&1
done
结果:日志中有信息

第三次测试:

#!/bin/bash

src=/usr/local/nginx/html/hello/
user=root
host2=192.168.8.167
$dst2=source-code

/usr/local/bin/inotifywait \
-mrq --timefmt '%d/%m/%y' \
--format '%T %w%f%e' \
-e modify,delete,create,attrib \
/usr/local/nginx/html/hello/ | while read files
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/usr/local/rsync/conf/server.pass $src $user@$host2::$dst2
echo "${files} was rsyncd" >>/tmp/rsync.log 2>&1
done

/usr/bin/rsync -vzrtopg --progress --delete root@192.168.8.169::source-code/usr/local/nginx/html/hello/* /usr/local/nginx/html/hello/

使用/usr/local/bin/inotifywait命令报错:
/usr/local/bin/inotifywait: error while loading shared libraries: libinotifytool
s.so.0: cannot open shared object file: No such file or directory

[root@db zzh]# ll /proc/sys/fs/inotify (如果有下列三项则支持inotifytools)
total 0
-rw-r--r-- 1 root root 0 Sep 20 16:52 max_queued_events
-rw-r--r-- 1 root root 0 Sep 20 16:52 max_user_instances
-rw-r--r-- 1 root root 0 Sep 20 16:52 max_user_watches

解决:
ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0

rsync -avzP root@192.168.8.169::source-code /data/test/

rsync常见错误
https://blog.51cto.com/loveyan/713816

169开发环境

166负载均衡服务器
167 web服务器1
168 web服务器2

169 修改代码同步到167和168上。
169作为rsync客户端,
167和168作为rsync服务端,

服务端配置(即167和168的配置):
(1)服务安装
yum install rsync xinetd
(2)为 rsyncd 服务编辑配置文件,默认没有,需自己编辑
vim /etc/rsyncd.conf
写入以下内容:
uid = root
gid = root
use chroot = no
max connections = 5
timeout = 600
pid file = /var/run/rsyncd.pid
lockfile = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[web1]
path = /usr/local/nginx/html/hello/
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.8.169
hosts deny = *
list = yes
auth users = web
secrets file = /etc/web.passwd
(3)创建文件同步的目录,上面配置里的path,如果有就不用创建了
mkdir /usr/local/nginx/html/hello/
(4)创建配置中的密码文件,并增加权限:
echo "web:123" > /etc/web.passwd
chmod 600 /etc/web.passwd
(5)重新启动
service xinetd restart

客户端配置(即169):
(1)安装软件
yum -y install rsync
(2)创建web目录
mkdir /usr/local/nginx/html/hello/
(3)设置密码并设置权限
echo "123"> /tmp/rsync.password
chmod 600 /tmp/rsync.password

测试:
rsync -avzP --delete --password-file=/tmp/rsync.password /usr/local/nginx/html/hello/ web@192.168.8.167::web1

数据实时同步
环境:Rsync + Inotify-tools。
wget https://jaist.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
tar -zxvf inotify-tools-3.13.tar.gz
mkdir /usr/local/inotify
cd inotify-tools-3.13
./configure --prefix=/usr/local/inotify/
make && make install

设置环境变量
# vim /root/.bash_profile
export PATH=/usr/local/inotify/bin/:$PATH
# source /root/.bash_profile
# echo '/usr/local/inotify/lib' >> /etc/ld.so.conf --加载库文件
# ldconfig
# ln -s /usr/local/inotify/include /usr/include/inotify

vim /etc/profile
在末尾增加一行:
export PATH=$PATH:/usr/local/inotify/bin
使配置生效:
source /etc/profile
创建shell文件:
vim /test.sh
输入以下内容:
#!/bin/bash

src=/usr/local/nginx/html/hello/
user=web
host1=192.168.8.167
dst1=web1
passpath=/tmp/rsync.password

/usr/local/inotify/bin/inotifywait \
-mrq --timefmt '%d/%m/%y' \
--format '%T %w%f%e' \
-e modify,delete,create,attrib \
/usr/local/nginx/html/hello/ | while read files
do
rsync -vzrtopg --delete --progress --passfile=$passfile-path $src $user@$host1::$dst1
echo "${files} was rsyncd" >>/tmp/rsync.log 2>&1
done

chmod 755 /data/test/test.sh
# /data/test/test.sh &
# echo '/data/test/test.sh &' >> /etc/rc.local --设置开机自启

Rsync实现负载均衡的数据同步的更多相关文章

  1. 通过Nginx+tomcat+redis实现反向代理 、负载均衡及session同步

    一直对于负载均衡比较陌生,今天尝试着去了解了一下,并做了一个小的实验,对于这个概念有一些认识,在此做一个简单的总结 什么是负载均衡 负载均衡,英文 名称为Load Balance,指由多台服务器以对称 ...

  2. rsync实现负载均衡集群文件同步,搭建线上测试部署环境

    闲来无事,搭建一个负载均衡集群,至于负载均衡集群搭建过程,找时间写下.这次主要写集群之间的文件同步,以及线上测试环境的搭建. 笔者看过很多公司都没有线上测试环境,真是崩溃了,不造怎么确保线上线下环境一 ...

  3. linux+nginx+tomcat负载均衡,实现session同步

    第一部分:nginx反向代理tomcat 一.软件及环境 软件 系统 角色 用途 安装的软件 ip地址 Centos6.5x86_64 nginx 反向代理用户请求 nginx 172.16.249. ...

  4. nginx和tomcat配置负载均衡和session同步

    一.背景 因业务需求,现需配置多台服务器,实现负载均衡. 二.解决方案 使用 nginx + tomcat,在这一台应用服务器部署一个nginx和两个tomcat.通过nginx修改配置后reload ...

  5. rsync安装与配置使用 数据同步方案(centos6.5)

    rsync + crond   ==定时数据同步 sersync(inotify)  + rsync  ==实时数据同步,利用rsync实现 ##应用场景 ..1 主备服务器之间同步数据定时    = ...

  6. Windows下cwrsync客户端与rsync群辉存储客户端数据同步

    cwRsync简介 cwRsync是Rsync在Windows上的实现版本,Rsync通过使用特定算法的文件传输技术,可以在网络上传输只修改了的文件. cwRsync主要用于Windows上的远程文件 ...

  7. nginx + tomcat + memcached 做负载均衡及session同步

    1.nginx配置 # For more information on configuration, see: # * Official English Documentation: http://n ...

  8. 大数据高并发系统架构实战方案(LVS负载均衡、Nginx、共享存储、海量数据、队列缓存)

    课程简介: 随着互联网的发展,高并发.大数据量的网站要求越来越高.而这些高要求都是基础的技术和细节组合而成的.本课程就从实际案例出发给大家原景重现高并发架构常用技术点及详细演练. 通过该课程的学习,普 ...

  9. Linux系统备份还原工具4(rsync/远程数据同步工具)

    rsync即是能备份系统也是数据同步的工具. 在Jenkins上可以使用rsync结合SSH的免密登录做数据同步和分发.这样一来可以达到部署全命令化,不需要依赖任何插件去实现. 命令参考:http:/ ...

随机推荐

  1. 权重随机算法Java实现

    权重随机算法在抽奖,资源调度等系统中应用还是比较广泛的,一个简单的按照权重来随机的实现,权重为几个随机对象(分类)的命中的比例,权重设置越高命中越容易,之和可以不等于100: 简单实现代码如下: ? ...

  2. 快速安装pycharm,最详细的pycharm安装图文教程

    大家都知道python的开发工具Pycharm吧,它是由JetBrains打造的一款Python IDE,它功能强大,已经是python开发者使用最多的编辑工具.首先,它支持多平台(Linux.WIn ...

  3. python2.X与3.X比较及Python编译器选择

  4. 小型自动化运维工具pssh和传输工具rsync

    一.简单介绍 1.pssh全称是parallel-ssh,基于Python编写的并发在多台服务器上批量执行命令的工具.包括pssh,pscp,prsync,pnuke和pslurp.该项目包括pssh ...

  5. 【VS开发】【图像处理】V4L2 pixel format

    目录(?)[-] v4l2_pix_format定义 2 具体Pixel Format定义 1. v4l2_pix_format定义 [cpp] view plain copy /* *  V I D ...

  6. vue-cli 3 ----- 项目频繁发送‘sockjs-node/info’请求

    在vue-cli3跑项目时发现了这个问题,浏览器一直在频繁发送这个请求,导致联调时很不方便,而且本地开发时项目也不能实时更新. 看了网上很多的 (1)  解决方案, 大多都是直接去node_modul ...

  7. sql回显注入-笔记

     拼接sql命令查询数据   注释 常用于sql注入            # 井号 单行注释 注意:URL编码 %23          -- 两个减号加空格 单行注释           /*   ...

  8. tableau备份

    备份:数据库备份:https://help.tableau.com/current/server-linux/zh-cn/cli_maintenance_tsm.htm#tsm https://hel ...

  9. 【校内test】桶哥的问题

    (以上题目出自_rqy两年前) #A:桶哥的问题——买桶[链接] [题目描述] 桶哥要买一些全家桶.他有a元钱,而每个桶要花b元钱.他能不能买到c个桶? [输入格式] 一行三个整数a, b, c [输 ...

  10. Codeforces Round #589 (Div. 2) (e、f没写)

    https://codeforces.com/contest/1228/problem/A A. Distinct Digits 超级简单嘻嘻,给你一个l和r然后寻找一个数,这个数要满足的条件是它的每 ...