add option to enable gzip and set level

This commit is contained in:
HorizonCode 2022-08-08 15:58:34 +02:00
parent c23c09441f
commit a4929c86c5
2 changed files with 21 additions and 4 deletions

View File

@ -3,12 +3,13 @@ const path = require('path');
const { EventEmitter } = require('events'); const { EventEmitter } = require('events');
class Archiver { class Archiver {
constructor(backupPath, backupPaths) { constructor(backupPath, backupPaths, gzip, gzipLevel) {
this.backupPath = backupPath; this.backupPath = backupPath;
this.backupPaths = backupPaths; this.backupPaths = backupPaths;
this.eventEmitter = new EventEmitter(); this.eventEmitter = new EventEmitter();
this.archive = require('archiver')('tar', { this.archive = require('archiver')('tar', {
zlib: { level: 9 } gzip: gzip,
gzipOptions: { level: gzipLevel }
}); });
this.totalFiles = 0; this.totalFiles = 0;
} }

View File

@ -96,7 +96,11 @@ const run = async (programArgs) => {
.replace("{taskName}", PATH.parse(taskName).name); .replace("{taskName}", PATH.parse(taskName).name);
const DB_FILE = PATH.join(TEMP_DIR, require('crypto').randomBytes(16).toString('hex') + ".sql"); const DB_FILE = PATH.join(TEMP_DIR, require('crypto').randomBytes(16).toString('hex') + ".sql");
const ARCHIVE = new BACKUP_ARCHIVE(PATH.join(BACKUPS_DIRECTORY, PATH.parse(taskName).name, REPLACED_FILENAME + ".tar.gz"), TASK_FILE_CONTENTS.filesystem.targets); const ARCHIVE = new BACKUP_ARCHIVE(PATH.join(BACKUPS_DIRECTORY, PATH.parse(taskName).name,
REPLACED_FILENAME + ".tar.gz"),
TASK_FILE_CONTENTS.filesystem.targets,
TASK_FILE_CONTENTS.general.gzip,
TASK_FILE_CONTENTS.general.gzipLevel);
ARCHIVE.eventEmitter.on('progress', (progressInfo) => { ARCHIVE.eventEmitter.on('progress', (progressInfo) => {
const TOTAL = ARCHIVE.totalFiles; const TOTAL = ARCHIVE.totalFiles;
@ -232,7 +236,9 @@ const run = async (programArgs) => {
const TASK_CONFIG = { const TASK_CONFIG = {
"general": { "general": {
"dateFormat": "yyyy-MM-DD HH-mm-ss", "dateFormat": "yyyy-MM-DD HH-mm-ss",
"outputFile": "{date} - {taskName}" "outputFile": "{date} - {taskName}",
"gzip": true,
"gzipLevel": 6
}, },
"vacuum": { "vacuum": {
"enabled": true, "enabled": true,
@ -294,6 +300,16 @@ const validate = (taskConfig) => {
} else { } else {
ERRORS.push("general.outputFile is not defined or is not a string"); ERRORS.push("general.outputFile is not defined or is not a string");
} }
if (typeof taskConfig.general.gzip === "boolean") {
if (taskConfig.general.gzip) {
if (!taskConfig.general.gzipLevel && typeof taskConfig.general.gzipLevel !== "number") {
ERRORS.push("general.gzipLevel is not defined or is not a number");
}
}
} else {
ERRORS.push("general.gzip is not defined or is not a boolean");
ERRORS.push("general.gzipLevel is not defined or is not a number");
}
} else { } else {
ERRORS.push("general section is missing or not an object"); ERRORS.push("general section is missing or not an object");
ERRORS.push("general.dateFormat is not defined or is not a string"); ERRORS.push("general.dateFormat is not defined or is not a string");