Files
cloudron-box/src/scripts/backupupload.js
T

80 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-11-05 16:07:28 -08:00
#!/usr/bin/env node
2017-09-19 08:19:01 -07:00
'use strict';
if (process.argv[2] === '--check') return console.log('OK');
var assert = require('assert'),
async = require('async'),
2018-11-30 09:42:15 -08:00
backups = require('../backups.js'),
database = require('../database.js'),
debug = require('debug')('box:backupupload'),
2020-11-05 14:30:45 -08:00
settings = require('../settings.js'),
v8 = require('v8');
2017-09-19 08:19:01 -07:00
function initialize(callback) {
assert.strictEqual(typeof callback, 'function');
async.series([
database.initialize,
settings.initCache
], callback);
2017-09-19 08:19:01 -07:00
}
// Main process starts here
2018-12-20 14:33:29 -08:00
const backupId = process.argv[2];
const format = process.argv[3];
const dataLayoutString = process.argv[4];
2017-09-19 08:19:01 -07:00
debug(`Backing up ${dataLayoutString} to ${backupId}`);
2017-09-19 08:19:01 -07:00
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);
});
2019-04-03 11:43:12 -07:00
// send progress every n seconds
function throttledProgressCallback(msecs) {
let lastProgress = null;
return function (progress) {
let now = Date.now();
if (lastProgress && ((now - lastProgress) < msecs)) return;
process.send(progress);
lastProgress = now;
};
}
2020-11-05 14:30:45 -08:00
// https://github.com/josefzamrzla/gc-heap-stats#readme
// https://stackoverflow.com/questions/41541843/nodejs-v8-getheapstatistics-method
function dumpMemoryInfo() {
const mu = process.memoryUsage();
const hs = v8.getHeapStatistics();
debug(`process: rss: ${mu.rss} heapTotal: ${mu.heapTotal} heapUsed: ${mu.heapUsed} external: ${mu.external}`);
2020-11-05 16:08:57 -08:00
debug(`v8 heap : used ${hs.used_heap_size} total: ${hs.total_heap_size} max: ${hs.heap_size_limit}`);
2020-11-05 14:30:45 -08:00
}
2017-09-19 08:19:01 -07:00
initialize(function (error) {
if (error) throw error;
2020-11-05 14:30:45 -08:00
dumpMemoryInfo();
const timerId = setInterval(dumpMemoryInfo, 30000);
2019-04-03 11:43:12 -07:00
backups.upload(backupId, format, dataLayoutString, throttledProgressCallback(5000), function resultHandler(error) {
debug('upload completed. error: ', error);
2017-09-19 08:19:01 -07:00
process.send({ result: error ? error.message : '' });
2020-11-05 14:30:45 -08:00
clearInterval(timerId);
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);
});
});