大數據量sersync實時同步

1、Rsync+Inotify-tools
(1):Inotify-tools只能記錄下被監聽的目錄發生了變化(包括增加、刪除、修改),並沒有把具體是哪個文件或者哪個目錄發生了變化記錄下來;
(2):rsync在同步的時候,並不知道具體是哪個文件或者哪個目錄發生了變化,每次都是對整個目錄進行同步,當數據量很大時,整個目錄同步非常耗時(rsync要對整個目錄遍歷查找對比文件),因此,效率很低。
2、Rsync+sersync
(1):sersync可以記錄下被監聽目錄中發生變化的(包括增加、刪除、修改)具體某一個文件或某一個目錄的名字;
(2):rsync在同步的時候,只同步發生變化的這個文件或者這個目錄(每次發生變化的數據相對整個同步目錄數據來說是很小的,rsync在遍歷查找比對文件時,速度很快),因此,效率很高。
小結:當同步的目錄數據量不大時,建議使用Rsync+Inotify-tools;當數據量很大(幾百G甚至1T以上)、文件很多時,建議使用Rsync+sersync
一、需求
有臺500G容量的磁盤需要更改成300G的磁盤,數據有200G一直有數據進來。
1、sersync+rsync實時同步
2、關閉服務再次同步數據
3、切換磁盤

sersync+rsync實時同步
1.1、備份服務器配置
升級rsync到3.0以上
rsync --version|head -2
rsync version 3.1.2 protocol version 31
1、rsync服務的配置文件:
vi /etc/rsyncd.conf

uid = root
gid = root
port=873
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = test
hosts allow = 172.0.0.0/8
hosts deny = 0.0.0.0/32
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.password
motd file = /etc/rsyncd.Motd
[rsync]
path = /data1/clickhouse
comment = rsync

######
uid = root #設置rsync運行權限爲root
gid = root #設置rsync運行權限爲root
port=873 #默認端口
use chroot = no #默認爲true,修改爲no,增加對目錄文件軟連接的備份
read only = no #設置rsync服務端文件爲讀寫權限
list = no #不顯示rsync服務端資源列表
max connections = 200 #最大連接數
timeout = 600 #設置超時時間
auth users = test #執行數據同步的用戶名,可以設置多個,用英文狀態下逗號隔開
hosts allow = 172.0.0.0/8 #允許進行數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開
hosts deny = 0.0.0.0/32 #禁止數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開
log file = /var/log/rsyncd.log #日誌文件位置,啓動rsync後自動產生這個文件,無需提前創建
pidfile = /var/run/rsyncd.pid #pid文件的存放位置
lock file = /var/run/rsync.lock #支持max connections參數的鎖文件
secrets file = /etc/rsync.pass #用戶認證配置文件,裏面保存用戶名稱和密碼,後面會創建這個文件
motd file = /etc/rsyncd.Motd #rsync啓動時歡迎信息頁面文件位置(文件內容自定義)
[rsync] #自定義名稱
path = /opt/rsync/ #rsync服務端數據目錄路徑
comment = rsync #模塊名稱與[home_www.osyunwei.com]自定義名稱相同
########
上面的uid、gid要換成您服務器的相應的同步用戶;注意,rsync服務賬戶(本文用的是root)要有對被同步目錄(/data/)的寫入更新權限;
2、創建用戶認證文件,設置權限爲600
echo "rsync_backup:rsync_backup">/etc/rsync.password
chmod 600 /etc/rsync.password
#格式,用戶名:密碼,可以設置多個,每行一個用戶名:密碼
chmod 600 /etc/rsyncd.conf
chmod 600 /etc/rsync.password
啓動守護進程,並寫入開機自啓動
rsync --daemon
systemctl enable rsyncd
lsof -i:873
3、創建同步目錄
mkdir /opt/rsync
二、master端被備份的服務器
1、在master上配置rsync客戶端相關權限認證:
echo "test123" > /etc/rsyncd.conf
chmod 600 /etc/rsyncd.conf
2、mter主服務器上手工測試rsync同步情況
特別提示:此步非常關鍵,如果測試不成功,後面的sersync配好了也不會同步數據
/data1/clickhouse #備份到目的端的路徑

測試:把本地/mnt/sersync2.5.4拷貝到定義的rsync路徑/data1/clickhouse指定密碼文件
rsync -avz /mnt/sersync2.5.4_64bit_binary_stable_final.tar.gz [email protected]::rsync --password-file=/etc/rsync.pass
查看客戶端/opt/tongbu/是否有數據過來
安裝sersync

wget  https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz
tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/src
mv /usr/local/src/GNU-Linux-x86 /usr/local/sersync
cd /usr/local/sersync
cp confxml.xml confxml.xml.$(date +%F)

更改優化sersync配置文件:
vim confxml.xml
a) 修改24--28行
<localpath watch="/opt/tongbu"> # 本地要同步的目錄、備份的源路徑
<remote ip="127.0.0.1" name="tongbu1"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>--> # 同步到哪臺機器上 tongbu模塊rsync端模塊名字
<!--<remote ip="192.168.8.40" name="tongbu"/>--> # 同步到哪臺機器上 tongbu模塊
</localpath>
更改後

   <sersync>
        <localpath watch="/data/clickhouse">
            <remote ip="10.10.44.95" name="rsync"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
        <!--################################################## -->
         <localpath watch="/opt/tongbu1">
             <remoteip="172.21.1.9" name="bbs"/>
             <remoteip="172.21.1.10" name="bbs"/>
         </localpath>
        <sersync>

b)修改31--34行,認證部分【rsync密碼認證】
<rsync>
<commonParams params="-artuz"/>
<auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
修改後:

<rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="test" passwordfile="/etc/rsync.password"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="true" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>    
修改內容爲 rsync的密碼文件以及 同步所使用的賬號類似:
rsync -avz /opt/tongbu/ [email protected]::rsync --password-file=/etc/rsync.password  

c)修改37行
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->

當同步失敗後,日誌記錄到/usr/local/sersync/logs/rsync_fail_log.sh路徑中,並且每60分鐘對失敗的log進行重新同步

修改後:    
        <failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"
mkdir /usr/local/sersync/logs

查看sersync參數
/usr/local/sersync/sersync2 -h
啓動sersync
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml

關閉服務再次同步數據
2.1 停止服務
停止服務後再次實時同步下數據
more stop.sh 

#!/bin/bash
echo "關閉所有服務"
supervisorctl stop all
docker stop 6806d7682289 393365cb2259
sleep 2
/usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml

切換磁盤
3.1、切換磁盤
卸載老的磁盤後掛載新磁盤加入到開機啓動
more mount.sh 

#!/bin/bash
pkill rsync
umount /data
umount /data1
sed -i '/data/'d /etc/fstab
echo "掛載磁盤"
mount /dev/vdc /data
systemctl daemon-reload
echo "開機自動掛載"
UUID=$(blkid |grep vdc|awk -F ' ' '{print $2}')
echo "$UUID /data xfs defaults 0 0" >> /etc/fstab

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章