Skip to content

Commit

Permalink
refactor(backups): backup & restore with new folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
nicotsx committed Dec 24, 2024
1 parent 9e5a2fc commit 623e404
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions packages/backend/src/modules/backups/backup.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);

Expand All @@ -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...');

Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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<void> {
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)));
}

/**
Expand Down

0 comments on commit 623e404

Please sign in to comment.