rsync + notify 同步
1. 配置rysnc server:同步機,同步被同步機更新的文件,很多臺
vi /etc/rsyncd.conf
uid=root
gid=root
use chroot=no
max connections=10
timeout=600
strict modes=yes
port=873
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
log file=/var/log/rsyncd.log
[module_pk10]
path=/data/www/DePK10
comment=rsync pk10 unity
auth users=pk10
uid=root
gid=root
secrets file=/etc/rsyncd.secrets
read only=no
list=no
hosts allow=103.x.x.x
hosts deny=0.0.0.0/32
vi /etc/rsyncd.secrets
pk10:[email protected]
chmod 600 /etc/rsyncd.secrets
啟動:rsync --daemon
添加防火墻開放 873端口
-A INPUT -m state --state NEW -m tcp -p tcp -s 103.x.x.x --dport 873 -j ACCEPT
2. 客戶端:被同步機一臺,更新文件只需要更新到這臺
vi /etc/rsync_client.pwd
[email protected]
chmod 600 /etc/rsync_client.pwd
mkdir /etc/rsyncd.d/
touch /etc/rsyncd.d/rsync_exclude.lst
安裝inotify:yum install -y inotify-tools
監控和同步腳本:
vi rsync.sh
#rsync auto sync script with inotify
#variables
current_date=$(date +%Y%m%d_%H%M%S)
source_path=/data/www/DePK10/
log_file=/var/log/rsync_client.log
#rsync
rsync_server="xxx.xxx.xxx.xxx
xxx.xxx.xxx.xxx"
rsync_user=pk10
rsync_pwd=/etc/rsync_client.pwd
rsync_module=module_pk10
INOTIFY_EXCLUDE=‘(.*/*\.log|.*/*\.swp)$‘
RSYNC_EXCLUDE=‘/etc/rsyncd.d/rsync_exclude.lst‘
#rsync client pwd check
if [ ! -e ${rsync_pwd} ];then
echo -e "rsync client passwod file ${rsync_pwd} does not exist!"
exit 0
fi
#inotify_function
inotify_fun(){
/usr/bin/inotifywait -mrq --timefmt ‘%Y/%m/%d-%H:%M:%S‘ --format ‘%T %w %f‘ \
--exclude ${INOTIFY_EXCLUDE} -e modify,delete,create,move,attrib ${source_path} \
| while read file
do
for server in $rsync_server
do
/usr/bin/rsync -auvrtzopgP --exclude-from=${RSYNC_EXCLUDE} --progress --bwlimit=200 --password-file=${rsync_pwd} ${source_path} ${rsync_user}@${server}::${rsync_module}
done
done
}
#inotify log
inotify_fun >> ${log_file} 2>&1 &
rsync + notify 同步