npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-xen-backup

v0.3.2

Published

node-xen-backup is a Node.js module that uses xen-api to take a snapshot of a Xenserver VM and export to a file for backup. The module includes a cli to execute direct on bash.

Downloads

14

Readme

node-xen-backup

JavaScript Style Guide NPM version

node-xen-backup is a Node.js module that uses xen-api to take a snapshot of a Xenserver VM and export to a file for backup. The module includes a cli to execute direct on bash.

You can contribute

It is a free software and its use is free for everyone, but if you want to contribute anyway, please submit a donation.

Donate

Support

  • Node.js (tested on >= 7.x)

Installation

Installing global for cli usage

> npm install -g node-xen-backup

Installing as node module

> npm install node-xen-backup
  const XenBackup = require('node-xen-backup')

Cli

Getting help

> xen-backup --help

# Usage: xen-backup [options]


# Options:
#
#    -V, --version              output the version number
#    -H, --host [host]          IP or hostname from Xenserver. Default: localhost.
#    -P, --port [port]          TCP port from Xenserver. Default: 80.
#    -u, --username [username]  Username to login in Xenserver.
#    -p, --password <password>  Password to login in Xenserver.
#    -f, --filename <filename>  Path to target file of exported snapshot.
#    -t, --type <type>          Type of object to export. <vm|vdi> default vm.
#    -v, --vm <vm>              VM name label or uuid to backup.
#    -h, --help                 output usage information

Creating a backup to a VM

# Backup command
> xen-backup -H 192.168.1.1 -P 80 -u root -p password -f /backup/VMLinux.xva -v VMLinux
# Normal output
- Searching for VM VMLinux
- Searching VM real reference for UUID 38d15c62-a1a2-f5f8-de3d-55fb360e96c8
- Taking snapshot...
- Getting snapshot data...
- Exporting snapshot VMLinux1516035235 taked in January 15, 2018
- Will be saved on file /backup/VMLinux.xva
- The file has size 784.93MB
- Removing snapshot...
- Snapshot removed successfuly

API

Creating a backup

Example creating a backup using as Node.js module. This is very much like procedure used in cli.

// [another requires...]

const XenBackup = require('node-xen-backup')

const connOptions = {
  username: 'root',
  password: 'password',
  host: '192.168.1.1',
  port: 80,
  sessionId: null
}
const destFileName = "/backup/filename.xva"

// First sets connecion options.
XenBackup.setClient(connOptions.host, connOptions.port)
// Login in xenserver xen-api.
XenBackup.login(connOptions.username, connOptions.password, (err, sessionId) => {
  if (err) return console.error(err)
  // Once logged sets session ID
  connOptions.sessionId = sessionId
  // Calls function to get itens
  getItens((err, itens) => {
    if (err) return console.error(err)
    let founded = false
    // Search for VM or VDI in returned itens
    for (let i in itens) {
      if (itens[i].uuid.toLowerCase() === program.vm.toLowerCase() || itens[i].name.toLowerCase() === program.vm.toLowerCase()) {
        founded = true
        // Takes snapshot
        itens[i].snapshot(itens[i].name.replace(/\s/g, '') + moment().unix(), (err, snapshot) => {
          if (err) return console.error(err)
          let protocol = 'http'
          if ((connOptions.port === '443') || (connOptions.port === 443)) protocol = 'https'
          // Call function to export snapshot
          downloadBackup(protocol, snapshot.uuid, (err, download) => {
            if (err) console.error(err)
            // Removes snapshot
            snapshot.remove((err, removed) => {
              if (err) return console.error(err)
              // SUCCESS!!!
              return console.log("VM OR VDI EXPORTED")
            })
          })
        })
      }
    }
    if (!founded) {
      return console.error('VDI or VHD not found in Xenserver.')
    }
  })
})

function getItens (callback) {
  if (program.type === 'vm') {
    XenBackup.getAllVMs((err, vms) => {
      if (err) return callback(err, null)
      return callback(null, vms)
    })
  }
  if (program.type === 'vdi') {
    XenBackup.getAllVDIs((err, vdis) => {
      if (err) return callback(err, null)
      return callback(null, vdis)
    })
  }
  if ((program.type !== 'vdi') && (program.type !== 'mv')) {
    return callback(new Error('Cannot define object type <vm|vdi>.'))
  }
}

function downloadBackup (protocol, uuid, callback) {
  let sufix = null
  if (program.type === 'vm') {
    sufix = `/export?session_id=${connOptions.sessionId}&use_compression=true&uuid=`
  }
  if (program.type === 'vdi') {
    sufix = `/export_raw_vdi?session_id=${connOptions.sessionId}&format=vhd&vdi=`
  }
  let req = request.get(`${protocol}://${connOptions.host}:${connOptions.port}${sufix}${uuid}`)
    .on('end', function (response) {
      return callback(null, response)
    })
    .on('error', function (err) {
      console.log(err)
      return callback(err, null)
    })
  if (program.type === 'vdi') {
    req.pipe(GZip).pipe(fs.createWriteStream(destFileName))
  } else {
    req.pipe(fs.createWriteStream(destFileName))
  }
}