Rsync sync log: stream live output from tee'd log file, preserve last run

This commit is contained in:
Gmer4Lfe
2026-06-06 16:06:42 -04:00
parent d05c5ac198
commit 57773bcaf5
3 changed files with 110 additions and 55 deletions
+42
View File
@@ -3,6 +3,48 @@ header('Content-Type: application/json');
require_once dirname(__DIR__) . '/include/config.php';
require_once dirname(__DIR__) . '/include/monitor.php';
// ── Live sync log ─────────────────────────────────────────────────────────────
$action = $_GET['action'] ?? '';
if ($action === 'rsync_log') {
$lockDir = '/tmp/unraid_locks';
$lines = [];
$profile = null;
$live = false;
$elapsed = 0;
// Active sync — read live log
foreach (glob("$lockDir/rsync_*.lock") ?: [] as $lf) {
$content = trim(@file_get_contents($lf) ?: '');
[$pid, $locked_name] = array_pad(explode(':', $content, 2), 2, '');
if (!$pid || !file_exists("/proc/$pid")) continue;
$profile = preg_replace('/^rsync_/', '', $locked_name ?: basename($lf, '.lock'));
$elapsed = time() - (int)filemtime($lf);
$liveLog = "$lockDir/rsync_{$profile}.log";
$raw = file_exists($liveLog) ? (file($liveLog, FILE_IGNORE_NEW_LINES) ?: []) : [];
$live = true;
$lines = $raw;
break;
}
// No active sync — use most recent last.log
if (!$live) {
$lastLogs = glob("$lockDir/rsync_*.last.log") ?: [];
if ($lastLogs) {
usort($lastLogs, fn($a, $b) => filemtime($b) <=> filemtime($a));
$lastLog = $lastLogs[0];
$profile = preg_replace('/^rsync_(.+)\.last\.log$/', '$1', basename($lastLog));
$lines = file($lastLog, FILE_IGNORE_NEW_LINES) ?: [];
}
}
// Strip ANSI escape codes, keep last 200 lines
$lines = array_map(fn($l) => preg_replace('/\x1b\[[0-9;]*[mGKHF]/', '', $l), $lines);
$lines = array_slice($lines, -200);
echo json_encode(['ok' => true, 'live' => $live, 'profile' => $profile, 'elapsed' => $elapsed, 'lines' => array_values($lines)]);
exit;
}
$base = vv_rsync_status();
$vars = vv_conf_vars();