-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup-handler.ts
More file actions
executable file
·117 lines (95 loc) · 4.5 KB
/
backup-handler.ts
File metadata and controls
executable file
·117 lines (95 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import * as path from 'path';
import { copy } from 'fs-extra';
import { statSync } from 'node:fs';
import { cliux, sanitizePath, log } from '@contentstack/cli-utilities';
import { fileHelper } from './index';
import { ImportConfig } from '../types';
export default async function backupHandler(importConfig: ImportConfig): Promise<string> {
log.debug('Starting backup handler process');
if (importConfig.hasOwnProperty('useBackedupDir')) {
log.debug(`Using existing backup directory: ${importConfig.useBackedupDir}`);
return importConfig.useBackedupDir;
}
const sourceDir = importConfig.branchDir || importConfig.contentDir;
log.debug(
`Using source directory for backup: ${sourceDir} (branchDir: ${importConfig.branchDir}, contentDir: ${importConfig.contentDir})`,
);
let backupDirPath: string;
const subDir = isSubDirectory(importConfig, sourceDir);
if (subDir) {
backupDirPath = path.resolve(sanitizePath(sourceDir), '..', '_backup_' + Math.floor(Math.random() * 1000));
log.debug(`Detected subdirectory configuration, creating backup at: ${backupDirPath}`);
if (importConfig.createBackupDir) {
cliux.print(
`Warning!!! Provided backup directory path is a sub directory of the content directory, Cannot copy to a sub directory. Hence new backup directory created - ${backupDirPath}`,
{
color: 'yellow',
},
);
}
} else {
// NOTE: If the backup folder's directory is provided, create it at that location; otherwise, the default path (working directory).
backupDirPath = path.join(process.cwd(), '_backup_' + Math.floor(Math.random() * 1000));
log.debug(`Using default backup directory: ${backupDirPath}`);
if (importConfig.createBackupDir) {
log.debug(`Custom backup directory specified: ${importConfig.createBackupDir}`);
if (fileHelper.fileExistsSync(importConfig.createBackupDir)) {
log.debug(`Removing existing backup directory: ${importConfig.createBackupDir}`);
fileHelper.removeDirSync(importConfig.createBackupDir);
}
log.debug(`Creating backup directory: ${importConfig.createBackupDir}`);
fileHelper.makeDirectory(importConfig.createBackupDir);
backupDirPath = importConfig.createBackupDir;
}
}
if (backupDirPath) {
// Check dataset size before backup to prevent memory issues
try {
const stats = statSync(sourceDir);
const sizeGB = stats.size / (1024 * 1024 * 1024);
const sizeThresholdGB = 1; // Skip backup for datasets larger than 1GB
log.debug(`Source directory size: ${sizeGB.toFixed(2)}GB`, importConfig.context);
if (sizeGB > sizeThresholdGB) {
const skipMessage = `Large dataset detected (${sizeGB.toFixed(2)}GB > ${sizeThresholdGB}GB threshold). Skipping backup to save memory and prevent OOM errors.`;
log.warn(skipMessage, importConfig.context);
cliux.print(skipMessage, { color: 'yellow' });
// Return the source directory as the "backup" directory
log.debug(`Using source directory directly: ${sourceDir}`, importConfig.context);
return sourceDir;
}
} catch (error) {
log.debug(`Could not determine source directory size: ${error}. Proceeding with backup.`, importConfig.context);
}
log.debug(`Starting content copy to backup directory: ${backupDirPath}`);
log.info('Copying content to the backup directory...', importConfig.context);
return new Promise((resolve, reject) => {
return copy(sourceDir, backupDirPath, (error: any) => {
if (error) {
return reject(error);
}
log.debug(`Successfully created backup at: ${backupDirPath}`);
resolve(backupDirPath);
});
});
}
}
/**
* Check whether provided backup directory path is sub directory or not
* @param importConfig
* @returns
*/
function isSubDirectory(importConfig: ImportConfig, sourceDir: string) {
log.debug('Checking if backup directory is a subdirectory');
const parent = sourceDir;
const child = importConfig.createBackupDir ? importConfig.createBackupDir : process.cwd();
const relative = path.relative(parent, child);
log.debug(`Parent directory: ${parent}, Child directory: ${child}, Relative path: ${relative}`);
if (relative) {
const isSubDir = !relative.startsWith('..') && !path.isAbsolute(relative);
log.debug(`Is subdirectory: ${isSubDir}`);
return isSubDir;
}
// true if both parent and child have same path
log.debug('Parent and child directories are the same');
return true;
}