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,
|
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-17 14:46:19 +02:00
|
|
|
copyAppRestoreConfig: copyAppRestoreConfig,
|
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
|
|
|
|
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'),
|
2017-04-14 15:15:50 +02:00
|
|
|
zlib = require('zlib'),
|
2017-04-14 17:46:25 +02:00
|
|
|
crypto = require('crypto'),
|
2017-04-11 11:00:55 +02:00
|
|
|
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';
|
2017-04-14 15:49:42 +02:00
|
|
|
var REMOVE_BACKUP_CMD = path.join(__dirname, '../scripts/rmbackup.sh');
|
2016-09-15 11:29:45 +02:00
|
|
|
|
2017-04-17 14:46:19 +02:00
|
|
|
// internal only
|
|
|
|
|
function copyFile(source, destination, callback) {
|
|
|
|
|
callback = once(callback);
|
|
|
|
|
|
|
|
|
|
var readStream = fs.createReadStream(source);
|
|
|
|
|
var writeStream = fs.createWriteStream(destination);
|
|
|
|
|
|
|
|
|
|
readStream.on('error', callback);
|
|
|
|
|
writeStream.on('error', callback);
|
|
|
|
|
writeStream.on('close', callback);
|
|
|
|
|
|
|
|
|
|
readStream.pipe(writeStream);
|
|
|
|
|
}
|
|
|
|
|
|
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 });
|
2017-04-14 17:46:25 +02:00
|
|
|
var cipher = crypto.createCipher('aes-256-cbc', apiConfig.key || '');
|
2016-09-16 11:21:08 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
fileStream.on('error', function (error) {
|
2017-04-17 14:46:19 +02:00
|
|
|
console.error('[%s] backup: out stream error.', backupId, error);
|
2017-04-11 11:00:55 +02:00
|
|
|
});
|
2016-09-16 11:21:08 +02:00
|
|
|
|
2017-04-14 17:46:25 +02:00
|
|
|
cipher.on('error', function (error) {
|
2017-04-17 14:46:19 +02:00
|
|
|
console.error('[%s] backup: cipher stream error.', backupId, error);
|
2017-04-14 17:46:25 +02:00
|
|
|
});
|
|
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
archive.on('error', function (error) {
|
2017-04-17 14:46:19 +02:00
|
|
|
console.error('[%s] backup: archive 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-14 17:46:25 +02:00
|
|
|
archive.pipe(cipher).pipe(fileStream);
|
2017-04-17 14:46:19 +02:00
|
|
|
|
|
|
|
|
sourceDirectories.forEach(function (directory) {
|
|
|
|
|
// archive does not like destination beginning with a slash
|
|
|
|
|
directory.destination = path.normalize(directory.destination).replace(/^\//, '');
|
|
|
|
|
|
|
|
|
|
archive.directory(directory.source, directory.destination);
|
2017-04-11 11:00:55 +02:00
|
|
|
});
|
2017-04-17 14:46:19 +02:00
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
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-17 14:46:19 +02:00
|
|
|
// tar-fs reports without slash at the beginning
|
|
|
|
|
directory.source = path.normalize(directory.source).replace(/^\//, '');
|
|
|
|
|
|
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);
|
2017-04-14 17:46:25 +02:00
|
|
|
var decipher = crypto.createDecipher('aes-256-cbc', apiConfig.key || '');
|
2017-04-14 15:15:50 +02:00
|
|
|
var gunzipStream = zlib.createGunzip({});
|
2017-04-17 14:46:19 +02:00
|
|
|
|
|
|
|
|
var IGNORE_PREFIX = '__ignore__';
|
|
|
|
|
|
|
|
|
|
var extract = tar.extract(directory.destination, {
|
|
|
|
|
ignore: function (name, header) { return header.name.startsWith(IGNORE_PREFIX); },
|
|
|
|
|
map: function (header) {
|
|
|
|
|
// ignore is called after map, we mark everything we dont want!
|
|
|
|
|
// else slice off the mapping prefix
|
|
|
|
|
if (!header.name.startsWith(directory.source)) header.name = IGNORE_PREFIX + header.name;
|
|
|
|
|
else header.name = header.name.slice(directory.source.length);
|
|
|
|
|
|
|
|
|
|
return header;
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-04-11 11:00:55 +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 17:46:25 +02:00
|
|
|
decipher.on('error', function (error) {
|
|
|
|
|
console.error('[%s] restore: decipher stream error.', error);
|
|
|
|
|
callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
|
|
|
|
});
|
|
|
|
|
|
2017-04-14 15:15:50 +02:00
|
|
|
gunzipStream.on('error', function (error) {
|
|
|
|
|
console.error('[%s] restore: gunzip stream error.', error);
|
|
|
|
|
callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
|
|
|
|
});
|
|
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
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-14 15:24:30 +02:00
|
|
|
extract.on('finish', function () {
|
2017-04-11 11:00:55 +02:00
|
|
|
debug('[%s] restore: directory %s done.', backupId, directory.source);
|
|
|
|
|
callback();
|
|
|
|
|
});
|
2016-10-10 18:11:23 +02:00
|
|
|
|
2017-04-14 17:46:25 +02:00
|
|
|
fileStream.pipe(decipher).pipe(gunzipStream).pipe(extract);
|
2017-04-11 11:00:55 +02:00
|
|
|
});
|
|
|
|
|
}, function (error) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
debug('[%s] restore: done', backupId);
|
|
|
|
|
|
2017-04-17 14:46:19 +02:00
|
|
|
callback(null);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
var oldFilePath = path.join(apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER, oldBackupId + '.tar.gz');
|
|
|
|
|
var newFilePath = path.join(apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER, newBackupId + '.tar.gz');
|
|
|
|
|
|
|
|
|
|
copyFile(oldFilePath, newFilePath, function (error) {
|
|
|
|
|
if (error) {
|
|
|
|
|
console.error('Unable to copy backup %s -> %s.', oldFilePath, newFilePath, error);
|
|
|
|
|
return callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 11:00:55 +02:00
|
|
|
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-14 15:49:42 +02:00
|
|
|
var backupFilePath = path.join(apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER, backupId + '.tar.gz');
|
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
|
|
|
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-17 14:46:19 +02:00
|
|
|
function copyAppRestoreConfig(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-17 14:46:19 +02:00
|
|
|
var oldFilePath = path.join(apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER, oldBackupId) + '.json';
|
|
|
|
|
var newFilePath = path.join(apiConfig.backupFolder || FALLBACK_BACKUP_FOLDER, newBackupId + '.json');
|
2016-09-16 11:55:59 +02:00
|
|
|
|
2017-04-17 14:46:19 +02:00
|
|
|
copyFile(oldFilePath, newFilePath, function (error) {
|
2017-04-14 15:49:42 +02:00
|
|
|
if (error) {
|
2017-04-17 14:46:19 +02:00
|
|
|
console.error('Unable to copy app restore config %s -> %s.', oldFilePath, newFilePath, error);
|
2017-04-14 15:49:42 +02:00
|
|
|
return callback(new BackupsError(BackupsError.INTERNAL_ERROR, error));
|
|
|
|
|
}
|
2016-09-16 11:55:59 +02:00
|
|
|
|
2017-04-17 14:46:19 +02:00
|
|
|
callback(null);
|
2017-04-11 11:00:55 +02:00
|
|
|
});
|
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
|
|
|
|
2017-04-14 15:49:42 +02:00
|
|
|
shell.sudo('deleteBackup', [ REMOVE_BACKUP_CMD, filePath ], function (error) {
|
2016-10-10 16:10:51 +02:00
|
|
|
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();
|
|
|
|
|
}
|