Rollback: restore a server's world from any of its backups, with a Current marker
Manage gets a Backup section (between World and Logs) listing the server's automatic and manual backups. Rolling back is only allowed while the server is stopped and nothing else, such as an automatic backup, is running on it; the server claims the busy flag atomically so the two can't race. A run players were on is backed up first. The new world is extracted to a staging folder and checked before the old one is deleted. The backup that holds the world as it is now is marked Current (its button is disabled, and the Backups page tags it). It's tracked in backups/<game>/<container>.current: set by automatic backups and rollbacks, none while players have been on, newest automatic backup when there's no record. Rolling back to an automatic backup can keep the Current backup as a manual one (or make one if nothing is current), warning when that pushes out the oldest of 5 manual backups. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
87b92f8332
commit
51db28f3d5
5 changed files with 429 additions and 12 deletions
|
|
@ -240,6 +240,25 @@ function applyManageGuard() {
|
|||
btn.disabled = !!why;
|
||||
$(`#${id}-tip`).title = why;
|
||||
}
|
||||
const why = rollbackBlockReason();
|
||||
for (const btn of document.querySelectorAll('#rollback-list .rollback-btn')) {
|
||||
btn.disabled = rollingBack || !!why;
|
||||
btn.parentElement.title = rollingBack ? '' : why;
|
||||
}
|
||||
if ($('#rollback-dialog').open && !rollingBack) {
|
||||
$('#rollback-confirm').disabled = !!why;
|
||||
$('#rollback-blocked').textContent = why;
|
||||
$('#rollback-blocked').classList.toggle('hidden', !why);
|
||||
}
|
||||
// An automatic backup (or another job) just finished: show the new list.
|
||||
const s = servers.find((x) => x.id === managed?.id);
|
||||
const busyNow = !!(s?.busy || busy.has(managed?.id));
|
||||
// Players joining (the world moves past every backup) changes which backup is current too.
|
||||
const pendingNow = !!s?.backup_pending;
|
||||
if (((managedWasBusy && !busyNow) || pendingNow !== managedWasPending) && !rollingBack
|
||||
&& !$('#rollback-section').classList.contains('hidden')) loadRollbackList();
|
||||
managedWasBusy = busyNow;
|
||||
managedWasPending = pendingNow;
|
||||
}
|
||||
|
||||
// Official Steam library art, stored in static/art/ (<game>-poster.jpg 300x450, <game>-header.jpg 460x215).
|
||||
|
|
@ -348,11 +367,16 @@ async function openManage(s) {
|
|||
$('#settings-error').textContent = '';
|
||||
$('#delete-data').checked = false;
|
||||
$('#autostop-error').textContent = '';
|
||||
$('#rollback-section').classList.add('hidden');
|
||||
$('#rollback-list').innerHTML = '';
|
||||
$('#rollback-messages').innerHTML = '';
|
||||
renderPlayers();
|
||||
$('#settings-fields').innerHTML = '<p class="muted small">Loading…</p>';
|
||||
$('#manage-dialog').showModal();
|
||||
try {
|
||||
const { fields, values, backups, mods, auto_stop } = await api(`/api/servers/${s.id}/settings`);
|
||||
$('#rollback-section').classList.toggle('hidden', !backups);
|
||||
if (backups) loadRollbackList();
|
||||
$('#autostop-enabled').checked = auto_stop.enabled;
|
||||
$('#autostop-minutes').value = auto_stop.minutes;
|
||||
$('#mods-section').classList.toggle('hidden', !mods);
|
||||
|
|
@ -428,6 +452,151 @@ $('#world-download').addEventListener('click', async (e) => {
|
|||
delete a.dataset.busy; a.textContent = 'Download';
|
||||
});
|
||||
|
||||
// ---------- rollback (manage dialog) ----------
|
||||
|
||||
let serverBackups = { auto: [], manual: [], keep: 5 }; // the managed server's backups, newest first
|
||||
let managedWasBusy = false;
|
||||
let managedWasPending = false;
|
||||
let currentBackupId = null; // the backup holding the managed server's world as it is now
|
||||
let rollbackTarget = null;
|
||||
let rollingBack = false;
|
||||
|
||||
// Rolling back is only allowed while the server is stopped and nothing else (an automatic backup,
|
||||
// a start, a delete...) is running on it; the server checks this too.
|
||||
function rollbackBlockReason() {
|
||||
const s = servers.find((x) => x.id === managed?.id) || managed;
|
||||
if (!s) return '';
|
||||
if (s.busy) return `Rolling back is disabled while this runs: ${s.busy}`;
|
||||
if (busy.has(s.id)) return 'Rolling back is disabled while another action on this server finishes.';
|
||||
if (!['exited', 'created'].includes(s.state)) return 'Stop the server before rolling back.';
|
||||
return '';
|
||||
}
|
||||
|
||||
const fmtWhen = (iso) => (iso ? new Date(iso).toLocaleString() : 'unknown time');
|
||||
|
||||
async function loadRollbackList() {
|
||||
const s = managed;
|
||||
let data;
|
||||
try { data = await api('/api/backups'); } catch (err) {
|
||||
$('#rollback-list').innerHTML = `<p class="error">${esc(err.message)}</p>`;
|
||||
return;
|
||||
}
|
||||
if (managed !== s) return;
|
||||
const mine = data.backups.filter((b) => b.container === s.container);
|
||||
serverBackups = { auto: mine.filter((b) => b.type === 'auto'), manual: mine.filter((b) => b.type === 'manual'), keep: data.keep };
|
||||
currentBackupId = data.current?.[s.container] || null;
|
||||
if (!mine.length) {
|
||||
$('#rollback-list').innerHTML = '<p class="muted small" style="margin:0">This server has no backups yet. See <b>About Backups</b> on the Backups page for when they are made.</p>';
|
||||
return;
|
||||
}
|
||||
$('#rollback-list').innerHTML = BACKUP_KINDS.map(([kind, label]) => {
|
||||
const rows = serverBackups[kind];
|
||||
if (!rows.length) return '';
|
||||
return `<div class="backup-kind">
|
||||
<h4>${label} <span class="muted small">${rows.length} of ${data.keep} kept</span></h4>
|
||||
<table class="backups-table">
|
||||
<tr><th>Created</th><th>Session notes</th><th></th></tr>
|
||||
${rows.map((b) => `<tr>
|
||||
<td>${esc(fmtWhen(b.created))}</td>
|
||||
<td class="muted">${esc(sessionNotes(b))}</td>
|
||||
<td class="row-end">${b.id === currentBackupId
|
||||
? '<span class="tip" title="The server\'s world is this backup, so rolling back to it would change nothing."><button class="small" disabled>Current</button></span>'
|
||||
: `<span class="tip"><button class="small rollback-btn" data-id="${esc(b.id)}">Roll back</button></span>`}</td>
|
||||
</tr>`).join('')}
|
||||
</table>
|
||||
</div>`;
|
||||
}).join('');
|
||||
applyManageGuard();
|
||||
}
|
||||
|
||||
$('#rollback-list').addEventListener('click', (e) => {
|
||||
const btn = e.target.closest('.rollback-btn');
|
||||
if (!btn) return;
|
||||
const b = [...serverBackups.auto, ...serverBackups.manual].find((x) => x.id === btn.dataset.id);
|
||||
if (b) openRollback(b);
|
||||
});
|
||||
|
||||
function openRollback(b) {
|
||||
rollbackTarget = b;
|
||||
const s = servers.find((x) => x.id === managed.id) || managed;
|
||||
const backupPending = !!s.backup_pending; // players were on since the last automatic backup
|
||||
const kindLabel = b.type === 'auto' ? 'automatic' : 'manual';
|
||||
$('#rollback-title').textContent = `Roll back ${s.server_name}?`;
|
||||
$('#rollback-what').innerHTML = `The current world is replaced with the ${kindLabel} backup from <b>${esc(fmtWhen(b.created))}</b>`
|
||||
+ ` <span class="muted">(${esc(sessionNotes(b))})</span>.`;
|
||||
|
||||
// Auto: offer to keep the world being rolled back from as a manual backup. That's the current
|
||||
// backup if it's automatic, the backup of the run players were just on, or a fresh manual backup
|
||||
// when no backup holds it. A manual current backup is kept already.
|
||||
const cur = [...serverBackups.auto, ...serverBackups.manual].find((x) => x.id === currentBackupId);
|
||||
const alreadyKept = b.type === 'auto' && !backupPending && cur?.type === 'manual';
|
||||
const canPromote = b.type === 'auto' && !alreadyKept;
|
||||
$('#rollback-promote').classList.toggle('hidden', !canPromote);
|
||||
const keptNote = $('#rollback-kept-note');
|
||||
keptNote.classList.toggle('hidden', !alreadyKept);
|
||||
if (alreadyKept) {
|
||||
keptNote.textContent = `The world you're rolling back from is already kept as the manual backup from ${fmtWhen(cur.created)}.`;
|
||||
}
|
||||
if (canPromote) {
|
||||
$('#rollback-promote-check').checked = true;
|
||||
const keep = ', so you can return to the point you rolled back from without newer automatic backups pushing it out.';
|
||||
$('#rollback-promote-help').textContent = backupPending
|
||||
? `Players were on since the last automatic backup, so the current world is backed up first and that backup is moved to the manual backups${keep}`
|
||||
: cur
|
||||
? `The current world's backup (automatic, from ${fmtWhen(cur.created)}) is moved to the manual backups${keep}`
|
||||
: `No backup holds the current world, so a manual backup of it is made${keep}`;
|
||||
const manual = serverBackups.manual;
|
||||
const full = manual.length >= serverBackups.keep;
|
||||
$('#rollback-promote-full').classList.toggle('hidden', !full);
|
||||
if (full) {
|
||||
$('#rollback-promote-full').textContent = `There are already ${manual.length} manual backups, so this pushes the`
|
||||
+ ` oldest one (from ${fmtWhen(manual[manual.length - 1].created)}) out of the list.`;
|
||||
}
|
||||
}
|
||||
$('#rollback-running').textContent = 'The server stays stopped; start it when you\'re ready. '
|
||||
+ (backupPending && !canPromote ? 'Players were on since the last automatic backup, so the current world is backed up automatically first.' : '');
|
||||
$('#rollback-confirm').textContent = 'Roll back';
|
||||
$('#rollback-error').textContent = '';
|
||||
applyManageGuard(); // sets the confirm button and the reason it's disabled, if any
|
||||
$('#rollback-dialog').showModal();
|
||||
}
|
||||
|
||||
$('#rollback-cancel').addEventListener('click', () => { if (!rollingBack) $('#rollback-dialog').close(); });
|
||||
$('#rollback-dialog').addEventListener('cancel', (e) => { if (rollingBack) e.preventDefault(); });
|
||||
|
||||
$('#rollback-confirm').addEventListener('click', async () => {
|
||||
const b = rollbackTarget;
|
||||
const promote = !$('#rollback-promote').classList.contains('hidden') && $('#rollback-promote-check').checked;
|
||||
const btn = $('#rollback-confirm');
|
||||
btn.disabled = true; btn.textContent = 'Rolling back…';
|
||||
$('#rollback-cancel').disabled = true;
|
||||
$('#rollback-error').textContent = '';
|
||||
$('#rollback-blocked').classList.add('hidden');
|
||||
rollingBack = true;
|
||||
const id = managed.id;
|
||||
busy.add(id); renderServers(); applyManageGuard();
|
||||
try {
|
||||
const res = await api(`/api/servers/${id}/rollback`, {
|
||||
method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id: b.id, promote }),
|
||||
});
|
||||
managed = res.server; // a Project Zomboid server with pending changes is recreated, so its id can change
|
||||
$('#rollback-dialog').close();
|
||||
let msg = `Rolled back to the ${b.type === 'auto' ? 'automatic' : 'manual'} backup from ${fmtWhen(b.created)}.`;
|
||||
if (res.promoted) msg += ` The world you rolled back from (backup from ${fmtWhen(res.promoted.created)}) is now a manual backup.`;
|
||||
$('#rollback-messages').innerHTML = `<div class="msg msg-success">${esc(msg)}</div>`;
|
||||
} catch (err) {
|
||||
$('#rollback-error').textContent = err.message;
|
||||
btn.textContent = 'Roll back';
|
||||
btn.disabled = false;
|
||||
} finally {
|
||||
busy.delete(id);
|
||||
rollingBack = false;
|
||||
$('#rollback-cancel').disabled = false;
|
||||
loadRollbackList();
|
||||
refresh();
|
||||
}
|
||||
});
|
||||
|
||||
$('#manage-logs').addEventListener('click', () => openLogs(managed));
|
||||
$('#open-mods').addEventListener('click', () => openMods(managed));
|
||||
|
||||
|
|
@ -498,7 +667,9 @@ const BACKUP_KINDS = [['auto', 'Automatic'], ['manual', 'Manual']];
|
|||
|
||||
function sessionNotes(b) {
|
||||
const sn = b.session;
|
||||
if (!sn) return b.type === 'manual' && 'session' in b ? 'Server was stopped' : '–';
|
||||
const promoted = b.promoted ? 'Kept from a rollback' : '';
|
||||
if (!sn) return promoted || (b.type === 'manual' && 'session' in b ? 'Server was stopped' : '–');
|
||||
if (promoted) return `${promoted} · ${fmtDuration(sn.length)} session · peak ${sn.peak} player${sn.peak === 1 ? '' : 's'}`;
|
||||
const peak = `peak ${sn.peak} player${sn.peak === 1 ? '' : 's'}`;
|
||||
return sn.running ? `${fmtDuration(sn.length)} into a running session · ${peak}` : `${fmtDuration(sn.length)} session · ${peak}`;
|
||||
}
|
||||
|
|
@ -506,7 +677,7 @@ function sessionNotes(b) {
|
|||
async function loadBackups() {
|
||||
let data;
|
||||
try { data = await api('/api/backups'); } catch (e) { return; }
|
||||
const { backups, keep } = data;
|
||||
const { backups, keep, current } = data;
|
||||
// One card per server, ordered by its newest backup (the list arrives newest first).
|
||||
const byServer = new Map();
|
||||
for (const b of backups) {
|
||||
|
|
@ -525,7 +696,8 @@ async function loadBackups() {
|
|||
<table class="backups-table">
|
||||
<tr><th>Created</th><th>Session notes</th><th>Size</th><th></th></tr>
|
||||
${rows.map((b) => `<tr>
|
||||
<td>${b.created ? esc(new Date(b.created).toLocaleString()) : ''}</td>
|
||||
<td>${b.created ? esc(new Date(b.created).toLocaleString()) : ''}${current?.[b.container] === b.id
|
||||
? ' <span class="current-tag" title="Holds the server\'s world as it is now">Current</span>' : ''}</td>
|
||||
<td class="muted">${esc(sessionNotes(b))}</td>
|
||||
<td>${fmtBytes(b.size)}</td>
|
||||
<td class="row-end"><a class="button ghost small" href="/api/backups/${b.id.split('/').map(encodeURIComponent).join('/')}" download>Download</a></td>
|
||||
|
|
|
|||
|
|
@ -97,6 +97,17 @@
|
|||
<li>While the server is running this is its last autosave (Valheim saves about every 30 minutes). Stop the
|
||||
server first for an up-to-the-minute copy.</li>
|
||||
</ul>
|
||||
<h3>Rolling back</h3>
|
||||
<ul class="about-list">
|
||||
<li>Use the <b>Backup</b> section of a server's Manage panel to replace its world with one of its backups.
|
||||
The server must be stopped, and rolling back waits until any automatic backup in progress has finished.</li>
|
||||
<li>If players were on since the last automatic backup, that world is backed up automatically first.</li>
|
||||
<li>Rolling back to an <b>automatic</b> backup can keep the world you roll back from (the <b>Current</b> backup)
|
||||
as a manual backup, so you can return to it later without newer automatic backups pushing it out.</li>
|
||||
<li>The backup marked <b>Current</b> holds the server's world as it is now, so it can't be rolled back to. That's
|
||||
the newest automatic backup, or the backup last rolled back to. Once players join, the world moves past every
|
||||
backup and nothing is marked Current until the next automatic backup.</li>
|
||||
</ul>
|
||||
<h3>Retention</h3>
|
||||
<ul class="about-list">
|
||||
<li>The newest <b>5 automatic</b> and newest <b>5 manual</b> backups are kept for each server. Older ones are
|
||||
|
|
@ -237,6 +248,14 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<div class="manage-section hidden" id="rollback-section">
|
||||
<h3>Backup</h3>
|
||||
<p class="muted small" style="margin-top:0">Roll the world back to one of this server's automatic or manual backups.
|
||||
Stop the server first: rolling back is only available while it is stopped and no backup is being made.</p>
|
||||
<div id="rollback-list"></div>
|
||||
<div id="rollback-messages" class="msgs"></div>
|
||||
</div>
|
||||
|
||||
<div class="manage-section">
|
||||
<h3>Logs</h3>
|
||||
<div class="row-between"><span class="muted small">Live, color-coded output from the game server.</span><button class="ghost" id="manage-logs">View logs</button></div>
|
||||
|
|
@ -249,6 +268,26 @@
|
|||
</div>
|
||||
</dialog>
|
||||
|
||||
<!-- Rollback confirmation -->
|
||||
<dialog id="rollback-dialog">
|
||||
<h2 style="margin-top:0" id="rollback-title">Roll back?</h2>
|
||||
<p id="rollback-what"></p>
|
||||
<div id="rollback-promote" class="hidden">
|
||||
<label class="radio"><input type="checkbox" id="rollback-promote-check" checked> Keep the world I'm rolling back from as a manual backup</label>
|
||||
<p class="muted small" id="rollback-promote-help"></p>
|
||||
<p class="msg msg-warning hidden" id="rollback-promote-full"></p>
|
||||
</div>
|
||||
<p class="msg msg-info hidden" id="rollback-kept-note"></p>
|
||||
<p class="muted small" id="rollback-running"></p>
|
||||
<p class="msg msg-warning hidden" id="rollback-blocked"></p>
|
||||
<p class="error" id="rollback-error"></p>
|
||||
<div class="wiz-actions">
|
||||
<button type="button" class="ghost" id="rollback-cancel">Cancel</button>
|
||||
<span class="spacer"></span>
|
||||
<button type="button" class="danger" id="rollback-confirm">Roll back</button>
|
||||
</div>
|
||||
</dialog>
|
||||
|
||||
<!-- Mod manager (Project Zomboid) -->
|
||||
<dialog id="mods-dialog" class="wide">
|
||||
<div class="logs-head">
|
||||
|
|
|
|||
|
|
@ -69,6 +69,8 @@ header { display: flex; justify-content: space-between; align-items: center; gap
|
|||
.backups-table th { text-align: left; color: var(--muted); font-weight: normal; padding: 0 8px 6px 0; }
|
||||
.backups-table td { padding: 6px 8px 6px 0; border-top: 1px solid var(--border); vertical-align: middle; }
|
||||
.backups-table a.button.small { padding: 4px 10px; font-size: 13px; }
|
||||
.current-tag { display: inline-block; margin-left: 6px; padding: 1px 7px; border-radius: 999px; font-size: 11px;
|
||||
color: var(--ok); border: 1px solid currentColor; vertical-align: 1px; }
|
||||
#about-backups h3 { font-size: 15px; margin: 16px 0 6px; }
|
||||
.about-list { margin: 0; padding-left: 20px; font-size: 14px; }
|
||||
.about-list li { margin: 4px 0; }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue