建一個內容分發腳本,解決對多臺機器的相同操作,例如把某一文件拷貝到所有集羣機器上

首先 cd ~

 vim xsync

然後寫入內容

  #!/bin/bash
#1. 判斷參數個數
if [ $# -lt 1 ]
then
    echo Not Enough Arguement!
    exit;
fi

#2. 遍歷所有目錄,挨個發送
for file in $@
do
    #4.5 判斷文件是否存在
    if [ -e $file ]
    then
        #3. 獲取父目錄
        pdir=$(cd -P $(dirname $file); pwd)
        
        #4. 獲取當前文件的名稱
        fname=$(basename $file)
        
        
        
        #5. 遍歷集羣所有機器,拷貝
        for host in hadoop102 hadoop103 hadoop104
        do
            echo ====================    $host    ============= =======
			ssh $host "mkdir -p $pdir"
            rsync -av $pdir/$fname $USER@$host:$pdir
        done
    else
        echo $file does not exists!
    fi
done

修改文件權限

    chmod +x xsync
將配置好的文件拷貝到/bin下,這樣哪個用戶都可以使用
 sudo cp xsync /bin
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章