主服务器需安装配置inotify-tools,用于实时监控服务器文件变化。
inotify-tool实时监控并发送文件到rsync从服务器脚本:
#/bin/bash
host=https://www.haiyun.me
src=/var/www/html
dst=www
user=back
passwd=passwd
echo "$passwd" > /etc/rsync.pass
chmod 600 /etc/rsync.pass
inotifywait -mrq -e close_write,create,delete,attrib --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' $src\
|while read files
do
file=`echo $files|awk '{print $3}'`
rsync -vzrtopg --delete --progress $file $user@$host::$dst --password-file=/etc/rsync.pass
echo "$files was rsynced" >> /var/log/rsync.log
done
从服务器配置rsync:
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
[www]
path = /home/wwwroot
comment = www file
ignore errors
hosts allow = *
list = false
auth users = onovps
secrets file = /etc/server.pass
read only = no
write only = no
uid = root
gid = root
建立验证文件,root权限可查看:
echo "onovps:passwd" > /etc/server.pass
chmod 600 /etc/server.pass
启动rsync进程:
rsync --daemon
echo "rsync --daemon" >> /etc/rc.local