# cat db_check.sh #!/bin/bash # written by ITSYOU ############################################################################################ db log 저장 LOGFILE=/var/log/dbcheck_$(date '+%Y-%m-%d').log ############################################################################################ 배너 clear echo -e "\e[1;33m -------------------------------------------------------------------------------------------------------------------------------------------------------------- #### # #### # #### # ### ##### ### # # ##### ### # # ### ### ### # ### #### ### #### # ### #### # # ## # ## # # ## # # # # # # # # # # # # # # # # # # # # # # # # # ## # # # # # # # # # # ### # # ### #### # ##### #### # ## ### # # # # # # # ### # # # # # # # # ##### # ##### # # ##### ## # # # # # # ### ## # #### # #### # ## # ##### #### # # # # # # # # # # # # # # # ## # # # # ## # # # # # ## # # # # # # # # # # # # # # #### # # # # # #### # # ### #### ### # # #### ### # # ### ### # # # # # ### # # # # # #
-------------------------------------------------------------------------------------------------------------------------------------------------------------- \e[0m " ############################################################################################ DB 패스워드 입력받기 function dbpass(){ echo -e " \033[1;36m \033[1;35m ####################################################################################### ###################### DB 의 root 패스워드를 입력하세요 ########################## #######################################################################################\033[0m\033[1;36m \033[0m" echo -e " ▶ 패스워드 : \c" read PW echo "" echo "" echo -n -e "\\033[01;32m root 패스워드는 "$PW "가 맞습니까? [y/n] : " read ANSWER until [ "$ANSWER" = "y" ] ; do echo -e "\033[1;31m 패스워드를 다시 입력하세요!!! \033[0m" echo "" echo -e " ▶ 패스워드 : \c" read PW echo "" echo "" echo -n -e "\\033[01;32m root 패스워드는 "$PW "가 맞습니까? [y/n] : " read ANSWER done echo -e "\033[1;30m 패스워드가 확인되었습니다\033[0m" sleep 1 clear selectmenu } ############################################################################################ 작업번호 선택하기 function selectmenu(){ clear echo -e " \033[1;36m \033[1;35m ####################################################################################### ############################## 작업을 선택하세요 ################################ #######################################################################################\033[0m\033[1;36m 1. 테이블 검사 및 복구 2. 전체 데이터베이스 검사 및 복구 3. 작업 종료하기 \033[0m" echo -e " 작업번호 : \c" read JOB if [ $JOB = 1 ]; then clear echo -e "" echo -e "\033[1;34m▶ 테이블에 대한 검사를 시작합니다. \033[0m " ;num fi if [ $JOB = 2 ]; then echo -e "" echo -e "\033[1;34m▶ 모든 데이터베이스를 검사한 후 자동으로 복구합니다.\033[0m " ;num fi if [ $JOB = 3 ]; then echo -e "" echo -e "\033[1;34m▶ 스크립트를 종료합니다. \033[0m " exit 0 fi until [ "$JOB" -lt 4 ]; do echo -e "\033[1;31m * 잘못된 응답입니다. 다시 입력하세요. \033[0m" echo "" echo -e " 작업번호 : \c" read JOB echo "" echo "" done } ############################################################################################ 기존메뉴로 돌아가기 function back(){ echo "" echo "" echo "" echo "" echo -e " 이전 메뉴로 돌아가시겠습니까? [y/n] : \c" read ANSWER2 if [ "$ANSWER2" = "y" ] || [ "$ANSWER2" = "Y" ]; then echo -e "\033[0m" selectmenu else echo -e "" echo -e "\033[1;31m 스크립트 실행을 종료합니다.\033[0m" exit 1 fi echo -e "" echo -e "\033[0m" } ############################################################################################ 작업진행 function num(){ case "$JOB" in 1) echo "" echo -e "\033[1;34m> SHOW DATABASES \033[0m " echo "show databases;" | mysql -u"root" -p"$PW" -t echo "" echo -e "- table 을 조회하기 위해 DB 명을 입력해주세요 : \c" read DBNAME echo "" echo "" echo "" echo -e "\033[1;34m> SHOW TABLES $DBNAME \033[0m " echo "show tables;" | mysql -u"root" -p"$PW" $DBNAME -t echo "" echo "" echo -e " ▶ 체크할 Table : \c" read CHECK_TABLE echo "use $DBNAME ; check table $CHECK_TABLE;" |mysql -u"root" -p"$PW" -t echo "" echo "" echo -e "\033[01;32m ▶ 해당 Table 에 대한 복구 작업을 시작하시겠습니까? [y/n] : \c \033[0m" read ANSWER3 if [ "$ANSWER3" = "y" ] || [ "$ANSWER3" = "Y" ]; then echo -e "\033[0m" echo -e "use $DBNAME ; repair table $CHECK_TABLE;" |mysql -u"root" -p"$PW" -t echo "" sleep 1 echo "" echo -e "\033[01;36m ▶ $CHECK_TABLE 복구 작업 결과를 출력합니다.\033[0m " echo "" echo "use $DBNAME ; check table $CHECK_TABLE;" |mysql -u"root" -p"$PW" -t echo "" echo "" echo "" else back fi echo -e "\033[01;32m ▶ 작업 내용을 저장합니다. (/var/log/dbcheck_****-**-**.log) : \033[0m" save_log ;; 2) clear echo "" echo -e "\033[1;34m▶ 모든 데이터베이스를 검사한 후 자동으로 복구합니다.\033[0m " mysqlcheck -u"root" -p"$PW" --auto-repair --all-databases back ;; esac } ############################################################################################ 결과저장 (수동) # #function save_result(){ #echo -e "결과내용을 저장할 위치를 입력하세요 : \c" #read LOCATE #echo -e "파일이름을 지정하세요 : \c:" #read FILENAME #echo -e "" > $LOCATE/"$FILENAME".txt #echo -e "" >> $LOCATE/"$FILENAME".txt #echo -e "" >> $LOCATE/"$FILENAME".txt #echo -e "◇ 작업일자 : $(date +%Y)년 $(date +%m)월 $(date +%d)일 $(date +%H:%M)" >> $LOCATE/"$FILENAME".txt #echo -e "" >> $LOCATE/"$FILENAME".txt #echo -e "" >> $LOCATE/"$FILENAME".txt #echo -e "◇ 데이터베이스 : $DBNAME" >> $LOCATE/"$FILENAME".txt #echo -e "◇ 테이블 : $CHECK_TABLE" >> $LOCATE/"$FILENAME".txt #echo -e "" >> $LOCATE/"$FILENAME".txt #echo -e "" >> $LOCATE/"$FILENAME".txt #echo -e "◇ $CHECK_TABLE 의 오류내용 " >> $LOCATE/"$FILENAME".txt #echo "use $DBNAME ; repair table $CHECK_TABLE;" |mysql -u"root" -p"$PW" -t >> $LOCATE/"$FILENAME".txt #echo -e "" >> $LOCATE/"$FILENAME".txt #echo -e "" >> $LOCATE/"$FILENAME".txt #echo -e "◇ 복구작업 결과" >> $LOCATE/"$FILENAME".txt #echo -e "use $DBNAME ; check table $CHECK_TABLE;" |mysql -u"root" -p"$PW" -t >> $LOCATE/"$FILENAME".txt #echo "" #back #echo "" #} # ############################################################################################ 결과저장 (자동) function save_log(){ echo -e "---------------------------------------------------------------------------------------------------------" >> $LOGFILE echo -e "◇ 작업일자 : $(date +%Y)년 $(date +%m)월 $(date +%d)일 $(date +%H:%M)" >> $LOGFILE echo -e "" >> $LOGFILE echo -e "" >> $LOGFILE echo -e "◇ 데이터베이스 : $DBNAME" >> $LOGFILE echo -e "◇ 테이블 : $CHECK_TABLE" >> $LOGFILE echo -e "" >> $LOGFILE echo -e "" >> $LOGFILE echo -e "◇ $CHECK_TABLE 의 오류내용 " >> $LOGFILE echo "use $DBNAME ; repair table $CHECK_TABLE;" |mysql -u"root" -p"$PW" -t >> $LOGFILE echo -e "" >> $LOGFILE echo -e "" >> $LOGFILE echo -e "◇ 복구작업 결과" >> $LOGFILE echo -e "use $DBNAME ; check table $CHECK_TABLE;" |mysql -u"root" -p"$PW" -t >> $LOGFILE echo -e "---------------------------------------------------------------------------------------------------------" >> $LOGFILE echo "" back echo "" } dbpass exit 0 |
'개발 > BASH' 카테고리의 다른 글
서버 포트 체크 (0) | 2022.06.13 |
---|---|
리소스 사용률 퍼센트 확인 (0) | 2022.06.13 |
원격서버에 대한 리소스 모니터링 확인 스크립트 (0) | 2020.11.17 |
서비스 데몬 자동으로 시작하기 (0) | 2020.09.29 |