一、rsync服务端安装

1、查看rsync安装包

# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64

2、安装rsync

系统默认都会安装rsync软件包的,如果查看发现没有安装,执行yum安装即可
# yum install rsync -y

3、添加rsync服务的用户,管理本地目录的

# useradd -s /sbin/nologin -M rsync
# id rsync

4、生成rsyncd.conf配置文件

# cat /etc/rsyncd.conf

#rsync_config______________________start
#created by oldboy 15:00 2016-11-15
##rsyncd.conf start##
uid = rsync # 用户 远端的命令使用rsync访问共享目录
gid = rsync # 用户组
use chroot = no # 安全相关
max connections = 200 # 最大连接数
timeout = 300 # 超时时间
pid file = /var/run/rsyncd.pid # 进程对应的进程号文件
lock file = /var/run/rsyncd.lock # 锁文件
log file = /var/log/rsyncd.log # 日志文件
ignore errors # 忽略错误
read only = false # 可写
list = false # 不能列表
hosts allow = 172.16.1.0/24 # 允许连接的服务器
hosts deny = 0.0.0.0/32 # 后勤组连接的服务器
auth users = rsync_backup # 虚拟用户
secrets file = /etc/rsync.password # 虚拟用户对应的用户和密码文件 [backup] # 模块名称
path = /backup # 服务端提供访问的目录 [nfsbackup]
path = /nfsbackup
#rsync_config______________________end

5、根据rsyncd.conf的auth users配置帐户,远程连接的,并根据secrets file 参数生成密码文件。

# echo "rsync_backup:hkrt" > /etc/rsync.password
# cat /etc/rsync.password
rsync_backup:hkrt

6、为密码文件配置权限

# chmod 600 /etc/rsync.password
# ls -l /etc/rsync.password
-rw------- 1 root root 20 Nov 15 23:35 /etc/rsync.password

7、创建共享目录并授权rsync服务管理

# mkdir /backup -p
# chown -R rsync.rsync /backup

8、启动rsync服务并检查

# rsync --daemon
# ps -ef | grep rsync|grep -v grep
# lsof -i:873

9、加入开机自启动

# echo "/usr/bin/rsync --daemon" >> /etc/rc.local
# tail -1 /etc/rc.local

二、rsync客户端

1、查看rsync安装包

# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64

2、安装rsync

系统默认都会安装rsync软件包的,如果查看发现没有安装,执行yum安装即可
# yum install rsync -y

3、添加rsync服务的用户,管理本地目录的

# useradd -s /sbin/nologin -M rsync
# id rsync

4、生成连接服务器需要的密码文件

# echo "hkrt" > /etc/rsync.password
# cat /etc/rsync.password
hkrt

5、为密码文件配置权限

# chmod 600 /etc/rsync.password
# ls -1 /etc/rsync.password
-rw------- 1 root root 7 Nov 15 23:48 /etc/rsync.password

6、同步文件语法

基于rsync daemon同步语法:

拉取: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
推送: rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

7、测试手动推送是否正常

[root@nfs01 backup]# pwd
/backup [root@nfs01 backup]# ls
a.txt c.txt e.txt g.txt i.txt k.txt m.txt o.txt q.txt s.txt u.txt w.txt y.txt
b.txt d.txt f.txt h.txt j.txt l.txt n.txt p.txt r.txt t.txt v.txt x.txt z.txt # rsync -avz /backup/ rsync_backup@172.16.1.41::backup/ --password-file=/etc/rsync.password # 执行手动推送
sending incremental file list
./
a.txt
b.txt
c.txt
d.txt
e.txt
f.txt
g.txt
h.txt
i.txt
j.txt
k.txt
l.txt
m.txt
n.txt
o.txt
p.txt
q.txt
r.txt
s.txt
t.txt
u.txt
v.txt
w.txt
x.txt
y.txt
z.txt sent 1229 bytes received 505 bytes 3468.00 bytes/sec
total size is 0 speedup is 0.00
从结果看:应该是同推送成功了,我们到rsync daemon服务器来查看下,是不是已经同步完成了啊。
[root@backup backup]# pwd
/backup [root@backup backup]# ls
a.txt c.txt e.txt g.txt i.txt k.txt m.txt o.txt q.txt s.txt u.txt w.txt y.txt
b.txt d.txt f.txt h.txt j.txt l.txt n.txt p.txt r.txt t.txt v.txt x.txt z.txt
 
我们看到rsync daemon 服务器/backup目录已经有文件同步过来了,说明我们已经配置成功了。接下来我们还会学习下,如何做到实例同步,我会通过rsync+inotify、rsync+serync两种方案来实现数据实时同步!
 
三、rsync + inotify 实时同步方案
 
本想新写一篇博文来介绍rsync + inotify实时同步方案了,但是要实现这个方案需要满足以下条件:
1、rsync daemon安装成功 
2、rsync 客户端可以正常推送文件到rsync daemon端 
3、安装配置inotify 
前两个我们已经配置完成了,下面我们只需要在rsync 客户端安装inotify并配置即可实现实时同步。
 
1、安装inotify
# yum install inotify-tools –y

# inotifywa    # 安装完成后会生成以下两个命令
inotifywait   inotifywatch

 
2、执行inotifywait命令监测/data目录是否有变化
[root@nfs01 scripts]# /usr/bin/inotifywait -mrq --format '%w%f' -e close_write,delete /data/
 
3、在/data目录创建测试(hkrt.txt)文件,然后看看inotifywait命令是否监测到了变化
# pwd
/data
# touch hkrt.txt [root@nfs01 scripts]# /usr/bin/inotifywait -mrq --format '%w%f' -e close_write,delete /data/
/data/hkrt.txt

4、创建监控脚本

我们可以看到,在/data目录下创建的hkrt.txt文件,已经被inotifywait命令监测到了。这样inotify就快搞定了,下面我们只需要写个监控脚本就可以了,脚本内容就是监测到了变化,就执行rsync推送文件到备份服务器就好了,下面看下脚本是怎么写的吧! 
# cat inotify.sh 
#/bin/bash
Path=/data
Ip=172.16.1.41
/usr/bin/inotifywait -mrq --format '%w%f' -e close_write,delete $Path \
|while read file
do
cd $Path && \
rsync -az ./ --delete rsync_backup@$Ip::nfsbackup --password-file=/etc/rsync.password &
done

5、创建启动服务脚本并赋予执行权限

# cat /etc/init.d/syncd
#!/bin/bash
#chkconfig: 2345 38 46
. /etc/init.d/functions
if [ $# -ne 1 ];then
usage: $0 [start|stop]
exit
fi case "$1" in
start)
/bin/bash /root/scripts/inotify.sh &
echo $$ > /var/run/inotify.pid
if [ `ps -ef|grep inotify|wc -l` -gt 2 ];then
action "inotify service is started" /bin/true
else
action "inotify service is started" /bin/false
fi
;;
stop)
kill -9 `cat /var/run/inotify.pid` > /dev/null 2>&1
pkill inotifywait
sleep 2
if [ `ps -ef|grep inotify|grep -v grep|wc -l` -eq 0 ];then
action "inotify service is stopped" /bin/true
else
action "inotify service is stopped" /bin/false
fi
;;
*)
usage: $0 [start|stop]
exit 1
esac
 
# chmod +x /etc/init.d/syncd

6、添加syncd服务并设置开机自启动

chkconfig --add syncd
chkconfig syncd on

7、启动/停止inotifywait服务

# /etc/init.d/syncd start
inotify service is started [ OK ]
[root@nfs01 scripts]# /etc/init.d/syncd stop
inotify service is stopped [ OK ]

8、接下来我们就可以验证实时同步了

[root@nfs01 data]# pwd
/data
[root@nfs01 data]# touch stu{01..10}
[root@nfs01 data]# ls
stu01 stu02 stu03 stu04 stu05 stu06 stu07 stu08 stu09 stu10

我们到备份服务器看看有没有实时同步过去

[root@backup nfsbackup]# pwd
/nfsbackup
[root@backup nfsbackup]# ls
stu01 stu02 stu03 stu04 stu05 stu06 stu07 stu08 stu09 stu10

可以看到,新创建的文件已经被实时同步到备份服备器了,rsync + inotify 实时同步也就部署完成了,可能过程中有些内容写的不是很详细,只是个安装过程,大家需要了解更多的内容,可以参考官方技术文档或查看其它资料。好了,就介绍到这吧。希望大家有所收获。

四、rsync + sersync 实现实时同步

实现rsync+sersync实时同步我们继续使用这个环境,所在先要停掉inotify服务。

[root@nfs01 data]# /etc/init.d/syncd stop
inotify service is stopped [ OK ]
[root@nfs01 local]# chkconfig --list syncd
syncd 0:off 1:off 2:off 3:off 4:off 5:off 6:off

如果是新环境,我们就可以直接安装sersync了!

1、安装sersync,下载后直接使用就可以了,不需要安装,我们先看下目录结构吧,这目录结构是修改过的,如果从官方下载可以会不一样,不过也没有太的区别了,只不过有用的就是这两个文件而已。

# tree sersync/
sersync/
├── bin
│ └── sersync2 # 启动命令
├── conf
│ └── confxml.xml # 配置文件 (我们主要关注的就是这个文件了)
└── logs

2、我们看下配置文件的内容

# cat /usr/local/sersync/conf/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> # 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="/data"> # 需要同步的路径
<remote ip="172.16.1.41" name="nfsbackup"/> # 指定rsync daemon 备份服务器IP地址、模块名称
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-avz"/> # rsync 参数设置
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
<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>
<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>

3、配置密码文件,我们这里使用的环境之前已经配置好了,这里就不在配置了,你应该也会明白吧。我们看下吧,记着修改权限哦!

[root@nfs01 local]# cat /etc/rsync.password
oldboy
[root@nfs01 local]# ll /etc/rsync.password
-rw------- 1 root root 7 Nov 15 23:48 /etc/rsync.password

4、启动sersync服务

[root@nfs01 local]# /usr/local/sersync/bin/sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -r rsync all the local files to the remote servers before the sersync work
option: -d run as a daemon
option: -o config xml name: /usr/local/sersync/conf/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
daemon start,sersync run behind the console
use rsync password-file :
user is rsync_backup
passwordfile is /etc/rsync.password
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data && rsync -avz -R --delete ./ rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password >/dev/null 2>&1
run the sersync:
watch path is: /data
[root@nfs01 local]# ps -ef | grep sersync
root 4130 1 0 14:45 ? 00:00:00 /usr/local/sersync/bin/sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml
root 4146 1949 1 14:45 pts/0 00:00:00 grep sersync

5、我们验证看看有没有实时同步吧

在sersync端/data目录下创建几个文件

[root@nfs01 local]# cd /data/
[root@nfs01 data]# ls
[root@nfs01 data]# touch stu{01..05}
[root@nfs01 data]# ls
stu01 stu02 stu03 stu04 stu05

在rsync daemon端查看/nfsbackup目录有没有刚刚创建的文件

[root@backup nfsbackup]# pwd
/nfsbackup
[root@backup nfsbackup]# ls
stu01 stu02 stu03 stu04 stu05

可以看到,文件已经同步过来了,说明没有问题了,也达到了实时同步的目的。好的,就介绍到这吧。介绍了两种实时同步的方案。

rsync命令实例:

cd /data/tomcat1/webapps/ROOT/attachment
rsync -avz ./ rsync_backup@10.16.1.201::img_attachment --password-file=/etc/rsync.password

rsync 服务快速部署手册的更多相关文章

  1. Rsync服务端部署流程

    Rsync服务端部署流程       Rsync服务端部署流程: 一.rsync服务端配置流程 配置rsync配置文件/etc/rsyncd.conf 创建同步的本地目录/dingjian 并根据需要 ...

  2. rsync 服务及部署

    1 rsync简介 1.1 什么是rsync rsync: - a fast, versatile, remote (and local) file-copying toolrsync:是一种快速,多 ...

  3. NFS 网络文件系统快速部署手册

    NFS服务端部署配置 一.安装nfs-utils和rpcbind服务,安装完后检查 # yum install -y nfs-utils rpcbind # rpm -qa nfs-utils rpc ...

  4. Redis服务快速部署

    官方对Redis的阐述: Redisis an open source, BSD licensed, advanced key-value cache and store. It is often r ...

  5. ubuntu搭建nodejs生产环境——快速部署手册

    为什么不用CentOS而用Ubuntu作为生产环境的运行平台?这个我也比较好奇,公司订的只能沿用传统,从使用成本的角度来说,此举也是值得肯定的. 测试环境 腾讯云 Ubuntu 16.04 阿里云 U ...

  6. Rsync服务部署使用

    rsync服务搭建过程(daemon模式) 配置服务 在/etc/rsyncd.conf文件中写入相应的配置: uid = root gid = root use chroot = no max co ...

  7. rsync 服务部署详解

    第1章 rsync 软件介绍 1.1 什么是rsync rsync 是一款开源的.快速的.多功能的.可实现全量及增量的本地或远程数据同步备份的优秀工具. http://www.samba.org/ft ...

  8. rsync快速部署记录

    rsync快速部署记录 安装rsync和使用环境:客户端:10.192.30.59 fudao_db_cluster_002 (将本地文件备份到服务端)服务端:10.192.30.60 fudao_d ...

  9. Rsync 服务部署与参数详解

    Rsync 简介 rsync 是一款开源的.快速的.多功能的.可实现全量及增量的本地或远程数据同步备份的优秀工具.Rsync软件适用于unix/linux/windows等多种操作系统平台. 传统的 ...

随机推荐

  1. Junit单元测试学习

    一.首先选择学习工具是IDEA 1>官网下载IDEA 1:官网地址IntelliJ IDEA,官网上对于不同的操作系统(windows,macOS,Linux)都有两个版本可供下载| 其中蓝色下 ...

  2. 删除指定路径下固定格式,以.log结尾、三天前的文件,或删除空的日志文件

    师出‘百测’besttest 删除指定路径下固定格式,以.log结尾.三天前的文件,或删除空的日志文件. 日志文件格式:XXXX_2019-01-01.log. import os,datetime ...

  3. Java 基础篇之泛型

    背景 在没有泛型前,一旦把一个对象丢进集合中,集合就会忘记对象的类型,把所有的对象都当成 Object 类型处理.当程序从集合中取出对象后,就需要进行强制类型转换,这种转换很容易引起 ClassCas ...

  4. [计蒜客T2238]礼物_线段树_归并排序_概率期望

    礼物 题目大意: 数据范围: 题解: 这题有意思啊($md$卡常 直接做怎么做? 随便上个什么东西,维护一下矩阵乘和插入,比如说常数还算小的$KD-Tree$(反正我是没见人过过 我们漏掉了一个条件, ...

  5. 【java基础学习001】概述

    001.1    一个简单的Java程序 public class hello { public static void main(String[] args) { System.out.printl ...

  6. Zookeeper 配置和原理探究

    一 Zookeeper是什么? 服务集群对外提供服务的过程中,有很多的配置需要随时更新,服务间需要协调工作,那么这些信息如何推送到各个节点?并且保证信息的一致性和可靠性?我们知道分布式协调服务很难正确 ...

  7. Linux系列:进阶之tomcat安装

    思路:作者是在Windows上从Apache官网下载的tomcat,之后将tomcat文件放到我的ftp站点中,在Linux访问ftp站点下载tomcat文件 ,将tomcat放在我自己的安装目录中, ...

  8. P1003铺地毯

    这道题是2011年提高组第一题,在洛谷被评为普及-.看到题目后直接写了一个纯模拟,结果第一次提交全部RE,后将数组开大,随即MLE.然后又去思索其余方法,采用先将每一个地毯的对角线存下来,然后i--看 ...

  9. peewee无外键连接

    # 参考:https://blog.csdn.net/weixin_34273479/article/details/87587183 res = Name.select(Name, User.xxx ...

  10. 带坑使用微信小程序框架WePY组件化开发项目,附带第三方插件使用坑

    纯粹用来记录wepy及相关联内容,以防再犯~ 1. 接手的wepy项目版本是 1.7.2 ,so我没有初始化的过程.... 2. 安装wepy命令工具,npm install wepy-cli -g ...