0x0 測試環境
總部生產服務器與分部備份服務器要求實現異地數據備份,環境如下
**centos 6.5**
生產服務器目錄: /home/zytest/files
備份服務器目錄: /home/zytest/files
用戶 / 密碼: zytest / zytest
0x1 生產服務器環境搭建
0x1.1 安裝gcc編譯器和rsync
yum install gcc rsync -y
0x1.2 拷貝inotify到服務器并解壓
cd /root
tar xfvz inotify-tools-3.13.tar.gz
0x1.3 進入inotify 目錄安裝
cd inotify-tools-3.13
./configure
make
make install
/usr/local/bin/inotifywait ##檢查是否安裝成功
0x2 備份服務器環境搭建
0x2.1 安裝 xinetd 和 rsync
yum install xinetd rsync -y
0x3 以下內容兩臺服務器同步操作
useradd -u 600 zytest
passwd zytest
zytest
su - zytest -c 'mkdir /home/zytest/files' ##創建同步目錄
0x4 備份服務器上配置rsyncd
0x4.1 編輯/etc/xinetd.d/rsync按照以下內容修改
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上,所以備份服務器安裝xinetd
0x4.3 把密碼寫入調用的密碼文件并賦予權限
echo zytest:zytest >> /home/rsync-dst.ps
chmod 600 /home/rsync-dst.ps
0x4.4 通過xinetd啟動rsync
/etc/rc.d/init.d/xinetd restart
0x5 主服務器上配置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 賦予腳本權限,設置開機啟動
chmod a+x /root/inosync
cp /root/inosync /etc/rc.d/init.d
0x5.3 配置調用的主腳本文件 /root/inosync.so.1
rhost=**備份服務器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腳本權限,拷貝到/usr/local/bin
chmod a+x /root/inosync.so.1
cp /root/inosync.so.1 /usr/local/bin
0x5.5 把密碼寫入調用的密碼文件并賦予權限
echo zytest >> /home/rsync-src.ps
chmod 600 /home/rsync-src.ps
0x6 目標服務器設置inosync自動啟動并開啟inosync服務
chkconfig --level 3 inosync on
/etc/rc.d/init.d/inosync start
0x7 測試 END
在生產服務器/home/zytest/files目錄下創建文件和文件夾,查看備份存儲是否也同步了文件和文件夾,同步即成功。
過程可通過日志查看
tail -f /root/inosync.log
總結
到此這篇關于Linux 通過Rsync+Inotify實現本、異地遠程數據實時同步功能的文章就介紹到這了,更多相關rsync+inotify實現遠程實時同步內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!