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,
|
2017-04-17 14:46:19 +02:00
|
|
|
copyBackup: copyBackup,
|
2017-04-17 15:45:58 +02:00
|
|
|
removeBackup: removeBackup,
|
2016-09-16 11:21:08 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
getDownloadStream: getDownloadStream,
|
2016-09-16 10:59:17 +02:00
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2017-04-20 15:15:49 +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-18 15:32:59 +02:00
|
|
|
crypto = require('crypto'),
|
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'),
|
2017-04-11 11:00:55 +02:00
|
|
|
mkdirp = require('mkdirp'),
|
|
|
|
|
once = require('once'),
|
2017-04-18 15:32:59 +02:00
|
|
|
path = require('path'),
|
2016-10-11 11:36:25 +02:00
|
|
|
SettingsError = require('../settings.js').SettingsError,
|
2017-04-11 11:00:55 +02:00
|
|
|
tar = require('tar-fs'),
|
2017-04-18 15:32:59 +02:00
|
|
|
zlib = require('zlib');
|
2016-09-16 11:55:59 +02:00
|
|
|
|
2016-09-16 14:10:34 +02:00
|
|
|
var FALLBACK_BACKUP_FOLDER = '/var/backups';
|
2017-04-18 14:39:48 +02:00
|
|
|
var FILE_TYPE = '.tar.gz';
|
2016-09-15 11:29:45 +02:00
|
|
|
|
2017-04-17 14:46:19 +02:00
|
|
|
// internal only
|
2017-04-18 14:39:48 +02:00
|
|
|
function getBackupFilePath(apiConfig, backupId) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof backupId, 'string');
|
|
|
|
|
|
|
|
|
|
return path.join(apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER, backupId.endsWith(FILE_TYPE) ? backupId : backupId+FILE_TYPE);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-18 15:32:59 +02:00
|
|
|
// storage api
|
2017-04-20 15:15:49 +02:00
|
|
|
function backup(apiConfig, backupId, source, 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');
|
2017-04-20 15:15:49 +02:00
|
|
|
assert.strictEqual(typeof source, '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 10:58:34 +02:00
|
|
|
|
2017-04-17 16:18:18 +02:00
|
|
|
// to allow setting 777 for real
|
|
|
|
|
var oldUmask = process.umask(0);
|
|
|
|
|
var oldCallback = callback;
|
|
|
|
|
callback = function (error) {
|
|
|
|
|
process.umask(oldUmask);
|
|
|
|
|
oldCallback(error);
|
|
|
|
|
};
|
|
|
|
|
|
2017-04-18 14:39:48 +02:00
|
|
|
var backupFilePath = getBackupFilePath(apiConfig, backupId);
|
2016-09-16 10:58:34 +02:00
|
|
|
|
2017-04-20 15:15:49 +02:00
|
|
|
debug('[%s] backup: %s -> %s', backupId, source, backupFilePath);
|
2016-09-15 11:29:45 +02:00
|
|
|
|
2017-04-17 15:36:17 +02:00
|
|
|
mkdirp(path.dirname(backupFilePath), { mode: 0o777 }, 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-20 15:15:49 +02:00
|
|
|
var pack = tar.pack(source);
|
|
|
|
|
var gzip = zlib.createGzip({});
|
2017-04-20 15:35:52 +02:00
|
|
|
var encrypt = crypto.createCipher('aes-256-cbc', apiConfig.key || '');
|
|
|
|
|
var fileStream = fs.createWriteStream(backupFilePath, { mode: 0o777 });
|
2016-09-16 11:21:08 +02:00
|
|
|
|
2017-04-20 15:35:52 +02:00
|
|
|
pack.on('error', function (error) {
|
|
|
|
|
console.error('[%s] backup: tar stream error.', backupId, error);
|
2017-04-14 17:46:25 +02:00
|
|
|
});
|
|
|
|
|
|
2017-04-20 15:15:49 +02:00
|
|
|
gzip.on('error', function (error) {
|
|
|
|
|
console.error('[%s] backup: gzip stream error.', backupId, error);
|
2017-04-11 11:00:55 +02:00
|
|
|
});
|
|
|
|
|
|
2017-04-20 15:35:52 +02:00
|
|
|
encrypt.on('error', function (error) {
|
|
|
|
|
console.error('[%s] backup: encrypt stream error.', backupId, error);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
fileStream.on('error', function (error) {
|
|
|
|
|
console.error('[%s] backup: out stream error.', backupId, error);
|
|
|
|
|
});
|
|
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
fileStream.on('close', function () {
|
|
|
|
|
debug('[%s] backup: done.', backupId);
|
2017-04-17 14:46:19 +02:00
|
|
|
callback(null);
|
2017-04-11 11:00:55 +02:00
|
|
|
});
|
|
|
|
|
|
2017-04-20 15:35:52 +02:00
|
|
|
pack.pipe(gzip).pipe(encrypt).pipe(fileStream);
|
2017-04-11 11:00:55 +02:00
|
|
|
});
|
2016-09-16 11:21:08 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-20 15:15:49 +02:00
|
|
|
function restore(apiConfig, backupId, destination, 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');
|
2017-04-20 15:15:49 +02:00
|
|
|
assert.strictEqual(typeof destination, 'string');
|
2016-09-15 11:29:45 +02:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2017-04-18 14:39:48 +02:00
|
|
|
var sourceFilePath = getBackupFilePath(apiConfig, backupId);
|
2017-04-11 11:00:55 +02:00
|
|
|
|
2017-04-20 15:15:49 +02:00
|
|
|
debug('[%s] restore: %s -> %s', backupId, sourceFilePath, destination);
|
2017-04-11 11:00:55 +02:00
|
|
|
|
|
|
|
|
if (!fs.existsSync(sourceFilePath)) return callback(new BackupsError(BackupsError.NOT_FOUND, 'backup file does not exist'));
|
|
|
|
|
|
2017-04-20 15:15:49 +02:00
|
|
|
mkdirp(destination, function (error) {
|
|
|
|
|
if (error) return callback(error);
|
2017-04-11 11:00:55 +02:00
|
|
|
|
2017-04-20 15:15:49 +02:00
|
|
|
var fileStream = fs.createReadStream(sourceFilePath);
|
|
|
|
|
var decipher = crypto.createDecipher('aes-256-cbc', apiConfig.key || '');
|
|
|
|
|
var gunzip = zlib.createGunzip({});
|
|
|
|
|
var extract = tar.extract(destination);
|
2017-04-14 17:46:25 +02:00
|
|
|
|
2017-04-20 15:15:49 +02:00
|
|
|
fileStream.on('error', function (error) {
|
|
|
|
|
console.error('[%s] restore: file stream error.', error);
|
|
|
|
|
callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
|
|
|
|
});
|
2017-04-14 15:15:50 +02:00
|
|
|
|
2017-04-20 15:15:49 +02:00
|
|
|
decipher.on('error', function (error) {
|
|
|
|
|
console.error('[%s] restore: decipher stream error.', error);
|
|
|
|
|
callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
|
|
|
|
});
|
2016-09-16 12:00:20 +02:00
|
|
|
|
2017-04-20 15:15:49 +02:00
|
|
|
gunzip.on('error', function (error) {
|
|
|
|
|
console.error('[%s] restore: gunzip stream error.', error);
|
|
|
|
|
callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
|
|
|
|
});
|
2016-10-10 18:11:23 +02:00
|
|
|
|
2017-04-20 15:15:49 +02:00
|
|
|
extract.on('error', function (error) {
|
|
|
|
|
console.error('[%s] restore: extract stream error.', error);
|
|
|
|
|
callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
2017-04-11 11:00:55 +02:00
|
|
|
});
|
|
|
|
|
|
2017-04-20 15:15:49 +02:00
|
|
|
extract.on('finish', function () {
|
|
|
|
|
debug('[%s] restore: %s done.', backupId);
|
|
|
|
|
callback();
|
|
|
|
|
});
|
2017-04-11 11:00:55 +02:00
|
|
|
|
2017-04-20 15:15:49 +02:00
|
|
|
fileStream.pipe(decipher).pipe(gunzip).pipe(extract);
|
2017-04-17 14:46:19 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function copyBackup(apiConfig, oldBackupId, newBackupId, callback) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof oldBackupId, 'string');
|
|
|
|
|
assert.strictEqual(typeof newBackupId, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
callback = once(callback);
|
|
|
|
|
|
2017-04-18 14:39:48 +02:00
|
|
|
var oldFilePath = getBackupFilePath(apiConfig, oldBackupId);
|
|
|
|
|
var newFilePath = getBackupFilePath(apiConfig, newBackupId);
|
2017-04-17 14:46:19 +02:00
|
|
|
|
2017-04-20 15:15:49 +02:00
|
|
|
debug('copyBackup: %s -> %s', oldFilePath, newFilePath);
|
|
|
|
|
|
|
|
|
|
var readStream = fs.createReadStream(oldFilePath);
|
|
|
|
|
var writeStream = fs.createWriteStream(newFilePath);
|
|
|
|
|
|
|
|
|
|
readStream.on('error', function (error) {
|
|
|
|
|
console.error('copyBackup: read stream error.', error);
|
|
|
|
|
callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
|
|
|
|
});
|
2017-04-17 14:46:19 +02:00
|
|
|
|
2017-04-20 15:15:49 +02:00
|
|
|
writeStream.on('error', function (error) {
|
|
|
|
|
console.error('copyBackup: write stream error.', error);
|
|
|
|
|
callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
2016-10-10 18:11:23 +02:00
|
|
|
});
|
2017-04-20 15:15:49 +02:00
|
|
|
|
|
|
|
|
writeStream.on('close', callback);
|
|
|
|
|
|
|
|
|
|
readStream.pipe(writeStream);
|
2016-09-15 11:29:45 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-17 15:45:58 +02:00
|
|
|
function removeBackup(apiConfig, backupId, appBackupIds, callback) {
|
|
|
|
|
assert.strictEqual(typeof apiConfig, 'object');
|
|
|
|
|
assert.strictEqual(typeof backupId, 'string');
|
|
|
|
|
assert(Array.isArray(appBackupIds));
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
async.each([backupId].concat(appBackupIds), function (id, callback) {
|
2017-04-18 14:39:48 +02:00
|
|
|
var filePath = getBackupFilePath(apiConfig, id);
|
2017-04-17 15:45:58 +02:00
|
|
|
|
|
|
|
|
fs.unlink(filePath, function (error) {
|
2017-04-18 17:33:17 +02:00
|
|
|
if (error) console.error('Unable to remove %s. Not fatal.', filePath, error);
|
2017-04-17 15:45:58 +02:00
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
}, callback);
|
|
|
|
|
}
|
|
|
|
|
|
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-18 14:39:48 +02:00
|
|
|
var backupFilePath = getBackupFilePath(apiConfig, 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
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|