环境:2台服务器,一台inotify服务器,一台rsync服务器。将inotify服务器www的数据同步到rsync的www目录
rsync服务器
配置rsync
vi /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 0
ignore errors
exclude = lost+found/
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
reverse lookup = no
hosts allow = 192.168.45.0/24 # 允许访问的ip段
[backup]
path = /www/
comment = backup
read only = no
auth users = rsyncuser
secrets file = /etc/rsync.pass
生成验证文件
echo "rsyncuser:密码" > /etc/rsync.pass
chmod 600 /etc/rsync.pass
准备目录
mkdir /www
启动rsync服务
rsync --daemon # 可加入/etc/rc.d/rc.local实现开机启动
systemctl start rsyncd # CentOS 7
inotify服务器
安装inotify
yum install inotify-tools
配置密码文件
echo "密码" > /etc/rsync.pass
chmod 600 /etc/rsync.pass
创建同步脚本
vim /script/inotify_rsync.sh
#!/bin/bash
SRC='/www/'
DEST='rsyncuser@rsync服务器IP::www'
inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w %f' -
e create,delete,moved_to,close_write,attrib ${SRC} |while read DATE
TIME DIR FILE;do
FILEPATH=${DIR}${FILE}
rsync -az --delete --password-file=/etc/rsync.pass $SRC $DEST &&
echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync"
>> /var/log/changelist.log
done
rsync服务器监控变化
watch -nl ls -l /www