当前位置: 首页 > news >正文

7.linux同步服务器 rsync+sersync的配置

实现目标:将主机A(192.168.239.128)下的一个目录,同步到主机B(192.168.239.130)下的一个目录,并对A主机进行监控,当A主机该目录下的文件发生变化立即同步至主机B中。


1.安装rsync

rsync安装很简单,不管是同步端机器还是被同步端机器,都只需安装rsync包既可,如果配置了yum源,可直接使用yum安装,命令如下:

# yum -y install rsync

2.启动rsync

启动rsync的方法很简单,有以下两种方法,推荐使用第二种方法:

方法一:--daemon参数方式,是让rsync以服务器模式运行,命令如下:

启动

# /usr/bin/rsync --daemon --config=/etc/rsyncd.conf

查看端口

# netstat -tulnp | grep rsync

tcp   0    0 0.0.0.0:873     0.0.0.0:*        LISTEN      27064/rsync         

tcp   0     0 :::873         :::*             LISTEN      27064/rsync

 

注释:--config用于指定rsyncd.conf的位置,如果默认在/etc目录下默认可以不用指定

方法二:xinetd监管,只需将/etc/xinetd.d/rsync文件中的disable = yes 改为no即可,如下:

# default: off

# description: The rsync server is a good addition to an ftp server, as it \

#       allows crc checksumming etc.

service rsync

{

        disable         = no         #将yes改为no

        flags           = IPv6

        socket_type     = stream

        wait            = no

        user            = root

        server          = /usr/bin/rsync

        server_args     = --daemon

        log_on_failure  += USERID

}

然后启动xinetd服务

# service  xinetd start

Starting xinetd:              [  OK  ]

查看端口,会发现rsync并没有监听,但是xinetd监听了873端口

# netstat -tulnp | grep rsync

# netstat -tulnp | grep 873

tcp    0     0 :::873        :::*         LISTEN      28245/xinetd

修改/etc/xinetd.d/rsync主要是要打开rsync这个daemon, 一旦有rsync client要连接时, xinetd会把它转移给 rsyncd(port 873)



第一部分:B服务器端

1.配置文件参数说明

全局参数

说明

motd file

定义服务器信息,需自己编辑,默认没有,也可不配置

pid file

PID文件,默认没有,一般指定为:/var/run/rsyncd.pid

port

端口,默认指定为873

address

指定服务器IP地址

模块参数

说明

comment

描述信息,自定义

path

需要同步的目录路径

use chroot

如果为yes,rsync进程将chroot 到文件系统中的目录中,好处是保护系统被安装漏洞侵袭的可能。缺点是需要超级用户权限。另外对符号链接文件,将会排除在外。

max connetions

允许客户端最大链接数,0表示无限制

log file

日志文件,一般设定为:/var/log/rsync.log

lock file

锁文件,用来记录最大连接数,默认是/var/lock/rsyncd.lock

read only

只读,默认为yes,表示不让客户端上传文件到服务器

write only

只写,默认为no,表示客户端可以下载文件,yes表示不能下载

list

列出服务器上提供同步的数据目录,默认为yes

uid

服务器传输文件时使用哪个用户执行,默认是nobody,如果遇到权限文件,可能需要root用户

gid

服务器传输文件时使用哪个用户组执行,默认是nobody

exclude

排除不需要同步的目录或文件,多个用空格隔开

auth users

认证用户,必须是服务器上存在的用户

secrets file

指定密码文件路径

strict modes

是否检查密码文件权限,拥有组和其他人必须为0

hosts allow

允许哪些地址可以同步,可以是IP或网段,多个用空格隔开

hosts deny

拒绝哪些地址的同步,可以是IP或网段,多个用空格隔开

ignore errors

忽略IO错误

log format

日志格式

timeout

超时时间



2.B服务器端操作步骤

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
(1)准备步骤
mkdri  /data/         #创建一个目录,接受来自A主机的文件或目录
useradd  rsync         #增加一个用户,用于启动进程传输文件,也可以使用root
chown  -R  rsync : rsync  /data   #修改目录属性
 
 
(2)创建配置文件
vim  /etc/rsyncd .conf 
 
##rsync.conf config start
uid =  rsync
gid =  rsync
use chroot = no    
max connetctions = 200
timeout = 100
pid  file  /var/run/rsyncd .pid
lock  file  /var/run/rsync .lock
log  file  /var/log/rsyncd .log
 
[backup]
path =  /data/
ignore errors
read  only = no
list = no
hosts allow = 192.168.239.128
auth  users  = rsync_backup
secrets  file  /etc/rsync .password
##rsync config  end   
 
chmod   600  /etc/rsyncd .conf
 
(3)创建密码文件:
echo  "rsync_backup:123456" > /etc/rsync .password
 
chmod  600  /etc/rsync .password
 
 
(4)启动服务:
rsync  --daemon
 
(5)设置开机自启 rsync 服务:
vim  /etc/rc . local
添加
# rsync server progress
/usr/bin/rsync  --daemon






第二部分:A客户端操作步骤

1
2
3
4
5
6
7
8
9
10
(1)配置密码文件:
echo  "123456" > /etc/rsync .password
chmod  600  /etc/rsync .password
 
(2)手工测试数据推送:
rsync  -avzP  /etc/hosts  rsync_backup@192.168.239.130::backup --password- file = /etc/rsync .password
 
#传输一个文件到B上,看是否成功!
 
在B服务器端进行查看,客户端的 /etc/hosts 文件已经同步至备份的服务器中。


(3)在A客户端部署sersync服务

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
1、查看服务器内核是否支持inotify
ll  /proc/sys/fs/inotify 
 
2、修改inotify默认参数(inotify默认内核参数值太小)
查看系统默认参数值:
sysctl -a |  grep  max_queued_events
结果是:fs.inotify.max_queued_events = 16384
sysctl -a |  grep  max_user_watches
结果是:fs.inotify.max_user_watches = 8192
sysctl -a |  grep  max_user_instances
结果是:fs.inotify.max_user_instances = 128
 
修改参数:
sysctl -w fs.inotify.max_queued_events= "99999999"
sysctl -w fs.inotify.max_user_watches= "99999999"
sysctl -w fs.inotify.max_user_instances= "65535"
 
 
vi  /etc/sysctl .conf  #添加以下代码
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535
:wq!  #保存退出
 
 
 
3.下载sersync
下载地址:https: //storage .googleapis.com /google-code-archive-downloads/v2/code .google.com /sersync/sersync2 .5.4_64bit_binary_stable_final. tar .gz
 
 
4.配置sersync
#上传软件至A服务器上:
     sftp > put D:/软件/常用软件包 /sersync2 .5.4_64bit_binary_stable_final. tar .gz
#解压文件到对应目录:
     tar  zxvf sersync2.5.4_64bit_binary_stable_final. tar .gz -C  /usr/local/
#进入对应目录修改目录名:
     cd    /usr/local/
     mv  GNU-Linux-x86 sersync
#复制配置文件
     cd  sersync/
     cp  -a confxml.xml{,.` date  +%F`}    
 
   
#修改配置文件部分
  
#第1部分:设置本地监控的同步目录、远端服务器、备份模块(备份模块在远端服务器/etc/rsyncd.conf定义)
  <localpath  watch = "/data/" >
      <remote ip= "192.168.239.130"  name= "backup" />
      #<!--<remote ip="192.168.8.39" name="tongbu"/>-->
      #<!--<remote ip="192.168.8.40" name="tongbu"/>-->
  < /localpath >
 
#第2部分:设置认证部分、服务器端的认证用户与密码文件存放位置(在远端服务器/etc/rsyncd.conf定义)
  < rsync >
      <commonParams params= "-artuz" />
      <auth start= "true"  users = "rsync_backup"  passwordfile= "/etc/rsync.password" />
      <userDefinedPort start= "false"  port= "874" /><!-- port=874 -->
      <timeout start= "true"  time = "100" /><!-- timeout=100 -->
      < ssh  start= "false" />
  < /rsync >
 
类似于 rsync  -avzP  /etc/hosts   rsync_backup@192.168.239.130::backup --password- file = /etc/rsync .password
 
#第3部分:设置同步失败日志存放位置,当同步失败时记录下来,并且每60分钟对失败的log进行重新同步
<failLog path= "/tmp/rsync_fail_log.sh"  timeToExecute= "60" /><!--default every 60mins execute once-->
         < crontab  start= "true"  schedule= "600" ><!--600mins-->

修改完成后,完整的配置文件如下:

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
cat  /usr/local/sersync/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>
         <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= "192.168.239.130"  name= "backup" />
             <!--<remote ip= "192.168.8.39"  name= "tongbu" />-->
             <!--<remote ip= "192.168.8.40"  name= "tongbu" />-->
         < /localpath >
         < rsync >
             <commonParams params= "-artuz" />
             <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= "true"  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 >

(4)设置sersync监控开机自动执行

1
2
vi  /etc/rc .d /rc . local   #编辑,在最后添加一行
/usr/local/sersync/sersync2  -d -r -o   /usr/local/sersync/confxml .xml  #设置开机自动运行脚本


(5)添加脚本监控sersync是否正常运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
vi   /root/check_sersync .sh   #编辑,添加以下代码
#!/bin/bash
sersync= "/usr/local/sersync/sersync2"
confxml= "/usr/local/sersync/confxml.xml"
status=$( ps  aux | grep  'sersync2' | grep  - v  'grep' | wc  -l)
if  [ $status - eq  0 ];
then
$sersync -d -r -o $confxml &
else
exit  0;
fi
 
chmod  +x  /root/check_sersync .sh  #添加脚本执行权限
 
vi  /etc/crontab  #编辑,在最后添加下面一行
* /5  * * * * root  /root/check_sersync .sh >  /dev/null  2>&1   #每隔5分钟执行一次脚本
 
service crond reload   #重新加载服务



(6)客户端进行验证,在同步的服务器上进行查看

1
2
cd  /data/
for  in  ` seq  100`; do  mkdir  $i; done




补充:sersync命令参数详解

1.命令参数说明

Sersync参数说明
./sersync -r

-r参数作用是:开启实时监控的之前对主服务器目录与远程目标机器的目录进行一次整体同步;如果需要将sersync运行前,主服务器目录下已经存在的所有文件或目录全部同步到远端,则要以 -r参数运行sersync,将本地与远程整体同步一次;

提别说明:如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步;
./sersync -o xx.xml

不指定 -o参数: sersync使用sersync可执行文件目录下的默认配置文件confxml.xml

指定 -o 参数:可以指定多个不同的配置文件,从而实现sersync多进程多实例的数据同步
./sersync -n num

-n参数为:指定默认的线程池的线程总数;

例如: ./sersync -n 5 则指定线程总数为5,如果不指定,默认启动线程池数量是10,如果cpu使用过高,可以通过该参数调低,如果机器配置较高,可以调高默认的线程总数,提升同步效率;
./sersync -d-d参数为:后台服务,通常情况下使用 -r参数对本地到远端整体同步一遍后,在后台运行此参数启动守护进程实时同步;在第一次整体同步时,-d 和 -r参数经常会联合使用;

./sersync -m

pluginName

-m参数:不进行同步,只运行插件 ./sersync -m pluginName

例如:./sersync -m command,则在监控到事件后,不对远程目标服务器进行同步,而是直接运行command插件
组合命令使用说明:
-n 8 -o liubl.xml -r -d多个参数可以配合使用,例如:./sersync -n 16 -o config.xml -r -d 表示设置线程池工作线程为16个,指定liubl.xml作为配置文件,在实时监控前 做一次整体同步,以守护进程方式在后台运行;
./sersync --help很遗憾,它没有查看帮助(需要的话2条路,要么看源代码,要么自测求验证)



2.sersync服务配置文件参数详解:

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
1.xml配置文件的注释不用“ #”,而是<!-- 中间是注释内容 -->
 
2.Debug开启开关:<debug start= "false" /> 
     设置为 true ,表示开启debug模式,会在sersync正在运行的控制台打印inotify时间与 rsync 同步命令;
 
3.XFS文件系统开关:<fileSystem xfs= "false" />
     对于xfs文件系统的用户,需要将这个选项开启,才能使用sersync正常工作;
 
4.filter文件过滤功能
     <filter start= "false" >
              <exclude expression= "(.*)\.svn" >< /exclude >
              <exclude expression= "(.*)\.gz" >< /exclude >
              <exclude expression= "^info/*" >< /exclude >
             <exclude expression= "^static/*" >< /exclude >
      < /filter >
     排除一些文件,不需要
      
5.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 >
对于大多数应用,可以尝试把createFile(监控文件事件选项)设置为 false 来提高性能,减少 rsync 通讯;
因为拷贝文件到监控目录会产生create事件与close_write事件,所以如果关闭create事件,只监控文件拷贝结束时的时间close_write,同样可以实现文件完整同步;
注意:强将creatFolder保持为 true ,如果将createFolder设为 false ,则不会对产生的目录进行监控,该目录下的子文件与子目录也不会被监控;所以除非特殊需要,请开启; 默认情况下对创建文件(目录)事件与删除文件(目录)事件都进行监控,如果项目中不需要删除远程目标服务器的文件(目录),则可以将delete参数设置为 false ,则不对删除事件进行监控;


参考:http://www.osyunwei.com/archives/7447.html

本文转自   a8757906   51CTO博客,原文链接:http://blog.51cto.com/nxyboy/1940413


相关文章:

  • 【前端】手机端网页自动播放背景音乐相关资料
  • 使用require.js
  • 对Cookie和Session的深入理解
  • Spring MVC中@ControllerAdvice注解实现全局异常拦截
  • 【移动端兼容【移动端兼容问题研究】javascript事件机制详解(涉及移动兼容)研究】javascript事件机制详解(涉及移动兼容)...
  • 基于Jquery UI的autocompelet改写,自动补全控件,增加下拉选项,动态设置样式,点击显示所有选项,并兼容ie6+...
  • 第94天:CSS3 盒模型详解
  • 56.随机产生的id重复问题
  • MS SQL SERVER中的临时表
  • ExtJS 4.2 教程-03:使用Ext.define自定义类
  • 千古奇闻!明朝就已成功试爆两万吨原子弹?
  • C#和sqlserver中生成新的32位GUID
  • linux下安装oracle11g
  • 将文件加入到图形文件里;
  • Eclipse中已安装的插件如何卸载
  • 【347天】每日项目总结系列085(2018.01.18)
  • 【笔记】你不知道的JS读书笔记——Promise
  • Android单元测试 - 几个重要问题
  • Codepen 每日精选(2018-3-25)
  • CSS盒模型深入
  • css属性的继承、初识值、计算值、当前值、应用值
  • DataBase in Android
  • Git 使用集
  • JavaScript 事件——“事件类型”中“HTML5事件”的注意要点
  • Laravel核心解读--Facades
  • MySQL主从复制读写分离及奇怪的问题
  • nginx 配置多 域名 + 多 https
  • php ci框架整合银盛支付
  • storm drpc实例
  • 二维平面内的碰撞检测【一】
  • 浮现式设计
  • 驱动程序原理
  • 一个项目push到多个远程Git仓库
  • 因为阿里,他们成了“杭漂”
  • !! 2.对十份论文和报告中的关于OpenCV和Android NDK开发的总结
  • # 安徽锐锋科技IDMS系统简介
  • # 飞书APP集成平台-数字化落地
  • ###51单片机学习(1)-----单片机烧录软件的使用,以及如何建立一个工程项目
  • #{}和${}的区别是什么 -- java面试
  • #LLM入门|Prompt#1.7_文本拓展_Expanding
  • $ git push -u origin master 推送到远程库出错
  • (3)nginx 配置(nginx.conf)
  • (C++20) consteval立即函数
  • (done) 两个矩阵 “相似” 是什么意思?
  • (libusb) usb口自动刷新
  • (Python第六天)文件处理
  • (Redis使用系列) Springboot 使用Redis+Session实现Session共享 ,简单的单点登录 五
  • (机器学习-深度学习快速入门)第三章机器学习-第二节:机器学习模型之线性回归
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (十三)Java springcloud B2B2C o2o多用户商城 springcloud架构 - SSO单点登录之OAuth2.0 根据token获取用户信息(4)...
  • (算法)求1到1亿间的质数或素数
  • (最全解法)输入一个整数,输出该数二进制表示中1的个数。
  • .bat批处理(十):从路径字符串中截取盘符、文件名、后缀名等信息
  • .Net Core/.Net6/.Net8 ,启动配置/Program.cs 配置
  • .NET简谈互操作(五:基础知识之Dynamic平台调用)