天天看點

Linux運維 第三階段 (十四)rsync

一、相關概念:

rsync(remote synchronize,rsync.samba.org):遠端資料同步工具,通過網絡快速同步多台主機間的檔案,也可使用rsync同步本地硬碟中不同目錄中的資料,利用其自身算法(rsync算法)隻傳送兩個檔案的不同部分,通過ssh方式來傳輸檔案,是以保密性好;與cp、tar備份方式相比,rsync具有安全性高、備份迅速、支援增量備份的優點,通過rsync可以解決對實時性要求不高的資料備份需求,如定期備份伺服器資料到指定的伺服器、對本地磁盤定期做資料鏡像、叢集中負載均衡後端real server的網頁資料改動時同步到其它real server等場景

随着應用系統規模的不斷擴大,對資料的安全性和可靠性也提出更高的要求,rsync在高端業務系統中暴露出了很多不足:rsync同步資料時,需要掃描所有檔案後進行比對,進行差量傳輸,若檔案數量達到了百萬甚至千萬量級,這将非常消耗系統性能而且低效,有時變化的資料往往隻是很少的一部分;rsync不能實時的去監測、同步資料,雖然可以通過計劃任務方式觸發同步,但兩次觸發動作一定有時間差,這将導緻服務端和用戶端資料可能會不一緻,通過inotify可解決此問題

inotify(是一種強大的、細粒度的、異步的FS事件監控機制,檔案内容發生改變是核心管理的,核心将這種監控檔案本身是否發生改變的功能輸出給使用者空間,使用者空間才有可能知道,linux核心從2.6.13起支援inotify機制,通過inotify可以監控FS中添加、删除、修改、移動等各種細微事件,利用這個核心接口,通過第三方軟體(inotify-tools)就可監控FS下檔案的各種變化)

sersync(基于inotify開發,功能類似inotify,C++編寫,多線程同步,sersync可以記錄下被監聽目錄中發生變化的具體哪一個檔案或哪個目錄,然後使用rsync同步的時候,隻同步發生變化的)

rsync+inotify與rsync-sersync兩種架構的差別?

rsync+inotify(inotify監控FS的各種變化,一旦有變動時就觸發rsync同步,解決了資料同步實時性的問題,inotify僅記錄被監控的目錄發生了變化,并沒記錄哪個目錄或哪些檔案發生了變化,是以rsync每次都要在被監控的所有目錄和其下的檔案與目标目錄進行比對,再傳輸有差異的檔案(要周遊目錄下所有檔案找出有差異的才進行同步),若資料量很大這将非常耗時,是以效率很低)

rsync+sersync(sersync可以記錄被監控的目錄中具體哪個目錄哪些檔案發生了變化,rsyc在同步時隻同步發生變化的目錄或檔案,是以速度很快(節約時間)、節約帶寬、效率很高)

注:若資料量不大建議使用rsync+inotify,若資料量很大建議使用rsync+sersync

二、操作:

1、rsync+inotify的實作

舉例:實作兩個web伺服器的網頁檔案保持同步(一台為server,另一台為client,僅在server-side更新網頁檔案,檢視client-side是否有同步,資料流向是server-->client)

準備軟體:

rsync--3.0.9.tar.gz(https://rsync.samba.org/ftp/rsync/src/rsync-3.0.0.tar.gz)

inotify-tools-3.14.tar.gz(https://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz)

注:server-side安裝rsync和inotify,client-side僅安裝rsync

#ll /proc/sys/fs/inotify/(若此目錄下有max_queued_events、max_user_instances、max_user_watches這三項則此核心支援inotify機制,核心2.6.13之後才支援此功能)

server-side(192.168.41.131):

#tar xf  rsync-3.0.9.tar.gz

#cd rsync-3.0.9

#./configure  --prefix=/usr/local/rsync

#make

#make install

#vim /etc/profile.d/rsync.sh

export  PATH=$PATH:/usr/local/rsync/bin

#. !$

#vim /etc/man.config

MANPATH  /usr/local/rsync/share/man

#man rsync

#rsync options  SRC  DET(rsync指令格式)

options:-vzrtopg

-v(--verbose)

-z(--compress)

-r(--recursive)

-t(--times)

-o(--owner)

-p(--perms)

-g(--group)

-a(--archive)

-A(--acls)

-H(--hard-links)

-l(--links)

-D(--devices  --specials)

--delete(delete extraneousfiles from dest dirs,删除目标位置有而原始位置沒有的檔案)

--progress(show progress duringtransfer顯示傳輸過程)

--passwd-file=FILE(readdaemon-access password from FILE,指定密鑰檔案)

#echo  "redhat"  >  /usr/local/rsync/rsync.passwd(建立認證檔案,密碼為redhat,此處不需要使用者名)

#chmod 600  !$(為安全,将密碼檔案權限改為600)

#tar xf  inotify-tools-3.14.tar.gz

#cd inotify-tools-3.14

#./configure  --prefix=/usr/lcoal/inotify

#vim /etc/profile.d/inotify.sh

export PATH=$PATH:/usr/local/inotify/bin

MANPATH /usr/local/inotify/share/man

#man inotifywatch(gather filesystem access statistics using inotify,用于短期監控,任務完成後再出結果)

#man inotifywait(wait for changes to files using inotify,用于持續監控,實時輸出結果)

#inotifywait  -mrq -e  EVENT  --timefmt FMT  --format  FMT FILE(inotifywait指令用法)

-m(--monitor)

-q(--quiet)

-e(--event,Listen forspecific event(s) only)

--timefmt

--format(Output  in a  user-specified  format)

EVENT有:

modify(A watched file or afile within a watched directory was written to)

delete(A file or directorywithin a watched directory was deleted)

create(A file or directorywas created within a watched directory)

attrib(The metadata of awatched file or a file within a watched directory was modified)

move(A file or directorywas moved from or to a watched directory)

#vim /root/rsync.sh

#!/bin/bash

#

host=192.168.41.132

src=/var/www/html/

des=test

user=testuser

/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  --passwd-file=/usr/local/rsync/rsync.passwd  $src $user@$host::$des

echo “${files} was rsynced” >>  /var/log/rsync.log  2>&1

done

#chmod 764  /root/rsync.sh

#sh /root/rsync.sh  &(注意!待client-side啟動rsync服務後再運作此腳本)

client-side(192.168.41.132):

#echo  "testuser:redhat"  >  /usr/local/rsync/rsync.passwd(建立認證檔案)

#vim /usr/local/rsync/rsync.conf

uid = root

gid = root

use chroot = no(預設為true,增加對目錄檔案軟連結的備份)

max connections = 10

timeout = 600

strict modes = yes

pid file = /var/run/rsyncd.pid

lock file = /var/log/rsyncd.lock

log file = /var/log/rsyncd.log

motd file = /etc/rsyncd.motd(可在此檔案中編輯啟動時的歡迎資訊)

[test](自定義名稱)

path = /var/www/html/(資料同步到本地的目錄路徑)

comment = test(描述資訊)

ignore errors = yes

read only = no(設定rsync服務端檔案為讀寫權限)

hosts allow = 192.168.41.131(允許進行資料同步的主機位址,多個用逗号隔開)

hosts deny = *(禁止資料同步的主機位址)

list = false(不顯示rsync服務端資源清單)

uid = root(設定rsync運作權限)

port = 873(預設端口)

auth users = testuser(執行資料同步的使用者名,可以設定多個用逗号隔開)

secrets file =/user/local/rsync/rsync.passwd(使用者認證配置檔案,裡面包含使用者名和密碼)

#rsync --daemon --config=/usr/local/rsync/rsync.conf(啟動rsync)

測試:

server-side(/var/www/html下):

<a href="http://s2.51cto.com/wyfs02/M02/77/10/wKiom1ZiMsnS4l-qAABrmV9sixw732.jpg" target="_blank"></a>

client-side(/var/www/html下):

<a href="http://s2.51cto.com/wyfs02/M02/77/0F/wKioL1ZiM0XgifzPAAAb2Kb_Lvo556.jpg" target="_blank"></a>

2、rsync+sersync的實作:

server-side:

參照上例,安裝rsync、制作認證檔案并改權限為600

#mkdir /usr/local/sersync/

# tar  -xf  sersync_64bit_binary_stable_final.tar.gz

#cd  GNU-Linux-x86

#mv  confxml.xml  sersync2 /usr/local/sersync/

#cd /usr/local/sersync

#cp  confxml.xml confxml.xml.bak

#vim confxml.xml

……

&lt;sersync&gt;

   &lt;localpath watch="/var/www/html"&gt;

       &lt;remote ip="192.168.41.132" name="test"/&gt;

       &lt;!--&lt;remote ip="192.168.8.39" name="tongbu"/&gt;--&gt;

       &lt;!--&lt;remote ip="192.168.8.40" name="tongbu"/&gt;--&gt;

   &lt;/localpath&gt;

   &lt;rsync&gt;

       &lt;commonParams params="-vzrtopg"/&gt;

       &lt;auth start="true" users="testuser" passwordfile="/usr/local/rsync/rsync.passwd"/&gt;

       &lt;userDefinedPort start="false"port="874"/&gt;&lt;!-- port=874 --&gt;

       &lt;timeout start="false" time="100"/&gt;&lt;!--timeout=100 --&gt;

       &lt;ssh start="false"/&gt;

   &lt;/rsync&gt;

   &lt;failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/&gt;&lt;!--default every 60mins execute once--&gt;

   &lt;crontab start="true"schedule="600"&gt;&lt;!--600mins--&gt;

       &lt;crontabfilter start="false"&gt;

       &lt;exclude expression="*.php"&gt;&lt;/exclude&gt;

       &lt;exclude expression="info/*"&gt;&lt;/exclude&gt;

       &lt;/crontabfilter&gt;

   &lt;/crontab&gt;

   &lt;plugin start="false" name="command"/&gt;

&lt;/sersync&gt;

#vim /root/check_sersync.sh

SERSYNC="/usr/local/sersync/sersync2"

CONF_FILE="/usr/local/sersync/confxml.xml"

STATUS=`ps aux | grep 'sersync2' | grep -v'grep' | wc -l`

if [ $STATUS -eq 0 ];then

 $SERSYNC -d -r -o $CONF_FILE &amp;

else

exit 0;

fi

#crontab -e(為防止sersync2意外中斷,每5分鐘使用此腳本檢測,若sersync2未運作,則會自動開啟,進行實時同步)

*/5 * * * *  bash  /root/check_sersync.sh&amp;&gt; /dev/null

#/usr/local/sersync/sersync2  -d -r  -o  /usr/local/sersync/confxml.xml(開啟sersync服務)

-d(run as a daemon)

-r(rsync all the localfiles to the remote servers before the sersync work)

-o(config xml name:  ./confxml.xml)

client-side:

參照上例,安裝rsync,配置rsync.conf檔案,配置認證檔案,并開啟rsync服務

<a href="http://s4.51cto.com/wyfs02/M02/77/10/wKiom1ZiMvyAkYQbAABeKZMPEE4022.jpg" target="_blank"></a>

<a href="http://s2.51cto.com/wyfs02/M01/77/0F/wKioL1ZiM3iA804-AAA3yYJJMzY516.jpg" target="_blank"></a>

<a href="http://s4.51cto.com/wyfs02/M00/77/10/wKiom1ZiMyGSuvUTAAAqecFM-T8276.jpg" target="_blank"></a>

以上内容是自找資料整理。

本文轉自 chaijowin 51CTO部落格,原文連結:http://blog.51cto.com/jowin/1719774,如需轉載請自行聯系原作者

繼續閱讀