Ghost/core/server/data/db/backup.js
Vikas Potluri 00c324fa4e
Moved core/server/lib/common/logging to core/shared/logging (#11857)
- Represents that logging is shared across all parts of Ghost at present
  * moved core/server/lib/common/logging to core/shared/logging
  * updated logging path for generic imports
  * updated migration and schema imports of logging
  * updated tests and index logging import
  * 🔥 removed logging from common module
  * fixed tests
2020-05-28 19:30:23 +01:00

61 lines
1.7 KiB
JavaScript

// # Backup Database
// Provides for backing up the database before making potentially destructive changes
const fs = require('fs-extra');
const path = require('path');
const Promise = require('bluebird');
const config = require('../../../shared/config');
const logging = require('../../../shared/logging');
const urlUtils = require('../../../shared/url-utils');
const exporter = require('../exporter');
let writeExportFile;
let backup;
writeExportFile = function writeExportFile(exportResult) {
const filename = path.resolve(urlUtils.urlJoin(config.get('paths').contentPath, 'data', exportResult.filename));
return fs.writeFile(filename, JSON.stringify(exportResult.data)).return(filename);
};
const readBackup = async (filename) => {
const parsedFileName = path.parse(filename);
const sanitized = `${parsedFileName.name}${parsedFileName.ext}`;
const backupPath = path.resolve(urlUtils.urlJoin(config.get('paths').contentPath, 'data', sanitized));
const exists = await fs.pathExists(backupPath);
if (exists) {
const backup = await fs.readFile(backupPath);
return JSON.parse(backup);
} else {
return null;
}
};
/**
* ## Backup
* does an export, and stores this in a local file
* @returns {Promise<*>}
*/
backup = function backup(options) {
logging.info('Creating database backup');
options = options || {};
const props = {
data: exporter.doExport(options),
filename: exporter.fileName(options)
};
return Promise.props(props)
.then(writeExportFile)
.then(function successMessage(filename) {
logging.info('Database backup written to: ' + filename);
return filename;
});
};
module.exports = {
backup,
readBackup
};