PHP backup monitoring and alert script: prevent backup rot… 3

One of the key components with any IT software system is putting together an automated , reliable backup solution. Gone are the days of manually backing up files to a removable media, with our interconnected world we can simply setup some form of cloud back up and go.

In a previous post I discussed how to automatically backup small sites to Dropbox. In this post I’ll focus on monitoring those backups to make sure they are being done completely and correctly…

Backup Rot…

Backup rot (similar to software  rot)  is the situation where a previously working automated backup system stops or fails , and you still assume that it’s working.  I recently had this happen with an offsite solution I was using. There was a configuration change at the solution providers end, his email alert indicating a change took place got caught in a spam filter and it was only 3 months after the fact while doing some routine maintenance I discovered the site, I had assumed was being backed up daily was not backed up at all in the last 3 months. This is a fairly typical situation, the automated backups are working but then for some reason, they stop, with no indication to you.

Most back-up software fails to have an email alert setup so when things go south they can alert you, and even if they do they typically only work when they detect an issue they know about (out of disk space, not that an off-site transfer failed).

Backup monitor goals

To avoid this my solution is to create a simple file based backup monitor. Here’s how it works.

  1. You list the critical  file(s) (aka zipped  or tar ball files) , and paths of the backup locations that you want to monitor (network shares, ssh folders etc.)
  2. You indicated how old (fresh) a file needs to be that your monitoring and what its minimum size should be
  3. If the backed up file’s freshness (age) or file size exceeds a certain threshold send out email alerts
  4. Have the ability to run from a cron and or interactively
  5. The script is simple it checks the above metrics to make sure no backup is out of line.. then it alerts you.

And that’s what I created below, the backup monitor script in PHP takes a list of files, and monitors them periodically (via a crontab entry) and  alert you with an email if any of them have failed or exceeded their freshness threshold…

Sample Page

Here’s what the interactive page would look like (When run interactively via the web browser):

Backup Monitor sample screen capture

Backup Monitor sample screen capture

Backup monitor configuration

Review the top part of the script where all the configuration information is kept. Below is  a part of it.

/* When using command line other defined function must come first */
if (isset($_SERVER['argc']))
define('CLI', true);
else
define('CLI', false);

//include "backup_ssh.php"; /*class for ssh connections which require encryption support */

//USER Configurable Array for file information
$age_warn_limit= 3600* 24 ; //default to one day
$age_alert_limit= 3600* 24 ; //age limit before alerts sent out defaults to two days
$email=array();
$email["alerts_to"]="alert_email_recipient@somewhere.com"; //default address to send emails to, can be changed on a per file basis - see array

/* Array configuration file and format to monitor files */
/* Csv File format: file to be setup */
$monitor_config_file=null; /* configuration file name, null to use array below */

//Array Format as follows:
// section | type|protocol://host_name | url/path | description | username | password | warn_limit | alert_limit | email_to | size_alert_limit_bytes
$files_to_monitor = array(
array("Local Files","file://" , "/mnt/var/www/db/db.sql","Recent Database backup " ,null,null, $age_warn_limit , $age_alert_limit*2, $email["alerts_to"],1 ),
/* Nas Drive */
array("Local Nas","file://" , "/mnt/var/www/db/users.log","Recent Database log " ,null,null, $age_warn_limit , $age_alert_limit*2, $email["alerts_to"],1 ),
/* Remote Nas */
array("Remote Nas","file://" , "/mnt/remote/var/www/db/users.log","Recent Database log " ,null,null, $age_warn_limit , $age_alert_limit*2, $email["alerts_to"],1 )

); //end of array
/* email Alerts are sent via */
$email["alerts_enabled"]=CLI; /* true=turns on email alerts, tied to cron command line on */
$email["method"] = "mail"; /* Options: mail | smtp */

/* use with PHPMailer or similar */
$email["smtp_user"] = "server_user_name";
$email["smtp_pass"] ="server_user_pass";
$email["smtp_server"]="server_ip_hostname";
$email["smtp_from_user"]="alerts@hostname.com";
$email["smtp_from_name"]="Backup Monitor Alerts";
$email["subject"] ="Backup Alert --[ {filename} ]--";
$email["message_placeholders"] ="
The backup monitor running at: {time_now}\n
Description: {file_description} \n
Filename: {filename} \n
File Details: ({file_size} bytes) last modified at {filedatetime} .
Backup Age: {file_elapsedtime} > ( limit: {backup_alerttime} seconds )\n
------------------------------------------------\n
";

/* Are we using console/command line (such as cron) invoke the function directly */
if (CLI==true) {
echo "Running backup Monitor - Command Line \n ----------------------------\n";
main_backup_monitor("table_mon"); //call the function directly
echo "\n End Running backup Monitor - Command Line \n ";
} else {
if ($_REQUEST['d']=="t")
{
echo "Running backup Monitor - via direct Web request\n";
main_backup_monitor("table_mon"); //running directly via a web request
}
}

CRON Setup

To make this valuable you need it to run in a cron job so it can be triggered automatically instead of interactively. In fact when it runs interactively  it doesn’t send email alerts.  the process is very straigh forward in unix (Linux) please refer to Windows documentation for similar output.

open a command line (shell) and issue

[code lang=”bash”]</pre>
$ crontab -e

<insert>

@daily php /var/www/backup_monitor.php > /dev/null 2>&1  #Backup monitor script

<save cron>

$ crontab -l
<pre>[/code]

Source Files

Below are the two source files (zipped), the main one is backup_monitor.php the other one is just to produce an output page.
backup_monitor.zip

3 thoughts on “PHP backup monitoring and alert script: prevent backup rot…

  1. Reply alex Nov 4,2014 11:18 pm

    hello, i d’ont understand your ssh system & conf .
    can u explain me how config that

    thx

    • Reply Tony B. Nov 6,2014 2:33 pm

      The configuration happens all in the PHP file part where there is a script for SSH, I uses sshfs (http://fuse.sourceforge.net/sshfs.html) this allows you to mount filesystems as regular unix mounts then you can just refer to them like /mnt/ssh1_site1/destination_folder /mnt/ssh1_site2/destination_folder

      By using sshfs you can just refer to the local /mnt/folder_name as backup folder..

      LEt me know if you have more questions.

  2. Reply alex Nov 7,2014 5:58 pm

    ok thx , it’s “bypass” but it works 🙂
    sshfs with key pair really good!
    byebye

Leave a Reply