在/home/atguigu目录下创建bin目录,并在bin目录下创建xsync文件
[atguigu@hadoop101 hadoop] cd /home/atguigu
[atguigu@hadoop101 ~] mkdir bin
[atguigu@hadoop101 bin] touch xsync
[atguigu@hadoop101 bin] vim xsync
在该文件中编写如下代码
#!/bin/bash
#1. 判断参数个数
if [ $# -lt 1 ]
then
echo Not Enough Arguement!
exit;
fi
#2. 遍历集群所有机器
for host in hadoop102 hadoop103 hadoop104
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
本文介绍了一款名为xsync的bash脚本,用于一键将本地指定文件同步到多台服务器上。通过判断参数个数、遍历集群机器、检查文件存在性并利用rsync命令实现高效文件同步。
895

被折叠的 条评论
为什么被折叠?



