A modern, self-hosted backup solution for secure, encrypted backups across local and cloud storage.
refactor: http polling agent communication instead of mqtt
towfiqif3f7f4e1c963
diff --git a/backend/src/createApp.ts b/backend/src/createApp.tsindex 545b147..0a8e819 100644--- a/backend/src/createApp.ts+++ b/backend/src/createApp.ts@@ -79,7 +79,7 @@ export async function createApp(): Promise<{ app: Express }> {backupStore,storageStore,deviceStore,- null+ restoreStore);const backupService = new BackupService(localBackupAgent,@@ -87,30 +87,21 @@ export async function createApp(): Promise<{ app: Express }> {planStore,backupStore,restoreStore,- storageStore,- null+ storageStore);const restoreService = new RestoreService(localRestoreAgent,planStore,backupStore,restoreStore,- storageStore,- null- );- const deviceService = new DeviceService(- localSystemAgent,- deviceStore,- planStore,- storageStore,- null+ storageStore);+ const deviceService = new DeviceService(localSystemAgent, deviceStore, planStore, storageStore);const storageService = new StorageService(localStorageAgent,localSystemAgent,storageStore,- planStore,- null+ planStore);const settingsService = new SettingsService(settingsStore);diff --git a/backend/src/services/BackupServices.ts b/backend/src/services/BackupServices.tsindex 9737b80..ad306c6 100644--- a/backend/src/services/BackupServices.ts+++ b/backend/src/services/BackupServices.ts@@ -20,32 +20,26 @@ import { SnapShotFile } from '../types/restic';import { Backup } from '../db/schema/backups';export class BackupService {- protected broker: any;-constructor(protected localSnapshotAgent: BaseSnapshotManager,protected localPlanAgent: BaseBackupManager,protected planStore: PlanStore,protected backupStore: BackupStore,protected restoreStore: RestoreStore,- protected storageStore: StorageStore,-- broker: any- ) {- this.broker = broker;- }+ protected storageStore: StorageStore+ ) {}getSnapshotStrategy(deviceId: string, method?: string): SnapshotStrategy {const isRemote = deviceId !== 'main';return isRemote- ? new RemoteSnapshotStrategy(this.broker, deviceId)+ ? new RemoteSnapshotStrategy(deviceId): new LocalSnapshotStrategy(this.localSnapshotAgent);}getBackupStrategy(deviceId: string): BackupStrategy {const isRemote = deviceId !== 'main';return isRemote- ? new RemoteBackupStrategy(this.broker, deviceId)+ ? new RemoteBackupStrategy(deviceId): new LocalBackupStrategy(this.localPlanAgent);}diff --git a/backend/src/services/DeviceService.ts b/backend/src/services/DeviceService.tsindex f4c79f8..54d5866 100644--- a/backend/src/services/DeviceService.ts+++ b/backend/src/services/DeviceService.ts@@ -9,24 +9,18 @@ import { StorageStore } from '../stores/StorageStore';import { providers } from '../utils/providers';export class DeviceService {- protected broker: any;protected connectedDeviceIds: Set<string> | undefined;constructor(protected localSystemManager: BaseSystemManager,protected deviceStore: DeviceStore,protected planStore: PlanStore,- protected storageStore: StorageStore,- broker?: any- ) {- this.broker = broker;- }+ protected storageStore: StorageStore+ ) {}getSystemStrategy(deviceId: string): SystemStrategy {const isRemote = deviceId !== 'main';- return isRemote- ? new RemoteStrategy(this.broker, deviceId)- : new LocalStrategy(this.localSystemManager);+ return isRemote ? new RemoteStrategy(deviceId) : new LocalStrategy(this.localSystemManager);}/**diff --git a/backend/src/services/PlanService.ts b/backend/src/services/PlanService.tsindex 8cf8b40..f65a0a4 100644--- a/backend/src/services/PlanService.ts+++ b/backend/src/services/PlanService.ts@@ -4,6 +4,7 @@ import { BaseBackupManager } from '../managers/BaseBackupManager';import { PlanStore } from '../stores/PlanStore';import { BackupStore } from '../stores/BackupStore';import { StorageStore } from '../stores/StorageStore';+import { RestoreStore } from '../stores/RestoreStore';import { Plan, NewPlan, planInsertSchema, planUpdateSchema } from '../db/schema/plans';import { BackupPlanArgs, NewPlanReq, PlanLogItem } from '../types/plans';import { planLogger } from '../utils/logger';@@ -24,23 +25,19 @@ import { sanitizeStoragePath } from '../utils/sanitizeStoragePath';* and delegates remote execution to strategies.*/export class PlanService {- protected broker: any;-constructor(protected localAgent: BaseBackupManager,protected planStore: PlanStore,protected backupStore: BackupStore,protected storageStore: StorageStore,protected deviceStore: DeviceStore,- broker: any- ) {- this.broker = broker;- }+ protected restoreStore: RestoreStore+ ) {}getStrategy(plan: NewPlan | Plan): BackupStrategy | any {const isRemote = plan.sourceId !== 'main';return isRemote- ? new RemoteBackupStrategy(this.broker, plan.sourceId)+ ? new RemoteBackupStrategy(plan.sourceId): new LocalBackupStrategy(this.localAgent);}@@ -244,6 +241,7 @@ export class PlanService {}// Finally, remove from our database+ await this.restoreStore.deleteByPlanId(planId);await this.backupStore.deleteByPlanId(planId);const deleteRes = await this.planStore.delete(planId);if (!deleteRes) {diff --git a/backend/src/services/RestoreService.ts b/backend/src/services/RestoreService.tsindex eee8234..c61085a 100644--- a/backend/src/services/RestoreService.ts+++ b/backend/src/services/RestoreService.ts@@ -16,15 +16,12 @@ export class RestoreService {protected planStore: PlanStore,protected backupStore: BackupStore,protected restoreStore: RestoreStore,- protected storageStore: StorageStore,- protected broker: any+ protected storageStore: StorageStore) {}getRestoreStrategy(deviceId: string, method: string): RestoreStrategy {const isRemote = deviceId !== 'main';- return isRemote- ? new RemoteStrategy(this.broker, deviceId)- : new LocalStrategy(this.localAgent);+ return isRemote ? new RemoteStrategy(deviceId) : new LocalStrategy(this.localAgent);}async getAllRestores() {diff --git a/backend/src/services/StorageService.ts b/backend/src/services/StorageService.tsindex 4f0de4c..12dfc4f 100644--- a/backend/src/services/StorageService.ts+++ b/backend/src/services/StorageService.ts@@ -19,21 +19,17 @@ import { AppError, NotFoundError } from '../utils/AppError';* A class for managing storage operations.*/export class StorageService {- protected broker: any;constructor(protected storageManager: BaseStorageManager,protected systemManager: BaseSystemManager,protected storageStore: StorageStore,- protected planStore: PlanStore,- broker?: any- ) {- this.broker = broker;- }+ protected planStore: PlanStore+ ) {}getSystemStrategy(deviceId: string): SystemStrategy {const isRemote = deviceId !== 'main';return isRemote- ? new RemoteSystemStrategy(this.broker, deviceId)+ ? new RemoteSystemStrategy(deviceId): new LocalSystemStrategy(this.systemManager);}diff --git a/backend/src/services/events/DownloadEventService.ts b/backend/src/services/events/DownloadEventService.tsnew file mode 100644index 0000000..21f2501--- /dev/null+++ b/backend/src/services/events/DownloadEventService.ts@@ -0,0 +1,84 @@+import { BackupStore } from '../../stores/BackupStore';+import { planLogger } from '../../utils/logger';+import { DownloadCompleteEvent, DownloadErrorEvent, DownloadStartEvent } from '../../types/events';+import { BaseSnapshotManager } from '../../managers/BaseSnapshotManager';++export class DownloadEventService {+ constructor(+ protected backupStore: BackupStore,+ protected localAgent?: BaseSnapshotManager+ ) {}++ async onDownloadStart(eventPayload: DownloadStartEvent) {+ const { backupId, planId } = eventPayload;+ try {+ console.log('onDownloadStart :', eventPayload);+ const currentTime = Math.floor(new Date().getTime() / 1000);+ await this.backupStore.update(backupId, {+ download: {+ status: 'started',+ started: currentTime,+ },+ });+ planLogger('download', planId, backupId).info(+ `Download generation started for backup ${backupId}`+ );+ } catch (error: any) {+ console.log('[error] onDownloadStart :', error);+ planLogger('download', planId, backupId).error(+ `Failed to handle download generation start for backup ${backupId}: ${error.message}`+ );+ }+ }++ async onDownloadError(eventPayload: DownloadErrorEvent) {+ const { backupId, planId, error } = eventPayload;+ try {+ console.log('onDownloadError :', eventPayload);+ const backup = await this.backupStore.getById(backupId);+ await this.backupStore.update(backupId, {+ download: {+ ...(backup?.download || {}),+ status: 'failed',+ error: error,+ ended: Math.floor(Date.now() / 1000),+ },+ });++ planLogger('download', planId, backupId).error(+ `Failed to complete download generation for backup ${backupId}. Reason: ${error || 'Unknown'}`+ );+ } catch (error: any) {+ console.log('[error] onDownloadError :', error);+ planLogger('download', planId, backupId).error(+ `Failed to handle download generation error for backup ${backupId}: ${error.message}`+ );+ }+ }++ async onDownloadComplete(eventPayload: DownloadCompleteEvent) {+ const { backupId, planId, success } = eventPayload;+ try {+ console.log('onDownloadComplete :', eventPayload);++ const backup = await this.backupStore.getById(backupId);+ await this.backupStore.update(backupId, {+ download: {+ ...(backup?.download || {}),+ status: 'complete',+ error: '',+ ended: Math.floor(Date.now() / 1000),+ },+ });++ planLogger('download', planId, backupId).info(+ `Download generation completed for backup ${backupId}`+ );+ } catch (error: any) {+ console.log('[error] onDownloadComplete :', error);+ planLogger('download', planId, backupId).error(+ `Failed to handle download completion for backup ${backupId}. Reason: ${error?.message || 'Unknown'}`+ );+ }+ }+}diff --git a/backend/src/services/listeners/DownloadEventListener.ts b/backend/src/services/listeners/DownloadEventListener.tsindex 47274b1..c6a065f 100644--- a/backend/src/services/listeners/DownloadEventListener.ts+++ b/backend/src/services/listeners/DownloadEventListener.ts@@ -1,13 +1,16 @@import { BaseSnapshotManager } from '../../managers/BaseSnapshotManager';import { BackupStore } from '../../stores/BackupStore';-import { planLogger } from '../../utils/logger';import { DownloadCompleteEvent, DownloadErrorEvent, DownloadStartEvent } from '../../types/events';+import { DownloadEventService } from '../events/DownloadEventService';export class DownloadEventListener {+ protected downloadEventService: DownloadEventService;+constructor(protected localAgent: BaseSnapshotManager,protected backupStore: BackupStore) {+ this.downloadEventService = new DownloadEventService(backupStore, localAgent);this.registerEventListeners();}@@ -28,25 +31,7 @@ export class DownloadEventListener {}private async onDownloadStart(eventPayload: DownloadStartEvent) {- const { backupId, planId } = eventPayload;- try {- console.log('onDownloadStart :', eventPayload);- const currentTime = Math.floor(new Date().getTime() / 1000);- await this.backupStore.update(backupId, {- download: {- status: 'started',- started: currentTime,- },- });- planLogger('download', planId, backupId).info(- `Download generation started for backup ${backupId}`- );- } catch (error: any) {- console.log('[error] onDownloadStart :', error);- planLogger('download', planId, backupId).error(- `Failed to handle download generation start for backup ${backupId}: ${error.message}`- );- }+ await this.downloadEventService.onDownloadStart(eventPayload);}// private async onDownloadProgress(eventPayload: DownloadProgressEvent) {@@ -61,59 +46,10 @@ export class DownloadEventListener {// }private async onDownloadError(eventPayload: DownloadErrorEvent) {- const { backupId, planId, error } = eventPayload;- try {- console.log('onDownloadError :', eventPayload);- const backup = await this.backupStore.getById(backupId);- await this.backupStore.update(backupId, {- download: {- ...(backup?.download || {}),- status: 'failed',- error: error,- ended: Math.floor(Date.now() / 1000),- },- });-- planLogger('download', planId, backupId).error(- `Failed to complete download generation for backup ${backupId}. Reason: ${error || 'Unknown'}`- );- } catch (error: any) {- console.log('[error] onDownloadError :', error);- planLogger('download', planId, backupId).error(- `Failed to handle download generation error for backup ${backupId}: ${error.message}`- );- }+ await this.downloadEventService.onDownloadError(eventPayload);}private async onDownloadComplete(eventPayload: DownloadCompleteEvent) {- const { backupId, planId, success } = eventPayload;- try {- console.log('onDownloadComplete :', eventPayload);-- const backup = await this.backupStore.getById(backupId);- await this.backupStore.update(backupId, {- download: {- ...(backup?.download || {}),- status: 'complete',- error: '',- ended: Math.floor(Date.now() / 1000),- },- });- // try {- // const progressFile = `${appPaths.getProgressDir()}/download-${backupId}-progress.json`;- // if (existsSync(progressFile)) {- // await unlink(progressFile);- // }- // } catch (error) {}-- planLogger('download', planId, backupId).info(- `Download generation completed for backup ${backupId}`- );- } catch (error: any) {- console.log('[error] onDownloadComplete :', error);- planLogger('download', planId, backupId).error(- `Failed to handle download completion for backup ${backupId}. Reason: ${error?.message || 'Unknown'}`- );- }+ await this.downloadEventService.onDownloadComplete(eventPayload);}}diff --git a/backend/src/stores/RestoreStore.ts b/backend/src/stores/RestoreStore.tsindex e3738dc..7a94970 100644--- a/backend/src/stores/RestoreStore.ts+++ b/backend/src/stores/RestoreStore.ts@@ -66,4 +66,9 @@ export class RestoreStore {return result.changes > 0;}++ async deleteByPlanId(planId: string): Promise<boolean> {+ const result = await this.db.delete(restores).where(eq(restores.planId, planId));+ return result.changes > 0;+ }}diff --git a/backend/src/strategies/backup/RemoteStrategy.ts b/backend/src/strategies/backup/RemoteStrategy.tsindex f560c8e..0f30ada 100644--- a/backend/src/strategies/backup/RemoteStrategy.ts+++ b/backend/src/strategies/backup/RemoteStrategy.ts@@ -1,11 +1,9 @@import { BackupStrategy } from './BackupStrategy';export class RemoteStrategy implements BackupStrategy {- protected broker: any;protected deviceId: string;- constructor(broker: any, deviceId: string) {- this.broker = broker;+ constructor(deviceId: string) {this.deviceId = deviceId;}@@ -62,20 +60,7 @@ export class RemoteStrategy implements BackupStrategy {publishCommand(action: string, payload: any): Promise<{ success: boolean; result: any }> {return new Promise((resolve, reject) => {- this.broker.publish(- {- cmd: 'publish',- topic: `command/backup/${this.deviceId}/${action}`,- payload: JSON.stringify(payload),- qos: 0,- dup: false,- retain: false,- },- (error: any) => {- if (error) reject({ success: false, result: error?.message || 'Unknown Error' });- else resolve({ success: true, result: JSON.stringify(payload) });- }- );+ resolve({ success: true, result: null });});}}diff --git a/backend/src/strategies/restore/RemoteStrategy.ts b/backend/src/strategies/restore/RemoteStrategy.tsindex 144294e..95f26ad 100644--- a/backend/src/strategies/restore/RemoteStrategy.ts+++ b/backend/src/strategies/restore/RemoteStrategy.ts@@ -2,11 +2,9 @@ import { RestoreConfig, RestoreOptions } from '../../types/restores';import { RestoreStrategy } from './RestoreStrategy';export class RemoteStrategy implements RestoreStrategy {- protected broker: any;protected deviceId: string;- constructor(broker: any, deviceId: string) {- this.broker = broker;+ constructor(deviceId: string) {this.deviceId = deviceId;}@@ -41,20 +39,7 @@ export class RemoteStrategy implements RestoreStrategy {publishCommand(action: string, payload: any): Promise<{ success: boolean; result: any }> {return new Promise((resolve, reject) => {- this.broker.publish(- {- cmd: 'publish',- topic: `command/restore/${this.deviceId}/${action}`,- payload: JSON.stringify(payload),- qos: 0,- dup: false,- retain: false,- },- (error: any) => {- if (error) reject({ success: false, result: error?.message || 'Unknown Error' });- else resolve({ success: true, result: JSON.stringify(payload) });- }- );+ resolve({ success: true, result: null });});}}diff --git a/backend/src/strategies/snapshot/RemoteStrategy.ts b/backend/src/strategies/snapshot/RemoteStrategy.tsindex 084271a..b543882 100644--- a/backend/src/strategies/snapshot/RemoteStrategy.ts+++ b/backend/src/strategies/snapshot/RemoteStrategy.ts@@ -1,11 +1,9 @@import { SnapshotStrategy } from './SnapshotStrategy';export class RemoteStrategy implements SnapshotStrategy {- protected broker: any;protected deviceId: string;- constructor(broker: any, deviceId: string) {- this.broker = broker;+ constructor(deviceId: string) {this.deviceId = deviceId;}async removeSnapshot(@@ -25,8 +23,8 @@ export class RemoteStrategy implements SnapshotStrategy {return await this.publishCommand('DOWNLOAD_SNAPSHOT', { backupId, path, options });}- async getSnapshotDownload(backupId: string) {- return await this.publishCommand('GET_SNAPSHOT_DOWNLOAD', { backupId });+ async getSnapshotDownload(planId: string, backupId: string) {+ return await this.publishCommand('GET_SNAPSHOT_DOWNLOAD', { planId, backupId });}async cancelSnapshotDownload(backupId: string) {@@ -43,20 +41,7 @@ export class RemoteStrategy implements SnapshotStrategy {publishCommand(action: string, payload: any): Promise<{ success: boolean; result: any }> {return new Promise((resolve, reject) => {- this.broker.publish(- {- cmd: 'publish',- topic: `command/snapshot/${this.deviceId}/${action}`,- payload: JSON.stringify(payload),- qos: 0,- dup: false,- retain: false,- },- (error: any) => {- if (error) reject({ success: false, result: error?.message || 'Unknown Error' });- else resolve({ success: true, result: JSON.stringify(payload) });- }- );+ resolve({ success: true, result: null });});}}diff --git a/backend/src/strategies/system/RemoteStrategy.ts b/backend/src/strategies/system/RemoteStrategy.tsindex 3893301..3541936 100644--- a/backend/src/strategies/system/RemoteStrategy.ts+++ b/backend/src/strategies/system/RemoteStrategy.ts@@ -2,11 +2,9 @@ import { DeviceSettings, DiscoveredFilesystem } from '../../types/devices';import { SystemStrategy } from './SystemStrategy';export class RemoteStrategy implements SystemStrategy {- protected broker: any;protected deviceId: string;- constructor(broker: any, deviceId: string) {- this.broker = broker;+ constructor(deviceId: string) {this.deviceId = deviceId;}@@ -89,20 +87,7 @@ export class RemoteStrategy implements SystemStrategy {`command/system/${this.deviceId}/${action}`);return new Promise((resolve, reject) => {- this.broker.publish(- {- cmd: 'publish',- topic: `command/system/${this.deviceId}/${action}`,- payload: JSON.stringify(payload),- qos: 0,- dup: false,- retain: false,- },- (error: any) => {- if (error) reject({ success: false, result: error?.message || 'Unknown Error' });- else resolve({ success: true, result: JSON.stringify(payload) });- }- );+ resolve({ success: true, result: null });});}}