標籤:
1、環境部署
inotify-master 10.10.6.208
inotify-slave 10.10.6.149
2、兩台伺服器都安裝rsync
yum install -y rsync
3、inotify-slave部署
建立rsync使用者及模組目錄並更改其使用者組
useradd rsync -s /sbin/nologin #添加rsync使用者
mkdir /usr/local/backup #建立rsync daemon工作模式的模組目錄
chown rsync.rsync /usr/local/backup #更改模組目錄的使用者組
編寫rsync設定檔/etc/rsyncd.conf
vi /etc/rsyncd.conf
strict modes =yes #是否檢查口令檔案的許可權
port = 873 #預設連接埠873
log file = /var/log/rsyncd.log #日誌記錄檔案 原文中有的,我沒有使用,記錄檔
pid file = /var/log/rsyncd.pid #運行進程的ID寫到哪裡
[test]
max connections = 10 #用戶端最大串連數,預設0(沒限制)
uid = rsync #指定該模組傳輸檔案時守護進程應該具有的uid
gid = rsync #指定該模組傳輸檔案時守護進程應該具有的gid
path = /usr/local/backup/ # 需要做備份的目錄
ignore errors # 可以忽略一些無關的IO錯誤
read only = no #no用戶端可上傳檔案,yes唯讀
write only = no #no用戶端可下載檔案,yes不能下載
hosts allow = * #充許任何主機串連
hosts deny = 10.5.3.77 #禁止指定的主機串連
auth users = rsync_backup # 虛擬使用者
secrets file = /etc/rsync.password # 虛擬使用者口令檔案位置
配置虛擬使用者的密碼檔案
echo "rsync_backup:123456" >/etc/rsync.password#註:rsync_backup為虛擬使用者,123456為這個虛擬使用者的密碼
chmod 600 /etc/rsync.password #為密碼檔案提權,增加安全性
啟動rsync服務
rsync --daemon --config=/etc/rsyncd.conf
4、inotify-master部署
安裝inotify-tools
tar zxvf inotify-tools-3.13.tar
cd inotify-tools-3.13
./configure --prefix=/usr/local/inotify-tools
make
make install
測試推送
echo "123456" >/etc/rsync.password
chmod 600 /etc/rsync.password
echo "hello dsideal">test.txt
rsync -avz test.txt [email protected]::test --password-file=/etc/rsync.password
sending incremental file list
sent 30 bytes received 8 bytes 76.00 bytes/sec
total size is 14 speedup is 0.37
測試成功,查看10.10.6.149的/usr/local/backup目錄檔案已經同步過來。
編寫監控指令碼並載入到後台執行
vi inotify.sh
#!/bin/bash
#para
host01=10.10.6.149
src=/usr/local/backup/ #本地監控的目錄
dst=test #inotify-slave的rsync服務的模組名
user=rsync_backup #inotify-slave的rsync服務的虛擬使用者
rsync_passfile=/etc/rsync.password #本地調用rsync服務的密碼檔案
inotify_home=/usr/local/inotify-tools #inotify的安裝目錄
#judge
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f‘ -e close_write,delete,create,attrib $src | while read file
do
cd $src && /usr/bin/rsync -aruz -R --delete ./ [email protected]$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
echo "${files} was rsynced" >> /tmp/rsync.log 2>&1
echo "---------------------------------------------------------------------------"
done
exit 0
# sh inotify.sh & #將指令碼加入後台執行
CentOS利用inotify+rsync實現檔案同步