rsync 同步推送網(wǎng)上看過(guò)很多方法盅粪,實(shí)現(xiàn)免密同步只可以通過(guò)添加ssh-key免登錄或者以配置文件啟動(dòng)的守護(hù)進(jìn)程啟動(dòng);
例子:
假設(shè)服務(wù)器A作為源服務(wù)器,服務(wù)器B作為備份服務(wù)器
1、服務(wù)器A只需要配置/etc/rsync.pass設(shè)定的密碼就可以進(jìn)行同步
# echo "密碼" > /etc/rsync.pass
#chmod 600 /etc/rsync.pass
2、服務(wù)器B需要配置給rsync讀取的配置文件/etc/rsyncd.conf和/etc/rsync.pass設(shè)定用戶和密碼
# vim /etc/rsyncd.conf (下面會(huì)給出參考模板)
啟動(dòng)rsync服務(wù):
#rsync --daemon --config=/etc/rsyncd.conf
服務(wù)器A同步命令:
rsync?-avz?--delete?--progress?--password-file=/etc/rsync.pass ?'本地文件' 用戶@服務(wù)器A IP::配置的模塊名 本地同步的路徑
需要說(shuō)明一下的是配置的模塊+本地相對(duì)的路徑是怎么回事
假如使用的模塊是:www ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?目標(biāo)路徑是:/data/test/haha/
那么www模塊的path=/data/ ? ? ? ? ? ? ? ? ? ? ? ? ? ?本地相對(duì)路徑就是 /test/haha/ ?,即寫(xiě)成:
rsync -avz --delete --progress --password-file=/etc/rsync.pass? '本地文件' 用戶@服務(wù)器A IP::www/test/haha/
3咐汞、配置文件/etc/rsyncd.conf詳解:
全局參數(shù)
uid = root//運(yùn)行RSYNC守護(hù)進(jìn)程的用戶
gid = root//運(yùn)行RSYNC守護(hù)進(jìn)程的組
use chroot = no //不使用chroot
max connections = 4 // 最大連接數(shù)為4
strict modes =yes//是否檢查口令文件的權(quán)限
port = 873//默認(rèn)端口873
注釋:下面這3個(gè)文件是安裝完RSYNC服務(wù)后自動(dòng)生成的文件
pid file = /var/run/rsyncd.pid//pid文件的存放位置
lock file = /var/run/rsync.lock //鎖文件的存放位置
log file = /var/log/rsyncd.log//日志記錄文件的存放位置
模塊參數(shù)
[backup] //這里是認(rèn)證的模塊名,在client端需要指定
path = /home/backup///需要做鏡像的目錄,不可缺少!
comment = This is a test //這個(gè)模塊的注釋信息
ignore errors//可以忽略一些無(wú)關(guān)的IO錯(cuò)誤
read only = yes// 只讀
list = no //不允許列文件
auth users = hening //認(rèn)證的用戶名儒鹿,如果沒(méi)有這行則表明是匿名化撕,此用戶與系統(tǒng)無(wú)關(guān)
secrets file = /etc/rsync.pass //密碼和用戶名對(duì)比表,密碼文件自己生成
#hosts allow = 192.168.1.1, 10.10.10 .10//允許主機(jī)
#hosts deny = 0.0.0 .0/0 //禁止主機(jī)
#transfer logging = yes
附 rsync + inotify-tools實(shí)現(xiàn)實(shí)時(shí)同步更新腳本
一约炎、下載安裝inotify-tools
wget https://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools
./configure && make && make install
二植阴、同步腳本
Server B的inotify監(jiān)控腳本
(監(jiān)控客戶端修改同步上傳到服務(wù)器端)
#!/bin/bash
src=/data/tomcat8080/webapps/
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y/%H:%M' --format '%T %w %f' -e modify,delete,create,attrib $src | while read file
do
rsync -vzrtopg --delete --progress --password-file=/etc/rsync.pas $src root@192.168.100.247::wwwroot
echo "$src was rsynced"
done
文中有不妥或者錯(cuò)誤的地方還望指出,以免誤人子弟圾浅。參考總結(jié)自百度掠手。
再次感謝您耐心的讀完本篇文章。