2016-03-13 23:49:30 +03:00
|
|
|
// # Backup Database
|
|
|
|
// Provides for backing up the database before making potentially destructive changes
|
2017-12-13 21:15:29 +03:00
|
|
|
var fs = require('fs-extra'),
|
2017-12-11 21:14:05 +03:00
|
|
|
path = require('path'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
config = require('../../config'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../../lib/common'),
|
2019-06-18 16:13:55 +03:00
|
|
|
urlUtils = require('../../lib/url-utils'),
|
2018-07-20 16:59:54 +03:00
|
|
|
exporter = require('../exporter'),
|
2016-03-13 23:49:30 +03:00
|
|
|
|
|
|
|
writeExportFile,
|
|
|
|
backup;
|
|
|
|
|
|
|
|
writeExportFile = function writeExportFile(exportResult) {
|
2019-06-18 16:13:55 +03:00
|
|
|
var filename = path.resolve(urlUtils.urlJoin(config.get('paths').contentPath, 'data', exportResult.filename));
|
2016-03-13 23:49:30 +03:00
|
|
|
|
2017-12-13 22:03:07 +03:00
|
|
|
return fs.writeFile(filename, JSON.stringify(exportResult.data)).return(filename);
|
2016-03-13 23:49:30 +03:00
|
|
|
};
|
|
|
|
|
2019-12-16 14:34:26 +03:00
|
|
|
const readBackup = async (filename) => {
|
2019-12-17 11:08:04 +03:00
|
|
|
const parsedFileName = path.parse(filename);
|
|
|
|
const sanitized = `${parsedFileName.name}${parsedFileName.ext}`;
|
|
|
|
const backupPath = path.resolve(urlUtils.urlJoin(config.get('paths').contentPath, 'data', sanitized));
|
2019-12-16 14:34:26 +03:00
|
|
|
|
|
|
|
const exists = await fs.pathExists(backupPath);
|
|
|
|
|
|
|
|
if (exists) {
|
|
|
|
const backup = await fs.readFile(backupPath);
|
|
|
|
return JSON.parse(backup);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-13 23:49:30 +03:00
|
|
|
/**
|
|
|
|
* ## Backup
|
|
|
|
* does an export, and stores this in a local file
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
2017-08-01 16:27:13 +03:00
|
|
|
backup = function backup(options) {
|
2017-12-12 00:47:46 +03:00
|
|
|
common.logging.info('Creating database backup');
|
2017-08-01 16:27:13 +03:00
|
|
|
options = options || {};
|
2016-03-13 23:49:30 +03:00
|
|
|
|
|
|
|
var props = {
|
2017-08-01 16:27:13 +03:00
|
|
|
data: exporter.doExport(options),
|
|
|
|
filename: exporter.fileName(options)
|
2016-03-13 23:49:30 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return Promise.props(props)
|
|
|
|
.then(writeExportFile)
|
|
|
|
.then(function successMessage(filename) {
|
2017-12-12 00:47:46 +03:00
|
|
|
common.logging.info('Database backup written to: ' + filename);
|
2018-01-11 18:03:21 +03:00
|
|
|
return filename;
|
2016-03-13 23:49:30 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-12-16 11:29:24 +03:00
|
|
|
module.exports = {
|
2019-12-16 14:34:26 +03:00
|
|
|
backup,
|
|
|
|
readBackup
|
2019-12-16 11:29:24 +03:00
|
|
|
};
|