diff --git a/microServices/backup/package.json b/microServices/backup/package.json index e8d6bb701..4731f9fb7 100644 --- a/microServices/backup/package.json +++ b/microServices/backup/package.json @@ -10,8 +10,8 @@ "author": "", "license": "ISC", "dependencies": { - "node-schedule": "^1.3.0", "colors": "^1.1.2", - "archiver": "^3.0.0" + "node-schedule": "^1.3.0", + "yazl": "^3.1.0" } } diff --git a/microServices/backup/server.js b/microServices/backup/server.js index 08e7c04f1..9b1404902 100644 --- a/microServices/backup/server.js +++ b/microServices/backup/server.js @@ -2,8 +2,9 @@ require('colors'); const moment = require('moment'); const PATH = require('path'); const schedule = require('node-schedule'); -const archiver = require('archiver'); +const yazl = require('yazl'); const fs = require('fs'); +const fsPromises = fs.promises; const { spawn } = require('child_process'); const { folder, time } = require('../../config/backup.json'); const { @@ -80,45 +81,51 @@ async function backupDatabase() { }); } -async function compressedDir(dirPath, zipFilePath) { - return new Promise((resolve, reject) => { - try { - const targetDirPath = PATH.dirname(zipFilePath); - fs.mkdirSync(targetDirPath, { - recursive: true, - }); +async function addDirectoryToZip(zipfile, dirPath, basePath) { + const files = await fs.promises.readdir(dirPath); - const zipFile = fs.createWriteStream(zipFilePath); + for (const file of files) { + const filePath = PATH.join(dirPath, file); + const relativePath = PATH.join(basePath, file); + const stats = await fsPromises.stat(filePath); - zipFile.on('error', (err) => { - reject(err); - }); - zipFile.on('finish', () => { - resolve(); - }); + if (stats.isDirectory()) { + await addDirectoryToZip(zipfile, filePath, relativePath); + } else { + zipfile.addFile(filePath, relativePath); + } + } +} - const archive = archiver('zip', { - zlib: { level: 9 }, - }); - archive.on('warning', function (err) { - if (err.code === 'ENOENT') { - console.log(err); - } else { +async function compressedDir(dirPath, zipFilePath) { + return new Promise((resolve, reject) => { + (async () => { + try { + const targetDirPath = PATH.dirname(zipFilePath); + await fsPromises.mkdir(targetDirPath, { + recursive: true, + }); + const zipFile = fs.createWriteStream(zipFilePath); + + zipFile.on('error', (err) => { reject(err); - } - }); + }); + zipFile.on('finish', () => { + resolve(); + }); - archive.on('error', function (err) { - reject(err); - }); + const zip = new yazl.ZipFile(); - archive.directory(dirPath, false); + await addDirectoryToZip(zip, dirPath, './'); - archive.pipe(zipFile); - archive.finalize(); - } catch (err) { - reject(err); - } + zip.outputStream.pipe(zipFile).on('close', () => { + console.log('ZIP file created'); + }); + zip.end(); + } catch (err) { + reject(err); + } + })(); }); }