方案說明:
該方案是使用 rsync+inotify實作檔案實時同步,實作兩個檔案的實時同步,前置添加如下:
- 兩台linux配置相同的機器,硬碟大小最好能夠一樣,需要安裝centos 7.0
- 必須在同一内網,友善資料的傳輸。
- 最好兩個主機同時使用root 賬戶。
ps:執行個體方案使用的兩個ip如下:
10.100.12.247 主
基礎檔案安裝:
yum install rsync
yum install -y epel-release
yum --enablerepo=epel install inotify-tools
設定主機之間的免密登入
在主機(10.100.12.247) 上執行如下指令:
ssh-keygen
ssh-copy-id -i .ssh/id_rsa.pub [email protected]
建立源端和目标端的檔案目錄
mkdir /home/data/file
rsync -avPz --progress /home/data/file 10.100.12.252:/home/data/file
rsync -avPz --delete --progress /home/data/file 10.100.12.252: /home/data/file
最好使用如下指令進行同步完成後進行,下面的操作。防止同步任務堆積。
vim inotifywait-rsync.sh檔案
notifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib /home/data/file/ |
#while read file
#do
rsync -avPz --progress /home/data/file/ 10.100.12.247:/home/data/file/
rsync -avPz --delete /home/data/file/ 10.100.12.247:/home/data/file/
echo "${file} was synchronized"
#done
添加定時任務
vim /etc/crontab
*/2 * * * * root /home/data/inotifywait-rsync.sh(每兩分鐘同步一次)