项目作者: robertsLando

项目描述 :
Node influxdb backup/restore module.
高级语言: JavaScript
项目地址: git://github.com/robertsLando/node-influxdb-backup.git
创建时间: 2019-03-01T12:59:41Z
项目社区:https://github.com/robertsLando/node-influxdb-backup

开源协议:MIT License

下载


influx-backup

NPM version
Downloads

PRs Welcome
MIT Licence

NPM

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 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

  1. npm install influx-backup --save

Usage 🔧

Check JSDocs and the Express example.

Here are some lines from examples/app.js

  1. const BackupManager = require('influx-backup');
  2. const TEST_DB = "testDB";
  3. const manager = new BackupManager({db: TEST_DB});
  4. // Restore DB api
  5. app.post('/restore', (req, res) => {
  6. var restore_path = "";
  7. // create a temp directory to use for the backup
  8. manager.createDir()
  9. .then((dir) => {
  10. restore_path = dir;
  11. // Use multer to get the file from the req
  12. // and store it in the temp dir created
  13. var Storage = multer.diskStorage({
  14. destination: restore_path,
  15. filename: function(req, file, callback) {
  16. callback(null, file.originalname);
  17. }
  18. });
  19. // "restore" is the name attr of input file in html
  20. var upload = multer({storage: Storage}).single("restore");
  21. return multerPromise(upload, req, res)
  22. })
  23. .then((file) => {
  24. //now the file is stored correctly, I can start the restore command
  25. if(file){
  26. return manager.restore(restore_path, file.originalname)
  27. }else{
  28. throw Error("No file provided");
  29. }
  30. }) //the backup has been restored in a backup database, load the backup in the main database
  31. .then(() => manager.loadBackup())
  32. .then(() => res.json({success:true, message: "Backup restored successfully"}))
  33. .catch(err => {
  34. console.log(err);
  35. res.json({success:false, message: err.message})
  36. });
  37. });
  38. // Backup DB api
  39. app.get('/backup', (req, res) => {
  40. manager.backup()
  41. .then((file) => {
  42. //zip file is ready, send it to the client
  43. var stream = fs.createReadStream(file);
  44. res.setHeader('Content-disposition', 'attachment; filename=' + file.split('/').pop());
  45. stream.once("close", function () {
  46. stream.destroy(); // makesure stream closed, not close if download aborted.
  47. //IMPORTANT: remove backup files and zip
  48. manager.deleteDir(path.dirname(file));
  49. });
  50. stream.pipe(res);
  51. })
  52. .catch(err => {
  53. res.json({success: false, message: err});
  54. });
  55. });
  56. // Check testDB exists, if not create one and start app listening on port 8000
  57. influx.getDatabaseNames()
  58. .then(names => {
  59. if (!names.includes(TEST_DB)) {
  60. return influx.createDatabase(TEST_DB)
  61. }
  62. })
  63. .then(() => manager.init())
  64. .then(() => {
  65. app.listen(8000, function () {
  66. console.log('Listening on port 8000')
  67. })
  68. })
  69. .catch(err => {
  70. console.error(`Error creating Influx database!`)
  71. })

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

Author :bowtie:

Daniel Lando

Support me on Patreon :heart: