天天看點

檢測不同伺服器上代碼差别的shell的腳本

本腳本來自有學習阿銘的博文學習:

在生産中,為了防止高并發,單點,災變,和負載均衡等一些突發情況,會将多台伺服器用來跑用一代碼。但是如果上線的代碼出現了問題,将導緻代碼不一樣的情況。

#!/bin/bash
#用途:檢測不同伺服器上代碼差别的shell的腳本。
#作者:Caron maktini
#日期:2018年10月18日
#版本:v0.1

#假設B機器IP位址為192.168.0.1

B_ip=192.168.0.1 
dir=/data/wwwroot/www.abd.com
#首先檢查/tmp/md5.list檔案是否存在,存在的話就删除掉,避免影響後續操作
[ -f /tmp/md5.list ] && rm -f /tmp/md5.list

#把除了uploads以及tmp目錄外其他目錄下的全部檔案列出來 
  cd $ dir 
    find . \( -path "/uploads*" -o  -path  "./tmp*" \) 

#用while循環,求出所有檔案的md5值,并寫入一個檔案裡 
  cat  /tmp/file.Iist l while read line 
  do 
      md5sum  $line 
  done  >>  /tmp/md5.Iist 

#将md5.list拷貝到B機器 
scp  /tmp/md5.Iist  $B_ip:/tmp/ 

#判斷/tmp/check_md5.sh檔案是否存在 
[ -f  /tmp/check_md5.sh &&  rm -f  /tmp/check_md5.sh ]

#用Here Document編寫check_md5.sh腳本内容 
cat > /tmp/check_md5. sh << EOF 

#!/bin/bash 
dir=/data/wwwroot/www.abd.com 
##注意,這裡涉及到的特殊符号都需要脫義,比如反引号和$ 

n=\`wc -l /tmp/md5.list | awk '{print \ $1}'`\ 
for i in \`sep 1 \$n\`\
do 
    file_name=\` sed -n "\$i"p  /tmp/md5.list I awk '{print \$1}'\` 
    md5=\`sed -n  "\$i" p  /tmp/md5.Iist  l awk '{print \$2 }'\`
    if  [ -f  \$file_name ] 
    then 
      md5_b= \`md5sum  \$file_name\` 
    if  [  \$md5_b  != \$md5  ]
    then 
        echo  " \$file_name changed" 
    fi 
    else 
        echo  " \$file_name lose " 
    fi 
done  >   /data/change. Iog 
EOF 
scp  /tmp/check_md5.sh  $B_ip:/tmp/ 
ssh $B_ip  "/bin/bash  /tmp/check_md5.sh" 
           

繼續閱讀