3 配置远程同步脚本xsync
远程分发指令有:
- scp (secure copy)
- 之前都是sudo scp /opt/hello.txt atguigu@hadoop103:/opt/
- 也可以:sudo scp /opt/hello.txt hadoop103:/opt/
- rsync
- 主要用于备份和镜像,速度快,只对差异文件更新。
- sudo rsync -av hadoop hadoop:/opt/
步骤1:在/bin目录下创建一个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
步骤2:给该xsync文件增加可执行权限
sudo chmod +x xsync