{"id":373,"date":"2025-05-08T13:17:52","date_gmt":"2025-05-08T11:17:52","guid":{"rendered":"https:\/\/www.24x7serverguard.com\/blog\/?p=373"},"modified":"2025-05-08T13:17:52","modified_gmt":"2025-05-08T11:17:52","slug":"bash-script-that-can-check-and-repair-mysql-tables-automatically-log-the-results-and-optionally-notify-you-via-email-or-other-tools","status":"publish","type":"post","link":"https:\/\/www.24x7serverguard.com\/blog\/mysql\/bash-script-that-can-check-and-repair-mysql-tables-automatically-log-the-results-and-optionally-notify-you-via-email-or-other-tools\/","title":{"rendered":"Bash script that can check and repair MySQL tables automatically, log the results, and optionally notify you via email or other tools."},"content":{"rendered":"\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udee0\ufe0f <strong>MySQL Table Repair Automation Script<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">#!\/bin\/bash<br><br># === Configuration ===<br>DB_USER=\"root\"<br>DB_PASS=\"your_password\"<br>LOG_FILE=\"\/var\/log\/mysql_table_repair.log\"<br>TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')<br><br># Optional: List specific databases to check, or leave empty to check all<br>DATABASES=$(mysql -u$DB_USER -p$DB_PASS -e \"SHOW DATABASES;\" | grep -Ev \"(Database|information_schema|performance_schema|mysql|sys)\")<br><br>echo \"=== MySQL Table Check &amp; Repair Started at $TIMESTAMP ===\" >> $LOG_FILE<br><br>for DB in $DATABASES; do<br>    echo \"Checking database: $DB\" >> $LOG_FILE<br>    TABLES=$(mysql -u$DB_USER -p$DB_PASS -D $DB -e \"SHOW TABLES;\" | tail -n +2)<br><br>    for TABLE in $TABLES; do<br>        CHECK_RESULT=$(mysql -u$DB_USER -p$DB_PASS -D $DB -e \"CHECK TABLE \\`$TABLE\\`;\" | grep -i \"error\")<br><br>        if [[ ! -z \"$CHECK_RESULT\" ]]; then<br>            echo \"Found issue in $DB.$TABLE - Attempting repair...\" >> $LOG_FILE<br>            REPAIR_RESULT=$(mysql -u$DB_USER -p$DB_PASS -D $DB -e \"REPAIR TABLE \\`$TABLE\\`;\")<br>            echo \"$REPAIR_RESULT\" >> $LOG_FILE<br>        else<br>            echo \"$DB.$TABLE is OK.\" >> $LOG_FILE<br>        fi<br>    done<br>done<br><br>echo \"=== MySQL Table Check &amp; Repair Finished ===\" >> $LOG_FILE<\/mark><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccc <strong>Instructions<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Save this as <code>mysql_repair.sh<\/code>.<\/li>\n\n\n\n<li>Make it executable:<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">chmod <\/mark>+x mysql_repair.sh<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Run it manually or schedule via cron:<\/p>\n\n\n\n<p>crontab -e<\/p>\n\n\n\n<p>Add:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">0 3 * * * \/path\/to\/mysql_repair.sh<br><br>\ud83d\udd14 Optional: Email Notification (Add at End of Script)<br>mail -s \"<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">MySQL Table Repair Log<\/mark>\" you@example.com &lt; <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">$LOG_FILE<\/mark><br><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udee0\ufe0f MySQL Table Repair Automation Script #!\/bin\/bash# === Configuration ===DB_USER=&#8221;root&#8221;DB_PASS=&#8221;your_password&#8221;LOG_FILE=&#8221;\/var\/log\/mysql_table_repair.log&#8221;TIMESTAMP=$(date &#8216;+%Y-%m-%d %H:%M:%S&#8217;)# Optional: List specific databases to check, or leave empty to check allDATABASES=$(mysql -u$DB_USER -p$DB_PASS -e &#8220;SHOW DATABASES;&#8221; | grep -Ev &#8220;(Database|information_schema|performance_schema|mysql|sys)&#8221;)echo &#8220;=== MySQL Table Check &amp; Repair Started at $TIMESTAMP ===&#8221; >> $LOG_FILEfor DB in $DATABASES; do echo &#8220;Checking database: $DB&#8221; >> $LOG_FILE [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":374,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[363,361,362],"class_list":["post-373","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mysql","tag-and-optionally-notify-you-via-email-or-other-tools","tag-bash-script-that-can-check-and-repair-mysql-tables-automatically","tag-log-the-results"],"_links":{"self":[{"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/posts\/373","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/comments?post=373"}],"version-history":[{"count":1,"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/posts\/373\/revisions"}],"predecessor-version":[{"id":375,"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/posts\/373\/revisions\/375"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/media\/374"}],"wp:attachment":[{"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/media?parent=373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/categories?post=373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/tags?post=373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}