rsync簡介:
Rsync 是一個遠程數據同步工具,使用所謂的“Rsync 演算法”來使本地和遠程兩個主機之間的文件達到同步,這個算法只傳送兩個文件的不同部分,而不是每次都整份傳送,因此速度相當快。運行 Rsync server 的機器也叫 backup server,一個 Rsync server 可同時備份多個 client 的數據;也可以多個Rsync server 備份一個 client 的數據。Rsync 可以搭配 rsh 或 ssh 甚至使用 daemon 模式。Rsync server 會打開一個873的服務通道(port),等待對方 Rsync 連接。連接時,Rsync server 會檢查口令是否相符,若通過口令查覈,則可以開始進行文件傳輸。第一次連通完成時,會把整份文件傳輸一次,下一次就只傳送二個文件之間不同的部份
inotify-tools簡介:
inotify-tools 是爲linux下inotify文件監控工具提供的一套c的開發接口庫函數,同時還提供了一系列的命令行工具,這些工具可以用來監控文件系統的事件。 inotify-tools是用c編寫的,除了要求內核支持inotify外,不依賴於其他。inotify-tools提供兩種工具,一是inotifywait,它是用來監控文件或目錄的變化,二是inotifywatch,它是用來統計文件系統訪問的次數。現在介紹一下它的使用方法
環境部署:
inotify_slave 42.159.246.48:57893 數據備份端
inotify_master 42.159.246.48:57892 源數據端
一、部署數據備份端:
1. 備份端部署rsync服務:
[root@WEB2 ~]#yum -y install rsync
[root@WEB2 ~]#useradd rsync -s /sbin/nologin -M
[root@WEB2 ~]#mkdir -p /cmsresoure/p_w_picpath/backup
[root@WEB2 ~]#chown rsync:rsync /cmsresoure/p_w_picpath/backup
2. 添加配置文件/etc/rsyncd.conf
[root@WEB2 ~]#vim /etc/rsyncd.conf
##################################################################################
##rsyncd.conf start##
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[backup]
path = /cmsresoure/p_w_picpath/backup/
ignore errors
read only = false
list = false
hosts allow = *
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
#rsync_config_______________end
##################################################################################
3. 配置虛擬用戶的密碼文件並啓動服務
[root@WEB2 ~]# echo "rsync_backup:leesir" >/etc/rsync.password
[root@WEB2 ~]# chmod 600 /etc/rsync.password
[root@WEB2 ~]# rsync --daemon
[root@WEB2 ~]# ps -ef | grep rsync
4. 通過源數據端測試推送
[root@web1 ~]# echo "leesir" >/etc/rsync.password #注意:這裏只要寫密碼即可,切記。
[root@web1 ~]# chmod 600 /etc/rsync.password
[root@web1 ~]# rsync -avzlh inotify.sh [email protected]::backup --password-file=/etc/rsync.password
在源數據端推送後,去數據備份端檢查是否推送成功。
二、源數據端部署
1. 查看當前系統是否支持inotify
[root@web1 ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Oct 8 18:54 max_queued_events
-rw-r--r-- 1 root root 0 Oct 8 18:54 max_user_instances
-rw-r--r-- 1 root root 0 Oct 8 18:54 max_user_watches
Remarks:
#顯示這三個文件則證明支持。
/proc/sys/fs/inotify/max_queued_evnets
#表示調用inotify_init時分配給inotify instance中可排隊的event的數目的最大值,超出這個值的事件被丟棄,但會觸發IN_Q_OVERFLOW事件。
/proc/sys/fs/inotify/max_user_instances
#表示每一個real user ID可創建的inotify instatnces的數量上限。
/proc/sys/fs/inotify/max_user_watches
#表示每個inotify instatnces可監控的最大目錄數量。如果監控的文件數目巨大,需要根據情況,適當增加此值的大小。
例如: echo 30000000 > /proc/sys/fs/inotify/max_user_watches
2. 部署inotify服務:
[root@web1 ~]# yum -y install inotify-tools
[root@web1 ~]# rpm -qa | grep inotify-tools
inotify-tools-3.14-1.el6.x86_64
[root@web1 ~]# vim inotify.sh
##################################################################################
#!/bin/bash
#
#2015.10.8
#
DESTHOST=42.159.246.48
SRCDIR=/cmsresoure/p_w_picpath/backup/
PASSWD=/etc/rsync.password
USER=rsync_backup
inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e close_write,modify,create,attrib $SRCDIR | while read DATE TIME DIR FILE;do
FILECHANGE=${DIR}${FILE}
rsync -avztopglH $SRCDIR $USER@${DESTHOST}::backup --password-file=$PASSWD &>/dev/null && \
echo "At ${TIME} on ${DATE}, file $FILECHANGE was backed up via rsync" >> /var/log/p_w_picpath.log
done
##################################################################################
3. 測試腳本創建文件測試:
[root@web1 ~]# more /home/user/touch_file.sh
##################################################################################
#!/bin/sh
#
#2015.10.8
#
#Create 5 100K files, file name is a random number
#
#
for ((i=1;i<6;i++))
do
dd if=/dev/zero of=/cmsresoure/p_w_picpath/backup/$(sha1sum<<<$RANDOM|cut -b 5-10) bs=100k count=1
echo "touch $i done"
sleep 3
done
echo "the script is done"
##################################################################################
[root@web1 ~]# echo "*/30 * * * * /bin/bash /home/user/touch_file.sh > /dev/null 2>&1" >> /etc/cronteb
[root@web1 ~]# sh inotify.sh & #將腳本加入後臺執行
Setting up watches. Beware: since -r was given, this may take a while!
Watches established.
三. 實時同步測試
1. 源數據端操作:
[root@web1 ~]# sh /home/user/touch_file.sh
##################################################################################
1+0 records in
1+0 records out
102400 bytes (102 kB) copied, 0.000441594 s, 232 MB/s
touch 1 done
1+0 records in
1+0 records out
102400 bytes (102 kB) copied, 0.000603492 s, 170 MB/s
touch 2 done
1+0 records in
1+0 records out
102400 bytes (102 kB) copied, 0.000548994 s, 187 MB/s
touch 3 done
1+0 records in
1+0 records out
102400 bytes (102 kB) copied, 0.000423695 s, 242 MB/s
touch 4 done
the script is done
##################################################################################
[root@web1 ~]# ls /cmsresoure/p_w_picpath/backup/
039f90 15aaf2 1c2d4b 3b079d
2. 數據備份端檢查:
[root@WEB2 ~]# cd /cmsresoure/p_w_picpath/backup/
[root@WEB2 backup]# ls
039f90 15aaf2 1c2d4b 3b079d
四. 維護:
1. 設置在備份端監控Rsync服務的腳本:
[root@WEB2 shell]# vim rsync_check.sh
##################################################################################
#!/bin/bash
#
#2015.10.8
#
#The script will check the Rsync service, if you find that Rsync will restart the service.
RSYNC_CHECK=`netstat -npl | grep -c rsync`
if [ "$RSYNC_CHECK" -eq "0" ];
then
/usr/bin/rsync --daemon
fi
##################################################################################
[root@web1 shell]# echo "*/2 * * * * /bin/bash /shell/rsync_check.sh >/dev/null 2>&1" >>/etc/crontab
Remarks:
將腳本加入到計劃任務中,沒兩分鐘檢查一次rsync是否正常運行
2. 設置在源數據端檢查inotify服務的腳本:
[root@web1 shell]# vim check_inotify.sh
##################################################################################
#!/bin/bash
#
#2015.10.9
#
#the script will to check the inotify server
#
#
INOTIFY=/shell/inotify.sh
CHECK_INOTIFY=`ps -ef | grep -c inotify.sh`
if [ "$CHECK_INOTIFY" -eq "1" ];
then
/bin/bash "$INOTIFY" >/dev/null 2>&1
fi
##################################################################################
[root@web1 shell]# echo "*/2 * * * * /bin/bash /shell/check_inotify.sh >/dev/null 2>&1" >> /etc/crontab
五。測試結果:
rsync+inotify-tools同步效果:rsync可以完全實時同步監控的文件夾,並輸出同步日誌。