#!/bin/bash
#1. 判断参数个数
if [ $# -lt 1 ]
then
echo Not Enough Arguement!
exit;
fi
#2. 遍历集群所有机器
for host in master slave1 slave2
do
echo ==================== $host ====================
#3. 遍历所有目录,挨个发送
for file in $@
do
#4. 判断文件是否存在
if [ -e $file ]
then
#5. 获取父目录
pdir=$(cd -P $(dirname $file); pwd)
#6. 获取当前文件的名称
fname=$(basename $file)
ssh $host "mkdir -p $pdir"
rsync -av $pdir/$fname $host:$pdir
else
echo $file does not exists!
fi
done
done
1、复制脚本到 /bin 目录
2、为脚本添加执行权限
chmod +x xsync
3、测试脚本
>>(1) 将master服务器中根目录的test文件同步到 目标服务器的相同目录下
[root@master ~]# xsync test.txt
==================== master ====================
sending incremental file list
sent 47 bytes received 12 bytes 118.00 bytes/sec
total size is 6 speedup is 0.10
==================== slave1 ====================
sending incremental file list
test.txt
sent 100 bytes received 35 bytes 270.00 bytes/sec
total size is 6 speedup is 0.04
==================== slave2 ====================
sending incremental file list
test.txt
sent 100 bytes received 35 bytes 270.00 bytes/sec
total size is 6 speedup is 0.04
>> (2)查看同步结果