From 02c0d7aaa88911cc50f1008b49258b87f362fb07 Mon Sep 17 00:00:00 2001 From: undefined Date: Sat, 4 Feb 2023 13:59:17 +0800 Subject: [PATCH] core: backup&restore: enhanced message --- packages/hydrooj/bin/commands.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/hydrooj/bin/commands.ts b/packages/hydrooj/bin/commands.ts index 1ec11f301..783f447d6 100644 --- a/packages/hydrooj/bin/commands.ts +++ b/packages/hydrooj/bin/commands.ts @@ -2,10 +2,12 @@ import child from 'child_process'; import os from 'os'; import path from 'path'; +import readline from 'readline/promises'; import cac from 'cac'; import fs from 'fs-extra'; import superagent from 'superagent'; import tar from 'tar'; +import { size } from '@hydrooj/utils'; const argv = cac().parse(); let yarnVersion = 0; @@ -80,9 +82,10 @@ if (!argv.args[0] || argv.args[0] === 'cli') { exec('zip', ['-r', target, 'file'], { cwd: '/data', stdio: 'inherit' }); } exec('rm', ['-rf', dir]); - console.log(`Database backup saved at ${target}`); + const stat = fs.statSync(target); + console.log(`Database backup saved at ${target} , size: ${size(stat.size)}`); }); - cli.command('restore ').action((filename) => { + cli.command('restore ').action(async (filename) => { const dbConfig = fs.readFileSync(path.resolve(hydroPath, 'config.json'), 'utf-8'); const url = buildUrl(JSON.parse(dbConfig)); const dir = `${os.tmpdir()}/${Math.random().toString(36).substring(2)}`; @@ -90,6 +93,13 @@ if (!argv.args[0] || argv.args[0] === 'cli') { console.error('Cannot find file'); return; } + const rl = readline.createInterface(process.stdin, process.stdout); + const answer = await rl.question(`Overwrite current database with backup file ${filename}? [y/N]`); + rl.close(); + if (answer.toLowerCase() !== 'y') { + console.log('Abort.'); + return; + } exec('unzip', [filename, '-d', dir], { stdio: 'inherit' }); exec('mongorestore', [`--uri=${url}`, `--dir=${dir}/dump/hydro`, '--drop'], { stdio: 'inherit' }); if (fs.existsSync(`${dir}/file`)) {