方法一: 一、安裝inotify-tools(客戶端),實現數據屬實備份 1、檢查目錄是否有如下文檔,沒有表示操作系統不支持 ls -l /proc/sys/fs/inotify 2、安裝inotify-tools軟體 yum install inotify-tools -y 如果安裝失敗,可能是源 ...
方法一(inotify+rsync):
1.安裝inotify-tools(客戶端)【監聽工具】,實現數據屬實備份檢查目錄是否有如下文檔,沒有表示操作系統不支持
ls -l /proc/sys/fs/inotify
2、安裝inotify-tools軟體
yum install inotify-tools -y
如果安裝失敗,可能是源包的問題,需要安裝第三方的源(wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo)
3、新開一個視窗(原網址),進行測試
一個視窗進行監聽另外一個創建創建文件:
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,delete /backup
4、確保服務端及客戶端的rsync正常的情況下,可以下如下腳本進行實時推送
#!/bin/bash
Path=/data
IP=172.16.1.99
/usr/bin/inotifywait -mrq --format '%w%f' -e close_write,delete $Path \
|while read file
do
cd $Path &&\
rsync -az ./ --delete rsync_backup@$IP::nfsbackup/ --password-file=/etc/rsync.password
done
註:客戶端為/data目錄,服務端為/nfsbackup目錄
5、將其設置成一個服務
在/etc/init.d/syncd目錄下寫入如下代碼執行或開機自啟等操作,然後執行chkconfig --add syncd
#!/bin/bash
#chkconfig: 2345 38 46
. /etc/init.d/functions
if [ $# -ne 1 ]
then
echo "usage: $0 {start|stop|status}"
exit 1
fi
case "$1" in
start)
if [ -e "/var/run/inotify.pid" ]
then
action "inotify service start fail" /bin/false
echo "sync server is running......"
sleep 1
exit 1
fi
/bin/bash /server/scripts/inotify.sh &
echo $$ >/var/run/inotify.pid
if [ `ps -ef|grep inotify|wc -l` -gt 2 ]
then
action "inotify service is started" /bin/true
else
action "inotify service is started" /bin/false
fi
;;
stop)
if [ `ps -ef|grep inotify|grep -v grep|wc -l` -a -e "/var/run/inotify.pid" ]
then
rm -f /var/run/inotify.pid >/dev/null 2>&1
pkill inotifywait
else
action "inotify service stop fail" /bin/false
echo "sync server is not running"
sleep 1
exit 1
fi
sleep 1
if [ `ps -ef|grep inotify|grep -v grep|wc -l` -eq 0 -a ! -e "/var/run/inotify.pid" ]
then
action "inotify service is stoped" /bin/true
else
action "inotify service is stoped" /bin/false
fi
;;
status)
if [ `ps -ef|grep inotify|wc -l` -gt 2 ]
then
action "inotify service is running"
else
action "inotify service is stoped"
fi
;;
*)
echo "usage: $0 {start|stop|status}"
exit 1
esac
方法二(sersync+resync):
1、新建一個/application/目錄,網上下載個sersync文件,即解壓(客戶端配置)
2、查看GNU-Linux-x86/confxml.xml配置文件信息
3、修改GNU-Linux-x86/confxml.xm配置信息
4、查看啟動文件的屬性
/application/GNU-Linux-x86/sersync2 -h
5、執行測試
/application/GNU-Linux-x86/sersync2 -d -r -n 8 -o /application/GNU-Linux-x86/confxml.xml
6、測試如上述提示表成功寫入(/etc/rc.local)開機自啟即可
echo '/application/GNU-Linux-x86/sersync2 -d -r -n 8 -o /application/GNU-Linux-x86/confxml.xml' >>/etc/rc.local
小結:
伺服器端通過命令也可實現監聽(watch),但是延遲兩秒。