Skip to content

Instantly share code, notes, and snippets.

@zaak
Created September 28, 2019 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaak/c7a77e82f3bb478e89090be7295beea1 to your computer and use it in GitHub Desktop.
Save zaak/c7a77e82f3bb478e89090be7295beea1 to your computer and use it in GitHub Desktop.
ZoneMinder - remove old events
<?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