Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/robertslando/node-influxdb-backup

Node influxdb backup/restore module.
https://github.com/robertslando/node-influxdb-backup

backup backup-database influx influxd influxdb node node-influx restore restore-database

Last synced: about 20 hours ago
JSON representation

Node influxdb backup/restore module.

Awesome Lists containing this project

README

        

# influx-backup

[![NPM version](http://img.shields.io/npm/v/influx-backup.svg)](https://www.npmjs.com/package/influx-backup)
[![Downloads](https://img.shields.io/npm/dm/influx-backup.svg)](https://www.npmjs.com/package/influx-backup)

[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.png?v=103)](https://opensource.org/licenses/mit-license.php)

[![NPM](https://nodei.co/npm/influx-backup.png?downloads=true)](https://nodei.co/npm/influx-backup/)

## Description 📔

NodeJS module for InfluxDB backup/restore.

This module uses the `influxd` command to backup and restore the backup, the backup consists in a zip file with all the data generated by the `influxd` command.

Uses InfluxDB *portable backups* introduced in InfluxDb v1.5, check [docs](https://docs.influxdata.com/influxdb/v1.7/administration/backup_and_restore/#online-backup-and-restore-for-influxdb-oss) for more info.

## Requirements ❗

- `influxd` bin must be installed in your system. Check it by running `which influxd` command
- **InfluxDB version > v1.5**

## Install 🔌

Run the following command in the root directory of your project

npm install influx-backup --save

## Usage 🔧

Check [JSDocs](https://robertslando.github.io/node-influxdb-backup/) and the Express [example](https://github.com/robertsLando/node-influxdb-backup/tree/master/examples).

Here are some lines from `examples/app.js`

```javascript

const BackupManager = require('influx-backup');

const TEST_DB = "testDB";

const manager = new BackupManager({db: TEST_DB});

// Restore DB api
app.post('/restore', (req, res) => {

var restore_path = "";

// create a temp directory to use for the backup
manager.createDir()
.then((dir) => {

restore_path = dir;

// Use multer to get the file from the req
// and store it in the temp dir created
var Storage = multer.diskStorage({
destination: restore_path,
filename: function(req, file, callback) {
callback(null, file.originalname);
}
});

// "restore" is the name attr of input file in html
var upload = multer({storage: Storage}).single("restore");

return multerPromise(upload, req, res)
})
.then((file) => {
//now the file is stored correctly, I can start the restore command
if(file){
return manager.restore(restore_path, file.originalname)
}else{
throw Error("No file provided");
}
}) //the backup has been restored in a backup database, load the backup in the main database
.then(() => manager.loadBackup())
.then(() => res.json({success:true, message: "Backup restored successfully"}))
.catch(err => {
console.log(err);
res.json({success:false, message: err.message})
});
});

// Backup DB api
app.get('/backup', (req, res) => {

manager.backup()
.then((file) => {
//zip file is ready, send it to the client
var stream = fs.createReadStream(file);

res.setHeader('Content-disposition', 'attachment; filename=' + file.split('/').pop());

stream.once("close", function () {
stream.destroy(); // makesure stream closed, not close if download aborted.
//IMPORTANT: remove backup files and zip
manager.deleteDir(path.dirname(file));
});

stream.pipe(res);
})
.catch(err => {
res.json({success: false, message: err});
});

});

// Check testDB exists, if not create one and start app listening on port 8000
influx.getDatabaseNames()
.then(names => {
if (!names.includes(TEST_DB)) {
return influx.createDatabase(TEST_DB)
}
})
.then(() => manager.init())
.then(() => {
app.listen(8000, function () {
console.log('Listening on port 8000')
})
})
.catch(err => {
console.error(`Error creating Influx database!`)
})

```

## FAQ ❓

**Q: Why I can't restore directly to the main database?**

*A: Actually InfluxDB doesn't allows to restore data in an existing database, the
only way to do this is to load the backup in a temporary database and than use
a query to load the data from the temporary database to the existing one. More details [here](https://docs.influxdata.com/influxdb/v1.7/administration/backup_and_restore/#restore-examples)*

## Author :bowtie:

[Daniel Lando](https://github.com/robertsLando)

Support me on [Patreon](https://www.patreon.com/join/2409916) :heart: