mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 09:36:08 +08:00
core: backup&restore: enhanced message
This commit is contained in:
1 parent
50afa3df39
commit
02c0d7aaa8
1 file changed
+12
-2
@@ -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 <filename>').action((filename) => {
|
||||
cli.command('restore <filename>').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`)) {
|
||||
|
||||
Reference in new issue
Block a user