1、Rsync簡介mysql
2、Rsync優勢與不足linux
3、Inotify 簡介git
4、Rsync+Inotify組合實驗案例github
1、Rsync簡介web
Rsync(remote synchronize)是一個遠程數據同步工具,可經過LAN/WAN快速同步多臺主機間的文件。Rsync使用所謂的「Rsync算法」來使本地和遠程兩個主機之間的文件達到同步,這個算法只傳送兩個文件的不一樣部分,而不是每次都整份傳送,所以速度至關快。算法
2、Rsync優勢與不足
sql
優勢:
apache
快速:第一次同步時 rsync 會複製所有內容,但在下一次只傳輸修改過的文件。vim
安全:rsync 容許經過 ssh 協議來加密傳輸數據。安全
更少的帶寬:rsync 在傳輸數據的過程當中能夠實行壓縮及解壓縮操做,所以可使用更少的帶寬。
特權:安裝和執行 rsync 無需特別的權限
缺點:
因爲 rsync同步數據時,須要掃描全部文件後進行比對,進行差量傳輸,因此不適宜傳輸大數據
Rsync不能實時的去監測文件系統中添加、刪除、修改等操做,沒法及時的同步數據。
3、Inotify 簡介
Inotify 是一種強大的、細粒度的、異步的文件系統事件監控機制,linux內核從2.6.13起,加入了Inotify支持,經過Inotify能夠監控文件系統中添加、刪除,修改、移動等各類細微事件,利用這個內核接口,第三方軟件就能夠監控文件系統下文件的各類變化狀況,而inotify-tools就是這樣的一個第三方軟件。
4、Rsync+Inotify組合實驗案例
Web IP: 172.16.10.1
Mysql IP: 172.16.10.2
Backup IP: 172.16.10.3
Backup服務器配置
一、安裝部署Rsync服務器
[root@localhost ~]# yum install rsync -y [root@localhost ~]# rpm -qa | grep rsync rsync-3.0.6-9.el6.x86_64 [root@localhost ~]#
二、建立rsync的主配置文件
[root@localhost ~]# vim /etc/rsyncd.conf uid = nobody gid = nobody user chroot = no max connections = 200 timeout = 600 pid file = /var/run/rsyncd.pid lock file = /var/run/rsyncd.lock log file = /var/log/rsyncd.log [web] path=/backup/web ignore errors read only = no list = no hosts allow = 172.16.10.0/255.255.0.0 auth users = test secrets file = /etc/rsyncd.password [mysql] path=/backup/mysql ignore errors read only = no list = no hosts allow = 172.16.10.0/255.255.0.0 auth users = test secrets file = /etc/rsyncd.password
註釋:
第1-2行:指定備份的用戶和組,nobody爲任何用戶
第3行: 此處傳輸文件時無需chroot到path參數所指定的目錄下
第4行: 最大鏈接數
第5行: 覆蓋客戶指定的IP超時時間,也就是說rsync服務器不會永遠等待一個崩潰的客戶端。
第6-8行:指定pid,lock,log的文件
第9行: 指定認證模塊名稱,注意此模塊是對外公佈的模塊
第10行: 指定備份的路徑
第11行: 能夠忽略一些無關的IO錯誤
第12行: 容許可讀可寫
第13行: 不容許列清單
第14行: 只容許172.16.10.0網段進行同步,拒絕其它IP
第15行: 認證的用戶名
第16行: 指定密碼文件的存放路徑
三、建立備份目錄並設置目錄的權限並啓動rsync服務
[root@localhost ~]# cd / [root@localhost /]# mkdir -pv /backup/web [root@localhost /]# mkdir -pv /backup/mysql [root@localhost /]# chmod -R 777/backup/ [root@localhost /]# echo "test:test"> /etc/rsyncd.password [root@localhost /]# chmod 600/etc/rsyncd.password [root@localhost ~]#rsync --daemon --config=/etc/rsyncd.conf #啓動服務 [root@localhost ~]# netstat -anpt | grep rsync #rsync監聽873端口 tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1306/rsync [root@localhost ~]#
Web服務器與Mysql服務器配置
一、建立認證文件並設置權限 (web與mysql)
[root@localhost ~]# echo "test" > /etc/rsyncd.password [root@localhost ~]# chmod 600 /etc/rsyncd.password
二、服務器進行服務測試(web服務配置成功)
[root@localhost ~]# rsync -avp /etc/fstab test@172.16.10.3::web --password-file=/etc/rsyncd.password sending incremental file list fstab sent 848 bytes received 27 bytes 1750.00 bytes/sec total size is 779 speedup is 0.89 [root@localhost ~]#
此時rsync已經配置成功可是不能監控文件的變化此時就須要安裝inotify
因爲inotify特性須要Linux內核的支持,在安裝inotify-tools前要先確認Linux系統內核是否達到了2.6.13以上,若是Linux內核低於2.6.13版本,就須要從新編譯內核加入inotify的支持
三、判斷內核是否支持inotify:(web與mysql)
[root@localhost ~]# uname -r 2.6.32-358.el6.x86_64
四、檢查系統是否支持inotify(Web與Mysql)
[root@localhost ~]# ll /proc/sys/fs/inotify/ total 0 -rw-r--r-- 1 root root 0 Sep 1 10:02 max_queued_events -rw-r--r-- 1 root root 0 Sep 1 10:02 max_user_instances -rw-r--r-- 1 root root 0 Sep 1 10:02 max_user_watches [root@localhost ~]#
如果有以上3個文件說明系統默認是支持inotfiy功能的。
五、安裝inotfiy-tools工具(Web與Mysql)
[root@localhost ~]# wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz [root@localhost ~]#tar xf inotify-tools-3.14.tar.gz [root@localhost ~]# cd inotify-tools-3.14 [root@localhost inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify [root@localhost inotify-tools-3.14]# make && make install
六、建立rsync腳本(web服務器)
[root@localhost ~]# vim /etc/rsync.sh #!/bin/bash host=172.16.10.3 src=/usr/local/httpd/apache/htdocs des=web user=test /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \ | while read files do /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.password $src $user@$host::$des echo "${files} was rsynced" >>/tmp/rsync.log 2>&1 done
建立rsync腳本(Mysql服務器)
[root@localhost ~]# vim /etc/rsync.sh #!/bin/bash host=172.16.10.3 src=/data des=mysql user=test /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \ | while read files do /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.password $src $user@$host::$des echo "${files} was rsynced" >>/tmp/rsync.log 2>&1 done
註釋:
--timefmt:指定時間的輸出格式。
--format:指定變化文件的詳細信息。
七、設置腳本權限(Web與Mysql)
[root@localhost inotify-tools-3.14]# chmod 764 /etc/rsync.sh
八、Web服務器測試
[root@localhost inotify-tools-3.14]# bash -x /etc/rsync.sh + host=172.16.10.3 + src=/usr/local/httpd/apache/htdocs + des=web + user=test + read files + /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib /usr/local/httpd/apache/htdocs
此時腳本處於監控狀態:
Web服務器建立文件檢查是否能及時同步
[root@localhost ~]# cd /usr/local/httpd/apache/htdocs/ [root@localhost htdocs]# touch abc
查看腳本狀態:
在Mysql服務器上測試同理,此處省略。。
九、讓腳本開機時自動啓動(web與mysql)
[root@localhost inotify-tools-3.14]# echo "/etc/rsync.sh " >> /etc/rc.local
至此整個實驗過程告一段落。在此實驗中遇到的問題,總結以下:
同步時遇到的問題:
rsync: failed to connect to 172.16.10.2: Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6]
故障緣由:
服務沒有啓動
解決辦法:
rsync --daemon --config=/etc/rsyncd.conf
本博客至此結束,望廣大博友多提寶貴意見!!!謝謝。。