注:此脚本需要同步的几台机器之间免密通信。
免密通信见文章: http://t.csdn.cn/HZcZw
#!/bin/bash
#Author: 臆想的一只猫
#Created: 2022-07-13 15:10:03
#Description: 数据同步 [sh script_name.sh /filepath]
if [ $# -ne 1 ];
then
echo "Usage: $0 /path/to/file(绝对路径)"
exit
fi
# 判断文件是否存在
if [ ! -e $1 ];
then
echo "[ $1 ] dir or file not find!"
exit
fi
# 获取父路径
fullpath=`dirname $1`
# 获取子路径
basename=`basename $1`
# 进入到父路径
cd ${fullpath}
for ((host_id=1;host_id<=3;host_id++))
do
echo ===== rsyncing elastic-node${host_id}: ${basename} ====
# 将数据同步到另外两个节点
rsync -az ${basename} `whoami`@elastic-node${host_id}:${fullpath}
if [ $? -eq 0 ];
then
echo '命令执行成功!'
fi
done