fix(restore): survive large backups, disk-full and proxy limits
- extract restore helpers into lib/restore.js (space check, tar entry filter, temp dir sweep) - reject tar entries outside staging and symlinks/hardlinks - map ENOSPC/EQUOT to 507 with clear messages, keep data_restored flag when files fail after commit - raise request timeout using BACKUP_UPLOAD_LIMIT_MB and expose backup_upload_limit_mb via public settings - upload restore archive via XHR with progress, size pre-check and readable server errors in settings UI - document large-archive restore on a remote server
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
|
||||
const FREE_SPACE_RESERVE_BYTES = 16 * 1024 * 1024;
|
||||
|
||||
function formatBytes(bytes) {
|
||||
const n = Number(bytes) || 0;
|
||||
if (n >= 1024 * 1024 * 1024) return `${(n / (1024 * 1024 * 1024)).toFixed(1)} ГБ`;
|
||||
if (n >= 1024 * 1024) return `${Math.round(n / (1024 * 1024))} МБ`;
|
||||
return `${Math.round(n / 1024)} КБ`;
|
||||
}
|
||||
|
||||
function freeBytes(dir) {
|
||||
try {
|
||||
const st = fs.statfsSync(dir);
|
||||
return Number(st.bavail) * Number(st.bsize);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function assertRestoreSpace(dir, archiveBytes) {
|
||||
const free = freeBytes(dir);
|
||||
if (free == null) return;
|
||||
const need = Math.max(Number(archiveBytes) * 2, Number(archiveBytes) + FREE_SPACE_RESERVE_BYTES);
|
||||
if (free >= need) return;
|
||||
const err = new Error(
|
||||
`Недостаточно свободного места для распаковки архива: нужно примерно ${formatBytes(need)}, свободно ${formatBytes(free)}`
|
||||
);
|
||||
err.code = 'RESTORE_NO_SPACE';
|
||||
throw err;
|
||||
}
|
||||
|
||||
function isDiskFullError(e) {
|
||||
const code = String((e && (e.code || e.errno)) || '');
|
||||
if (code === 'ENOSPC' || code === 'EDQUOT') return true;
|
||||
return /no space left|disk quota|not enough space|enospc/i.test(String((e && e.message) || ''));
|
||||
}
|
||||
|
||||
function isEntryInside(root, entryPath) {
|
||||
if (!entryPath || path.isAbsolute(entryPath)) return false;
|
||||
const target = path.resolve(root, entryPath);
|
||||
const rel = path.relative(root, target);
|
||||
return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));
|
||||
}
|
||||
|
||||
function tarEntryFilter(root) {
|
||||
return (entryPath, entry) => {
|
||||
if (!isEntryInside(root, entryPath)) return false;
|
||||
if (entry && (entry.type === 'SymbolicLink' || entry.type === 'Link')) return false;
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
function sweepRestoreTempDirs() {
|
||||
let names = [];
|
||||
try {
|
||||
names = fs.readdirSync(os.tmpdir());
|
||||
} catch {
|
||||
return 0;
|
||||
}
|
||||
let removed = 0;
|
||||
for (const name of names) {
|
||||
if (!/^wido-(?:up|rst)-/.test(name)) continue;
|
||||
const dir = path.join(os.tmpdir(), name);
|
||||
try {
|
||||
if (!fs.statSync(dir).isDirectory()) continue;
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
removed++;
|
||||
} catch {}
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
formatBytes,
|
||||
freeBytes,
|
||||
assertRestoreSpace,
|
||||
isDiskFullError,
|
||||
isEntryInside,
|
||||
tarEntryFilter,
|
||||
sweepRestoreTempDirs,
|
||||
};
|
||||
Reference in New Issue
Block a user