1.先安裝rsync
yum install rsync //這里由於在docker里測試的,用yum安裝包,可使用其他安裝方式
2.創建rsync的配置文件
vi /etc/rsyncd.conf
motd file = /etc/rsyncd.motd
uid=root
gid=root
max connections=36000
use chroot=no
log file=/var/log/rsyncd.log
log format = %t %a %m %f %b
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
timeout = 300
#同步模塊
[tongbu]
path=/var/www/html #同步的目錄
list=yes
comment = this is comment
ignore errors = yes
read only = no
hosts allow = 172.17.0.3 #允許同步的服務器ip
hosts deny = *
auth users backup #授權同步用戶
3.創建同步用戶
useradd backup //創建用戶
passwd backup //設置密碼
4.啟動服務
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
ps -ef |grep rsyncd #查看服務是否啟動
5.在另外一台服務器,重新來一遍上面的操作,配置文件指定另一台的ip。
/usr/bin/rsync -vzrtopg --delete --progress /var/www/html/ backup@172.17.0.3::tongbu --password-file=/etc/rsync.password #執行同步
--password-file 為同步用戶的密碼
這樣在一台服務器新建文件,另外一台可同步創建。
inotify可實時監控文件目錄變化事件,可以利用Inotify來實現實時同步
創建同步腳本
vi inotify.sh
#!/bin/bash
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib /var/www/html/ | while read file #監聽目錄的modify,delete,create,attrib事件
do
/usr/bin/rsync -vzrtopg --delete --progress /var/www/html/ backup@172.17.0.3::tongbu --password-file=/etc/rsync.password #執行同步
echo "${files} was rsynced" >> /var/log/rsync.log 2>&1
done
多台服務器可實時多向同步