Files
cloudron-box/src/storage/filesystem.js

185 lines
6.5 KiB
JavaScript
Raw Normal View History

'use strict';
exports = module.exports = {
backup: backup,
restore: restore,
copyBackup: copyBackup,
removeBackups: removeBackups,
2016-09-16 11:21:08 +02:00
2017-01-04 16:22:58 -08:00
backupDone: backupDone,
testConfig: testConfig
};
var assert = require('assert'),
async = require('async'),
BackupsError = require('../backups.js').BackupsError,
config = require('../config.js'),
debug = require('debug')('box:storage/filesystem'),
fs = require('fs'),
mkdirp = require('mkdirp'),
once = require('once'),
2017-04-18 15:32:59 +02:00
path = require('path'),
safe = require('safetydance'),
targz = require('./targz.js');
var FALLBACK_BACKUP_FOLDER = '/var/backups';
var BACKUP_USER = config.TEST ? process.env.USER : 'yellowtent';
// internal only
function getBackupFilePath(apiConfig, backupId) {
assert.strictEqual(typeof apiConfig, 'object');
assert.strictEqual(typeof backupId, 'string');
const FILE_TYPE = apiConfig.key ? '.tar.gz.enc' : '.tar.gz';
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
function backup(apiConfig, backupId, sourceDir, callback) {
assert.strictEqual(typeof apiConfig, 'object');
assert.strictEqual(typeof backupId, 'string');
assert.strictEqual(typeof sourceDir, 'string');
assert.strictEqual(typeof callback, 'function');
callback = once(callback);
2016-09-16 10:58:34 +02:00
var backupFilePath = getBackupFilePath(apiConfig, backupId);
2016-09-16 10:58:34 +02:00
debug('[%s] backup: %s -> %s', backupId, sourceDir, backupFilePath);
mkdirp(path.dirname(backupFilePath), function (error) {
if (error) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
2016-09-16 11:21:08 +02:00
var fileStream = fs.createWriteStream(backupFilePath);
2016-09-16 11:21:08 +02:00
2017-04-20 15:35:52 +02:00
fileStream.on('error', function (error) {
2017-04-23 22:09:05 -07:00
debug('[%s] backup: out stream error.', backupId, error);
callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
2017-04-20 15:35:52 +02:00
});
fileStream.on('close', function () {
debug('[%s] backup: changing ownership.', backupId);
if (!safe.child_process.execSync('chown -R ' + BACKUP_USER + ':' + BACKUP_USER + ' ' + path.dirname(backupFilePath))) return callback(new BackupsError(BackupsError.INTERNAL_ERROR, safe.error.message));
debug('[%s] backup: done.', backupId);
callback(null);
});
targz.create([{ source: sourceDir, destination: '.' }], apiConfig.key || null, fileStream, callback);
});
2016-09-16 11:21:08 +02:00
}
function restore(apiConfig, backupId, destination, callback) {
assert.strictEqual(typeof apiConfig, 'object');
assert.strictEqual(typeof backupId, 'string');
assert.strictEqual(typeof destination, 'string');
assert.strictEqual(typeof callback, 'function');
callback = once(callback);
2017-04-30 17:42:55 -07:00
var sourceFilePath = getBackupFilePath(apiConfig, backupId);
debug('[%s] restore: %s -> %s', backupId, sourceFilePath, destination);
if (!fs.existsSync(sourceFilePath)) return callback(new BackupsError(BackupsError.NOT_FOUND, 'backup file does not exist'));
var fileStream = fs.createReadStream(sourceFilePath);
fileStream.on('error', function (error) {
2017-04-23 22:09:05 -07:00
debug('restore: file stream error.', error);
callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
});
2017-04-30 17:42:55 -07:00
targz.extract(fileStream, destination, apiConfig.key || null, callback);
}
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 = getBackupFilePath(apiConfig, oldBackupId);
var newFilePath = getBackupFilePath(apiConfig, newBackupId);
debug('copyBackup: %s -> %s', oldFilePath, newFilePath);
mkdirp(path.dirname(newFilePath), function (error) {
if (error) return callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
2017-04-20 15:41:25 +02:00
var readStream = fs.createReadStream(oldFilePath);
var writeStream = fs.createWriteStream(newFilePath);
readStream.on('error', function (error) {
2017-04-23 22:09:05 -07:00
debug('copyBackup: read stream error.', error);
callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
2017-04-20 15:41:25 +02:00
});
2017-04-20 15:41:25 +02:00
writeStream.on('error', function (error) {
2017-04-23 22:09:05 -07:00
debug('copyBackup: write stream error.', error);
callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message));
2017-04-20 15:41:25 +02:00
});
writeStream.on('close', function () {
if (!safe.child_process.execSync('chown -R ' + BACKUP_USER + ':' + BACKUP_USER + ' ' + path.dirname(newFilePath))) return callback(new BackupsError(BackupsError.INTERNAL_ERROR, safe.error.message));
callback();
});
2017-04-20 15:41:25 +02:00
readStream.pipe(writeStream);
});
}
function removeBackups(apiConfig, backupIds, callback) {
assert.strictEqual(typeof apiConfig, 'object');
assert(Array.isArray(backupIds));
assert.strictEqual(typeof callback, 'function');
async.eachSeries(backupIds, function (id, iteratorCallback) {
var filePath = getBackupFilePath(apiConfig, id);
if (!safe.fs.unlinkSync(filePath)) {
debug('removeBackups: Unable to remove %s : %s', filePath, safe.error.message);
}
safe.fs.rmdirSync(path.dirname(filePath)); // try to cleanup empty directories
iteratorCallback();
}, callback);
}
function testConfig(apiConfig, callback) {
assert.strictEqual(typeof apiConfig, 'object');
assert.strictEqual(typeof callback, 'function');
if ('backupFolder' in apiConfig && typeof apiConfig.backupFolder !== 'string') return callback(new BackupsError(BackupsError.BAD_FIELD, 'backupFolder must be string'));
// default value will be used
if (!apiConfig.backupFolder) return callback();
fs.stat(apiConfig.backupFolder, function (error, result) {
if (error) {
debug('testConfig: %s', apiConfig.backupFolder, error);
return callback(new BackupsError(BackupsError.BAD_FIELD, 'Directory does not exist or cannot be accessed'));
}
if (!result.isDirectory()) return callback(new BackupsError(BackupsError.BAD_FIELD, 'Backup location is not a directory'));
callback(null);
});
}
2017-01-04 16:22:58 -08:00
2017-04-21 10:31:43 +02:00
function backupDone(backupId, appBackupIds, callback) {
assert.strictEqual(typeof backupId, 'string');
assert(Array.isArray(appBackupIds));
2017-01-04 16:22:58 -08:00
assert.strictEqual(typeof callback, 'function');
callback();
}