Sersync是利用Inotify和Rsync技術實現的對服務器數據實時同步的解決方案
需求邏輯圖
安裝環境
角色 | 服務器配置 | 操作系統版本 | 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
[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 [email protected]::syner --password-file=/etc/rsync_c.password
rsync -avzP ./data1.txt [email protected]::syner --password-file=/etc/rsync_c.password
[root@C64-5-S 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
[root@C64-5-S 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
檢查是否同步過去了
[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 ./ [email protected]::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