服務(wù)器name->ip drbd1: 192.168.171.136 drbd2: 192.168.171.135
-
apt-get
安裝三個(gè)軟件xinetd
、lsyncd
、rsync
,配置好ssh免密鑰登錄 root@drbd2# vi /etc/rsyncd.conf
[diskb] --> 名字標(biāo)識(shí),用于rsync命令冒號(hào)后面 path = /mnt/diskb --> 目錄的路徑 list = true --> 列出服務(wù)器上提供同步數(shù)據(jù)的目錄,默認(rèn)是yes,為了安全建議no uid = root --> 運(yùn)行命令的用戶(hù),默認(rèn)是nobody,如果系統(tǒng)不存在這個(gè)用戶(hù),會(huì)出錯(cuò) gid = root --> 運(yùn)行命令的用戶(hù)組,默認(rèn)是nobody,如果系統(tǒng)不存在這個(gè)用戶(hù),會(huì)出錯(cuò) read only = false --> 只讀選項(xiàng),意思是不讓客戶(hù)端上傳文件到服務(wù)器 hosts allow = 192.168.171.136 --> 允許這個(gè)ip訪(fǎng)問(wèn),通常與hosts deny配合使用 hosts deny = * --> 禁止除允許列表中的所有地址的訪(fǎng)問(wèn)
root@drbd2# vi /etc/xinetd.d/rsync
service rsync { disable = no socket_type = stream wait = no user = root server = /usr/bin/rsync server_args = --daemon log_on_failure += USERID }
-
root@drbd2# service xinetd restart
重啟xinetd服務(wù)之后,在drbd1上用命令測(cè)試一下 -
root@drbd1# rsync -avz --delete /mnt/diskb/ 192.168.171.135::diskb
#測(cè)試命令,如果能夠同步成功,就往下走,否則檢查一下配置,直到成功
命令解析: a:--archive 歸檔模式,表示以遞歸方式傳輸文件,并保持所有文件屬性 v:--verbose 詳細(xì)模式輸出 z:--compress 對(duì)備份的文件在傳輸時(shí)進(jìn)行壓縮處理 ---其他的參數(shù)--- u:--update 僅僅進(jìn)行更新,也就是跳過(guò)所有已經(jīng)存在于DST,并且文件時(shí)間晚于要備份的文件。(不覆蓋更新的文件) q:--quiet 精簡(jiǎn)輸出模式 --delete 刪除那些DST中SRC沒(méi)有的文件
root@drbd1# vi /etc/lsyncd.conf
settings { logfile ="/tmp/lsyncd.log", --> 日志文件路徑 statusFile ="/tmp/lsyncd.status", --> 狀態(tài)文件路徑 inotifyMode = "CloseWrite", --> 在文件結(jié)束寫(xiě)之后同步,也可以寫(xiě)Modify maxProcesses = 7, --> 同時(shí)進(jìn)程最大個(gè)數(shù) } sync { default.rsync, --> 同步模式,有rsync、rsyncssh、direct三種 source = "/mnt/diskb", --> 監(jiān)聽(tīng)的目錄 target = "192.168.171.135::diskb", --> 也可以寫(xiě)為服務(wù)端地址以及rsync的名字標(biāo)識(shí) delete = "running", --> 為了保持target與souce完全同步,Lsyncd默認(rèn)會(huì)delete = true來(lái)允許同步刪除 delay = 5, --> 累計(jì)事件,等待rsync同步延時(shí)時(shí)間,即5秒內(nèi),對(duì)同一文件的修改只同步一次 rsync = { --> rsync參數(shù) binary = "/usr/bin/rsync", archive = true, compress = true, --> 開(kāi)啟壓縮 verbose = true } }
-
root@drbd1# lsyncd -log all /etc/lsyncd.conf
啟動(dòng)進(jìn)程 -
root@drbd1# vi /etc/rc.local
在exit 0
之前,加入lsyncd -log all /etc/lsyncd.conf
,完成開(kāi)機(jī)自動(dòng)執(zhí)行 - 至此配置好了一半:即
drbd1
的目錄/mnt/diskb
中的任意修改都會(huì)同步到drbd2
的/mnt/diskb
中,如果要實(shí)現(xiàn)另一半操作,交換主從,再配置一遍即可 - 單個(gè)文件的同步的配置:
/etc/rsyncd.conf
片段:
sync { default.rsync, source = "/tmp", target = "drbd2::html", delete = "running", delay = 5, rsync = { binary = "/usr/bin/rsync", archive = true, compress = true, verbose = true, _extra = {"--include-from=/etc/lsyncd.include", "--exclude=*"} } } --> _extra 參數(shù)是執(zhí)行rsync命令時(shí),不修改直接帶上的參數(shù)。 --> 2.1.*版本之前有rsyncOps={"--include-from=/etc/lsyncd.include","--exclude=*"}的寫(xiě)法,現(xiàn)在被_extra替代
- 包含文件
/etc/lsyncd.include
:
single.file test.file
其他要包含的文件,每個(gè)占一行