2016-09-15 11:29:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2017-04-11 11:00:55 +02:00
|
|
|
backup: backup,
|
|
|
|
|
restore: restore,
|
2016-09-16 11:21:08 +02:00
|
|
|
|
2017-04-14 13:48:39 +02:00
|
|
|
saveAppRestoreConfig: saveAppRestoreConfig,
|
2016-09-19 15:03:38 +02:00
|
|
|
getAppRestoreConfig: getAppRestoreConfig,
|
2017-04-14 13:48:39 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
getDownloadStream: getDownloadStream,
|
2016-09-16 10:59:17 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
copyBackup: copyBackup,
|
2016-10-11 11:36:25 +02:00
|
|
|
removeBackup: removeBackup,
|
|
|
|
|
|
2017-01-04 16:22:58 -08:00
|
|
|
backupDone: backupDone,
|
|
|
|
|
|
2016-10-11 11:36:25 +02:00
|
|
|
testConfig: testConfig
|
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,
|
2017-04-11 11:00:55 +02:00
|
|
|
debug = require('debug')('box:storage/filesystem'),
|
2016-09-16 11:55:59 +02:00
|
|
|
fs = require('fs'),
|
2016-09-19 15:03:38 +02:00
|
|
|
path = require('path'),
|
2017-04-11 11:00:55 +02:00
|
|
|
mkdirp = require('mkdirp'),
|
|
|
|
|
once = require('once'),
|
2016-10-10 16:10:51 +02:00
|
|
|
safe = require('safetydance'),
|
2016-10-11 11:36:25 +02:00
|
|
|
SettingsError = require('../settings.js').SettingsError,
|
2016-10-10 18:11:23 +02:00
|
|
|
shell = require('../shell.js'),
|
2017-04-11 11:00:55 +02:00
|
|
|
tar = require('tar-fs'),
|
|
|
|
|
archiver = require('archiver');
|
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
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
function backup(apiConfig, backupId, sourceDirectories, callback) {
|
2016-09-15 11:29:45 +02:00
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
2017-04-11 11:00:55 +02:00
|
|
|
assert.strictEqual(typeof backupId, 'string');
|
|
|
|
|
assert(Array.isArray(sourceDirectories));
|
2016-09-15 11:29:45 +02:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
callback = once(callback);
|
2016-09-16 10:58:34 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
var backupFilePath = path.join(apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER, backupId + '.tar.gz');
|
2016-09-16 10:58:34 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
debug('[%s] backup: %j -> %s', backupId, sourceDirectories, backupFilePath);
|
2016-09-15 11:29:45 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
mkdirp(path.dirname(backupFilePath), function (error) {
|
2017-04-14 13:48:39 +02:00
|
|
|
if (error) return callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
2016-09-16 11:21:08 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
var fileStream = fs.createWriteStream(backupFilePath);
|
|
|
|
|
var archive = archiver('tar', { gzip: true });
|
2016-09-16 11:21:08 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
fileStream.on('error', function (error) {
|
|
|
|
|
console.error('[%s] backup: out stream error.', error);
|
|
|
|
|
});
|
2016-09-16 11:21:08 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
archive.on('error', function (error) {
|
|
|
|
|
console.error('[%s] backup: archive stream error.', error);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
fileStream.on('close', function () {
|
|
|
|
|
debug('[%s] backup: done.', backupId);
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
archive.pipe(fileStream);
|
|
|
|
|
sourceDirectories.forEach(function (directoryMap) {
|
|
|
|
|
archive.directory(directoryMap.source, directoryMap.destination);
|
|
|
|
|
});
|
|
|
|
|
archive.finalize();
|
|
|
|
|
});
|
2016-09-16 11:21:08 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
function restore(apiConfig, backupId, destinationDirectories, callback) {
|
2016-09-15 11:29:45 +02:00
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
2017-04-11 11:00:55 +02:00
|
|
|
assert.strictEqual(typeof backupId, 'string');
|
|
|
|
|
assert(Array.isArray(destinationDirectories));
|
2016-09-15 11:29:45 +02:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
var sourceFilePath = path.join(apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER, backupId + '.tar.gz');
|
|
|
|
|
|
|
|
|
|
debug('[%s] restore: %s -> %j', backupId, sourceFilePath, destinationDirectories);
|
|
|
|
|
|
|
|
|
|
if (!fs.existsSync(sourceFilePath)) return callback(new BackupsError(BackupsError.NOT_FOUND, 'backup file does not exist'));
|
|
|
|
|
|
|
|
|
|
async.eachSeries(destinationDirectories, function (directory, callback) {
|
|
|
|
|
debug('[%s] restore: directory %s -> %s', backupId, directory.source, directory.destination);
|
|
|
|
|
|
2017-04-14 13:57:04 +02:00
|
|
|
mkdirp(directory.destination, function (error) {
|
2017-04-11 11:00:55 +02:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
var fileStream = fs.createReadStream(sourceFilePath);
|
|
|
|
|
var extract = tar.extract(directory);
|
|
|
|
|
|
|
|
|
|
fileStream.on('error', function (error) {
|
|
|
|
|
console.error('[%s] restore: file stream error.', error);
|
|
|
|
|
callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
extract.on('error', function (error) {
|
|
|
|
|
console.error('[%s] restore: extract stream error.', error);
|
|
|
|
|
callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
|
|
|
|
});
|
2016-09-16 12:00:20 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
extract.on('end', function () {
|
|
|
|
|
debug('[%s] restore: directory %s done.', backupId, directory.source);
|
|
|
|
|
callback();
|
|
|
|
|
});
|
2016-10-10 18:11:23 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
fileStream.pipe(extract);
|
|
|
|
|
});
|
|
|
|
|
}, function (error) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
debug('[%s] restore: done', backupId);
|
|
|
|
|
|
|
|
|
|
callback();
|
2016-10-10 18:11:23 +02:00
|
|
|
});
|
2016-09-15 11:29:45 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
function getDownloadStream(apiConfig, backupId, callback) {
|
2016-09-19 15:03:38 +02:00
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof backupId, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
var backupFilePath = path.join(apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER, backupId);
|
2016-09-19 15:03:38 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
debug('[%s] getDownloadStream: %s %s', backupId, backupId, backupFilePath);
|
2016-09-19 15:03:38 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
if (!fs.existsSync(backupFilePath)) return callback(new BackupsError(BackupsError.NOT_FOUND, 'backup file does not exist'));
|
|
|
|
|
|
|
|
|
|
var stream = fs.createReadStream(backupFilePath);
|
|
|
|
|
callback(null, stream);
|
2016-09-19 15:03:38 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-14 13:48:39 +02:00
|
|
|
function saveAppRestoreConfig(apiConfig, backupId, restoreConfig, callback) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof backupId, 'string');
|
|
|
|
|
assert.strictEqual(typeof restoreConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
var backupFilePath = path.join(apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER, backupId + '.json');
|
|
|
|
|
|
|
|
|
|
debug('[%s] saveAppRestoreConfig: %j -> %s', backupId, restoreConfig, backupFilePath);
|
|
|
|
|
|
|
|
|
|
mkdirp(path.dirname(backupFilePath), function (error) {
|
|
|
|
|
if (error) return callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
|
|
|
|
|
|
|
|
|
fs.writeFile(backupFilePath, JSON.stringify(restoreConfig), function (error) {
|
|
|
|
|
if (error) return callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
|
|
|
|
|
|
|
|
|
debug('[%s] saveAppRestoreConfig: done', backupId);
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
function getAppRestoreConfig(apiConfig, backupId, callback) {
|
2016-09-16 18:14:36 +02:00
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
2017-04-11 11:00:55 +02:00
|
|
|
assert.strictEqual(typeof backupId, 'string');
|
2016-09-16 18:14:36 +02:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
callback = once(callback);
|
|
|
|
|
|
|
|
|
|
var sourceFilePath = path.join(apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER, backupId + '.json');
|
|
|
|
|
|
|
|
|
|
debug('[%s] getAppRestoreConfig: %s', backupId, sourceFilePath);
|
2016-09-16 18:14:36 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
if (!fs.existsSync(sourceFilePath)) return callback(new BackupsError(BackupsError.NOT_FOUND, 'restore config file does not exist'));
|
|
|
|
|
|
|
|
|
|
var restoreConfig = safe.require(sourceFilePath);
|
|
|
|
|
if (!restoreConfig) {
|
|
|
|
|
console.error('[%s] getAppRestoreConfig: failed', safe.error);
|
|
|
|
|
return callback(new BackupsError(BackupsError.INTERNAL_ERROR, safe.error));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null, restoreConfig);
|
2016-09-16 18:14:36 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
function copyBackup(apiConfig, oldBackupId, newBackupId, callback) {
|
2016-09-15 11:29:45 +02:00
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
2017-04-11 11:00:55 +02:00
|
|
|
assert.strictEqual(typeof oldBackupId, 'string');
|
|
|
|
|
assert.strictEqual(typeof newBackupId, 'string');
|
2016-09-15 11:29:45 +02:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
callback = once(callback);
|
2016-09-16 11:55:59 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
var oldBackupFilePath = path.join(apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER, oldBackupId + '.tar.gz');
|
|
|
|
|
var newBackupFilePath = path.join(apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER, newBackupId + '.tar.gz');
|
2016-09-16 11:55:59 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
// FIXME this most likely has a permissions issue as this process runs as yellowtent not root
|
|
|
|
|
mkdirp(path.dirname(newBackupFilePath), function (error) {
|
|
|
|
|
if (error) return callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
2016-09-16 11:55:59 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
var readStream = fs.createReadStream(oldBackupFilePath);
|
|
|
|
|
var writeStream = fs.createWriteStream(newBackupFilePath);
|
|
|
|
|
|
|
|
|
|
readStream.on('error', callback);
|
|
|
|
|
writeStream.on('error', callback);
|
|
|
|
|
writeStream.on('close', callback);
|
2016-09-16 11:55:59 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
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');
|
|
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
async.each([backupId].concat(appBackupIds), function (id, callback) {
|
|
|
|
|
var filePath = path.join(apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER, id + '.tar.gz');
|
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
|
|
|
}
|
2016-10-11 11:36:25 +02:00
|
|
|
|
|
|
|
|
function testConfig(apiConfig, callback) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
if (typeof apiConfig.backupFolder !== 'string') return callback(new SettingsError(SettingsError.BAD_FIELD, 'backupFolder must be string'));
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
}
|
2017-01-04 16:22:58 -08:00
|
|
|
|
|
|
|
|
function backupDone(filename, app, appBackupIds, callback) {
|
|
|
|
|
assert.strictEqual(typeof filename, 'string');
|
|
|
|
|
assert(!app || typeof app === 'object');
|
|
|
|
|
assert(!appBackupIds || Array.isArray(appBackupIds));
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
}
|