主機名、ip:php
server 172.31.82.184linux
client 172.31.82.185web
需求:vim
一、server端 」/data/server「 作爲client端 「/data/client」 的備份目錄;bash
二、實現功能是client端對該目錄作增刪權限變化操做,server端能保持實時同步;ide
三、關閉防火牆和selinux測試
service iptables stopui
setenforce 0
spa
1、配置server端 orm
一、安裝rsync軟件
yum install -y rsync -y
二、建立rsync.conf配置文件,默認該文件不存在
vim /etc/rsyncd.conf
uid = root gid = root usechroot = no max connections = 20 timeout = 600 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log [web_log] ##此處是定義的模塊名,和「path = /data/server/」下server有關 rsync 同步文件的時候要使用web_log 自動關聯到path路徑 path = /data/server/ ignore errors read only = false writeonly = false list = false hosts allow = * auth users = backuser secrets file = /etc/rsync.password
三、建立備份目錄
mkdir /data/server -p
四、建立rsync用戶名和密碼文件
echo "backuser:123" >> /etc/rsync.password
五、爲/etc/rsync.password 受權爲600
chmod 600 /etc/rsync.password
六、啓動rsync服務並添加開機自動啓動
/usr/bin/rsync --daemon &
echo "/usr/bin/rsync --daemon" >> /etc/rc.local
2、配置client端
一、安裝rsync
yum install rsync -y
二、設置 rsync 客戶端的密碼文件,客戶端只須要設置 rsync 同步的密碼便可,不用設置用戶名
echo "123" > /etc/rsync.password
三、將密碼文件的權限設置成 600
chmod 600 /etc/rsync.password
配置inotyfi
一、安裝基礎編譯包
yum install -y gcc lrzsz
tar zxvf inotify-tools-3.14.tar.gz &&cd inotify-tools-3.14 &&./configure &&make &&make install
二、建立client端同步目錄
mkdir -p /data/client
三、在client端測試是否能夠同步文件
上傳文件
rsync -vzrtopg --progress /data/client/ backuser@172.31.82.184::server --password-file=/etc/rsync.password
下載文件
rsync -vzrtopg --progress backuser@172.31.82.184::server /opt/ --password-file=/etc/rsync.password
四、寫一個腳本實現當client端 「/data/client」目錄文件有變化時,讓server節點同步client數據
#!/bin/bash src=/data/client/ ##注意路徑 des1=server ##注意路徑 host1=172.31.82.184 ##server端ip user1=backuser ##同步數據使用的用戶 /usr/local/bin/inotifywait -mrq --timefmt %y/%m'%d %H:/%M' --format '%T %w%f' -emodify,delete,create,attrib $src | while read file; do /usr/bin/rsync -vzrtopg --delete --progress $src $user1@$host1::$des1 --password-file=/etc/rsync.password echo "${files} was rsynced" >> /var/log/rsync.log 2>&1 done
五、給腳本執行權限
chmod +x /root/inotify.sh
六、後臺執行腳本
/root/inotify.sh &
七、將腳本加入到系統自啓文件
echo "/root/inotify.sh" >> /etc/rc.local
八、向client端加入文件,在server端查看是否有同步
mkdir 11 22 33 44
測試經過
3、排除不想同步的文件和目錄
一、單個文件排除:好比我不想同步/opt/aa.php文件,直接使用 --exclude 「aa.php」
多個文件和目錄排除 --exclude-from="/usr/local/src/exclude.list"
腳本寫法:
vim /root/inotify.sh
#!/bin/bash
src=/data/client/
des1=server
host1=172.31.82.184
user1=backuser
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -emodify,delete,create,attrib $src | while read file
do
/usr/bin/rsync -vzrtopg --delete --progress --exclude-from="/usr/local/src/exclude.list" $src $user1@$host1::$des1 --password-file=/etc/rsync.password
echo `date +%m.%d.%H%.M`"${files} was rsynced" >> /var/log/rsync.log 2>&1
done
二、腳本寫完後還須要建立與腳本對應的文件,如下是不一樣步到server節點的文件和目錄:
vim /usr/local/src/exclude.list
exclude
11
22
33
test.php
三、殺死後臺運行的腳本進程
ps -elf |pgrep inotify|xargs kill -9
四、啓動同步腳本
sh /root/inotify.sh &
五、設置每個inotify實例相關聯的watchs的上限,不然傳輸的文件過多會報錯
echo 30000000 > /proc/sys/fs/inotify/max_user_watches
注意:
腳本修改後須要重啓後臺腳本:
ps -elf |pgrep inotify|xargs kill -9
sh /root/inotify.sh &