A modern, self-hosted backup solution for secure, encrypted backups across local and cloud storage.
import { BackupProgressData } from '../@types/backups';
const registeredBackupMessages: Record<string, string> = {};
const registeredRestoreMessages: Record<string, string> = {};
const registeredPhaseNames: Record<string, string> = {};
export const registerBackupActionMessages = (messages: Record<string, string>): void => {
Object.assign(registeredBackupMessages, messages);
};
export const registerRestoreActionMessages = (messages: Record<string, string>): void => {
Object.assign(registeredRestoreMessages, messages);
};
export const registerPhaseDisplayNames = (names: Record<string, string>): void => {
Object.assign(registeredPhaseNames, names);
};
// Actions that the event list shows as warnings, not as errors
const warningActions = new Set(['BACKUP_WARNING', 'RESTORE_WARNING', 'POST_BACKUP_PRUNE_FAILED', 'POST_BACKUP_REPO_STATS_FAILED']);
export const registerWarningActions = (actions: string[]): void => {
actions.forEach((action) => warningActions.add(action));
};
export const isWarningAction = (action: string): boolean => warningActions.has(action);
// Turn an unmapped action code (e.g. SOME_ACTION_START) into readable text
const humanizeAction = (action: string): string =>
action
.toLowerCase()
.split('_')
.filter(Boolean)
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ') || action;
export function generateBackupProgressMessage(progressData: BackupProgressData | null): string {
if (!progressData?.events?.length) {
return 'Initializing...';
}
// Find the last incomplete event or the most recent event
const lastIncompleteEvent = progressData.events
.slice()
.reverse()
.find((event) => !event.completed);
const currentEvent = lastIncompleteEvent || progressData.events[progressData.events.length - 1];
if (!currentEvent) {
return 'Initializing...';
}
const { phase, action } = currentEvent;
// Map phases to display names
const phaseDisplayNames: Record<string, string> = {
initializing: 'Initializing',
'pre-backup': 'Pre-Backup',
backup: 'Backup',
'post-backup': 'Post-Backup',
replicating: 'Replicating',
finished: 'Complete',
};
const phaseDisplay = phaseDisplayNames[phase] || registeredPhaseNames[phase] || phase;
const actionDisplay = getBackupEventActionMessage(action);
if (phase === 'finished') {
return actionDisplay;
}
return `${phaseDisplay}: ${actionDisplay}`;
}
// Helper function to handle script actions dynamically
const handleScriptAction = (action: string): string | null => {
const scriptTypes = {
ONBACKUPSTART: 'Start Script',
ONBACKUPCOMPLETE: 'Complete Script',
ONBACKUPERROR: 'Error Script',
ONBACKUPFAILURE: 'Failure Script',
ONBACKUPEND: 'End Script',
};
const actionStates = {
START: (scriptType: string, num: string) => `Running ${scriptType} ${num}...`,
COMPLETE: (_: string, num: string) => `Script ${num} Complete`,
FAIL: (_: string, num: string) => `Script ${num} Failed`,
ERROR: (scriptType: string, num: string) => `Error in ${scriptType} ${num}`,
};
// Generic pattern for all script actions: SCRIPTTYPE_SCRIPT_NUMBER_STATE
const scriptMatch = action.match(/^(ONBACKUP\w+)_SCRIPT_(\d+)_(START|COMPLETE|FAIL|ERROR)$/);
if (scriptMatch) {
const [, scriptTypeKey, scriptNum, state] = scriptMatch;
const scriptType = scriptTypes[scriptTypeKey as keyof typeof scriptTypes] || 'Script';
const stateHandler = actionStates[state as keyof typeof actionStates];
if (stateHandler) {
return stateHandler(scriptType, scriptNum);
}
}
return null;
};
// Function to extract numbers from action strings and create meaningful messages
export const getBackupEventActionMessage = (action: string, storageName?: string): string => {
// Handle script actions first
const scriptMessage = handleScriptAction(action);
if (scriptMessage) {
return scriptMessage;
}
// Handle retry attempts
const retryAttemptMatch = action.match(/RETRY_ATTEMPT_(\d+)_OF_(\d+)_START/);
if (retryAttemptMatch) {
const [, current, total] = retryAttemptMatch;
return `Retrying (${current}/${total})...`;
}
// Handle backup retry scheduling
const backupRetryMatch = action.match(/BACKUP_RETRY_(\d+)_OF_(\d+)_SCHEDULED/);
if (backupRetryMatch) {
const [, current, total] = backupRetryMatch;
return `Scheduling Retry (${parseInt(current)}/${total})...`;
}
// Handle replication retry scheduling
const replicationRetryScheduledMatch = action.match(/REPLICATION_RETRY_(\d+)_OF_(\d+)_SCHEDULED/);
if (replicationRetryScheduledMatch) {
const [, current, total] = replicationRetryScheduledMatch;
return `Retrying (${current}/${total})...`;
}
// Handle replication retry start
const replicationRetryStartMatch = action.match(/REPLICATION_RETRY_(\d+)_OF_(\d+)_START/);
if (replicationRetryStartMatch) {
const [, current, total] = replicationRetryStartMatch;
return `Retrying (${current}/${total})...`;
}
// Static action mappings
const staticMessages: Record<string, string> = {
INITIALIZE: 'Starting Backup...',
PRE_BACKUP_START: 'Preparing Backup...',
PRE_BACKUP_DRY_RUN_START: 'Performing Dry Run...',
PRE_BACKUP_DRY_RUN_COMPLETE: 'Dry Run Complete',
PRE_BACKUP_DRY_RUN_ERROR: 'Dry Run Encountered Errors',
PRE_BACKUP_CHECKS_START: 'Running Checks...',
PRE_BACKUP_CHECKS_COMPLETE: 'Checks Complete',
PRE_BACKUP_SCRIPTS_START: 'Running Scripts...',
PRE_BACKUP_SCRIPTS_COMPLETE: 'Scripts Complete',
PRE_BACKUP_UNLOCK_STALE_LOCKS: 'Unlocking Repository...',
PRE_BACKUP_COMPLETE: 'Pre-Backup Complete',
BACKUP_OPERATION_START: 'Backing Up Files...',
BACKUP_OPERATION_COMPLETE: 'Backup Complete',
BACKUP_OPERATION_ERROR: 'Backup Encountered Errors',
POST_BACKUP_START: 'Finalizing...',
POST_BACKUP_SCRIPTS_START: 'Running Cleanup Scripts...',
POST_BACKUP_SCRIPTS_COMPLETE: 'Cleanup Scripts Complete',
POST_BACKUP_PRUNE_START: 'Pruning Old Backups...',
POST_BACKUP_PRUNE_COMPLETE: 'Pruning Complete',
POST_BACKUP_PRUNE_FAILED: 'Pruning Failed',
POST_BACKUP_COMPLETE: 'Post-Backup Complete',
POST_BACKUP_REPO_STATS_START: 'Updating Repository Statistics...',
POST_BACKUP_REPO_STATS_COMPLETE: 'Repository Statistics Updated',
POST_BACKUP_REPO_STATS_FAILED: 'Failed to Update Repository Statistics',
TASK_COMPLETED: 'Completed Successfully',
TASK_CANCELLED: 'Backup Cancelled by User.',
TASK_FAILED: 'Backup Failed with Error.',
FAILED_PERMANENTLY: 'Failed Permanently',
ISO_CREATION_START: 'Creating ISO...',
ISO_CREATION_COMPLETE: 'ISO Created Successfully',
ISO_CREATION_FAILED: 'ISO Creation Failed',
REMOTE_BACKUP_START: 'Backing Up Data...',
REMOTE_BACKUP_COMPLETE: 'Data Backup Complete',
REMOTE_BACKUP_FAILED: 'Data Backup Failed',
ISO_ENCRYPTION_START: 'Encrypting ISO...',
ISO_ENCRYPTION_COMPLETE: 'ISO Encryption Complete',
ISO_UPLOAD_START: 'Uploading ISO...',
ISO_UPLOAD_COMPLETE: 'ISO Upload Complete',
ISO_UPLOAD_FAILED: 'ISO Upload Failed',
BACKUP_WARNING: 'Hiccup Detected During Backup',
// Replication actions
REPLICATION_START: 'Replicating Snapshots...',
REPLICATION_INIT_START: 'Initializing Replication Repository...',
REPLICATION_INIT_COMPLETE: 'Replication Repository Initialized',
REPLICATION_INIT_FAILED: 'Replication Repository Init Failed',
REPLICATION_UNLOCK_START: 'Unlocking Replication Repository...',
REPLICATION_UNLOCK_COMPLETE: 'Replication Repository Unlocked',
REPLICATION_COPY_START: 'Copying Snapshot to Replication...',
REPLICATION_COPY_COMPLETE: 'Snapshot Copied to Replication',
REPLICATION_COPY_FAILED: 'Replication Copy Failed',
REPLICATION_PRUNE_START: 'Pruning Replication Repository...',
REPLICATION_PRUNE_COMPLETE: 'Replication Prune Complete',
REPLICATION_PRUNE_FAILED: 'Replication Prune Failed',
REPLICATION_STATS_START: 'Updating Replication Stats...',
REPLICATION_STATS_COMPLETE: 'Replication Stats Updated',
REPLICATION_COMPLETE: 'Replication Complete',
REPLICATION_FAILED: 'Replication Failed',
REPLICATION_PARTIAL_FAILURE: 'Some Replications Failed',
REPLICATION_MANUAL_RETRY_START: 'Retrying Failed Replications...',
REPLICATION_MANUAL_RETRY_COMPLETE: 'Replication Retry Complete',
REPLICATION_MANUAL_RETRY_PARTIAL_FAILURE: 'Some Replication Retries Still Failed',
};
// Storage-name-aware replication action mappings
const replicationMessagesWithStorage: Record<string, (name: string) => string> = {
REPLICATION_START: (name) => `Replicating Snapshot to ${name}...`,
REPLICATION_INIT_START: (name) => `Initializing Replication Repository in ${name}...`,
REPLICATION_INIT_COMPLETE: (name) => `Replication Repository Initialized in ${name}`,
REPLICATION_INIT_FAILED: (name) => `Replication Repository Init Failed in ${name}`,
REPLICATION_UNLOCK_START: (name) => `Unlocking Replication Repository in ${name}...`,
REPLICATION_UNLOCK_COMPLETE: (name) => `Replication Repository Unlocked in ${name}`,
REPLICATION_COPY_START: (name) => `Copying Snapshot to ${name}...`,
REPLICATION_COPY_COMPLETE: (name) => `Snapshot Copied to ${name}`,
REPLICATION_COPY_FAILED: (name) => `Replication Copy Failed for ${name}`,
REPLICATION_PRUNE_START: (name) => `Pruning Replication Repository in ${name}...`,
REPLICATION_PRUNE_COMPLETE: (name) => `Replication Prune Complete for ${name}`,
REPLICATION_PRUNE_FAILED: (name) => `Replication Prune Failed for ${name}`,
REPLICATION_STATS_START: (name) => `Updating Replication Stats for ${name}...`,
REPLICATION_STATS_COMPLETE: (name) => `Replication Stats Updated for ${name}`,
REPLICATION_COMPLETE: (name) => `Replication Complete for ${name}`,
REPLICATION_FAILED: (name) => `Replication Failed for ${name}`,
};
if (storageName && replicationMessagesWithStorage[action]) {
return replicationMessagesWithStorage[action](storageName);
}
return staticMessages[action] || registeredBackupMessages[action] || humanizeAction(action);
};
export function generateRestoreProgressMessage(progressData: BackupProgressData | null): string {
if (!progressData?.events?.length) {
return 'Initializing...';
}
// Find the last incomplete event or the most recent event
const lastIncompleteEvent = progressData.events
.slice()
.reverse()
.find((event) => !event.completed);
const currentEvent = lastIncompleteEvent || progressData.events[progressData.events.length - 1];
if (!currentEvent) {
return 'Initializing...';
}
const { phase, action } = currentEvent;
// Map phases to display names
const phaseDisplayNames: Record<string, string> = {
initializing: 'Initializing',
'pre-restore': 'Pre-Restore',
restore: 'Restore',
'post-restore': 'Post-Restore',
retry: 'Retrying',
finished: 'Complete',
};
const phaseDisplay = phaseDisplayNames[phase] || registeredPhaseNames[phase] || phase;
const actionDisplay = getRestoreEventActionMessage(action);
if (phase === 'finished') {
return actionDisplay;
}
return `${phaseDisplay}: ${actionDisplay}`;
}
export const getRestoreEventActionMessage = (action: string): string => {
// Handle script actions first
const scriptMessage = handleScriptAction(action);
if (scriptMessage) {
return scriptMessage;
}
// Handle retry attempts
const retryAttemptMatch = action.match(/RETRY_ATTEMPT_(\d+)_OF_(\d+)_START/);
if (retryAttemptMatch) {
const [, current, total] = retryAttemptMatch;
return `Retrying (${current}/${total})...`;
}
// Handle backup retry scheduling
const backupRetryMatch = action.match(/RESTORE_RETRY_(\d+)_OF_(\d+)_SCHEDULED/);
if (backupRetryMatch) {
const [, current, total] = backupRetryMatch;
return `Scheduling Retry (${parseInt(current)}/${total})...`;
}
// Static action mappings
const staticMessages: Record<string, string> = {
INITIALIZE: 'Starting Restore...',
PRE_RESTORE_START: 'Preparing Restore...',
PRE_RESTORE_GET_SNAPSHOT: 'Getting Snapshot to Restore...',
PRE_RESTORE_GET_SNAPSHOT_COMPLETE: 'Snapshot Retrieved',
PRE_RESTORE_GET_SNAPSHOT_FAILED: 'Failed to Retrieve Snapshot',
PRE_RESTORE_DRY_RUN_START: 'Performing Dry Run...',
PRE_RESTORE_DRY_RUN_COMPLETE: 'Dry Run Complete',
PRE_RESTORE_CHECKS_START: 'Running Checks...',
PRE_RESTORE_CHECKS_COMPLETE: 'Checks Complete',
PRE_RESTORE_UNLOCK_STALE_LOCKS: 'Unlocking Repository...',
PRE_RESTORE_COMPLETE: 'Pre-Restore Complete',
RESTORE_OPERATION_START: 'Restoring Files...',
RESTORE_OPERATION_COMPLETE: 'Restore Complete',
POST_RESTORE_START: 'Finalizing...',
POST_RESTORE_COMPLETE: 'Post-Restore Complete',
POST_RESTORE_REPO_STATS_START: 'Updating Repository Statistics...',
POST_RESTORE_REPO_STATS_COMPLETE: 'Repository Statistics Updated',
POST_RESTORE_WINDOWS_MOVE_START: 'Moving Restored files from temp directory to target path...',
POST_RESTORE_WINDOWS_MOVE_ERROR: 'Failed to Move Restored files from temp directory to target path',
POST_RESTORE_WINDOWS_MOVE_COMPLETE: 'Moved Restored files to target path',
TASK_COMPLETED: 'Completed Successfully',
TASK_FAILED: 'Restore Failed with Error.',
TASK_CANCELLED: 'Restore Cancelled by User.',
FAILED_PERMANENTLY: 'Failed Permanently',
};
return staticMessages[action] || registeredRestoreMessages[action] || humanizeAction(action);
};
export function extractResticData(progressData: BackupProgressData | null) {
if (!progressData?.events?.length) {
return null;
}
// Find the most recent event with resticData
const eventWithResticData = progressData.events
.slice()
.reverse()
.find((event) => event.resticData);
return eventWithResticData?.resticData || null;
}
/**
* Generates a human-readable progress message for a specific replication mirror.
* Uses the mirror's latest event action and includes the storage name.
*/
export function generateMirrorProgressMessage(mirror: { storageName: string; events?: { action: string; completed: boolean }[] }): string {
if (!mirror.events?.length) {
return 'Pending...';
}
// Find the last incomplete event or the most recent event
const lastIncompleteEvent = mirror.events
.slice()
.reverse()
.find((event) => !event.completed);
const currentEvent = lastIncompleteEvent || mirror.events[mirror.events.length - 1];
if (!currentEvent) {
return 'Pending...';
}
return getBackupEventActionMessage(currentEvent.action, mirror.storageName);
}