fix: backup cmd

This commit is contained in:
Innei
2021-10-01 18:54:38 +08:00
parent d8066e49b1
commit 855492e476
3 changed files with 15 additions and 12 deletions

View File

@@ -51,9 +51,9 @@ export class BackupService {
return Promise.all(
backups.map(async (item) => {
const { path } = item
const { stdout } = await getFolderSize(path)
const size = getFolderSize(path)
delete item.path
return { ...item, size: stdout }
return { ...item, size }
}),
)
}

View File

@@ -1,12 +1,13 @@
import { forwardRef, Inject, Injectable, Logger } from '@nestjs/common'
import { Cron, CronExpression } from '@nestjs/schedule'
import { execSync } from 'child_process'
import COS from 'cos-nodejs-sdk-v5'
import dayjs from 'dayjs'
import { existsSync, readFileSync, rmSync, writeFileSync } from 'fs'
import mkdirp from 'mkdirp'
import { InjectModel } from 'nestjs-typegoose'
import { join } from 'path'
import { $, cd } from 'zx'
import { $ } from 'zx'
import { MONGO_DB } from '~/app.config'
import { CronDescription } from '~/common/decorator/cron-description.decorator'
import { RedisItems, RedisKeys } from '~/constants/cache.constant'
@@ -91,11 +92,10 @@ export class CronService {
mkdirp.sync(backupDirPath)
try {
await $`mongodump -h ${MONGO_DB.host} --port ${MONGO_DB.port} -d ${MONGO_DB.collectionName} -o ${backupDirPath} >/dev/null 2>&1`
cd(backupDirPath)
const debug = await nothrow($`pwd && ls -a`)
console.error(debug)
await $`zip -r backup-${dateDir} mx-space/* && rm -rf mx-space`
execSync(`zip -r backup-${dateDir} mx-space/* && rm -rf mx-space`, {
cwd: backupDirPath,
})
this.logger.log('--> 备份成功')
} catch (e) {

View File

@@ -1,10 +1,13 @@
import { execSync } from 'child_process'
export function getFolderSize(folderPath: string) {
try {
return $`du -shc ${folderPath} | head -n 1 | cut -f1`
} catch (e: any) {
if (e.exitCode == 141) {
return e.stdout.trim()
}
return (
execSync(`du -shc ${folderPath} | head -n 1 | cut -f1`, {
encoding: 'utf-8',
}).split('\t')[0] || 'N/A'
)
} catch {
return 'N/A'
}
}