在集群中同步文件和命令的shell工具-02

CmdSync.sh

[root@localhost sync]# cat CmdSync.sh
#!/bin/bash


#同步集群命令

CONF=../config

PraserConf(){
    num=`sed -n -e '/^\['$1'\]/=' $CONF`
    let temp=num+1
    sed -n ''$temp'p' $CONF
}

nodes_str=`PraserConf nodes`
old_ifs="$IFS"
IFS=","
nodes=($nodes_str)


CmdSync(){
    cmd=$1
    echo $1
    for node in ${nodes[*]}
    do  
        echo -e "\e[32m******$node******\e[0m"
        ssh root@$node "$cmd"
        if [ $? -eq 0 ]
        then
            echo cmd successful
        fi
    done
}

CmdSync "$1"

FileSync.sh:

[root@localhost sync]# cat FileSync.sh
#!/bin/bash


#同步集群文件
#入参为文件绝对路径

CONF=../config

PraserConf(){
    num=`sed -n -e '/^\['$1'\]/=' $CONF`
    let temp=num+1
    sed -n ''$temp'p' $CONF
}

nodes_str=`PraserConf nodes`
old_ifs="$IFS"
IFS=","
nodes=($nodes_str)


SyncFile(){
    base_dir=$1
    echo $1
    base_dir=`echo $base_dir|awk -F"/" '{for (i=1;i<=NF-1;i++) printf("%s/",$i)}'`
    echo $base_dir
    for node in ${nodes[*]}
    do
        ip addr|grep $node >/dev/null 2>&1
        if [ $? -ne 0 ]
        then
            scp -r $1 root@$node:$base_dir
            #>/dev/null 2>&1
            if [ $? -eq 1 ]
            then
                ssh root@$node "mkdir -p $base_dir"
                scp -r $1 root@$node:$base_dir
                #>/dev/null 2>&1
            fi
        fi
    done
}


#路径转换
#将路径转换为绝对路径
tran_path(){
    if [[ "$1" =~ ^/ ]]
    then
        if [[ "$1" =~ /$ ]]
        then
            base_dir=`echo $1|awk -F"/" '{for (i=2;i<=NF-1;i++) printf("/%s",$i)}'`
            echo $base_dir
        else
            echo $1
        fi
    elif [[ -d $1 ]]||[[ -z $1 ]]
    then
        path=`pwd`/$1
        cd $path >/dev/null
        real_path=`pwd`
        echo $real_path
    else
        path=`pwd`/$1
        base_dir=`echo $path|awk -F"/" '{for (i=1;i<=NF-1;i++) printf("%s/",$i)}'`
        cd $base_dir >/dev/null
        real_path=`pwd`/`basename $1`
        cd - >/dev/null
        echo $real_path
    fi
}

#tran_path $1
SyncFile `tran_path $1`

猜你喜欢

转载自www.cnblogs.com/wuchy/p/12177395.html