Skip to content

Instantly share code, notes, and snippets.

@krmax44
Created February 18, 2022 10:59
Show Gist options
  • Save krmax44/04032051222769747428f32f1a3dc3c2 to your computer and use it in GitHub Desktop.
Save krmax44/04032051222769747428f32f1a3dc3c2 to your computer and use it in GitHub Desktop.
Simple cron job to check if a scheduled backup was uploaded
<?php
// usage: php -f monitor-backups.php -- "./myfolder" "[email protected]" "[email protected]"
$folder = $argv[1];
$email = $argv[2];
$from = $argv[3];
$delay = isset($argv[4]) ? $argv[4] : 2 * 24 * 60 * 60; // 48 hours by default
if (!is_dir($folder)) {
throw new Exception("Folder does not exist.");
}
if (!$email) {
throw new Exception("Missing e-mail.");
}
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($folder));
$files = array();
foreach ($rii as $file) {
if ($file->isDir()) continue;
$files[] = $file->getPathname();
}
$files = array_combine($files, array_map("filemtime", $files));
arsort($files);
$last_modified = reset($files);
if ($last_modified == false) {
throw new Exception("No files found.");
}
if (time() - $last_modified > $delay) {
$last_date = date("r", $last_modified);
mail(
$email,
"Warning: Backup job failed",
"No backups were uploaded since $last_date.",
"From: $from"
);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment