diff --git a/src/backups.js b/src/backups.js index 6d309422c..82882a226 100644 --- a/src/backups.js +++ b/src/backups.js @@ -116,33 +116,23 @@ function getByAppIdPaged(page, perPage, appId, callback) { }); } -// backupId is the s3 filename. appbackup_%s_%s-v%s.tar.gz +// backupId is the filename. appbackup_%s_%s-v%s.tar.gz function getRestoreConfig(backupId, callback) { assert.strictEqual(typeof backupId, 'string'); assert.strictEqual(typeof callback, 'function'); - var configFile = backupId.replace(/\.tar\.gz$/, '.json'); - settings.getBackupConfig(function (error, backupConfig) { if (error) return callback(new BackupsError(BackupsError.INTERNAL_ERROR, error)); - api(backupConfig.provider).getRestoreUrl(backupConfig, configFile, function (error, result) { - if (error) return callback(error); + api(backupConfig.provider).getAppRestoreConfig(backupConfig, backupId, function (error, result) { + if (error) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error)); - superagent.get(result.url).buffer(true).timeout(30 * 1000).end(function (error, response) { - if (error && !error.response) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message)); - if (response.statusCode !== 200) return callback(new Error('Invalid response code when getting config.json : ' + response.statusCode)); - - var config = safe.JSON.parse(response.text); - if (!config) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, 'Error in config:' + safe.error.message)); - - return callback(null, config); - }); + callback(null, result); }); }); } -// backupId is the s3 filename. appbackup_%s_%s-v%s.tar.gz +// backupId is the filename. appbackup_%s_%s-v%s.tar.gz function getRestoreUrl(backupId, callback) { assert.strictEqual(typeof backupId, 'string'); assert.strictEqual(typeof callback, 'function'); diff --git a/src/storage/caas.js b/src/storage/caas.js index 7c80bb08d..398609e58 100644 --- a/src/storage/caas.js +++ b/src/storage/caas.js @@ -5,6 +5,7 @@ exports = module.exports = { getAppBackupDetails: getAppBackupDetails, getRestoreUrl: getRestoreUrl, + getAppRestoreConfig: getAppRestoreConfig, getLocalFilePath: getLocalFilePath, copyObject: copyObject @@ -13,6 +14,7 @@ exports = module.exports = { var assert = require('assert'), AWS = require('aws-sdk'), config = require('../config.js'), + safe = require('safetydance'), superagent = require('superagent'); function getBackupCredentials(apiConfig, callback) { @@ -106,6 +108,28 @@ function getRestoreUrl(apiConfig, filename, callback) { }); } +function getAppRestoreConfig(apiConfig, backupId, callback) { + assert.strictEqual(typeof apiConfig, 'object'); + assert.strictEqual(typeof backupId, 'string'); + assert.strictEqual(typeof callback, 'function'); + + var configFilename = backupId.replace(/\.tar\.gz$/, '.json'); + + getRestoreUrl(apiConfig, configFilename, function (error, result) { + if (error) return callback(error); + + superagent.get(result.url).buffer(true).timeout(30 * 1000).end(function (error, response) { + if (error && !error.response) return callback(new Error(error.message)); + if (response.statusCode !== 200) return callback(new Error('Invalid response code when getting config.json : ' + response.statusCode)); + + var config = safe.JSON.parse(response.text); + if (!config) return callback(new Error('Error in config:' + safe.error.message)); + + return callback(null, config); + }); + }); +} + function getLocalFilePath(apiConfig, filename, callback) { assert.strictEqual(typeof apiConfig, 'object'); assert.strictEqual(typeof filename, 'string'); diff --git a/src/storage/filesystem.js b/src/storage/filesystem.js index c6a3e5f38..7ac8a0621 100644 --- a/src/storage/filesystem.js +++ b/src/storage/filesystem.js @@ -5,6 +5,7 @@ exports = module.exports = { getAppBackupDetails: getAppBackupDetails, getRestoreUrl: getRestoreUrl, + getAppRestoreConfig: getAppRestoreConfig, getLocalFilePath: getLocalFilePath, copyObject: copyObject @@ -12,7 +13,8 @@ exports = module.exports = { var assert = require('assert'), fs = require('fs'), - path = require('path'); + path = require('path'), + safe = require('safetydance'); var FALLBACK_BACKUP_FOLDER = '/var/backups'; @@ -57,6 +59,20 @@ function getRestoreUrl(apiConfig, filename, callback) { callback(null, { url: restoreUrl }); } +function getAppRestoreConfig(apiConfig, backupId, callback) { + assert.strictEqual(typeof apiConfig, 'object'); + assert.strictEqual(typeof backupId, 'string'); + assert.strictEqual(typeof callback, 'function'); + + var backupFolder = apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER; + var configFilename = backupId.replace(/\.tar\.gz$/, '.json'); + + var restoreConfig = safe.require(path.join(backupFolder, configFilename)); + if (!restoreConfig) return callback(new Error('No app backup config found')); + + callback(null, restoreConfig); +} + function getLocalFilePath(apiConfig, filename, callback) { assert.strictEqual(typeof apiConfig, 'object'); assert.strictEqual(typeof filename, 'string'); diff --git a/src/storage/interface.js b/src/storage/interface.js index a8d82e91f..d399177c9 100644 --- a/src/storage/interface.js +++ b/src/storage/interface.js @@ -11,6 +11,7 @@ exports = module.exports = { getAppBackupDetails: getAppBackupDetails, getRestoreUrl: getRestoreUrl, + getAppRestoreConfig: getAppRestoreConfig, getLocalFilePath: getLocalFilePath, copyObject: copyObject @@ -53,6 +54,19 @@ function getRestoreUrl(apiConfig, filename, callback) { callback(new Error('not implemented')); } +function getAppRestoreConfig(apiConfig, backupId, callback) { + assert.strictEqual(typeof apiConfig, 'object'); + assert.strictEqual(typeof backupId, 'string'); + assert.strictEqual(typeof callback, 'function'); + + // var configFilename = backupId.replace(/\.tar\.gz$/, '.json'); + + // Result: {} <- Backup config object from .json file + // The resulting url must work with curl as it is passed into start.sh and restoreapp.sh + + callback(new Error('not implemented')); +} + function getLocalFilePath(apiConfig, filename, callback) { assert.strictEqual(typeof apiConfig, 'object'); assert.strictEqual(typeof filename, 'string'); diff --git a/src/storage/s3.js b/src/storage/s3.js index 6ac098d30..d31249ceb 100644 --- a/src/storage/s3.js +++ b/src/storage/s3.js @@ -5,13 +5,16 @@ exports = module.exports = { getAppBackupDetails: getAppBackupDetails, getRestoreUrl: getRestoreUrl, + getAppRestoreConfig: getAppRestoreConfig, getLocalFilePath: getLocalFilePath, copyObject: copyObject }; var assert = require('assert'), - AWS = require('aws-sdk'); + AWS = require('aws-sdk'), + safe = require('safetydance'), + superagent = require('superagent'); function getBackupCredentials(apiConfig, callback) { assert.strictEqual(typeof apiConfig, 'object'); @@ -86,6 +89,28 @@ function getRestoreUrl(apiConfig, filename, callback) { }); } +function getAppRestoreConfig(apiConfig, backupId, callback) { + assert.strictEqual(typeof apiConfig, 'object'); + assert.strictEqual(typeof backupId, 'string'); + assert.strictEqual(typeof callback, 'function'); + + var configFilename = backupId.replace(/\.tar\.gz$/, '.json'); + + getRestoreUrl(apiConfig, configFilename, function (error, result) { + if (error) return callback(error); + + superagent.get(result.url).buffer(true).timeout(30 * 1000).end(function (error, response) { + if (error && !error.response) return callback(new Error(error.message)); + if (response.statusCode !== 200) return callback(new Error('Invalid response code when getting config.json : ' + response.statusCode)); + + var config = safe.JSON.parse(response.text); + if (!config) return callback(new Error('Error in config:' + safe.error.message)); + + return callback(null, config); + }); + }); +} + function getLocalFilePath(apiConfig, filename, callback) { assert.strictEqual(typeof apiConfig, 'object'); assert.strictEqual(typeof filename, 'string');