2016-03-13 23:49:30 +03:00
|
|
|
// # Backup Database
|
|
|
|
// Provides for backing up the database before making potentially destructive changes
|
2017-01-25 22:34:31 +03:00
|
|
|
var fs = require('fs'),
|
2016-03-13 23:49:30 +03:00
|
|
|
path = require('path'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
config = require('../../config'),
|
2017-01-25 22:34:31 +03:00
|
|
|
logging = require('../../logging'),
|
2016-11-14 17:38:55 +03:00
|
|
|
utils = require('../../utils'),
|
2017-01-25 22:34:31 +03:00
|
|
|
exporter = require('../export'),
|
2016-03-13 23:49:30 +03:00
|
|
|
|
|
|
|
writeExportFile,
|
|
|
|
backup;
|
|
|
|
|
|
|
|
writeExportFile = function writeExportFile(exportResult) {
|
2016-11-14 17:38:55 +03:00
|
|
|
var filename = path.resolve(utils.url.urlJoin(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
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
2017-01-25 22:34:31 +03:00
|
|
|
backup = function backup() {
|
|
|
|
logging.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) {
|
2017-01-25 22:34:31 +03:00
|
|
|
logging.info('Database backup written to: ' + filename);
|
2016-03-13 23:49:30 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = backup;
|