2016-09-15 11:29:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2016-09-16 11:22:04 +02:00
|
|
|
getBoxBackupDetails: getBoxBackupDetails,
|
2016-09-16 11:21:08 +02:00
|
|
|
getAppBackupDetails: getAppBackupDetails,
|
|
|
|
|
|
2016-09-16 10:59:17 +02:00
|
|
|
getRestoreUrl: getRestoreUrl,
|
2016-09-19 15:03:38 +02:00
|
|
|
getAppRestoreConfig: getAppRestoreConfig,
|
2016-09-16 18:14:36 +02:00
|
|
|
getLocalFilePath: getLocalFilePath,
|
2016-09-16 10:59:17 +02:00
|
|
|
|
2016-10-10 15:04:28 +02:00
|
|
|
copyObject: copyObject,
|
|
|
|
|
removeBackup: removeBackup
|
2016-09-15 11:29:45 +02:00
|
|
|
};
|
|
|
|
|
|
2016-09-16 11:55:59 +02:00
|
|
|
var assert = require('assert'),
|
2016-10-10 16:10:51 +02:00
|
|
|
async = require('async'),
|
2016-10-10 13:21:45 +02:00
|
|
|
BackupsError = require('../backups.js').BackupsError,
|
2016-09-16 11:55:59 +02:00
|
|
|
fs = require('fs'),
|
2016-09-19 15:03:38 +02:00
|
|
|
path = require('path'),
|
2016-10-10 16:10:51 +02:00
|
|
|
safe = require('safetydance'),
|
|
|
|
|
shell = require('../shell.js');
|
2016-09-16 11:55:59 +02:00
|
|
|
|
2016-09-16 14:10:34 +02:00
|
|
|
var FALLBACK_BACKUP_FOLDER = '/var/backups';
|
2016-10-10 16:10:51 +02:00
|
|
|
var RMBACKUP_CMD = path.join(__dirname, '../scripts/rmbackup.sh');
|
2016-09-15 11:29:45 +02:00
|
|
|
|
2016-09-16 11:22:04 +02:00
|
|
|
function getBoxBackupDetails(apiConfig, id, callback) {
|
2016-09-15 11:29:45 +02:00
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
2016-09-16 10:58:34 +02:00
|
|
|
assert.strictEqual(typeof id, 'string');
|
2016-09-15 11:29:45 +02:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2016-09-16 11:55:59 +02:00
|
|
|
var backupFolder = apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER;
|
2016-09-16 10:58:34 +02:00
|
|
|
|
|
|
|
|
var details = {
|
|
|
|
|
backupScriptArguments: [ 'filesystem', backupFolder, id, apiConfig.key ]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
callback(null, details);
|
2016-09-15 11:29:45 +02:00
|
|
|
}
|
|
|
|
|
|
2016-09-16 11:21:08 +02:00
|
|
|
function getAppBackupDetails(apiConfig, appId, dataId, configId, callback) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof appId, 'string');
|
|
|
|
|
assert.strictEqual(typeof dataId, 'string');
|
2016-09-16 16:54:34 +02:00
|
|
|
assert.strictEqual(typeof configId, 'string');
|
2016-09-16 11:21:08 +02:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2016-09-16 11:55:59 +02:00
|
|
|
var backupFolder = apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER;
|
2016-09-16 11:21:08 +02:00
|
|
|
|
|
|
|
|
var details = {
|
|
|
|
|
backupScriptArguments: [ 'filesystem', appId, backupFolder, configId, dataId, apiConfig.key ]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
callback(null, details);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 11:29:45 +02:00
|
|
|
function getRestoreUrl(apiConfig, filename, callback) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof filename, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2016-09-16 12:00:20 +02:00
|
|
|
var backupFolder = apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER;
|
|
|
|
|
var restoreUrl = 'file://' + path.join(backupFolder, filename);
|
|
|
|
|
|
|
|
|
|
callback(null, { url: restoreUrl });
|
2016-09-15 11:29:45 +02:00
|
|
|
}
|
|
|
|
|
|
2016-09-19 15:03:38 +02:00
|
|
|
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));
|
2016-10-10 13:21:45 +02:00
|
|
|
if (!restoreConfig) return callback(new BackupsError(BackupsError.NOT_FOUND, 'No app backup config found for ' + configFilename));
|
2016-09-19 15:03:38 +02:00
|
|
|
|
|
|
|
|
callback(null, restoreConfig);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-16 18:14:36 +02:00
|
|
|
function getLocalFilePath(apiConfig, filename, callback) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof filename, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
var backupFolder = apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER;
|
|
|
|
|
|
|
|
|
|
callback(null, { filePath: path.join(backupFolder, filename) });
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 11:29:45 +02:00
|
|
|
function copyObject(apiConfig, from, to, callback) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof from, 'string');
|
|
|
|
|
assert.strictEqual(typeof to, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2016-09-16 11:55:59 +02:00
|
|
|
var calledBack = false;
|
|
|
|
|
function done (error) {
|
|
|
|
|
if (!calledBack) callback(error);
|
|
|
|
|
calledBack = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var backupFolder = apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER;
|
|
|
|
|
var readStream = fs.createReadStream(path.join(backupFolder, from));
|
|
|
|
|
var writeStream = fs.createWriteStream(path.join(backupFolder, to));
|
|
|
|
|
|
|
|
|
|
readStream.on('error', done);
|
|
|
|
|
writeStream.on('error', done);
|
|
|
|
|
|
|
|
|
|
writeStream.on('close', function () {
|
|
|
|
|
// avoid passing arguments
|
|
|
|
|
done(null);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
readStream.pipe(writeStream);
|
2016-09-15 11:29:45 +02:00
|
|
|
}
|
2016-10-10 15:04:28 +02:00
|
|
|
|
2016-10-10 15:45:12 +02:00
|
|
|
function removeBackup(apiConfig, backupId, appBackupIds, callback) {
|
2016-10-10 15:04:28 +02:00
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof backupId, 'string');
|
2016-10-10 15:45:12 +02:00
|
|
|
assert(Array.isArray(appBackupIds));
|
2016-10-10 15:04:28 +02:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2016-10-10 15:45:12 +02:00
|
|
|
var backupFolder = apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER;
|
2016-10-10 16:21:58 +02:00
|
|
|
var appBackupJSONFiles = appBackupIds.map(function (id) { return id.replace(/\.tar\.gz$/, '.json'); });
|
2016-10-10 15:45:12 +02:00
|
|
|
|
2016-10-10 16:21:58 +02:00
|
|
|
async.each([backupId].concat(appBackupIds).concat(appBackupJSONFiles), function (id, callback) {
|
2016-10-10 15:45:12 +02:00
|
|
|
var filePath = path.join(backupFolder, id);
|
2016-10-10 15:04:28 +02:00
|
|
|
|
2016-10-10 16:10:51 +02:00
|
|
|
shell.sudo('deleteBackup', [ RMBACKUP_CMD, filePath ], function (error) {
|
|
|
|
|
if (error) console.error('Unable to remove %s. Not fatal.', filePath, safe.error);
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
}, callback);
|
2016-10-10 15:04:28 +02:00
|
|
|
}
|