mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: backup: add withAddons option
This commit is contained in:
1 file changed
+24
-6
@@ -39,19 +39,28 @@ export function register(cli: CAC) {
|
|||||||
child.spawn('mongo', [url], { stdio: 'inherit' });
|
child.spawn('mongo', [url], { stdio: 'inherit' });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
cli.command('backup').option('--dbOnly', 'Only dump database', { default: false }).action(() => {
|
cli.command('backup')
|
||||||
|
.option('--dbOnly', 'Only dump database', { default: false })
|
||||||
|
.option('--withAddons', 'Include addons', { default: false })
|
||||||
|
.action(() => {
|
||||||
const url = getUrl();
|
const url = getUrl();
|
||||||
exec('mongodump', [url, `--out=${dir}/dump`], { stdio: 'inherit' });
|
exec('mongodump', [url, `--out=${dir}/dump`], { stdio: 'inherit' });
|
||||||
const target = `${process.cwd()}/backup-${new Date().toISOString().replace(':', '-').split(':')[0]}.zip`;
|
const target = `${process.cwd()}/backup-${new Date().toISOString().replace(':', '-').split(':')[0]}.zip`;
|
||||||
exec('zip', ['-r', target, 'dump'], { cwd: dir, stdio: 'inherit' });
|
const addToZip = (cwd: string, item: string) => exec('zip', ['-r', target, item], { cwd, stdio: 'inherit' });
|
||||||
if (!argv.options.dbOnly) {
|
addToZip(dir, 'dump');
|
||||||
exec('zip', ['-r', target, 'file'], { cwd: '/data', stdio: 'inherit' });
|
if (!argv.options.dbOnly) addToZip('/data', 'file');
|
||||||
|
if (argv.options.withAddons) {
|
||||||
|
if (fs.existsSync(path.join(hydroPath, 'addons'))) addToZip(hydroPath, 'addons');
|
||||||
|
if (fs.existsSync(path.join(hydroPath, 'addon.json'))) addToZip(hydroPath, 'addon.json');
|
||||||
}
|
}
|
||||||
exec('rm', ['-rf', dir]);
|
fs.removeSync(dir);
|
||||||
const stat = fs.statSync(target);
|
const stat = fs.statSync(target);
|
||||||
logger.success(`Database backup saved at ${target} , size: ${size(stat.size)}`);
|
logger.success(`Database backup saved at ${target} , size: ${size(stat.size)}`);
|
||||||
});
|
});
|
||||||
cli.command('restore <filename>').option('-y', 'Assume yes', { default: false }).action(async (filename) => {
|
cli.command('restore <filename>')
|
||||||
|
.option('-y', 'Assume yes', { default: false })
|
||||||
|
.option('--withAddons', 'Include addons', { default: false })
|
||||||
|
.action(async (filename) => {
|
||||||
const url = getUrl();
|
const url = getUrl();
|
||||||
if (!fs.existsSync(filename)) {
|
if (!fs.existsSync(filename)) {
|
||||||
logger.error('Cannot find file');
|
logger.error('Cannot find file');
|
||||||
@@ -72,6 +81,15 @@ export function register(cli: CAC) {
|
|||||||
exec('rm', ['-rf', '/data/file/hydro'], { stdio: 'inherit' });
|
exec('rm', ['-rf', '/data/file/hydro'], { stdio: 'inherit' });
|
||||||
exec('bash', ['-c', `mv ${dir}/file/* /data/file`], { stdio: 'inherit' });
|
exec('bash', ['-c', `mv ${dir}/file/* /data/file`], { stdio: 'inherit' });
|
||||||
}
|
}
|
||||||
|
if (argv.options.withAddons) {
|
||||||
|
if (fs.existsSync(`${dir}/addons.json`)) {
|
||||||
|
fs.copyFileSync(`${dir}/addons.json`, path.join(hydroPath, 'addons.json'));
|
||||||
|
}
|
||||||
|
if (fs.existsSync(`${dir}/addons`)) {
|
||||||
|
fs.removeSync(path.join(hydroPath, 'addons'));
|
||||||
|
fs.copySync(`${dir}/addons`, path.join(hydroPath, 'addons'));
|
||||||
|
}
|
||||||
|
}
|
||||||
fs.removeSync(dir);
|
fs.removeSync(dir);
|
||||||
logger.success('Successfully restored.');
|
logger.success('Successfully restored.');
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user