Created
February 18, 2022 10:59
-
-
Save krmax44/04032051222769747428f32f1a3dc3c2 to your computer and use it in GitHub Desktop.
Simple cron job to check if a scheduled backup was uploaded
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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