多服務器高效數據實時同步方案
實驗環境
docker模擬兩臺centos7服務器
名稱 | ip | 類型 |
---|---|---|
centos_a | 172.17.0.2 | 數據服務器 |
centos_b | 172.17.0.3 | 備份服務器 |
實現將a的數據實時同步至b
安裝配置軟件
備份服務器(b)
1. 安裝rsync
安裝
#安裝
yum -y install rsync
#啓動
systemctl start rsyncd && systemctl enable rsyncd
配置rsync文件
vim /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 4
# pid file = /var/run/rsyncd.pid
lock file = /var/run/syncd.lock
log file=/var/log/rsyncd.log
exclude = lost+found/
transfer logging = yes
timeout = 900
ignore nonreadable = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
[test]
path=/work/share
comment=zsg test comment
ignore errors
read only=no
write only=no
list=no
auth users=utest
secrets file=/etc/rsyncd-test.pwd
字段解釋
#工作中指定用戶(可以不指定爲0)
uid = 0
gid = 0
#相當於黑洞.出錯定位
use chroot = no
##有多少個客戶端同時傳文件
max connections =200
##超時時間
timeout = 300
##進程號文件
pid ifle = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
##日誌文件
log file = /var/log/rsyncd.log
##模塊開始(這個模塊就是待會兒對端寫腳本的裏面一個參數的名稱)
[msone]
##需要同步的目錄(準確的說是同步過來後放到哪裏的目錄路徑)
path = /data/www
##表示出現錯誤忽略錯誤
ignore errors
##表示網絡權限可寫(本地控制真正可寫)(親測這裏寫false報錯)
read only = no
##這裏設置IP或讓不讓同步
list = false
#允許的ip或者ip段
hosts allow = 192.168.1.238
##拒絕,以下表示都不拒絕
hosts deny = 0.0.0.0/32
##認證用戶
auth users = rsynclsl
##用戶名和密碼存放文件
secrets file = /etc/rsync.secrets
創建密碼文件並重啓
echo "utest:123456">/etc/rsyncd-test.pwd
chmod 600 /etc/rsyncd-test.pwd
systemctl restart rsyncd
數據服務器(a)
1. 安裝rsync
安裝
yum -y install rsync
配置密碼
echo "123456">/etc/rsyncd-test.pwd
僅作爲數據服務器,完成安裝後無需啓動
2.安裝Inotify
下載
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
#如果下載過慢可以使用下面
wget https://gitee.com/zsgwakk/mirror/raw/master/inotify-tools-3.14.tar.gz
安裝
tar xzf inotify-tools-3.14.tar.gz ;cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify
make
make install
編寫腳本 inotify-test
當文件發送變化時觸發同步
vim inotify-test.sh
#!/bin/bash
src=/work/share/
des=test
rsync_passwd_file=/etc/rsyncd-test.pwd
ip1=172.17.0.3
user=utest
inotify_exclude="--fromfile '$PWD/notify-fromfile'"#排除監控文件
rsync_exclude="--exclude-from=$PWD/rsync-exclude"#排除同步文件
cd ${src}
/usr/local/inotify/bin/inotifywait -mrq --format '%Xe %w%f' -e modify,create,delete,attrib,close_write,move $inotify_exclude ./ | while read file
do
INO_EVENT=$(echo $file | awk '{print $1}')
INO_FILE=$(echo $file | awk '{print $2}')
if [[ $INO_EVENT =~ 'CREATE' ]] || [[ $INO_EVENT =~ 'MODIFY' ]] || [[ $INO_EVENT =~ 'CLOSE_WRITE' ]] || [[ $INO_EVENT =~ 'MOVED_TO' ]]
then
echo '--CREATE or MODIFY or CLOSE_WRITE or MOVED_TO---'
echo $file
if [ -f $INO_FILE ]
then
#文件
sudo /usr/bin/rsync -avzc $rsync_exclude --password-file=${rsync_passwd_file} $INO_FILE ${user}@${ip1}::${des}/$INO_FILE
elif [ -d $INO_FILE ]
then
#目錄
sudo /usr/bin/rsync -avzc $rsync_exclude --password-file=${rsync_passwd_file} $INO_FILE/ ${user}@${ip1}::${des}/$INO_FILE
else
echo "file not found"
fi
fi
if [[ $INO_EVENT =~ 'DELETE' ]] || [[ $INO_EVENT =~ 'MOVED_FROM' ]]
then
echo '--DELETE or MOVED_FROM--'
echo $file
sudo /usr/bin/rsync -avz $rsync_exclude --password-file=${rsync_passwd_file} $(dirname ${INO_FILE})/ ${user}@${ip1}::${des}/$(dirname ${INO_FILE})
fi
#不同步屬性變化
# if [[ $INO_EVENT =~ 'ATTRIB' ]]
# then
# if [ ! -d "$INO_FILE" ]
# then
# echo 'ATTRIB'
# sudo /usr/bin/rsync -avzc --exclude-from=rsync-exclude --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip1}::${des}
# fi
# fi
done
排除監控目錄
vim notify-fromfile
#過濾掉log和temp目錄
@/work/share/runtime/log
@/work/share/runtime/temp
排除同步目錄
data/conf/*
data/cliruntime/*
data/runtime/*
編寫腳本inotify-test-all.sh
手動同步全部文件
#!/bin/bash
sudo /usr/bin/rsync -avzc --password-file=/etc/rsyncd-test.pwd --exclude=runtime/log --exclude=runtime/temp /work/share [email protected]::test
執行腳本
#給倆腳本執行權限
#手動同步全部文件到a
sh inotify-test-all.sh
#啓動inotify監控同步
nohup sh inotify-test.sh &
#可以設置開機啓動,自行google
開始使用
在a中修改文件內容
echo "hello world"> /work/share/test.txt
在b中對應目錄即可查看
參考文章
https://blog.csdn.net/wintershang/article/details/89354539
https://blog.csdn.net/in_christ/article/details/80568384