使用rsync服務通過inotify實現觸發式自動同步數據
實驗要求:對HK46 /data/目錄備份到HK36的/backup/下
(1) rsync服務依賴於xinetd超級服務管理,首先安裝xinetd服務
[root@HK36 ~]# yum -y install xinetd
(2) 進入rsync修改配置文件信息,將disable = yes改為no,重啟服務,查看873端口是否起來
[root@HK36 ~]# service xinetd restart
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
[root@HK36 ~]# netstat -anptu | grep 873
tcp 0 0 :::873 :::* LISTEN 2729/xinetd
(3) 創建非系統測試用戶,並對目錄/backup賦予用戶nobody權限
[root@HK36 backup]# vim /etc/rsyncd.conf
uid = nobody
gid = nobody
address = 192.168.2.102
pid = 873
host allow = 192.168.2.190
use chroot = yes
max_connetion = 5
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
motd file = /etc/rsyncd.motd
[backup]
path = /backup/
comment = data for backup
read only = no
auth users = gz
list = yes
[root@HK36 backup]# chown nobody:nobody /backup/
[root@HK36 backup]# vim /etc/rsyncd.motd 創建登入模板文件
welcome to backup data!
[root@HK36 backup]# vim /etc/rs.passwd 創建密碼
gz:ggrr50152412
(4) 測試,HK46下創建備份目錄測試:
[root@HK36 /]# mkdir /backup/
[root@HK46 /]# mkdir /data/
[root@HK46 data]# ls
22 55 a aaaaa erer kd s test
[root@HK46 data]# rsync -avz ./ --delete [email protected]::backup
(5) 上傳inotify並解壓安裝
[root@HK46 ~]# tar -xvf inotify-tools-3.14.tar.gz -C /usr/local/src/
[root@HK46 inotify-tools-3.14]#./configure --prefix=/usr/local/inotify-tools;make;make install
(6) 直接調用命令
[root@HK36 ~]# ln -s /usr/local/inotify-tools/bin/* /usr/bin/
[root@HK46 ~]# vim /root/passfile 創建密碼文件
ggrr50152412
[root@HK46 data]# chmod 600 /root/passfile並賦予600權限
(7) 測試
(8) 編輯腳本,並賦予執行權限
#!/bin/bash
backup_Server=192.168.2.102
/usr/bin/inotifywait -mrq --format '%w%f' -e create,close_write,delete /data/ | while read line
do
cd /data/
rsync -az ./ --delete gz@$backup_Server::backup --password-file=/root/ passfile
done
[root@HK46 ~]# vim inotify.sh
(9) 測試成功
使用LAMP環境搭建wordpress論壇 :http://blog.51cto.com/13719714/2108415
使用rsync服務通過inotify實現觸發式自動同步數據