UNPKG

vmsnap

Version:

A Node based backup and backup rotation tool for KVM domains.

3 lines (2 loc) 1.96 kB
#!/usr/bin/env node import c,{exit as s}from"process";import{exec as p}from"child_process";import f from"util";import{tmpdir as l}from"os";import{sep as R}from"path";import d from"yargs";import{lock as u,unlock as x}from"lockfile";import*as o from"winston";import{consoleFormat as E}from"winston-console-format";import _ from"yocto-spinner";import{checkCommand as g,checkDependencies as v,scrubCheckpointsAndBitmaps as k}from"./libs/general.js";import{performBackup as y}from"./libs/libnbdbackup.js";import{printStatusCheck as b,SCREEN_SIZE as S}from"./libs/print.js";import{setLibvirtUri as h}from"./libs/virsh.js";export const ERR_DOMAINS=1,ERR_OUTPUT_DIR=2,ERR_MAIN=3,ERR_REQS=4,ERR_SCRUB=5,ERR_LOCK_RELEASE=6,ERR_TOO_MANY_COMMANDS=7,ERR_INVALID_SCRUB_TYPE=8,spinner=_();const n=`${l()}${R}vmsnap.lock`;export const asyncExec=f.promisify(p);const t=d(c.argv.slice(2)).argv;t.connect&&h(t.connect);let a=[],m=[o.format.printf(i=>`${i.message}`)];t.verbose&&(a=[o.format.timestamp(),o.format.errors({stack:!0}),o.format.splat(),o.format.json(),o.format.ms()],m=[o.format.colorize(),o.format.splat({depth:1/0}),E({showMeta:!0,metaStrip:["timestamp","service"],inspectOptions:{depth:1/0,colors:!0,maxArrayLength:1/0,breakLength:S,compact:1/0}})]);export const logger=o.createLogger({format:o.format.combine(...a),defaultMeta:{service:"vmsnap"},transports:[new o.transports.Console({levels:o.config.cli.levels,silent:!1,format:o.format.combine(...m)})]});let r=0;u(n,{retries:10,retryWait:1e4},async i=>{if(i){logger.error(`Failed to acquire lock: ${i.message}`),s(ERR_MAIN);return}try{await v(),t.verbose&&logger.info("Dependencies are installed"),g(t),t.scrub?await k(t):t.backup?await y(t):await b(t)}catch(e){spinner.stop(),logger.error(e.message),r=typeof e.code=="number"?e.code:ERR_MAIN}finally{spinner.stop(),r===void 0&&logger.info("No exit code provided for lock release"),x(n,e=>{e&&(logger.error(e),r=ERR_LOCK_RELEASE),s(r!==void 0?r:ERR_MAIN)})}});