From 304ef494fd5787c996ad0dcb6edd2f517afce9e2 Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Wed, 16 Oct 2024 12:35:09 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=95=B0=E6=8D=AE=E5=BA=93=E5=A4=87?= =?UTF-8?q?=E4=BB=BD=E6=8F=92=E4=BB=B6=EF=BC=8C=E5=85=88=E5=8E=8B=E7=BC=A9?= =?UTF-8?q?=E5=86=8D=E5=A4=87=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin-other/plugins/plugin-db-backup.ts | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/packages/ui/certd-server/src/plugins/plugin-other/plugins/plugin-db-backup.ts b/packages/ui/certd-server/src/plugins/plugin-other/plugins/plugin-db-backup.ts index b7fc72cf..ac40be3b 100644 --- a/packages/ui/certd-server/src/plugins/plugin-other/plugins/plugin-db-backup.ts +++ b/packages/ui/certd-server/src/plugins/plugin-other/plugins/plugin-db-backup.ts @@ -12,7 +12,7 @@ const defaultFilePrefix = 'db-backup'; @IsTaskPlugin({ name: 'DBBackupPlugin', title: '数据库备份', - icon: 'ri:rest-time-line', + icon: 'lucide:database-backup', desc: '仅支持备份SQLite数据库', group: pluginGroups.other.key, default: { @@ -102,12 +102,24 @@ export class DBBackupPlugin extends AbstractPlusTaskPlugin { return; } + const dbTmpFilename = `${this.filePrefix}.${dayjs().format('YYYYMMDD.HHmmss')}.sqlite`; + const dbZipFilename = `${dbTmpFilename}.zip`; + const tempDir = path.resolve(os.tmpdir(), 'certd_backup'); + if (!fs.existsSync(tempDir)) { + await fs.promises.mkdir(tempDir, { recursive: true }); + } + const dbTmpPath = path.resolve(tempDir, dbTmpFilename); + const dbZipPath = path.resolve(tempDir, dbZipFilename); + + //复制到临时目录 + await fs.promises.copyFile(dbPath, dbTmpPath); //本地压缩 const zip = new JSZip(); - zip.file(dbPath); + const stream = fs.createReadStream(dbTmpPath); + // 使用流的方式添加文件内容 + zip.file(dbTmpFilename, stream, { binary: true, compression: 'DEFLATE' }); const content = await zip.generateAsync({ type: 'nodebuffer' }); - const dbZipFilename = `${this.filePrefix}.${dayjs().format('YYYYMMDD.HHmmss')}.sqlite.zip`; - const dbZipPath = path.resolve(os.tmpdir(), dbZipFilename); + await fs.promises.writeFile(dbZipPath, content); this.logger.info(`数据库文件压缩完成:${dbZipPath}`); @@ -116,11 +128,11 @@ export class DBBackupPlugin extends AbstractPlusTaskPlugin { const backupFilePath = `${backupDir}/${dbZipFilename}`; if (this.backupMode === 'local') { - await this.localBackup(dbPath, backupDir, backupFilePath); + await this.localBackup(dbZipPath, backupDir, backupFilePath); } else if (this.backupMode === 'ssh') { - await this.sshBackup(dbPath, backupDir, backupFilePath); + await this.sshBackup(dbZipPath, backupDir, backupFilePath); } else if (this.backupMode === 'oss') { - await this.ossBackup(dbPath, backupDir, backupFilePath); + await this.ossBackup(dbZipPath, backupDir, backupFilePath); } else { throw new Error(`不支持的备份方式:${this.backupMode}`); }