5.【xsync.sh】集群群发文件脚本

#! /bin/bash

# 1.判断参数个数
if [ $# -lt 1 ]
    then
        echo 'Not Enougth Arugement!'
        exit;
fi

# 2.遍历集群所有机器
for host in  pc001 pc002 pc003
do
    if [ $HOSTNAME != $host ]
        then
            echo "==================$host====================="
            # 3.遍历指定的目录
            for file in $@
            do
                # 4.判断指定文件是否存在
                if [ -e $file ]
                    then
                        # 4.1 获取父目录
                        pdir=$(cd -P $(dirname $file);pwd)
                        # 4.2 获取文件名称
                        fname=$(basename $file)
                        # 4.3 对目标host创建对应的目录
                        ssh $host "mkdir -p $pdir"
                        # rsync同步文件
                        rsync -av $pdir/$fname $host:$pdir
                    else
                        echo "The file $file is not exists!"
                fi
            done
    fi

done
原文地址:https://www.cnblogs.com/nuochengze/p/15417607.html