0x0 測(cè)試環(huán)境
總部生產(chǎn)服務(wù)器與分部備份服務(wù)器要求實(shí)現(xiàn)異地?cái)?shù)據(jù)備份,環(huán)境如下
**centos 6.5**
生產(chǎn)服務(wù)器目錄: /home/zytest/files
備份服務(wù)器目錄: /home/zytest/files
用戶 / 密碼: zytest / zytest
0x1 生產(chǎn)服務(wù)器環(huán)境搭建
0x1.1 安裝gcc編譯器和rsync
yum install gcc rsync -y
0x1.2 拷貝inotify到服務(wù)器并解壓
cd /root
tar xfvz inotify-tools-3.13.tar.gz
0x1.3 進(jìn)入inotify 目錄安裝
cd inotify-tools-3.13
./configure
make
make install
/usr/local/bin/inotifywait ##檢查是否安裝成功
0x2 備份服務(wù)器環(huán)境搭建
0x2.1 安裝 xinetd 和 rsync
yum install xinetd rsync -y
0x3 以下內(nèi)容兩臺(tái)服務(wù)器同步操作
useradd -u 600 zytest
passwd zytest
zytest
su - zytest -c 'mkdir /home/zytest/files' ##創(chuàng)建同步目錄
0x4 備份服務(wù)器上配置rsyncd
0x4.1 編輯/etc/xinetd.d/rsync按照以下內(nèi)容修改
disable = yes ==> disable = no
flags = IPv6 ==> flags = IPv4
server_args = --daemon ==> server_args = --daemon --config=/etc/rsyncd.conf
0x4.2 編輯/etc/rsyncd.conf 并添加以下腳本信息
uid = root
gid = root
use chroot = no
max connections = 1000
strict mode = yes
port = 873
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
# following for user "zytest", change for other users
[zytest]
path = /home/zytest
ignore errors
auth users =zytest
secrets file = /home/rsync-dst.ps
read only = no
list = false
Ps: rsyncd 配置文件在 xinetd上,所以備份服務(wù)器安裝xinetd
0x4.3 把密碼寫(xiě)入調(diào)用的密碼文件并賦予權(quán)限
echo zytest:zytest >> /home/rsync-dst.ps
chmod 600 /home/rsync-dst.ps
0x4.4 通過(guò)xinetd啟動(dòng)rsync
/etc/rc.d/init.d/xinetd restart
0x5 主服務(wù)器上配置inosync腳本文件
0x5.1 ** ##編輯/root/inosync添加腳本代碼**
#!/bin/sh
#chkconfig: 3 78 10
#This file exist from compile
if [ ! -f /usr/local/bin/inotifywait ]
then
echo "cannot start. file inotifywait NOT exist!"
exit
fi
#This file is runnable shell script
if [ ! -f /usr/local/bin/inosync.so.1 ]
then
echo "contact administrator. inosync.so.1 NOT exist!"
exit
fi
case "$1" in
'start')
/usr/local/bin/inosync.so.1 &
;;
'stop')
pid=`ps -ef | grep -v grep | grep "inotifywait" | awk '{print $2}'`
kill -9 $pid 2>&1
;;
'restart')
$0 stop
$0 start
;;
esac
0x5.2 賦予腳本權(quán)限,設(shè)置開(kāi)機(jī)啟動(dòng)
chmod a+x /root/inosync
cp /root/inosync /etc/rc.d/init.d
0x5.3 配置調(diào)用的主腳本文件 /root/inosync.so.1
rhost=**備份服務(wù)器IP**
user=zytest
src=/home/zytest/files
dst=zytest
#dst corresponding to [zytest] in file /etc/rsyncd.conf on dst server
log=/root/inosync.log
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M:%S' --format '%T %w%f %e' -e close_write,create,move,delete,attrib $src | while read files
do
echo == rsync begin == >> $log
rsync -avP --password-file=/home/rsync-src.ps --delete $src $user@$rhost::$dst >> $log 2>&1
echo -- rsyncd -- >> $log
date >> $log
echo "${files} was rsynced " >> $log 2>&1
done
PS: %T后面有空格 %f和%e之間也有空格
0x5.4 賦予inosync.so.1腳本權(quán)限,拷貝到/usr/local/bin
chmod a+x /root/inosync.so.1
cp /root/inosync.so.1 /usr/local/bin
0x5.5 把密碼寫(xiě)入調(diào)用的密碼文件并賦予權(quán)限
echo zytest >> /home/rsync-src.ps
chmod 600 /home/rsync-src.ps
0x6 目標(biāo)服務(wù)器設(shè)置inosync自動(dòng)啟動(dòng)并開(kāi)啟inosync服務(wù)
chkconfig --level 3 inosync on
/etc/rc.d/init.d/inosync start
0x7 測(cè)試 END
在生產(chǎn)服務(wù)器/home/zytest/files目錄下創(chuàng)建文件和文件夾,查看備份存儲(chǔ)是否也同步了文件和文件夾,同步即成功。
過(guò)程可通過(guò)日志查看
tail -f /root/inosync.log
總結(jié)
到此這篇關(guān)于Linux 通過(guò)Rsync+Inotify實(shí)現(xiàn)本、異地遠(yuǎn)程數(shù)據(jù)實(shí)時(shí)同步功能的文章就介紹到這了,更多相關(guān)rsync+inotify實(shí)現(xiàn)遠(yuǎn)程實(shí)時(shí)同步內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!