Linux 通過Rsync+Inotify實(shí)現(xiàn)本、異地遠(yuǎn)程數(shù)據(jù)實(shí)時(shí)同步功能
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 把密碼寫入調(diào)用的密碼文件并賦予權(quán)限
echo zytest:zytest >> /home/rsync-dst.ps chmod 600 /home/rsync-dst.ps
0x4.4 通過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è)置開機(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 把密碼寫入調(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)并開啟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ǔ)是否也同步了文件和文件夾,同步即成功。
過程可通過日志查看
tail -f /root/inosync.log
總結(jié)
到此這篇關(guān)于Linux 通過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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
CentOS7.2 忘記root密碼及重置root密碼的簡(jiǎn)單處理方法
這篇文章主要介紹了CentOS7.2 忘記root密碼的簡(jiǎn)單處理方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-08-08解決Linux系統(tǒng)yum安裝報(bào)錯(cuò)Cannot find a valid base
本文介紹了如何在Linux系統(tǒng)中設(shè)置本地yum源,包括修改yum配置文件、禁用默認(rèn)網(wǎng)絡(luò)源、創(chuàng)建掛載點(diǎn)以及掛載鏡像文件等步驟,操作詳細(xì),適合需要離線安裝軟件或更新系統(tǒng)的用戶參考2024-09-09虛擬機(jī)安裝centos7的坑之找不到網(wǎng)卡問題及解決
這篇文章主要介紹了虛擬機(jī)安裝centos7的坑之找不到網(wǎng)卡問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10Centos7服務(wù)器下啟動(dòng)jar包項(xiàng)目的最佳方法
這篇文章主要給大家分享介紹了關(guān)于Centos7服務(wù)器下啟動(dòng)jar包項(xiàng)目的最佳方法,文中通過示例代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03基于CentOS的Hadoop分布式環(huán)境的搭建開發(fā)
本篇文章介紹了基于CentOS的Hadoop分布式環(huán)境的搭建開發(fā),有興趣的可以了解一下。2016-11-11Linux下nginx配置https協(xié)議訪問的方法
這篇文章主要介紹了Linux下nginx配置https協(xié)議訪問的方法,需要的朋友可以參考下2016-07-07Linux命令學(xué)習(xí)總結(jié):詳解shutdown命令
本篇文章主要介紹了Linux命令學(xué)習(xí)總結(jié):詳解shutdown命令,該命令可以安全關(guān)閉或者重新啟動(dòng)系統(tǒng)。有興趣的可以了解一下。2016-12-12Linux中對(duì)MySQL優(yōu)化實(shí)例詳解
這篇文章主要介紹了Linux中對(duì)MySQL優(yōu)化實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05