2017-09-19 08:19:01 -07:00
|
|
|
#!/bin/bash
|
|
|
|
|
':' //# comment; exec /usr/bin/env node --max_old_space_size=300 "$0" "$@"
|
|
|
|
|
|
|
|
|
|
// to understand the above hack read http://sambal.org/2014/02/passing-options-node-shebang-line/
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
if (process.argv[2] === '--check') return console.log('OK');
|
|
|
|
|
|
|
|
|
|
require('supererror')({ splatchError: true });
|
|
|
|
|
|
|
|
|
|
var assert = require('assert'),
|
2017-09-19 20:27:36 -07:00
|
|
|
backups = require('./backups.js'),
|
2017-09-19 08:19:01 -07:00
|
|
|
database = require('./database.js'),
|
2018-11-26 15:55:00 -08:00
|
|
|
debug = require('debug')('box:backupupload');
|
2017-09-19 08:19:01 -07:00
|
|
|
|
|
|
|
|
function initialize(callback) {
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
database.initialize(callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Main process starts here
|
|
|
|
|
var backupId = process.argv[2];
|
2017-09-27 17:34:49 -07:00
|
|
|
var format = process.argv[3];
|
|
|
|
|
var dataDir = process.argv[4];
|
2017-09-19 08:19:01 -07:00
|
|
|
|
|
|
|
|
debug(`Backing up ${dataDir} to ${backupId}`);
|
|
|
|
|
|
|
|
|
|
process.on('SIGTERM', function () {
|
|
|
|
|
process.exit(0);
|
|
|
|
|
});
|
|
|
|
|
|
2018-11-26 19:11:30 -08:00
|
|
|
// this can happen when the backup task is terminated (not box code)
|
|
|
|
|
process.on('disconnect', function () {
|
|
|
|
|
debug('parent process died');
|
|
|
|
|
process.exit(0);
|
|
|
|
|
});
|
|
|
|
|
|
2017-09-19 08:19:01 -07:00
|
|
|
initialize(function (error) {
|
|
|
|
|
if (error) throw error;
|
|
|
|
|
|
2018-11-27 10:24:54 -08:00
|
|
|
backups.upload(backupId, format, dataDir, (progress) => process.send(progress), function resultHandler(error) {
|
2018-03-21 11:39:16 -07:00
|
|
|
if (error) debug('upload completed with error', error);
|
2017-09-19 08:19:01 -07:00
|
|
|
|
2018-03-21 11:39:16 -07:00
|
|
|
debug('upload completed');
|
2017-09-19 08:19:01 -07:00
|
|
|
|
2018-11-26 15:21:48 -08:00
|
|
|
process.send({ result: error ? error.message : '' });
|
2017-09-19 08:19:01 -07:00
|
|
|
|
|
|
|
|
// https://nodejs.org/api/process.html are exit codes used by node. apps.js uses the value below
|
|
|
|
|
// to check apptask crashes
|
|
|
|
|
process.exit(error ? 50 : 0);
|
|
|
|
|
});
|
|
|
|
|
});
|