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,
|
|
|
|
|
|
|
|
|
|
copyObject: copyObject
|
2016-09-15 11:29:45 +02:00
|
|
|
};
|
|
|
|
|
|
2016-09-16 11:55:59 +02:00
|
|
|
var assert = require('assert'),
|
|
|
|
|
fs = require('fs'),
|
|
|
|
|
path = require('path');
|
|
|
|
|
|
2016-09-16 14:10:34 +02:00
|
|
|
var FALLBACK_BACKUP_FOLDER = '/var/backups';
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|