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

54 lines
1.4 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';
2022-04-04 14:13:27 -07:00
if (process.argv[2] === '--check') {
console.log('OK');
process.exit(0);
}
2017-09-19 08:19:01 -07:00
2022-04-28 21:29:11 -07:00
const backuptask = require('../backuptask.js'),
2018-11-30 09:42:15 -08:00
database = require('../database.js'),
debug = require('debug')('box:backupupload'),
2023-08-11 19:41:05 +05:30
safe = require('safetydance');
2017-09-19 08:19:01 -07:00
// Main process starts here
2022-04-04 14:13:27 -07:00
const remotePath = process.argv[2];
2018-12-20 14:33:29 -08:00
const format = process.argv[3];
const dataLayoutString = process.argv[4];
2017-09-19 08:19:01 -07:00
2022-04-04 14:13:27 -07:00
debug(`Backing up ${dataLayoutString} to ${remotePath}`);
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) {
const now = Date.now();
2019-04-03 11:43:12 -07:00
if (lastProgress && ((now - lastProgress) < msecs)) return;
process.send(progress);
lastProgress = now;
};
}
2022-04-28 21:29:11 -07:00
(async function main() {
await database.initialize();
2017-09-19 08:19:01 -07:00
2025-08-11 19:30:22 +05:30
const [uploadError, result] = await safe(backuptask.upload(remotePath, format, dataLayoutString, throttledProgressCallback(5000)));
2023-04-16 10:49:59 +02:00
debug('upload completed. error: %o', uploadError);
2017-09-19 08:19:01 -07:00
2025-08-11 19:30:22 +05:30
process.send({ result, errorMessage: uploadError?.message });
2017-09-19 08:19:01 -07:00
2022-04-28 21:29:11 -07:00
process.exit(uploadError ? 50 : 0);
})();