The first thing that I wanted to do was to set-up a system that showed very quickly if the backups were running correctly. Originally checking backups was a tedious process. I would have to SSH to the box and check that the files were there, which was slow and annoying (Which leads me to stop doing it on a regular basis).

In the new management system I wanted to have a “dashboard” when you first login that shows the health of the site. First one that I want to cover was the backup check. This “widget” needs to show me in a few seconds if everything is OK, of if there is something I need to look into.

Overview

So the backups are run by script on a daily basis at approx 2:15am. Each database is backed up to its own file in a folder with today’s date. What the widget needs to do is not only check that the folder is there but that what is in the folder is what we expect (i.e. is there a backup file for each sub site?). There is little point running a backup of “stuff” and not actually backing up what we need to backup.

So lets get in a see how it was done.

<?php
	/* Variables */
	$correct = "n"; //Set the traffic light to red to start. 
	$client_list = array("supportdb", "maindb"); //An array of expected databases.
	$backup_location = "/home/user/MySQLBackups/"; //Where we find the backups.
	$usrmsg = ""; //A blank user message
	
	/* Get today's date. If it is before 3:00AM consider it yesterday. */
	if(date("G") > 3) {
		/* Before 3 */
		$today = date("Y-m-d");
	} else {
		$today = date("Y-m-d", strtotime("yesterday"));
	} //if else set date. 
	$todays_backup = $backup_location .$today; //Todays backup folder name
	
	/* Get a list of databases that we are expecting to find. */
	$sql = "SELECT `dbname` FROM `site_data` WHERE `active` = 'y' ";
	$results = mysql_query($sql)
		or die(mysql_error());
	while($row = mysql_fetch_array($results)) {
		/* Update the client_list array for databases that we expect to find. */
		$client_list[] = "prefix_" .$row['dbname'];
	} //while results
	
	/* Now we will check if todays backup folder exists. */
	if(file_exists($todays_backup)) {
		$correct = "y"; //Change the value to OK. 
		/* Today's folder exist 
		* Now we need to check that all the databases that we expect to find are there 
		*/
		foreach($client_list as $client) {
			$client_backup = "";
			$client_backup = $todays_backup ."/$today-$client.gz";
			
			if(!file_exists($client_backup)) {
				//If the file is missing
				$correct = "n"; //Show an error
				$usrmsg .= "$client is missing from today's backup.<br /> ";
			} //if file exists
		} //foreach client
	} //check for todays backup folder
	
	/* Display the output. */
	echo "<h1>Backup Status</h1>";
	if($correct == "y") {
		?>
        	<img src="../images/ok.png" />
        <?
	} else { 
		?>
        	<img src="../images/fail.png" />
        <?
	} //if else display message
	
	/* Now display the usrmsg if it is not blank */
	if($usrmsg != "") {
		echo "<br />$usrmsg";
	} else { 
		echo "<br />Backups OK!";
	} //if usrmsg not blank
?>

Now that is complete all there was to do was to link to the file from the main page. As you can see from the different states below the widget either shows a nice green tick or an error with additional information (i.e. what files are missing)

All is well

All is well

FAIL!

FAIL!

As you can see this provides a very quick, yet accurate insight into the heath of the backups. I do plan to create another widget that will show the backup health in relation to the retention available but after thinking about it that is different to if the backups have run or not.