Files

7 lines
1.5 KiB
JavaScript

'use strict';
const {createBackup}=require('../src/platform/backup/snapshot.cjs');
const {verifyBackup}=require('../src/platform/backup/manifest.cjs');
const {restoreBackup}=require('../src/platform/backup/restore.cjs');
async function main(args){const command=args.shift(),allowed={create:['destination','state','output'],verify:['source'],restore:['source','destination']}[command];if(!allowed)throw Object.assign(Error('invalid command'),{code:'BACKUP_USAGE'});const options={};while(args.length){const key=args.shift();if(!key.startsWith('--')||!allowed.includes(key.slice(2))||Object.hasOwn(options,key.slice(2))||!args.length||args[0].startsWith('--'))throw Object.assign(Error('invalid option'),{code:'BACKUP_USAGE'});options[key.slice(2)]=args.shift();}if((command!=='create'&&!options.source)||(command!=='verify'&&!options.destination))throw Object.assign(Error('missing option'),{code:'BACKUP_USAGE'});if(command==='create')await createBackup({stateDir:options.state,outputDir:options.output,destination:options.destination});else if(command==='verify')verifyBackup(options.source);else await restoreBackup(options);process.stdout.write(JSON.stringify({ok:true,operation:command})+'\n');}
main(process.argv.slice(2)).catch(error=>{const code=/^(?:BACKUP_[A-Z_]+|ORDER_INSTANCE_OWNED)$/.test(error.code||'')?error.code:'BACKUP_FAILED';process.stderr.write(JSON.stringify({ok:false,code,message:'操作未完成。请检查服务已停止、备份完整性、目标为空及 Windows 身份兼容性。'})+'\n');process.exitCode=1;});