Quelltext des aktuellen Standes
#!/usr/bin/env php
<?php
/**
* Cleanup für unmatched_devices
*
* Löscht Devices, die älter als 6 Tage sind
* Zu Crontab hinzufügen: 0 2 * * * php /path/to/bin/cleanup-unmatched-devices.php
*
* Ausführung: php bin/cleanup-unmatched-devices.php
*/
declare(strict_types=1);
// Pfade setzen
define('ROOT_PATH', dirname(__DIR__));
define('CONFIG_PATH', ROOT_PATH . '/config');
// Konfiguration laden
$config = include CONFIG_PATH . '/app.php';
$dbConfig = include CONFIG_PATH . '/database.php';
// Datenbankverbindung
try {
$pdo = new PDO(
sprintf(
'mysql:host=%s;port=%d;dbname=%s;charset=utf8mb4',
$dbConfig['host'],
$dbConfig['port'],
$dbConfig['database']
),
$dbConfig['user'],
$dbConfig['password'],
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]
);
} catch (PDOException $e) {
error_log('[ERROR] Datenbankverbindung fehlgeschlagen: ' . $e->getMessage());
exit(1);
}
// Cleanup: Lösche unmatched_devices älter als 6 Tage
try {
$sixDaysAgo = date('Y-m-d H:i:s', strtotime('-6 days'));
$stmt = $pdo->prepare('DELETE FROM unmatched_devices WHERE first_seen_at < ?');
$stmt->execute([$sixDaysAgo]);
$deletedCount = $stmt->rowCount();
echo sprintf(
'[%s] INFO: %d unmatched_devices gelöscht (älter als 6 Tage)' . PHP_EOL,
date('Y-m-d H:i:s'),
$deletedCount
);
// Audit-Log (optional, wenn AuditLogger verfügbar)
// Für CLI-Scripts nicht kritisch, daher hier nur Info-Ausgabe
exit(0);
} catch (Exception $e) {
error_log('[ERROR] Cleanup fehlgeschlagen: ' . $e->getMessage());
exit(1);
}