Linux-14-Sersync

Sersync是利用Inotify和Rsync技术实现的对服务器数据实时同步的解决方案

需求逻辑图

安装环境

角色服务器配置操作系统版本IP地址机器名
Sersync服务VMCentOS 6.10(2.6.32-754.3.5.el6)2.2.2.5C64-5-S
Rsync服务VMCentOS 6.10(2.6.32-754.3.5.el6)2.2.2.6C64-6-B
Rsync服务VMCentOS 6.10(2.6.32-754.3.5.el6)2.2.2.7C64-7-C

 

配置服务

1.在Server端部署rsync服务

因为我们要把文件从Server同步到奥Client端,所以这里需要在Client端配置rsync的守护进程

首先在Client端编写rsync守护进程的配置文件

cat>/etc/rsyncd.conf<<EOF
#Create by Pual
uid = root
gid = root
use chroot = no
max conntions = 2000
timeout = 600
pid file = /var/run/rsyncd.pid
lock filr = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 2.2.2.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync_s.password
[syner]
path = /home/syner
comment = syner by paul 2018
EOF
[root@C64-6-B ~]# less /etc/rsyncd.conf 
#Rsync server
#Create by Pual
uid = root
gid = root
use chroot = no
max conntions = 2000
timeout = 600
pid file = /var/run/rsyncd.pid
lock filr = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 2.2.2.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync_s.password
[syner]
path = /home/syner
comment = syner by paul 2018
[root@C64-7-C ~]# less /etc/rsyncd.conf 
#Create by Pual
uid = root
gid = root
use chroot = no
max conntions = 2000
timeout = 600
pid file = /var/run/rsyncd.pid
lock filr = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 2.2.2.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync_s.password
[syner]
path = /home/syner
comment = syner by paul 2018

配置密码文件

echo "rsync_backup:syner">>/etc/rsync_s.password 
chmod 600 /etc/rsync_s.password 
[root@C64-6-B ~]# echo "rsync_backup:syner">>/etc/rsync_s.password 
[root@C64-6-B ~]# less /etc/rsync_s.password 
rsync_backup:syner
[root@C64-6-B ~]# chmod 600 /etc/rsync_s.password 
[root@C64-6-B ~]# ll /etc/rsync_s.password 
-rw------- 1 root root 25 Sep 26 15:07 /etc/rsync_s.password
[root@C64-7-C ~]# echo "rsync_backup:syner">>/etc/rsync_s.password
[root@C64-7-C ~]# less /etc/rsync_s.password 
rsync_backup:syner
[root@C64-7-C ~]# chmod 600 /etc/rsync_s.password 
[root@C64-7-C ~]# ll /etc/rsync_s.password 
-rw------- 1 root root 19 Sep 26 15:09 /etc/rsync_s.password

启动rsync守护进程

rsync --daemon
[root@C64-6-B ~]# rsync --daemon

[root@C64-6-B ~]# ps -ef | grep rsync|grep -v grep
root     27202     1  0 15:31 ?        00:00:00 rsync --daemon

[root@C64-6-B ~]# netstat -lnt | grep 873
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      
tcp        0      0 :::873                      :::*                        LISTEN      

[root@C64-6-B ~]# lsof -i:873
COMMAND   PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
rsync   27202 root    3u  IPv4 3887958      0t0  TCP *:rsync (LISTEN)
rsync   27202 root    5u  IPv6 3887959      0t0  TCP *:rsync (LISTEN)
[root@C64-7-C ~]# rsync --daemon

[root@C64-7-C ~]# ps -ef | grep rsync| grep -v grep
root      4312     1  0 15:32 ?        00:00:00 rsync --daemon

[root@C64-7-C ~]# netstat -lnt | grep 873
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      
tcp        0      0 :::873                      :::*                        LISTEN   
   
[root@C64-7-C ~]# lsof -i:873
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rsync   4312 root    3u  IPv4  23469      0t0  TCP *:rsync (LISTEN)
rsync   4312 root    5u  IPv6  23470      0t0  TCP *:rsync (LISTEN)

添加到开机自启动服务中

which rsync
echo "/usr/bin/rsync --daemon">>/etc/rc.local
[root@C64-6-B ~]# which rsync
/usr/bin/rsync

[root@C64-6-B ~]# echo "/usr/bin/rsync --daemon">>/etc/rc.local
[root@C64-6-B ~]# grep daemon /etc/rc.local 
/usr/bin/rsync --daemon
[root@C64-7-C ~]# which rsync 
/usr/bin/rsync

[root@C64-7-C ~]# echo "/usr/bin/rsync --daemon">>/etc/rc.local 
[root@C64-7-C ~]# grep daemon /etc/rc.local 
/usr/bin/rsync --daemon

 

2.在client端配置rsync

echo "syner">>/etc/rsync_c.password 
chmod 600 /etc/rsync_c.password 
cat /etc/rsync_c.password 
ll /etc/rsync_c.password 
[root@C64-5-S ~]# echo "syner">>/etc/rsync_c.password 
[root@C64-5-S ~]# chmod 600 /etc/rsync_c.password 
[root@C64-5-S ~]# cat /etc/rsync_c.password 
syner
[root@C64-5-S ~]# ll /etc/rsync_c.password 
-rw------- 1 root root 25 Sep 26 15:49 /etc/rsync_c.password

3.测试rsync

rsync -avzP ./data1.txt rsync_backup@2.2.2.6::syner --password-file=/etc/rsync_c.password
rsync -avzP ./data1.txt rsync_backup@2.2.2.7::syner --password-file=/etc/rsync_c.password 
[root@C64-5-S syner]# rsync -avzP ./data1.txt rsync_backup@2.2.2.6::syner --password-file=/etc/rsync_c.password 
sending incremental file list
data1.txt
       10240 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 110 bytes  received 27 bytes  91.33 bytes/sec
total size is 10240  speedup is 74.74
[root@C64-5-S syner]# rsync -avzP ./data1.txt rsync_backup@2.2.2.7::syner --password-file=/etc/rsync_c.password 
sending incremental file list
data1.txt
       10240 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 110 bytes  received 27 bytes  91.33 bytes/sec
total size is 10240  speedup is 74.74

检查是否同步过去了

[root@C64-6-B syner]# ll /home/syner/| grep data1.txt 
-rw-rw-r-- 1 syner syner   10240 Sep 25 21:01 data1.txt
[root@C64-7-C ~]# ll /home/syner/| grep data1.txt 
-rw-rw-r-- 1 syner syner 10240 Sep 25 21:01 data1.txt

 

4.安装Sersync

wget --no-check-certificate https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz
tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz 

规范目录名称

mv GNU-Linux-x86/ sersync
cd sersync
mkdir conf bin log backup
mv confxml.xml conf
mv sersync2 bin/sersync
tree
[root@C64-5-S tool]# cd sersync
[root@C64-5-S sersync]# mkdir conf bin log backup
[root@C64-5-S sersync]# mv confxml.xml conf
[root@C64-5-S sersync]# mv sersync2 bin/sersync
[root@C64-5-S sersync]# tree
.
├── backup
├── bin
│   └── sersync
├── conf
│   └── confxml.xml
└── log

4 directories, 2 files

 

5.配置Sersync

备份配置文件

/bin/cp conf/confxml.xml backup/confxml.xml.bak.$(date +%F)
[root@C64-5-S sersync]# /bin/cp conf/confxml.xml backup/confxml.xml.bak.$(date +%F)
[root@C64-5-S sersync]# ll backup
total 4
-rwxr-xr-x 1 root root 2214 Sep 26 20:51 confxml.xml.bak.2018-09-26

更改配置文件

     24   <localpath watch="/home/syner">
     25       <remote ip="2.2.2.6" name="syner"/>
     26       <remote ip="2.2.2.7" name="syner"/>
     27   </localpath>
     28   <rsync>
     29       <commonParams params="-aruz"/>
     30       <auth start="true" users="rsync_backup" passwordfile="/etc/rsync_c.password"/>
     31       <userDefinedPort start="false" port="874"/><!-- port=874 -->
     32       <timeout start="false" time="100"/><!-- timeout=100 -->
     33       <ssh start="false"/>
     34   </rsync>
35   <failLog path="/home/syner/tool/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><        !--default every 60mins execute once-->

 

6.开启Sersync守护进程同步数据

echo 'export PATH=$PATH:/home/syner/tool/sersync/bin'>>/etc/profile
tail -1 /etc/profile
source /etc/profile
which sersync
[root@C64-5-S ~]# echo 'export PATH=$PATH:/home/syner/tool/sersync/bin'>>/etc/profile
[root@C64-5-S ~]# tail -1 /etc/profile
export PATH=$PATH:/home/syner/tool/sersync/bin
[root@C64-5-S ~]# source /etc/profile
[root@C64-5-S ~]# which sersync
/home/syner/tool/sersync/bin/sersync
sersync -rdo /home/syner/tool/sersync/conf/confxml.xml 
[root@C64-5-S ~]# sersync -rdo /home/syner/tool/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:  /home/syner/tool/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_c.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 /home/syner && rsync -aruz -R --delete ./ rsync_backup@2.2.2.6::syner --password-file=/etc/rsync_c.password >/dev/null 2>&1 

这时我们发现B、C客户端机器的syner目录已经被同步了

[syner@C64-6-B ~]$ ll
total 4040
-rw-rw-r-- 1 syner syner   10240 Sep 25 21:01 data1.txt
-rw-rw-r-- 1 syner syner   30720 Sep 25 21:01 data2.txt
-rw-rw-r-- 1 syner syner   51200 Sep 25 21:01 data3.txt
-rw-r--r-- 1 syner syner     230 Sep 25 17:08 inotify.log
-rw-r--r-- 1 root  root        0 Sep 25 15:11 inotify.txt
drwxrwxr-x 2 syner syner 4022272 Sep 25 21:25 r_inotify
-rw-rw-r-- 1 syner syner      17 Sep 25 16:58 s_inotify
-rw-rw-r-- 1 syner syner       0 Sep 25 16:26 s_test.txt
-rw-rw-r-- 1 syner syner       0 Sep 26 16:28 syner
-rw-rw-r-- 1 syner syner       0 Sep 26 15:54 test_rsync.dat
drwxrwxr-x 3 syner syner    4096 Sep 26 17:36 tool
[syner@C64-7-C ~]$ ll
total 1636
-rw-rw-r-- 1 syner syner   10240 Sep 25 21:01 data1.txt
-rw-rw-r-- 1 syner syner   30720 Sep 25 21:01 data2.txt
-rw-rw-r-- 1 syner syner   51200 Sep 25 21:01 data3.txt
-rw-r--r-- 1 syner syner     230 Sep 25 17:08 inotify.log
-rw-r--r-- 1 root  root        0 Sep 25 15:11 inotify.txt
drwxrwxr-x 2 syner syner 1560576 Sep 26 21:15 r_inotify
-rw-rw-r-- 1 syner syner      17 Sep 25 16:58 s_inotify
-rw-rw-r-- 1 syner syner       0 Sep 25 16:26 s_test.txt
-rw-rw-r-- 1 syner syner       0 Sep 26 16:28 syner
-rw-rw-r-- 1 syner syner       0 Sep 26 15:54 test_rsync.dat
drwx------ 2 root  root     4096 Sep 26 21:14 tool

将命令加到rc.local中,开机自启动

cat >>/etc/rc.local<<EOF
sersync -do /home/syner/tool/sersync/conf/confxml.xml 
EOF
[root@C64-5-S conf]# cat >>/etc/rc.local<<EOF
> sersync -do /home/syner/tool/sersync/conf/confxml.xml 
> EOF
[root@C64-5-S conf]# less /etc/rc.local 
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
#NFS and RPC services config by test 20180910
/etc/init.d/rpcbind start
/etc/init.d/nfs start
/usr/bin/rsync --daemon
sersync -do /home/syner/tool/sersync/conf/confxml.xml 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值