分发同步脚本编写

分发同步脚本

目的:一般在部署集群的时候,可以同步分发到批量集群,节约时间按,也不容易出错
1.编写脚本

#!/bin/bash
if [ $# -lt 1 ]
then
  echo Not Enough Arguement!
  exit;
fi
for host in hadoop01 hadoop02 hadoop03
do
  echo ============== $host =============
  # $@表示所有参数
  for file in $@
  do
    #判断文件是否存在
    if [ -e $file ]
      then
        #获取父目录,这里加-P 即使是软连接,也能获取到真正的物理目录
        pdir=$(cd -P $(dirname $file);pwd)
        #获取当前文件的名称
        fname=$(basename $file)
        #这里加 -p ,如果目录存就不创建
        ssh $host "mkdir -p $pdir"
        #rsync增量同步文件,scp完全覆盖同步
        rsync -av $pdir/$fname $host:$pdir
      else
        echo $file does not exists!
    fi
  done
done

2.在bin目录下添加此内容的脚本并命名为xsync
cd /bin
vim xsync
3.赋予执行权限
chmod +x xsync
4.可以在任意路径使用此命令,如ls,cd一样,还可以同时增量同步多个文件,堪称神器
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44468025/article/details/124565046