refactor(mikrotikBackupRoutes, mikrotikBackupScheduler, mikrotikApplyService): replace direct export command with file-based export method for improved reliability and error handling
Publish Fast Tabler Docker image / build-and-push-fast (push) Successful in 3m59s

This commit is contained in:
2026-02-10 01:29:39 +07:00
parent f2372ef4c9
commit 0c40d2726b
3 changed files with 45 additions and 51 deletions
+39
View File
@@ -344,6 +344,44 @@ async function applyBlock(client, block, dryRun) {
return results;
}
/**
* Получить полный экспорт конфигурации MikroTik через REST API.
* API не возвращает текст /export напрямую — только при export file=... сохраняется в файл.
* Мы экспортируем во временный файл, читаем его, удаляем.
*
* @param {object} client - ros-rest client (createRosClient)
* @returns {Promise<string>} - текст конфигурации (export compact)
*/
async function fetchExportViaFile(client) {
const crypto = require('crypto');
const basename = `backup_${Date.now()}_${crypto.randomBytes(4).toString('hex')}.rsc`;
await client.command('export', { compact: '', file: basename });
const printRes = await client.command('file/print', {
'.proplist': 'contents,.id',
'.query': [`name=${basename}`],
});
const data = printRes?.data;
const list = Array.isArray(data) ? data : (data ? [data] : []);
const file = list[0];
if (!file || file.contents == null) {
try {
const id = file?.['.id'];
if (id) await client.remove(`file/${id}`);
} catch (_) {}
throw new Error('Failed to read export file from MikroTik');
}
const config = String(file.contents || '');
try {
const id = file['.id'];
if (id) await client.remove(`file/${id}`);
} catch (_) {}
return config;
}
module.exports = {
createRosClient,
rosPrint,
@@ -353,4 +391,5 @@ module.exports = {
rosRemove,
applyOperation,
applyBlock,
fetchExportViaFile,
};