编写集群之间的同步脚本
(1)需求:循环复制文件到所有节点的相同目录下:
(2)期望脚本:基于rsync技术将文件从一个节点同步到其他所有节点
xsync 要同步的文件名称
(3)脚本实现
在/home/atguigu目录下创建bin目录,并在bin目录下创建xsync文件
说明:在/home/atguigu/bin这个目录下存放的脚本,atguigu用户可以在系统任何
地方直接执行。因为/home/atguigu/bin在系统的PATH环境变量中。
[atguigu@hadoop101 ~] pwd
/home/atguigu
[atguigu@hadoop101 ~] mkdir bin
[atguigu@hadoop101 ~] cd bin
[atguigu@hadoop101 bin] touch xsync
[atguigu@hadoop101 bin] vim xsync
(a)在该文件中编写如下代码
#!/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
(b)修改脚本 xsync 具有执行权限
chmod +x xsync
(c)测试脚本
xsync /home/atguigu/xsync