From 99678c1635c5be188e6ebdc95095d86c7a21a696 Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Tue, 15 Oct 2024 19:42:39 +0800 Subject: [PATCH] chore: --- .../plugin-other/plugins/plugin-db-backup.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 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 96bc6423..ee2fbf53 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 @@ -102,14 +102,14 @@ export class DBBackupPlugin extends AbstractPlusTaskPlugin { this.logger.info('当前备份方式:', this.backupMode); const backupDir = this.backupDir || defaultBackupDir; - const backupPath = `${backupDir}/${this.filePrefix}.${dayjs().format('YYYYMMDD.HHmmss')}.sqlite`; + const backupFile = `${backupDir}/${this.filePrefix}.${dayjs().format('YYYYMMDD.HHmmss')}.sqlite`; if (this.backupMode === 'local') { - await this.localBackup(dbPath, backupDir, backupPath); + await this.localBackup(dbPath, backupDir, backupFile); } else if (this.backupMode === 'ssh') { - await this.sshBackup(dbPath, backupDir, backupPath); + await this.sshBackup(dbPath, backupDir, backupFile); } else if (this.backupMode === 'oss') { - await this.ossBackup(dbPath, backupDir, backupPath); + await this.ossBackup(dbPath, backupDir, backupFile); } else { throw new Error(`不支持的备份方式:${this.backupMode}`); } @@ -119,12 +119,13 @@ export class DBBackupPlugin extends AbstractPlusTaskPlugin { private async localBackup(dbPath: string, backupDir: string, backupPath: string) { if (!backupPath.startsWith('/')) { - backupPath = path.resolve('./data/', backupPath); + backupPath = path.join('./data/', backupPath); } - const dir = backupDir; + const dir = path.dirname(backupPath); if (!fs.existsSync(dir)) { await fs.promises.mkdir(dir, { recursive: true }); } + backupPath = path.resolve(backupPath); await fs.promises.copyFile(dbPath, backupPath); this.logger.info('备份文件路径:', backupPath); @@ -162,12 +163,12 @@ export class DBBackupPlugin extends AbstractPlusTaskPlugin { // 删除过期备份 this.logger.info('开始删除过期备份文件'); const isWin = access.windows; - let script = ''; + let script: string[] = []; if (isWin) { throw new Error('删除过期文件暂不支持windows系统'); // script = `forfiles /p ${backupDir} /s /d -${this.retainDays} /c "cmd /c del @path"`; } else { - script = `find ${backupDir} -type f -mtime +${this.retainDays} -name '${this.filePrefix}*' -exec rm -f {} \\;`; + script = [`cd ${backupDir}`, 'echo 备份目录', 'pwd', `find . -type f -mtime +${this.retainDays} -name '${this.filePrefix}*' -exec rm -f {} \\;`]; } await sshClient.exec({