I was bored so:
I recently had a situation where MYSql tables were crashing on me and because of that all the applications that were accessing the affected tables would grind to a big messy halt.
So, I invented a little automated table checking and repair utility that I could launch from the crontab on a regular interval to try to keep the database up and running.
My original app checks multiple databases and has a lot of process checking to make sure that no collisions occur.
I have modified it down to just check all the tables in one database.
And now, I give it to you.
Enjoy
Ok, it is not letting me put all the code in one message so I will have to split it into 2 pieces.
Here is piece one:
Code:
<?php
// check_tables.php
// This application will check all tables in a single database
// for errors and attempt to repair them if found.
// It will email a report to a precoded address or set of addresses
// and presnt a report on the screen or send it to a dump file.
//
// This application uses a process file to make sure that multiple instances
// of the application do not collide. It probably would not be a good thing to
// have 2 different instances of the program attempting the same repairs
// at the same time.
//
// The process file is created in a folder called 'running'. You need to create
// this folder on your server and change the path to it on the appropriate line
// in the housekeeping section below.
//
// This folder must be writeable to the program. This for me means I need to use 777 permissions.
// This folder should be set to the strictest permissions that will still allow
// the application to write to it.
//
//
// Start
//
// Put out a message just because its cool to keep track of how long things take.
echo "Start of run " . date("M d Y H:i:s", time()) . "<br>\n";
// Housekeeping
//
// counters
//
// how many tables need repair
$needrepair=0;
// how many tables were repaired
$repaired=0;
//
// holder for status during check and repair operation
$last_status="";
//
// initial output message
$outstr="A Maintenance run started at " . date("M d Y H:i:s", time()) . "<br><br>\n";
//
// initial error message
$errorstr="There was an error in the maintenance program.\n\n";
//
// ******* All information to be modified is in this section
//
// This is the address to send from
// *** change the line below to reflect an address from your domain
$fromaddress="Database Maintenance auto report<[email protected]>";
//
// This is the address to send the report and errors to
// *** change the line below to indicate the addresses you wish to send reports to
$toaddress="[email protected];[email protected]";
//
// This is the process file. we put this out there at the beginning of the run
// and remove it at the end of the run. If this file exists then
// a maintenance run is already occurring
// **** Change the path below to the proper path on your server
// usually something like /user/web/domain/running
// If you only use it from the browser then this could be
// just running/maint.txt if you create the running folder as a
// subfolder of the folder the app is running from
$processfl="/user/web/domain/running/maint.txt";
//
// database information
//
// *** change the information below to reflect your database settings
//
// database host
$dbhost = "localhost";
//
// database user
$dbuser = "dbusername";
//
// database password
$dbpass = "dbpassword";
//
// database name
$dbname = "dbname";
//
// *** end of data that needs to be modified
//
// End of housekeeping
//