2016-03-13 23:49:30 +03:00
|
|
|
// # Backup Database
|
|
|
|
// Provides for backing up the database before making potentially destructive changes
|
|
|
|
var _ = require('lodash'),
|
|
|
|
fs = require('fs'),
|
|
|
|
path = require('path'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
config = require('../../config'),
|
|
|
|
exporter = require('../export'),
|
|
|
|
|
|
|
|
writeExportFile,
|
|
|
|
backup;
|
|
|
|
|
|
|
|
writeExportFile = function writeExportFile(exportResult) {
|
2016-09-13 18:41:14 +03:00
|
|
|
var filename = path.resolve(config.get('paths').contentPath + '/data/' + exportResult.filename);
|
2016-03-13 23:49:30 +03:00
|
|
|
|
|
|
|
return Promise.promisify(fs.writeFile)(filename, JSON.stringify(exportResult.data)).return(filename);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ## Backup
|
|
|
|
* does an export, and stores this in a local file
|
|
|
|
*
|
2016-03-21 15:44:23 +03:00
|
|
|
* @param {{info: logger.info, warn: logger.warn}} [logger]
|
2016-03-13 23:49:30 +03:00
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
2016-03-21 15:44:23 +03:00
|
|
|
backup = function backup(logger) {
|
|
|
|
// If we get passed a function, use it to output notices, else don't do anything
|
|
|
|
logger = logger && _.isFunction(logger.info) ? logger : {info: _.noop};
|
2016-03-13 23:49:30 +03:00
|
|
|
|
2016-03-21 15:44:23 +03:00
|
|
|
logger.info('Creating database backup');
|
2016-03-13 23:49:30 +03:00
|
|
|
|
|
|
|
var props = {
|
|
|
|
data: exporter.doExport(),
|
|
|
|
filename: exporter.fileName()
|
|
|
|
};
|
|
|
|
|
|
|
|
return Promise.props(props)
|
|
|
|
.then(writeExportFile)
|
|
|
|
.then(function successMessage(filename) {
|
2016-03-21 15:44:23 +03:00
|
|
|
logger.info('Database backup written to: ' + filename);
|
2016-03-13 23:49:30 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = backup;
|