您的位置:首页 > 运维架构 > Linux

Centos+Serync+inotify实时同步数据文件(二)

2016-02-23 17:09 429 查看
这2天一直忙其它的事,忘记把剩余的同步过程分享到博客中,望谅解!上周把rsync已经配置完成,可以手动的同步,下面我们将使用Serync进行实时的同步,其实就手动同步而言也可以配置crontab定时备份,因人而异方法很多,选一种适合你自己的。
三、Serync Configuration
首先下载软件
[root@nagios01 /]#cd /home/qiuyuetao/
[root@nagios01 qiuyuetao]# wget http://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz[/code] 
【http://code.google.com/p/sersync/】下载网站

1,安装sersync
[root@nagios01 qiuyuetao]# tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/(-C 解压到指定路径)
[root@nagios01 qiuyuetao]# mv /usr/local/GNU-Linux-x86 sersync
[root@nagios01 local]# tree sersync/
sersync/
├── confxml.xml
└── sersync2


2,规范 sersync 目录结构(自行抉择是否规范)
mkdir conf bin logs (配置文件放到conf、二进制命令Bin下、脚本logs下)
[root@nagios01 local]# cd sersync/
[root@nagios01 sersync]# mkdir conf bin logs
[root@nagios01 sersync]# mv confxml.xml conf/
[root@nagios01 sersync]# mv sersync2 bin/sersync
[root@nagios01 sersync]# tree
.
├── bin
│   └── sersync
├── conf
│   └── confxml.xml
└── logs
3 directories, 2 files


3,编辑配置文件
备份 cp confxml.xml confxml.xmlq.$(date +%F)


4,更改及优化sersync 配置
(1)定义本地的同步目录
24         <localpath watch="/opt/tongbu">要同步的目录
25             <remote ip="127.0.0.1" name="tongbu1"/>
26             <!--<remote ip="192.168.8.39" name="tongbu"/>-->
同步到哪台机器       同步到那个模块目录
27             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
28         </localpath>
修改后内容为:
<sersync>
<localpath watch="/usr/local/nagios/etc/cfgs">
<remote ip="192.168.1.206" name="cfgs"/>
</localpath>
<!--###############################################################-->
<localpath watch="/usr/local/nagios/etc/objects">
<remote ip="192.168.1.206" name="objects"/>
</localpath>
<!--###############################################################-->
修改前
<rsync>
40             <commonParams params="-artuz"/>
41             <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
使用认证
42             <userDefinedPort start="false" port="874"/><!-- port=874 -->
43             <timeout start="false" time="100"/><!-- timeout=100 -->
44             <ssh start="false"/>
45         </rsync>
修改后内容为:拼接同步命令
<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>
修改36-37行 失败后会写入log
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
修改后内容
<failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins
execute once-->


4,开启sersync守护进程同步数据(全局变量)
echo 'export PATH=$PATH:/usr/local/sersync/bin'>>/etc/profile
tail -1 /etc/profile  (查看是否添加)
source /etc/profile   (使/etc/Porofile 立即生效)
which sersync         (查看sersync命令路径能否找到)


5,《启动同步命令》
sersync -r(先做一次同步) -d(后台启动) -o(指定文件) /usr/local/sersync/conf/confxml.xml
sersync -r -d -o /usr/local/sersync/conf/confxml.xml
[root@Server conf]# sersync -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 默认启动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: 32最大线程数 = 12(Thread pool nums) + 20(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 /usr/local/nagios/etc/cfgs && rsync -artuz -R --delete ./  --timeout=100
rsync_backup@192.168.1.206::cfgs --password-file=/etc/rsync.password >/dev/null 2>&1
[root@nagios01 conf]# run the sersync:
watch path is: /usr/local/nagios/etc/cfgs
成功
6,#####测试结果:发现一个问题,仅能同步一个模块目录 cfgs模块 / objects 没有同步

[root@nagios01 conf]# touch /usr/local/nagios/etc/cfgs/1.txt /usr/local/nag
ios/etc/objects/1.txt
[root@nagios01 conf]# tree /usr/local/nagios/etc/
/usr/local/nagios/etc/
├── cfgs
│   ├── 1.txt
│   ├── cfgs.log
├── objects
│   ├── 1.txt
│   ├── objects.log
│
[root@Nagios02 /usr/local/nagios/etc]$tree
├── cfgs
│   ├── 1.txt
│   ├── cfgs.log
├── objects
│   ├── objects.log


7,####如下是采用多实例的方法进行 同时同步2个目录#####
创建2个文件
cp confxml.xml confxml.xml_new
cp confxml.xml cfgs_confxml.xml
cp confxml.xml objects_confxml.xml
规范目录看着舒服多了
[root@nagios01 conf]# ll
total 12
-rwxr-xr-x. 1 root root 2328 Feb 23 00:49 cfgs_confxml.xml
-rwxr-xr-x. 1 root root 2339 Feb 23 00:50 objects_confxml.xml
drwxr-xr-x. 2 root root 4096 Feb 23 00:51 tmp


8,配置文件内容删除多余节点目录 修改日志报错名称 如下是比对结果
[root@nagios01 conf]# diff cfgs_confxml.xml objects_confxml.xml
23,24c23,24
<  <localpath watch="/usr/local/nagios/etc/cfgs">
<                   <remote ip="192.168.1.206" name="cfgs"/>
---
>    <localpath watch="/usr/local/nagios/etc/objects">
>                   <remote ip="192.168.1.206" name="objects"/>
34c34
< <failLog path="/usr/local/sersync/logs/cfgs_rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins
execute once-->
---
> <failLog path="/usr/local/sersync/logs/objects_rsync_fail_log.sh" timeToExecute="60"/><!--default every
60mins execute once-->
9,更新sersync运行状态

[root@nagios01 conf]# ps -ef|grep sersync
root      6134     1  0 Feb22 ?        00:00:00 sersync -r -d -o /usr/local/sersync/conf/confxml.xml
root      7178  2428  0 00:55 pts/0    00:00:00 grep sersync
[root@nagios01 conf]# pkill sersync
[root@nagios01 conf]# ps -ef|grep sersync
root      7187  2428  0 00:56 pts/0    00:00:00 grep sersync
10,同时启动2个配置文件,各自指定各自的配置文件(配置要求高些,进程数随之调整)

sersync -r -d -o /usr/local/sersync/conf/cfgs_confxml.xml
sersync -r -d -o /usr/local/sersync/conf/objects_confxml.xml
[root@nagios01 conf]# ps -ef |grep sersync
root      7290     1  0 01:01 ?        00:00:00 sersync -r -d -o /usr/local/sersync/conf/cfgs_confxml.xml
root      7308     1  0 01:01 ?        00:00:00 sersync -r -d -o /usr/local/sersync/conf/objects_confxml.xml
root      7327  2428  0 01:01 pts/0    00:00:00 grep sersync
11,在做一下测试

[root@nagios01 conf]# touch /usr/local/nagios/etc/cfgs/2.txt /usr/local/nagios/etc/objects/2.txt
[root@nagios01 conf]# tree /usr/local/nagios/etc/
/usr/local/nagios/etc/
├── cfgs
│   ├── 1.txt
│   ├── 2.txt
│   ├── cfgs.log
├── objects
│   ├── 1.txt
│   ├── 2.txt
│   ├── objects.log
│
[root@Nagios02 /usr/local/nagios/etc]$tree
├── cfgs
│   ├── 1.txt
│   ├── 2.txt
│   ├── cfgs.log
├── objects
│   ├── 1.txt
│   ├── 2.txt
│   ├── objects.log
Sersync 多实例 实时同步配置成功,希望大家多多支持。http://qiuyt.blog.51cto.com

本文出自 “实用笔记” 博客,请务必保留此出处http://qiuyt.blog.51cto.com/1229789/1744376
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: