Last active
February 23, 2020 19:07
-
-
Save robertsLando/bf5f691772ccf00e2a144e4f1ea4ee3a to your computer and use it in GitHub Desktop.
Influx DB (v 1.5.0+) Backup/Restore, Retention Policy and APIs helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Influx = require('influx'); | |
var QL = require('influx-ql'); //influx query builder | |
var logger = require('../config/logger.js'); //winston logger | |
var configDB = require('../config/database.js'); //database constants configuration file | |
var cmd = require('node-cmd'); //to execute commands | |
var backup_path = __dirname + configDB.influx_backup; //path to temporary folder used to store backups files | |
// UTILS FUNCTIONS | |
function dateToNano(date){ | |
return (date.getTime() * 1000000).toString(); | |
} | |
// InfluxDB (https://node-influx.github.io/class/src/index.js~InfluxDB.html) | |
// Query builder https://vicanso.github.io/influx-ql/QL.html | |
const influx = new Influx.InfluxDB({ | |
database: configDB.influx_DB, | |
host: 'localhost', | |
port: 8086, | |
// username: configDB.influx_user, | |
// password: configDB.influx_psw, | |
}); | |
module.exports = { | |
createDatabase: function(callback){ | |
influx.query(`CREATE DATABASE ${configDB.influx_DB}`).then(rows => { | |
logger.info("Influx DB Database Created"); | |
if(callback) callback(); | |
}).catch(err => { | |
logger.error(err); | |
if(callback) callback(err); | |
}); | |
}, | |
dropDatabase: function(callback){ | |
influx.query(`DROP DATABASE ${configDB.influx_DB}`).then(rows => { | |
logger.info("Influx DB Database Dropped"); | |
if(callback) callback(); | |
}).catch(err => { | |
logger.error(err); | |
if(callback) callback(err); | |
}); | |
}, | |
setRetentionPolicy: function(days, callback){ | |
var query = `ALTER RETENTION POLICY "autogen" ON "${configDB.influx_DB}" DURATION ${days == 0 ? 'INF' : days + 'd'}`; | |
influx.query(query).then(rows => { | |
callback(null); | |
}).catch(err => { | |
callback(err); | |
}) | |
}, | |
getRetentionPolicy: function(callback){ | |
var query = "SHOW RETENTION POLICIES"; | |
influx.query(query).then(rows => { | |
if(rows && rows.length > 0) | |
callback(null, rows); | |
else | |
callback("Error while getting retention policy", null); | |
}).catch(err => { | |
callback(err, null); | |
}) | |
}, | |
backupDatabase: function(start, end, callback){ | |
var command = `influxd backup -portable -database ${configDB.influx_DB} `; | |
//add period if valid | |
if(start && end) | |
command += `-start ${start.toISOString()} -end ${end.toISOString()} `; | |
command += './'; | |
var backup_name = (new Date()).toISOString(); | |
backup_name = backup_name.split(':').join(''); | |
backup_name = backup_name.split('-').join(''); | |
backup_name = backup_name.split('.')[0] + 'Z'; | |
var zip_name = `backup_${backup_name}`; | |
var download_path = `${backup_path}/${zip_name}.zip`; | |
//- go to backups folder | |
//- backup database | |
//- zip backup files | |
command = `cd ${backup_path} && ${command} && zip ${zip_name} ${backup_name}*` | |
cmd.get(command, function(err, data, stderr){ | |
if(err){ | |
logger.error(err); | |
if(callback) callback(err); | |
}else { | |
logger.info('Backup correctly created and zipped'); | |
if(callback) callback(null, download_path); | |
} | |
}); | |
}, | |
clearBackupFolder: function(callback){ | |
cmd.get(`cd ${backup_path} && rm -f *`, function(err, data, stderr){ | |
if(err){ | |
logger.error(err); | |
if(callback) callback(false) | |
}else { | |
if(callback) callback(true); | |
logger.info('Backup folder cleared'); | |
} | |
}); | |
}, | |
restoreBackup: function(fileName, callback){ | |
//- drop existing db | |
//- go to backups folder | |
//- unzip uploaded backup file | |
//- restore backup | |
//- remove backup files | |
//NOTE: Be sure the directory contains just one backup | |
command = `influx -execute 'drop database ${configDB.influx_DB}' && cd ${backup_path} && unzip ${fileName} && influxd restore -portable ./ && rm -f *`; | |
cmd.get(command, function(err, data, stderr){ | |
if(err){ | |
logger.error(err); | |
if(callback) callback(err) | |
}else { | |
if(callback) callback(null); | |
logger.info('Backup restored'); | |
} | |
}); | |
} | |
//ADD HERE YOUR QUERIES | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment