rsync 服务快速部署手册

一、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配置文件

 
#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::DESTrsync [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.txtsent 1229 bytes  received 505 bytes  3468.00 bytes/sec
total size is 0  speedup is 0.00

[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推送文件到备份服务器就好了,下面看下脚本是怎么写的吧! 
 
#/bin/bash
Path=/data
Ip=172.16.1.41
/usr/bin/inotifywait -mrq --format '%w%f' -e close_write,delete $Path \
|while read filedocd $Path && \rsync -az ./ --delete rsync_backup@$Ip::nfsbackup --password-file=/etc/rsync.password &done

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

 
#!/bin/bash
#chkconfig: 2345 38 46
. /etc/init.d/functions
if [ $# -ne 1 ];thenusage: $0 [start|stop]exit
ficase "$1" in
start)/bin/bash /root/scripts/inotify.sh &echo $$ > /var/run/inotify.pidif [ `ps -ef|grep inotify|wc -l` -gt 2 ];thenaction "inotify service is started" /bin/trueelseaction "inotify service is started" /bin/falsefi;;
stop)kill -9 `cat /var/run/inotify.pid` > /dev/null 2>&1pkill inotifywaitsleep 2if [ `ps -ef|grep inotify|grep -v grep|wc -l` -eq 0 ];thenaction "inotify service is stopped" /bin/trueelseaction "inotify service is stopped" /bin/falsefi;;
*)usage: $0 [start|stop]exit 1
esac
 

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

chkconfig --add syncd
chkconfig syncd on

7、启动/停止inotifywait服务

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

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服务


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: /dataroot      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

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

转载于:https://www.cnblogs.com/miclesvic/p/6189540.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/417565.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C# 用户控件事件

当用户控件中btnNeed按钮被点击时&#xff0c;激活本用户控件的BtnClick事件&#xff0c;此事件可以在页面中捕获。-----------用户控件---------------protected void btnNeed_Click(object sender, EventArgs e) { if (BtnClick ! null) Bt…

html不支持元素video,Html5中的Video元素使用方法

现在互联网视频大都使用Flash来实现。但是不同的浏览器可能使用不同的插件。在HTML5中则提供了一个统一的方式来展示视频内容。HTML5 video在Internet Explorer 9, Firefox, Opera, Chrome, 和Safari都支持。IE8及其更早的浏览器不支持。代码如下Your browser does not support…

Matlab命令大全

MATLAB命令大全管理命令和函数help 在线帮助文件doc 装入超文本说明what M、MAT、MEX文件的目录列表type 列出M文件lookfor 通过help条目搜索关键字which 定位函数和文件Demo 运行演示程序Path 控制MATLAB的搜索路径管理变量和工作空间…

千牛通知栏常驻是什么意思_店铺运营|内贸1688 店铺真正的权重是什么?

想必大家都听说过店铺权重/单品权重/客户标签 这些名词。那到底是怎么操作的呢&#xff1f; 以下是简单的大纲&#xff1a;店铺权重的拆解拆分里面的小指标&#xff0c;看看我们有没有操作的空间单品的权重拆分里面的小指标&#xff0c;有哪些因素是我们能够控制的&#xff1f;…

初入网络系列笔记(6)TFTP协议

一、借鉴说明&#xff0c;本博文借鉴以下博文 1.锤子&#xff0c;FTP协议&#xff0c;http://www.cnblogs.com/loadrunner/archive/2008/01/09/1032264.html 2.sunada&#xff0c;FTP协议及工作原理详解&#xff0c;http://www.cnblogs.com/sunada2005/articles/2781712.html 3…

计算机科学与技术班级口号八字,班级的八字口号

第1篇&#xff1a;初中班级八字口号1.拼搏奋进、永远进步2.全民健身、利国利民3.健康第一、从我做起4.零二零二&#xff0c;独一无二5.零三零三&#xff0c;奋勇争先6.四班四班&#xff0c;锐不可当7.展现自我、争创新高8.奥运精神、永驻我心9.强身健体、立志成材10.顽强拼搏、…

Prolog学习笔记100805

//love(zhangxueyou,wanfei). 爱(张学友,王菲). “张学友爱王菲”。prolog是不允许使用除了基本字符以外字符的。最末尾的“.”一定不能掉&#xff0c;它表示一个句子结束。 //“:-”在prolog中表示“如果”的意思&#xff0c;我们使用它来定义规则。lovers(X,Y):-love(X,Y),lo…

charts引入icon图片_v-charts 踩坑之路

最近要做一个大屏 没有使用echarts 使用了更适合vue封装的v-charts组件库&#xff0c;第一次使用 期间踩了不少坑&#xff0c;记录下来和大家分享一下。废话不多说 开始搞起来&#xff01;一、安装 引入什么的大家自行百度 百度一下&#xff0c;你就知道​www.baidu.com二、2.1…

今天星期一在家值班

只盼着不要出问题 转载于:https://www.cnblogs.com/bkchengzheng/p/6196193.html

高一学生计算机知识现状分析,关于高中信息技术课教学现状的思考

一、 当前信息技术教学现状的分析(一)课程开设的现状高中信息技术课已经开展好多年了&#xff0c;总的来说&#xff0c;越来越被教育部门重视了&#xff0c;从刚开始的仅仅要求学生认识计算机&#xff0c;到学习一些简单处理软件&#xff0c;到把信息技术整合成一门象样的…

魔兽争霸 地图编辑器 常用属性【原创】

物体单位 技能-英雄&#xff1a;选择英雄的技能文本-名字&#xff1a;英雄的名字状态-初始力量、敏捷、智力&#xff1a;初始三围状态-初始属性&#xff1a;初始英雄类别&#xff08;力量、敏捷、智力&#xff09;状态-每等级所加力量、敏捷、智力&#xff1a;每升一级增加三围…

pla3d打印材料密度_模具粉必看!总有一款粉末能解决您的问题-毅速3D打印研制...

金属3D打印最常见的形式是粉末床熔融。这类工艺使用热源&#xff08;SLM工艺使用激光&#xff0c;EBM工艺使用电子束&#xff09;逐点将粉末颗粒熔融在一起&#xff0c;逐层加工至物件完成。在金属3D打印过程中&#xff0c;可能存在很多设备操作者试图避免的问题&#xff0c;包…

魔兽争霸 地图编辑器 笔记

//高级——游戏平衡常数英雄最大XP取得范围&#xff1a;最大经验值英雄最大等级限制 //玩家属性——势力重新定义添加势力——我方共享单位控制、共享高级单位控制

【Networking】容器网络大观 SDN 资料汇总

SDNLAB技术分享&#xff08;十五&#xff09;&#xff1a;容器网络大观SDNLAB君 • 16-06-17 •2957 人围观编者按&#xff1a;本文系SDNLAB技术分享系列&#xff0c;本次分享来自SDN撕X群&#xff08;群主&#xff1a;大猫猫&#xff09;群直播&#xff0c;我们希望通过SDNLAB…

七年级计算机室使用计划表,七年级信息技术教学工作计划

时间眨眼而过&#xff0c;又是一年工作计划的时刻啦!下面是出国留学网小编为大家整理的“七年级信息技术教学工作计划”&#xff0c;欢迎参阅。内容仅供参考&#xff0c;了解更多关于工作计划内容&#xff0c;请关注出国留学工作计划栏目。七年级信息技术教学工作计划【一】一、…

java 建立ssh隧道_SSH基础

SSH基本概念SSH 为 Secure Shell 的缩写&#xff0c;由 IETF 的网络小组&#xff08;Network Working Group&#xff09;所制定&#xff1b;SSH 为建立在应用层基础上的安全协议。SSH 是较可靠&#xff0c;专为远程登录会话和其他网络服务提供安全性的协议。利用 SSH 协议可以有…

windows 批处理

暂停 pause>nul 批处理删除自身 attrib -h -s -r -a %0 del %0 第一行是把自身可能有的隐藏&#xff0c;系统&#xff0c;只读&#xff0c;存档属性去掉 第二行代码是删除自身&#xff08;%0代表自身&#xff09; 设置默认的控制台前景和背景颜色 COLOR [attr] attr …

GJM : Unity3D HIAR -【 快速入门 】 三、导入 SDK

导入 SDK 本文将向您介绍如何在 Unity 工程中导入 HiAR SDK for Unity。在开始之前&#xff0c;请先访问 HiAR 官网下载最新版本的 SDK。 下载 HiAR SDK for Unity Step 1. 下载解压 SDK 前往下载页面下载 SDK 包&#xff0c;可以获得一个 zip 文件&#xff1a;hiar_sdk_unity_…