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

需求逻辑图

Linux-14-Sersync

安装环境

角色 服务器配置 操作系统版本 IP地址 机器名
Sersync服务 VM CentOS 6.10(2.6.32-754.3.5.el6) 2.2.2.5 C64-5-S
Rsync服务 VM CentOS 6.10(2.6.32-754.3.5.el6) 2.2.2.6 C64-6-B
Rsync服务 VM CentOS 6.10(2.6.32-754.3.5.el6) 2.2.2.7 C64-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
[[email protected]-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
[[email protected] ~]# 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 
[[email protected] ~]# echo "rsync_backup:syner">>/etc/rsync_s.password 
[[email protected] ~]# less /etc/rsync_s.password 
rsync_backup:syner
[[email protected] ~]# chmod 600 /etc/rsync_s.password 
[[email protected] ~]# ll /etc/rsync_s.password 
-rw------- 1 root root 25 Sep 26 15:07 /etc/rsync_s.password
[[email protected] ~]# echo "rsync_backup:syner">>/etc/rsync_s.password
[[email protected] ~]# less /etc/rsync_s.password 
rsync_backup:syner
[[email protected] ~]# chmod 600 /etc/rsync_s.password 
[[email protected] ~]# ll /etc/rsync_s.password 
-rw------- 1 root root 19 Sep 26 15:09 /etc/rsync_s.password

启动rsync守护进程

rsync --daemon
[[email protected] ~]# rsync --daemon

[[email protected] ~]# ps -ef | grep rsync|grep -v grep
root     27202     1  0 15:31 ?        00:00:00 rsync --daemon

[[email protected] ~]# netstat -lnt | grep 873
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      
tcp        0      0 :::873                      :::*                        LISTEN      

[[email protected] ~]# 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)
[[email protected] ~]# rsync --daemon

[[email protected] ~]# ps -ef | grep rsync| grep -v grep
root      4312     1  0 15:32 ?        00:00:00 rsync --daemon

[[email protected] ~]# netstat -lnt | grep 873
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      
tcp        0      0 :::873                      :::*                        LISTEN   
   
[[email protected] ~]# 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
[[email protected] ~]# which rsync
/usr/bin/rsync

[[email protected] ~]# echo "/usr/bin/rsync --daemon">>/etc/rc.local
[[email protected] ~]# grep daemon /etc/rc.local 
/usr/bin/rsync --daemon
[[email protected] ~]# which rsync 
/usr/bin/rsync

[[email protected] ~]# echo "/usr/bin/rsync --daemon">>/etc/rc.local 
[[email protected] ~]# 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 
[[email protected] ~]# echo "syner">>/etc/rsync_c.password 
[[email protected] ~]# chmod 600 /etc/rsync_c.password 
[[email protected] ~]# cat /etc/rsync_c.password 
syner
[[email protected] ~]# ll /etc/rsync_c.password 
-rw------- 1 root root 25 Sep 26 15:49 /etc/rsync_c.password

3.测试rsync

rsync -avzP ./data1.txt [email protected]::syner --password-file=/etc/rsync_c.password
rsync -avzP ./data1.txt [email protected]::syner --password-file=/etc/rsync_c.password 
[[email protected] syner]# rsync -avzP ./data1.txt [email protected]::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
[[email protected] syner]# rsync -avzP ./data1.txt [email protected]::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

检查是否同步过去了

[[email protected] syner]# ll /home/syner/| grep data1.txt 
-rw-rw-r-- 1 syner syner   10240 Sep 25 21:01 data1.txt
[[email protected] ~]# 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
[[email protected] tool]# cd sersync
[[email protected] sersync]# mkdir conf bin log backup
[[email protected] sersync]# mv confxml.xml conf
[[email protected] sersync]# mv sersync2 bin/sersync
[[email protected] 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)
[[email protected] sersync]# /bin/cp conf/confxml.xml backup/confxml.xml.bak.$(date +%F)
[[email protected] 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
[[email protected] ~]# echo 'export PATH=$PATH:/home/syner/tool/sersync/bin'>>/etc/profile
[[email protected] ~]# tail -1 /etc/profile
export PATH=$PATH:/home/syner/tool/sersync/bin
[[email protected] ~]# source /etc/profile
[[email protected] ~]# which sersync
/home/syner/tool/sersync/bin/sersync
sersync -rdo /home/syner/tool/sersync/conf/confxml.xml 
[[email protected] ~]# 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 ./ [email protected]::syner --password-file=/etc/rsync_c.password >/dev/null 2>&1 

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

[[email protected] ~]$ 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
[[email protected] ~]$ 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
[[email protected] conf]# cat >>/etc/rc.local<<EOF
> sersync -do /home/syner/tool/sersync/conf/confxml.xml 
> EOF
[[email protected] 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 

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-08
  • 2021-04-17
  • 2021-06-11
  • 2021-10-09
  • 2022-12-23
  • 2021-09-01
猜你喜欢
  • 2022-12-23
  • 2021-12-24
  • 2021-05-29
  • 2021-09-08
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案