Linux同步脚本

1、脚本

#!/bin/bash
#1. 判断参数个数
if [ $# -lt 1 ]
then
  echo Not Enough Arguement!
  exit;
fi
#2. 遍历集群所有机器,-----可以根据自己得机器进行修改
for host in hadoop101 hadoop102 hadoop103
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、使用案例

echo "nihao" >> hadoop103.txt

./xsync hadoop103.txt 

3、linux系统其实有同步脚本。路径为/root/bin/xsync.sh 。也只需要自己修改主机名即可使用

posted @ 2021-04-21 16:20  小白啊小白,Fighting  阅读(112)  评论(0编辑  收藏  举报