Created
September 28, 2019 09:24
-
-
Save zaak/c7a77e82f3bb478e89090be7295beea1 to your computer and use it in GitHub Desktop.
ZoneMinder - remove old events
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 | |
function rrmdir($dir) | |
{ | |
if (is_dir($dir)) { | |
$objects = scandir($dir); | |
foreach ($objects as $object) { | |
if ($object != "." && $object != "..") { | |
if (is_dir($dir . "/" . $object)) | |
rrmdir($dir . "/" . $object); | |
else | |
unlink($dir . "/" . $object); | |
} | |
} | |
rmdir($dir); | |
} | |
} | |
$pdo = new \PDO('mysql:host=localhost;port=3306;dbname=zm', 'zmuser', 'zmpass'); | |
$maxTime = (new \DateTime())->modify('-7 days'); | |
$query = 'select Id,Cause,StartTime,MonitorId from Events where StartTime < \'' . ($maxTime->format('Y-m-d')) . '\' order by Id'; | |
echo $query . "\n\n"; | |
$rows = $pdo->query($query)->fetchAll(\PDO::FETCH_ASSOC); | |
echo count($rows) . " events to remove.\n\n"; | |
foreach ($rows as $r) { | |
$id = (int)$r['Id']; | |
$mId = (int)$r['MonitorId']; | |
$eDate = explode(' ', $r['StartTime'])[0]; | |
// var_dump($r); | |
$path = "/var/cache/zoneminder/events/$mId/$eDate/$id"; | |
echo "Deleting $id ... $path \n"; | |
rrmdir($path); | |
$pdo->query("DELETE FROM Events where Id=$id"); | |
$pdo->query("DELETE FROM Frames where EventId=$id"); | |
$pdo->query("DELETE FROM Stats where EventId=$id"); | |
} | |
echo "\nDone.\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment