1. 雙機
1.1. 機器IP
173.168.1.131 //主
173.168.1.132 //從
1.2. 同步目錄
/home/www
2. SSH2免密碼登錄
2.1. 生成rsa秘鑰
使主機能免密碼登錄從機
在主機執行
ssh-keygen -t rsa
2.2. 公鑰傳輸給從機
在主機執行
scp /root/.ssh/id_rsa.pub root@173.168.1.132:/root/id_rsa.pub
在從機執行
cat /root/id_rsa.pub >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
2.3. 驗證
在主機執行
ssh root@173.168.1.132
如果
不需要
密碼可以登錄則成功
3. Rsync
3.1. 安裝
主從都需要安裝
yum install rsync -y
3.2. 配置rsync密碼
主機執行
echo password > /etc/rsync.passwd
chmod 0600 /etc/rsync.passwd
在從機上執行
echo root:password > /etc/rsync.passwd
chmod 0600 /etc/rsync.passwd
3.3. 配置rsync
在從機上執行
vi /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[www]
path = /home/www/
comment = www file
ignore errors
read only = no
write only = no
hosts allow = 173.168.1.131
hosts deny = *
auth users = root
secrets file = /etc/rsync.passwd
3.4. 啟動rsync服務
在從機上執行
rsync --daemon --port=873 --config=/etc/rsyncd.conf
4. Inotify-tools
4.1. 下載inotify-tools
主機下載安裝,監控文件變化
wget https://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
4.2. 安裝inotify-tools
tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install
4.3. 使用inotify-tools
在主機編寫shell腳本
vi /root/push.sh
#!/bin/bash
/usr/local/bin/inotifywait -mrq --timefmt '%y%m%d %H%M%S' --format '%T %w %e %f' --exclude "exclude/" -e delete -e moved_to -e moved_from -e close_write -e create /home/www/ | while read file
do
echo ${file} >> /var/log/inotify/www_inotifywait.log
DATENOW=$(date +%Y-%m-%d:%H:%M:%S)
state=`echo ${file} | awk '{print $4}'`
echo $state
case "${state}" in
CLOSE_WRITE,CLOSE|MOVED_TO|MOVED_FROM|DELETE)
echo $RSYNC_TIME:$RSYNC_FILE >> /var/log/inotify/www_filecreate.log
echo "---------start-----------"
echo $RSYNC_FILE
( rsync -vzrtog --delete --exclude "exclude/" --password-file=/etc/rsync.passwd /home/www/ root@173.168.1.132::www ) &
echo "---------end-----------"
echo $DATENOW > /var/log/inotify/www_tbstat.log
;;
DELETE,ISDIR )
echo "delete ,is dirr"
;;
esac
done
chmod +x /root/push.sh
/root/push.sh > /dev/null 2>&1 &
5. 添加目錄同步
修改從機配置文件,重啟從機rsync
vi /etc/rsyncd.conf
[xxx]
path = /xxx/
comment = xxx file
ignore errors
read only = no
write only = no
hosts allow = 173.168.1.131
hosts deny = *
auth users = root
secrets file = /etc/rsync.passwd
主機添加
shell
腳本vi /root/xxx.sh
#!/bin/bash
/usr/local/bin/inotifywait -mrq --timefmt '%y%m%d %H%M%S' --format '%T %w %e %f' --exclude "xxx/" -e delete -e moved_to -e moved_from -e close_write -e create /xxx/ | while read file
do
echo ${file} >> /var/log/inotify/xxx_inotifywait.log
DATENOW=$(date +%Y-%m-%d:%H:%M:%S)
state=`echo ${file} | awk '{print $4}'`
echo $state
case "${state}" in
CLOSE_WRITE,CLOSE|MOVED_TO|MOVED_FROM|DELETE)
echo $RSYNC_TIME:$RSYNC_FILE >> /var/log/inotify/xxx_filecreate.log
echo "---------start-----------"
echo $RSYNC_FILE
( rsync -vzrtog --delete --exclude "xxx/" --password-file=/etc/rsync.passwd /home/www/ root@173.168.1.132::xxx ) &
echo "---------end-----------"
echo $DATENOW > /var/log/inotify/xxx_tbstat.log
;;
DELETE,ISDIR )
echo "delete ,is dirr"
;;
esac
done
chmod +x /root/xxx.sh
/root/xxx.sh > /dev/null 2>&1 &