From 623e40480573e1bb7c28dd7b350e3eb13fa08f8d Mon Sep 17 00:00:00 2001 From: Nicolas Meienberger Date: Tue, 24 Dec 2024 11:44:05 +0100 Subject: [PATCH] refactor(backups): backup & restore with new folder structure --- .../src/modules/backups/backup.manager.ts | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/packages/backend/src/modules/backups/backup.manager.ts b/packages/backend/src/modules/backups/backup.manager.ts index 56cce78743..f415ee71a4 100644 --- a/packages/backend/src/modules/backups/backup.manager.ts +++ b/packages/backend/src/modules/backups/backup.manager.ts @@ -4,6 +4,7 @@ import { ConfigurationService } from '@/core/config/configuration.service'; import { FilesystemService } from '@/core/filesystem/filesystem.service'; import { LoggerService } from '@/core/logger/logger.service'; import { Injectable } from '@nestjs/common'; +import { AppFilesManager } from '../apps/app-files-manager'; @Injectable() export class BackupManager { @@ -12,10 +13,11 @@ export class BackupManager { private readonly logger: LoggerService, private readonly config: ConfigurationService, private readonly filesystem: FilesystemService, + private readonly appFilesManager: AppFilesManager, ) {} public backupApp = async (appId: string) => { - const { dataDir, appDataDir } = this.config.get('directories'); + const { dataDir } = this.config.get('directories'); const backupName = `${appId}-${new Date().getTime()}`; const backupDir = path.join(dataDir, 'backups', appId); @@ -30,13 +32,15 @@ export class BackupManager { // Ensure backup directory exists await this.filesystem.createDirectory(tempDir); + const { appDataDir, appInstalledDir } = this.appFilesManager.getAppPaths(appId); + // Move app data and app directories - await this.filesystem.copyDirectory(path.join(appDataDir, appId), path.join(tempDir, 'app-data'), { + await this.filesystem.copyDirectory(appDataDir, path.join(tempDir, 'app-data'), { recursive: true, filter: (src) => !src.includes('backups'), }); - await this.filesystem.copyDirectory(path.join(dataDir, 'apps', appId), path.join(tempDir, 'app')); + await this.filesystem.copyDirectory(appInstalledDir, path.join(tempDir, 'app')); this.logger.info('Creating archive...'); @@ -59,7 +63,7 @@ export class BackupManager { }; public restoreApp = async (appId: string, filename: string) => { - const { dataDir, appDataDir } = this.config.get('directories'); + const { dataDir } = this.config.get('directories'); const restoreDir = await this.filesystem.createTempDirectory(appId); if (!restoreDir) { @@ -84,19 +88,18 @@ export class BackupManager { this.logger.debug('stderr:', stderr); this.logger.debug('stdout:', stdout); - const appDataDirPath = path.join(appDataDir, appId); - const appDirPath = path.join(dataDir, 'apps', appId); + const { appInstalledDir, appDataDir } = this.appFilesManager.getAppPaths(appId); // Remove old data directories - await this.filesystem.removeDirectory(appDataDirPath); - await this.filesystem.removeDirectory(appDirPath); + await this.filesystem.removeDirectory(appDataDir); + await this.filesystem.removeDirectory(appInstalledDir); - await this.filesystem.createDirectory(appDataDirPath); - await this.filesystem.createDirectory(appDirPath); + await this.filesystem.createDirectory(appDataDir); + await this.filesystem.createDirectory(appInstalledDir); // Copy data from the backup folder - await this.filesystem.copyDirectory(path.join(restoreDir, 'app-data'), appDataDirPath); - await this.filesystem.copyDirectory(path.join(restoreDir, 'app'), appDirPath); + await this.filesystem.copyDirectory(path.join(restoreDir, 'app-data'), appDataDir); + await this.filesystem.copyDirectory(path.join(restoreDir, 'app'), appInstalledDir); // Delete restore folder await this.filesystem.removeDirectory(restoreDir); @@ -119,12 +122,12 @@ export class BackupManager { /** * Delete all backups for an app - * @param appId - The app id + * @param appId - The app id */ public async deleteAppBackupsById(appId: string): Promise { const backups = await this.listBackupsByAppId(appId); - await Promise.all(backups.map((backup) => this.deleteBackup(appId, backup.id))) + await Promise.all(backups.map((backup) => this.deleteBackup(appId, backup.id))); } /**