参考
https://www.ruanyifeng.com/blog/2020/08/rsync.html
https://cloud.tencent.com/developer/article/1373541
限速参数
rsync -avH -e 'ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no' --delete --bwlimit=1m --progress root@172.28.144.143:/root/dir1/ /root/dir1
脚本
#!/bin/bash
if [[ ! -f slave_hosts.cache ]]; then
touch slave_hosts.cache
fi
cluster_node=$1 # master
src_dir=$2 # src_dir=/root/dir1
master_host=172.28.144.143
slave_hosts=172.30.59.86
slave_hosts_arr=(${slave_hosts//,/ })
# 处理参数
echo "input params: $cluster_node , $src_dir , $target_host"
src_dir=${src_dir%*/}
echo "opt src_dir: $src_dir"
OLD_SLAVE_HOSTS=$(cat slave_hosts.cache)
echo "$slave_hosts" > slave_hosts.cache
# 创建文件夹
mkdir -p "$src_dir"
# 文件监控和同步
PIDS=`ps -ef |grep inotifywait |grep -v grep | awk '{print $2}'`
if [ "$PIDS" != "" ]; then
echo "$cluster_node inotifywait is runing! "
if [ "master" == "$cluster_node" ] && [ "$OLD_SLAVE_HOSTS" == "$slave_hosts" ]; then
exit 0
fi
if [ "slave" == "$cluster_node" ] || [ "$OLD_SLAVE_HOSTS" != "$slave_hosts" ]; then
echo "slave node kill inotifywait!"
kill -9 "$PIDS"
fi
fi
if [[ "$OLD_SLAVE_HOSTS" != "$slave_hosts" ]]; then
echo "slave_hosts change old: [$OLD_SLAVE_HOSTS] new:[$slave_hosts] "
fi
echo "check rsync pid"
RSYNC_PIDS=`ps -ef |grep rsync |grep -v grep | awk '{print $2}'`
if [ "$RSYNC_PIDS" != "" ]; then
echo "rsync is runing exit."
exit 0
fi
#运行进程
if [[ "slave" == "$cluster_node" ]]; then
echo "slave node rsync pull master:$master_host"
rsync -avH -e 'ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no' --delete root@"$master_host":"$src_dir"/ "$src_dir"
fi
#运行进程
if [[ "master" == "$cluster_node" ]]; then
# 全部同步一次
for target_host in ${slave_hosts_arr[@]}
do
echo "master node to rsync $target_host, ignore delete!"
rsync -avH -e 'ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no' --rsync-path="mkdir -p $src_dir && rsync" "$src_dir"/ root@"$target_host":"$src_dir"
done
echo "double check inotifywait pid..."
PIDS=`ps -ef |grep inotifywait |grep -v grep | awk '{print $2}'`
if [ "$PIDS" != "" ]; then
echo "inotifywait is runing! exit script......"
exit 0
fi
echo "master node to run inotifywait!"
#/usr/bin/inotifywait -mrq --format '%Xe %w%f' -e modify,create,delete,attrib,close_write,move "$src_dir" | while read file
/usr/bin/inotifywait -mrq --format '%Xe %w%f' -e modify,create,delete,attrib,move "$src_dir" | while read file
do
echo "$(date "+%Y-%m-%d %H:%M:%S") $file"
INO_EVENT=$(echo $file | awk '{print $1}')
INO_FILE=$(echo $file | awk '{print $2}')
# SYNC_DIR=$(dirname ${INO_FILE})
# SYNC_DIR=$(echo $INO_FILE | awk -F '/' '{print "/"$2"/"$3"/"$4"/"$5}')
SYNC_DIR=$( echo $INO_FILE | awk -F '/' '{ if(NF>5){print "/"$2"/"$3"/"$4"/"$5} else if(NF>4){print "/"$2"/"$3"/"$4} else{print "'$src_dir'"}; }' )
if [[ ! -d "$SYNC_DIR" ]]; then
# 如果文件夹不存在,找上层
SYNC_DIR=$(dirname ${SYNC_DIR})
fi
# 处理每个host
echo "SYNC_DIR:$SYNC_DIR"
for target_host in ${slave_hosts_arr[@]}
do
echo "$(date "+%Y-%m-%d %H:%M:%S") $file ---> $target_host"
rsync -avH -e 'ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no' --delete --rsync-path="mkdir -p $SYNC_DIR && rsync" "$SYNC_DIR"/ root@"$target_host":"$SYNC_DIR"
done
done
fi