此腳本的主要用途是檢測mysql服務器上所有的db或者單獨db中的壞表。
檢測完後會在/tmp 目錄下生成日誌文件。
檢測指定數據庫的效果圖如下:
檢查所有數據庫:
- #!/bin/bash
- #此腳本的主要用途是檢測mysql服務器上所有的db或者單獨db中的壞表
- #變量說明 pass mysql賬戶口令 name mysql賬號名稱 data_path mysql目錄路徑 directory_list 目錄列表 file_list文件列表 db_name 數據庫名稱 repair_count單庫中待修復的表總數
- #變量說明 repair_count_all所有庫中待修復的表總數 mysql_version mysql版本 _file_name 數據表名稱
- echo -e "此腳本的主要用途是檢測mysql服務器上所有的db或者單獨db中的壞表\n\n"
- pass=123456
- name=root
- read -p "輸入mysql存儲路徑: " choose
- data_path=$choose
- unset choose
- read -p "請輸入mysql命令路徑: " mysql_version
- #標準輸入、標準輸出、標準錯誤輸出的文件標示符 由 0、1、2標識
- read -p "請選擇是檢查服務器上所有數據庫還是指定的數據庫 1:檢查全部數據庫 2:只檢查指定數據庫: " choose
- if [ $choose == 1 ]; then
- cd $data_path
- for directory_list in $(ls)
- do
- if [ -d $directory_list ];then
- if [ "mysql" != "${directory_list}" -a "test" != "${directory_list}" ];then
- cd ${directory_list}
- echo "當前檢查數據庫爲:"${directory_list}
- for file_list in $(ls *.frm)
- do
- _file_name=${file_list%.frm}
- echo -e "\n" >> /tmp/check_table_all.log
- ${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
- done
- cd ..
- fi
- fi
- done
- cat /tmp/check_table_all.log | grep "Table is marked as crashed" > /tmp/check_table_repair.log
- repair_count_all=` awk 'END{print NR}' /tmp/check_table_repair.log `
- echo -e "所有數據庫用有${repair_count_all}張表需要修復!"
- more /tmp/check_table_repair.log
- else
- read -p "請輸入要檢查的數據庫名稱: " db_name
- cd ${data_path}/${db_name}
- for file_list in $(ls *.frm)
- do
- _file_name=${file_list%.frm}
- echo -e "\n" >> /tmp/check_${db_name}.log
- ${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
- done
- cat /tmp/check_${db_name}.log | grep "Table is marked as crashed" > /tmp/check_${db_name}_Repair.log
- repair_count=`awk 'END{print NR}' /tmp/check_${db_name}_Repair.log`
- echo -e "${db_name}中共有${repair_count}個表需要修復!\n "
- more /tmp/check_${db_name}_Repair.log
- fi