Add LXC container backup functionality

- Add backup capability before updates or as standalone action
- Implement storage service to fetch and parse backup-capable storages from PVE nodes
- Add backup storage selection modal for user choice
- Support backup+update flow with sequential execution
- Add standalone backup option in Actions menu
- Add storage viewer in server section to show available storages
- Parse /etc/pve/storage.cfg to identify backup-capable storages
- Cache storage data for performance
- Handle backup failures gracefully (warn but allow update to proceed)
This commit is contained in:
Michel Roegl-Brunner
2025-11-14 10:30:27 +01:00
parent 4ea49be97d
commit d50ea55e6d
4 changed files with 173 additions and 136 deletions

140
server.js
View File

@@ -705,14 +705,19 @@ class ScriptExecutionHandler {
* @param {string} executionId
* @param {string} storage
* @param {ServerInfo} server
* @param {Function} [onComplete] - Optional callback when backup completes
*/
async startSSHBackupExecution(ws, containerId, executionId, storage, server) {
startSSHBackupExecution(ws, containerId, executionId, storage, server, onComplete = null) {
const sshService = getSSHExecutionService();
return new Promise((resolve, reject) => {
try {
const backupCommand = `vzdump ${containerId} --storage ${storage} --mode snapshot`;
const execution = await sshService.executeCommand(
// Wrap the onExit callback to resolve our promise
let promiseResolved = false;
sshService.executeCommand(
server,
backupCommand,
/** @param {string} data */
@@ -733,42 +738,79 @@ class ScriptExecutionHandler {
},
/** @param {number} code */
(code) => {
if (code === 0) {
this.sendMessage(ws, {
type: 'end',
data: `Backup completed successfully with exit code: ${code}`,
timestamp: Date.now()
});
} else {
// Don't send 'end' message here if this is part of a backup+update flow
// The update flow will handle completion messages
const success = code === 0;
if (!success) {
this.sendMessage(ws, {
type: 'error',
data: `Backup failed with exit code: ${code}`,
timestamp: Date.now()
});
}
// Send a completion message (but not 'end' type to avoid stopping terminal)
this.sendMessage(ws, {
type: 'end',
data: `Backup execution ended with exit code: ${code}`,
type: 'output',
data: `\n[Backup ${success ? 'completed' : 'failed'} with exit code: ${code}]\n`,
timestamp: Date.now()
});
if (onComplete) onComplete(success);
// Resolve the promise when backup completes
// Use setImmediate to ensure resolution happens in the right execution context
if (!promiseResolved) {
promiseResolved = true;
const result = { success, code };
// Use setImmediate to ensure promise resolution happens in the next tick
// This ensures the await in startUpdateExecution can properly resume
setImmediate(() => {
try {
resolve(result);
} catch (resolveError) {
console.error('Error resolving backup promise:', resolveError);
reject(resolveError);
}
});
}
this.activeExecutions.delete(executionId);
}
);
).then((execution) => {
// Store the execution
this.activeExecutions.set(executionId, {
process: /** @type {any} */ (execution).process,
ws
});
} catch (error) {
// Note: Don't resolve here - wait for onExit callback
}).catch((error) => {
console.error('Error starting backup execution:', error);
this.sendMessage(ws, {
type: 'error',
data: `SSH backup execution failed: ${error instanceof Error ? error.message : String(error)}`,
timestamp: Date.now()
});
if (onComplete) onComplete(false);
if (!promiseResolved) {
promiseResolved = true;
reject(error);
}
});
} catch (error) {
console.error('Error in startSSHBackupExecution:', error);
this.sendMessage(ws, {
type: 'error',
data: `SSH backup execution failed: ${error instanceof Error ? error.message : String(error)}`,
timestamp: Date.now()
});
if (onComplete) onComplete(false);
reject(error);
}
});
}
/**
@@ -792,72 +834,48 @@ class ScriptExecutionHandler {
// Create a separate execution ID for backup
const backupExecutionId = `backup_${executionId}`;
let backupCompleted = false;
let backupSucceeded = false;
// Run backup and wait for it to complete
await new Promise<void>((resolve) => {
// Create a wrapper websocket that forwards messages and tracks completion
const backupWs = {
send: (data) => {
try {
const message = typeof data === 'string' ? JSON.parse(data) : data;
const backupResult = await this.startSSHBackupExecution(
ws,
containerId,
backupExecutionId,
backupStorage,
server
);
// Forward all messages to the main websocket
ws.send(JSON.stringify(message));
// Check for completion
if (message.type === 'end') {
backupCompleted = true;
backupSucceeded = !message.data.includes('failed') && !message.data.includes('exit code:');
if (!backupSucceeded) {
// Backup completed (successfully or not)
if (!backupResult || !backupResult.success) {
// Backup failed, but we'll still allow update (per requirement 1b)
ws.send(JSON.stringify({
this.sendMessage(ws, {
type: 'output',
data: '\n⚠ Backup failed, but proceeding with update as requested...\n',
timestamp: Date.now()
}));
}
resolve();
} else if (message.type === 'error' && message.data.includes('Backup failed')) {
backupCompleted = true;
backupSucceeded = false;
ws.send(JSON.stringify({
});
} else {
// Backup succeeded
this.sendMessage(ws, {
type: 'output',
data: '\n⚠️ Backup failed, but proceeding with update as requested...\n',
data: '\n Backup completed successfully. Starting update...\n',
timestamp: Date.now()
}));
resolve();
});
}
} catch (e) {
// If parsing fails, just forward the raw data
ws.send(data);
}
}
};
// Start backup execution
this.startSSHBackupExecution(backupWs, containerId, backupExecutionId, backupStorage, server)
.catch((error) => {
} catch (error) {
console.error('Backup error before update:', error);
// Backup failed to start, but allow update to proceed
if (!backupCompleted) {
backupCompleted = true;
backupSucceeded = false;
ws.send(JSON.stringify({
this.sendMessage(ws, {
type: 'output',
data: `\n⚠️ Backup error: ${error.message}. Proceeding with update...\n`,
data: `\n⚠️ Backup error: ${error instanceof Error ? error.message : String(error)}. Proceeding with update...\n`,
timestamp: Date.now()
}));
resolve();
});
}
});
});
// Small delay before starting update
await new Promise(resolve => setTimeout(resolve, 1000));
}
// Send start message for update
// Send start message for update (only if we're actually starting an update)
this.sendMessage(ws, {
type: 'start',
data: `Starting update for container ${containerId}...`,

View File

@@ -61,6 +61,7 @@ export function InstalledScriptsTab() {
const [backupStorages, setBackupStorages] = useState<Storage[]>([]);
const [isLoadingStorages, setIsLoadingStorages] = useState(false);
const [showBackupWarning, setShowBackupWarning] = useState(false);
const [isPreUpdateBackup, setIsPreUpdateBackup] = useState(false); // Track if storage selection is for pre-update backup
const [editingScriptId, setEditingScriptId] = useState<number | null>(null);
const [editFormData, setEditFormData] = useState<{ script_name: string; container_id: string; web_ui_ip: string; web_ui_port: string }>({ script_name: '', container_id: '', web_ui_ip: '', web_ui_port: '' });
const [showAddForm, setShowAddForm] = useState(false);
@@ -660,6 +661,7 @@ export function InstalledScriptsTab() {
if (wantsBackup) {
// User wants backup - fetch storages and show selection
if (pendingUpdateScript.server_id) {
setIsPreUpdateBackup(true); // Mark that this is for pre-update backup
void fetchStorages(pendingUpdateScript.server_id, false);
setShowStorageSelection(true);
} else {
@@ -682,12 +684,13 @@ export function InstalledScriptsTab() {
setShowStorageSelection(false);
// Check if this is for a standalone backup or pre-update backup
if (pendingUpdateScript && !showBackupPrompt) {
if (isPreUpdateBackup) {
// Pre-update backup - proceed with update
setIsPreUpdateBackup(false); // Reset flag
proceedWithUpdate(storage.name);
} else if (pendingUpdateScript) {
// Standalone backup - execute backup directly
executeStandaloneBackup(pendingUpdateScript, storage.name);
} else {
// Pre-update backup - proceed with update
proceedWithUpdate(storage.name);
}
};
@@ -718,6 +721,7 @@ export function InstalledScriptsTab() {
});
// Reset state
setIsPreUpdateBackup(false); // Reset flag
setPendingUpdateScript(null);
setBackupStorages([]);
};
@@ -745,7 +749,8 @@ export function InstalledScriptsTab() {
id: pendingUpdateScript.id,
containerId: pendingUpdateScript.container_id!,
server: server,
backupStorage: backupStorage ?? undefined
backupStorage: backupStorage ?? undefined,
isBackupOnly: false // Explicitly set to false for update operations
});
// Reset state
@@ -779,6 +784,7 @@ export function InstalledScriptsTab() {
}
// Store the script and fetch storages
setIsPreUpdateBackup(false); // This is a standalone backup, not pre-update
setPendingUpdateScript(script);
void fetchStorages(script.server_id, false);
setShowStorageSelection(true);

View File

@@ -4,7 +4,6 @@ import { getDatabase } from "~/server/database-prisma";
import { createHash } from "crypto";
import type { Server } from "~/types/server";
import { getStorageService } from "~/server/services/storageService";
import { SSHService } from "~/server/ssh-service";
// Helper function to parse raw LXC config into structured data
function parseRawConfig(rawConfig: string): any {
@@ -2063,6 +2062,7 @@ EOFCONFIG`;
}
const storageService = getStorageService();
const { default: SSHService } = await import('~/server/ssh-service');
const sshService = new SSHService();
// Test SSH connection first
@@ -2118,6 +2118,7 @@ EOFCONFIG`;
};
}
const { default: SSHService } = await import('~/server/ssh-service');
const sshService = new SSHService();
// Test SSH connection first

View File

@@ -29,7 +29,12 @@ class StorageService {
let currentStorage: Partial<Storage> | null = null;
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
const rawLine = lines[i];
if (!rawLine) continue;
// Check if line is indented (has leading whitespace/tabs) BEFORE trimming
const isIndented = /^[\s\t]/.test(rawLine);
const line = rawLine.trim();
// Skip empty lines and comments
if (!line || line.startsWith('#')) {
@@ -37,8 +42,10 @@ class StorageService {
}
// Check if this is a storage definition line (format: "type: name")
// Storage definitions are NOT indented
if (!isIndented) {
const storageMatch = line.match(/^(\w+):\s*(.+)$/);
if (storageMatch) {
if (storageMatch && storageMatch[1] && storageMatch[2]) {
// Save previous storage if exists
if (currentStorage && currentStorage.name) {
storages.push(this.finalizeStorage(currentStorage));
@@ -53,13 +60,16 @@ class StorageService {
};
continue;
}
}
// Parse storage properties (indented lines)
if (currentStorage && /^\s/.test(line)) {
const propertyMatch = line.match(/^\s+(\w+)\s+(.+)$/);
if (propertyMatch) {
const key = propertyMatch[1];
const value = propertyMatch[2];
// Parse storage properties (indented lines - can be tabs or spaces)
if (currentStorage && isIndented) {
// Split on first whitespace (space or tab) to separate key and value
const match = line.match(/^(\S+)\s+(.+)$/);
if (match && match[1] && match[2]) {
const key = match[1];
const value = match[2].trim();
switch (key) {
case 'content':
@@ -73,11 +83,13 @@ class StorageService {
break;
default:
// Store other properties
if (key) {
(currentStorage as any)[key] = value;
}
}
}
}
}
// Don't forget the last storage
if (currentStorage && currentStorage.name) {