2017-04-21 15:28:25 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
create: create,
|
|
|
|
|
extract: extract
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var assert = require('assert'),
|
|
|
|
|
BackupsError = require('../backups.js').BackupsError,
|
|
|
|
|
crypto = require('crypto'),
|
2017-04-22 11:50:12 -07:00
|
|
|
debug = require('debug')('box:storage/targz'),
|
2017-04-21 15:28:25 -07:00
|
|
|
mkdirp = require('mkdirp'),
|
|
|
|
|
progress = require('progress-stream'),
|
|
|
|
|
tar = require('tar-fs'),
|
|
|
|
|
zlib = require('zlib');
|
|
|
|
|
|
|
|
|
|
function create(sourceDirectories, key, outStream, callback) {
|
|
|
|
|
assert(Array.isArray(sourceDirectories));
|
2017-04-27 09:47:31 -07:00
|
|
|
assert(key === null || typeof key === 'string');
|
2017-04-21 15:28:25 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
var pack = tar.pack('/', {
|
2017-05-01 09:46:30 -07:00
|
|
|
dereference: false, // pack the symlink and not what it points to
|
2017-04-21 15:28:25 -07:00
|
|
|
entries: sourceDirectories.map(function (m) { return m.source; }),
|
|
|
|
|
map: function(header) {
|
|
|
|
|
sourceDirectories.forEach(function (m) {
|
|
|
|
|
header.name = header.name.replace(new RegExp('^' + m.source + '(/?)'), m.destination + '$1');
|
|
|
|
|
});
|
|
|
|
|
return header;
|
2017-05-01 09:46:30 -07:00
|
|
|
},
|
|
|
|
|
strict: false // do not error for unknown types (skip fifo, char/block devices)
|
2017-04-21 15:28:25 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var gzip = zlib.createGzip({});
|
|
|
|
|
var progressStream = progress({ time: 10000 }); // display a progress every 10 seconds
|
|
|
|
|
|
|
|
|
|
pack.on('error', function (error) {
|
2017-04-23 22:09:05 -07:00
|
|
|
debug('backup: tar stream error.', error);
|
2017-04-21 15:28:25 -07:00
|
|
|
callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
gzip.on('error', function (error) {
|
2017-04-23 22:09:05 -07:00
|
|
|
debug('backup: gzip stream error.', error);
|
2017-04-21 15:28:25 -07:00
|
|
|
callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
progressStream.on('progress', function(progress) {
|
|
|
|
|
debug('backup: %s@%s', Math.round(progress.transferred/1024/1024) + 'M', Math.round(progress.speed/1024/1024) + 'Mbps');
|
|
|
|
|
});
|
|
|
|
|
|
2017-04-27 09:47:31 -07:00
|
|
|
if (key !== null) {
|
|
|
|
|
var encrypt = crypto.createCipher('aes-256-cbc', key);
|
|
|
|
|
encrypt.on('error', function (error) {
|
|
|
|
|
debug('backup: encrypt stream error.', error);
|
|
|
|
|
callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
|
|
|
|
|
});
|
|
|
|
|
pack.pipe(gzip).pipe(encrypt).pipe(progressStream).pipe(outStream);
|
|
|
|
|
} else {
|
|
|
|
|
pack.pipe(gzip).pipe(progressStream).pipe(outStream);
|
|
|
|
|
}
|
2017-04-21 15:28:25 -07:00
|
|
|
}
|
|
|
|
|
|
2017-04-27 09:43:34 -07:00
|
|
|
function extract(inStream, destination, key, callback) {
|
2017-04-21 15:28:25 -07:00
|
|
|
assert.strictEqual(typeof destination, 'string');
|
2017-04-27 09:47:31 -07:00
|
|
|
assert(key === null || typeof key === 'string');
|
2017-04-21 15:28:25 -07:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
mkdirp(destination, function (error) {
|
|
|
|
|
if (error) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
|
|
|
|
|
|
|
|
|
|
var gunzip = zlib.createGunzip({});
|
|
|
|
|
var progressStream = progress({ time: 10000 }); // display a progress every 10 seconds
|
|
|
|
|
var extract = tar.extract(destination);
|
|
|
|
|
|
|
|
|
|
progressStream.on('progress', function(progress) {
|
|
|
|
|
debug('restore: %s@%s', Math.round(progress.transferred/1024/1024) + 'M', Math.round(progress.speed/1024/1024) + 'Mbps');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
gunzip.on('error', function (error) {
|
2017-04-23 22:09:05 -07:00
|
|
|
debug('restore: gunzip stream error.', error);
|
2017-04-21 15:28:25 -07:00
|
|
|
callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
extract.on('error', function (error) {
|
2017-04-23 22:09:05 -07:00
|
|
|
debug('restore: extract stream error.', error);
|
2017-04-21 15:28:25 -07:00
|
|
|
callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
extract.on('finish', function () {
|
|
|
|
|
debug('restore: done.');
|
|
|
|
|
callback(null);
|
|
|
|
|
});
|
|
|
|
|
|
2017-04-27 09:47:31 -07:00
|
|
|
if (key !== null) {
|
|
|
|
|
var decrypt = crypto.createDecipher('aes-256-cbc', key);
|
|
|
|
|
decrypt.on('error', function (error) {
|
|
|
|
|
debug('restore: decrypt stream error.', error);
|
|
|
|
|
callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
|
|
|
|
|
});
|
|
|
|
|
inStream.pipe(progressStream).pipe(decrypt).pipe(gunzip).pipe(extract);
|
|
|
|
|
} else {
|
|
|
|
|
inStream.pipe(progressStream).pipe(gunzip).pipe(extract);
|
|
|
|
|
}
|
2017-04-21 15:28:25 -07:00
|
|
|
});
|
|
|
|
|
}
|