1 #!/bin/bash 2 #此腳本的主要用途是檢測mysql服務器上所有的db或者單獨db中的壞表 3 #變量說明 pass mysql賬戶口令 name mysql賬號名稱 data_path mysql目錄路徑 directory_list 目錄列表 file_list文件列表 db_name 數據庫名稱 repair_count單庫中待修復的表總數 4 #變量說明 repair_count_all所有庫中待修復的表總數 mysql_version mysql版本 _file_name 數據表名稱 5 echo -e "此腳本的主要用途是檢測mysql服務器上所有的數據庫或者單獨數據庫中的壞表\n\n" 6 pass=123456 7 name=root 8 read -p "輸入mysql存儲路徑: " choose 9 data_path=$choose 10 unset choose 11 read -p "請輸入mysql命令路徑: " mysql_version 12 #標准輸入、標准輸出、標准錯誤輸出的文件標示符 由 0、1、2標識 13 read -p "請選擇是檢查服務器上所有數據庫還是指定的數據庫 1:檢查全部數據庫 2:只檢查指定數據庫: " choose 14 if[ $choose ==1];then 15 cd $data_path 16 for directory_list in $(ls) 17 do 18 if[-d $directory_list ];then 19 if["mysql"!="${directory_list}"-a "test"!="${directory_list}"];then 20 cd ${directory_list} 21 echo "當前檢查數據庫為:"${directory_list} 22 for file_list in $(ls *.frm) 23 do 24 _file_name=${file_list%.frm} 25 echo -e "\n">>/tmp/check_table_all.log 26 ${mysql_version}-h 127.0.0.1-u${name}-p${pass}-e "check table "${directory_list}.${_file_name}2>&1>>/tmp/check_table_all.log 27 done 28 cd .. 29 fi 30 fi 31 done 32 cat /tmp/check_table_all.log | grep "Table is marked as crashed">/tmp/check_table_repair.log 33 repair_count_all=` awk 'END{print NR}' /tmp/check_table_repair.log ` 34 echo -e "所有數據庫用有${repair_count_all}張表需要修復!" 35 more /tmp/check_table_repair.log 36 else 37 read -p "請輸入要檢查的數據庫名稱: " db_name 38 cd ${data_path}/${db_name} 39 for file_list in $(ls *.frm) 40 do 41 _file_name=${file_list%.frm} 42 echo -e "\n">>/tmp/check_${db_name}.log 43 ${mysql_version}-h 127.0.0.1-u${name}-p${pass}-e "check table "${db_name}.$_file_name 2>&1>>/tmp/check_${db_name}.log 44 done 45 cat /tmp/check_${db_name}.log | grep "Table is marked as crashed">/tmp/check_${db_name}_Repair.log 46 repair_count=`awk 'END{print NR}' /tmp/check_${db_name}_Repair.log` 47 echo -e "${db_name}中共有${repair_count}個表需要修復!\n " 48 more /tmp/check_${db_name}_Repair.log 49 fi