Monitor Borg backup status

A simple PHP solution to monitor if backups are running.

Merijn Schering Merijn ScheringAugust 07, 2025
Monitor Borg backup status

To backup our Group-Office instances we use the fantastic tool Borg backup. Setting up automatic backups is straightforward but we also need a way to know that they are running every day. For this purpose I wrote a simple PHP script.
It checks the backup log and finds out the date of the last backup and if it was successful. If it was succesful and not older than one day then a success status is given. Otherwise it will report an error.
We use it in combination with UptimeRobot which polls this script every 5 minutes. So when a backup fails our mobiles will warn us with a very nasty sound.
Maybe it's useful to you to so heere's the PHP script:

<?php
// Location of the log file. It will also check for the file name with ".1" appended to support logrotate
$log = "/var/log/borgbackup.log";

function fail($msg) {
    http_response_code(500);
    exit($msg."\n");
}

// Finds all successful backup status messages with their dates
function findbackups($log) {
	exec('fgrep "Backup, Prune, and Compact finished successfully" '.$log, $output, $ret);
	return $output;
}

$output = findbackups($log);
if(empty($output)) {
	$output = findbackups($log.".1"); // for logrotate
}
if(empty($output)) {
	fail("No successful backups found");
}

// Now parse the date from the status line
$statusLine = array_pop($output);

$pos = strpos($statusLine, "Backup, Prune, and Compact finished successfully");

if($pos === false) {
	fail("Backup failed: " . $statusLine);
}

$date = strtotime(substr($statusLine, 0, $pos));

// check if the backup is recent. It runs daily and allow 6 hours for a run.
if($date < strtotime("-1 day, -6 hours")) {
	fail("Last backup too old: " . date("c", $date));
}
http_response_code(200);
echo "Last successful backup: " . date("c", $date) ."\n";
	

While this is great in checking successful runs automatically it is still needed to check your backup manually regularly and see if you can actually restore from backup.

Twitter LinkedIn GitHub Mastodon