Skip to content

Commit

Permalink
Merge pull request #990 from pxgo/master
Browse files Browse the repository at this point in the history
修复
  • Loading branch information
pxgo authored Oct 22, 2024
2 parents c6a5525 + 1c39310 commit 62a0eb7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
4 changes: 2 additions & 2 deletions microServices/backup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
73 changes: 40 additions & 33 deletions microServices/backup/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
})();
});
}

Expand Down

0 comments on commit 62a0eb7

Please sign in to comment.