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天前
HAProxy的高级配置选项-配置haproxy支持https协议及服务器动态上下线
文章介绍了如何配置HAProxy以支持HTTPS协议和实现服务器的动态上下线。
26 8
HAProxy的高级配置选项-配置haproxy支持https协议及服务器动态上下线
|
5天前
|
监控 Apache
HAProxy的高级配置选项-Web服务器状态监测
这篇文章介绍了HAProxy的高级配置选项,特别是如何进行Web服务器状态监测,包括基于四层传输端口监测、基于指定URI监测和基于指定URI的request请求头部内容监测三种方式,并通过实战案例展示了配置过程和效果。
25 8
HAProxy的高级配置选项-Web服务器状态监测
|
1天前
|
编解码 小程序
无影云电脑产品使用黑神话悟空之:游戏服务器更新/配置问题
该文档主要介绍了使用无影云电脑玩《黑神话:悟空》时可能遇到的问题及解决方法,包括游戏服务器更新、配置问题、画质建议及如何开启帧数显示等内容,并提供了详细的步骤指导与参考链接。
|
2天前
|
存储 安全 Linux
离线Linux服务器环境搭建
【9月更文挑战第3天】在离线环境下搭建Linux服务器需按以下步骤进行:首先确定服务器用途及需求,准备安装介质与所需软件包;接着安装Linux系统并配置网络;然后设置系统基础参数,如主机名与时区;安装必要软件并配置服务;最后进行安全设置,包括关闭非必要服务、配置防火墙、强化用户认证及定期备份数据。整个过程需确保软件包的完整性和兼容性。
|
4天前
|
存储 监控 Linux
监控Linux服务器
详细介绍了如何监控Linux服务器,包括监控CPU、内存、磁盘存储和带宽的使用情况,以及使用各种系统监控工具如vmstat、iostat、sar、top和dstat来分析系统性能,并推荐了一些开源监控系统。
16 0
监控Linux服务器
|
5天前
|
监控 安全 网络协议
快速配置Linux云服务器
快速配置Linux云服务器
|
9天前
|
缓存 Linux 调度
Linux服务器如何查看CPU占用率、内存占用、带宽占用
Linux服务器如何查看CPU占用率、内存占用、带宽占用
40 0
|
弹性计算 Shell Windows
阿里云学生专享ecs服务器体验及个人配置
阿里云学生优惠专享的云服务器个人使用体验
阿里云学生专享ecs服务器体验及个人配置
|
23天前
|
机器学习/深度学习 编解码 人工智能
阿里云gpu云服务器租用价格:最新收费标准与活动价格及热门实例解析
随着人工智能、大数据和深度学习等领域的快速发展,GPU服务器的需求日益增长。阿里云的GPU服务器凭借强大的计算能力和灵活的资源配置,成为众多用户的首选。很多用户比较关心gpu云服务器的收费标准与活动价格情况,目前计算型gn6v实例云服务器一周价格为2138.27元/1周起,月付价格为3830.00元/1个月起;计算型gn7i实例云服务器一周价格为1793.30元/1周起,月付价格为3213.99元/1个月起;计算型 gn6i实例云服务器一周价格为942.11元/1周起,月付价格为1694.00元/1个月起。本文为大家整理汇总了gpu云服务器的最新收费标准与活动价格情况,以供参考。
阿里云gpu云服务器租用价格:最新收费标准与活动价格及热门实例解析
|
5天前
|
存储 弹性计算 运维
阿里云日常运维-购买服务器
这篇文章是关于如何在阿里云购买和配置云服务器ECS的教程。
25 6
阿里云日常运维-购买服务器
下一篇
DDNS