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


相关文章
|
5月前
|
弹性计算 ice
阿里云4核8G云服务器配置价格:热门ECS实例及CPU处理器型号说明
阿里云2025年4核8G服务器配置价格汇总,涵盖经济型e实例、计算型c9i等热门ECS实例,CPU含Intel Xeon及AMD EPYC系列,月费159元起,年付低至1578元,按小时计费0.45元起,实际购买享折扣优惠。
1780 1
|
5月前
|
弹性计算 定位技术 数据中心
阿里云服务器配置选择方法:付费类型、地域及CPU内存配置全解析
阿里云服务器怎么选?2025最新指南:就近选择地域,降低延迟;长期使用选包年包月,短期灵活选按量付费;企业选2核4G5M仅199元/年,个人选2核2G3M低至99元/年,高性价比爆款推荐,轻松上云。
529 11
|
5月前
|
存储 Linux 开发工具
Linux环境下使用Buildroot配置软件包
使用Buildroot可以大大简化嵌入式Linux系统的开发和维护工作,但它需要对Linux系统和交叉编译有深入的理解。通过上述步骤,可以有效地配置和定制软件包,为特定的嵌入式应用构建高效、稳定的系统。
642 11
|
5月前
|
存储 弹性计算 网络协议
超详细的阿里云服务器购买流程,ECS自定义购买配置教程
本文详细图解阿里云ECS服务器自定义购买全流程,涵盖付费模式、地域选择、网络配置、实例规格、镜像、存储、安全组及登录设置等核心步骤,助您轻松掌握专业级云服务器搭建方法。
|
5月前
|
存储 弹性计算 监控
阿里云渠道商:如何挑选阿里云服务器配置?
本文详解通用型、计算型、内存型等实例适用场景,结合性能评估与成本优化策略,助力用户按需选择。以日均1万访问企业网站为例,2核4G+3M带宽月费约200元,性价比高。合理配置更省钱。
|
5月前
|
弹性计算
阿里云ECS云服务器8核16G配置收费价格,多种ECS实例CPU及费用清单
阿里云8核16G云服务器价格因实例类型而异。计算型c9i约743元/月,一年6450元(7折);通用算力型u1仅673元/月,一年4225元(5.1折)。实际价格享时长折扣,详情见ECS官网。
|
11月前
|
安全 Linux
阿里云linux服务器使用脚本通过安全组屏蔽异常海外访问ip
公网网站可能会遭受黑客攻击导致访问异常,使用此脚本可以屏蔽掉异常IP 恢复访问。也可自行设置定时任务定期检测屏蔽。
791 28