Backup endpoint improvements (#9365)
closes #9297 - backup endpoint returns JSON - allows setting of export filename - DRY up code - the endpoint is not documented, no breaking change
This commit is contained in:
parent
b9d3f45402
commit
b69b9780a9
@ -1,17 +1,13 @@
|
||||
// # DB API
|
||||
// API for DB operations
|
||||
var Promise = require('bluebird'),
|
||||
path = require('path'),
|
||||
fs = require('fs-extra'),
|
||||
pipeline = require('../lib/promise/pipeline'),
|
||||
localUtils = require('./utils'),
|
||||
exporter = require('../data/export'),
|
||||
importer = require('../data/importer'),
|
||||
backupDatabase = require('../data/db/backup'),
|
||||
models = require('../models'),
|
||||
config = require('../config'),
|
||||
common = require('../lib/common'),
|
||||
urlService = require('../services/url'),
|
||||
docName = 'db',
|
||||
db;
|
||||
|
||||
@ -23,26 +19,26 @@ var Promise = require('bluebird'),
|
||||
db = {
|
||||
/**
|
||||
* ### Archive Content
|
||||
* Generate the JSON to export - for Moya only
|
||||
* Generate the JSON to export
|
||||
*
|
||||
* @public
|
||||
* @returns {Promise} Ghost Export JSON format
|
||||
*/
|
||||
backupContent: function () {
|
||||
var props = {
|
||||
data: exporter.doExport(),
|
||||
filename: exporter.fileName()
|
||||
};
|
||||
backupContent: function (options) {
|
||||
var tasks;
|
||||
|
||||
return Promise.props(props)
|
||||
.then(function successMessage(exportResult) {
|
||||
var filename = path.resolve(urlService.utils.urlJoin(config.get('paths').contentPath, 'data', exportResult.filename));
|
||||
options = options || {};
|
||||
|
||||
return fs.writeFile(filename, JSON.stringify(exportResult.data))
|
||||
.then(function () {
|
||||
return filename;
|
||||
});
|
||||
});
|
||||
function jsonResponse(filename) {
|
||||
return {db: [{filename: filename}]};
|
||||
}
|
||||
|
||||
tasks = [
|
||||
backupDatabase,
|
||||
jsonResponse
|
||||
];
|
||||
|
||||
return pipeline(tasks, options);
|
||||
},
|
||||
/**
|
||||
* ### Export Content
|
||||
|
@ -35,6 +35,7 @@ backup = function backup(options) {
|
||||
.then(writeExportFile)
|
||||
.then(function successMessage(filename) {
|
||||
common.logging.info('Database backup written to: ' + filename);
|
||||
return filename;
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -21,6 +21,13 @@ exportFileName = function exportFileName(options) {
|
||||
var datetime = (new Date()).toJSON().substring(0, 10),
|
||||
title = '';
|
||||
|
||||
options = options || {};
|
||||
|
||||
// custom filename
|
||||
if (options.filename) {
|
||||
return Promise.resolve(options.filename + '.json');
|
||||
}
|
||||
|
||||
return models.Settings.findOne({key: 'title'}, _.merge({}, modelOptions, options)).then(function (result) {
|
||||
if (result) {
|
||||
title = security.string.safe(result.get('value')) + '.';
|
||||
|
@ -116,7 +116,28 @@ describe('DB API', function () {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.match(/data/);
|
||||
|
||||
(typeof res.body).should.be.Object;
|
||||
should.exist(res.body.db[0].filename);
|
||||
fsStub.calledOnce.should.eql(true);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('export can be triggered and named by backup client', function (done) {
|
||||
backupQuery = '?client_id=' + backupClient.slug + '&client_secret=' + backupClient.secret + '&filename=test';
|
||||
fsStub = sandbox.stub(fs, 'writeFile').resolves();
|
||||
request.post(testUtils.API.getApiQuery('db/backup' + backupQuery))
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(200)
|
||||
.end(function (err, res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
|
||||
(typeof res.body).should.be.Object;
|
||||
res.body.db[0].filename.should.match(/test\.json/);
|
||||
fsStub.calledOnce.should.eql(true);
|
||||
|
||||
done();
|
||||
|
Loading…
Reference in New Issue
Block a user