first commit

This commit is contained in:
Alexander Sabino
2026-05-04 17:15:51 +01:00
commit f8e9aca195
16 changed files with 4718 additions and 0 deletions
+850
View File
@@ -0,0 +1,850 @@
const path = require('path');
const fs = require('fs/promises');
const { randomUUID } = require('crypto');
function shellQuote(value) {
return `'${String(value).replace(/'/g, `"'"'`)}'`;
}
function slugify(value) {
return value.replace(/[^a-zA-Z0-9_-]+/g, '-').replace(/^-+|-+$/g, '').toLowerCase() || 'item';
}
function formatStamp(date = new Date()) {
return date.toISOString().replace(/[:.]/g, '-');
}
function normalizeDockerHostPath(inputPath) {
const value = String(inputPath || '').trim();
if (/^[A-Za-z]:[\\/]/.test(value)) {
const drive = value[0].toLowerCase();
const suffix = value.slice(2).replace(/\\/g, '/').replace(/^\/+/, '');
return `/run/desktop/mnt/host/${drive}/${suffix}`;
}
return value.replace(/\\/g, '/');
}
function normalizeContainerPath(inputPath) {
const value = String(inputPath || '').trim().replace(/\\/g, '/');
if (!value.startsWith('/')) {
throw new Error('Em execucao via Docker, o diretorio de backup deve ser absoluto dentro do container (ex.: /app/data/backups).');
}
return value;
}
function normalizeMounts(containerInspect) {
return (containerInspect.Mounts || [])
.filter((mount) => mount.Type === 'bind' || mount.Type === 'volume')
.map((mount) => ({
type: mount.Type,
name: mount.Name || null,
source: mount.Source,
destination: mount.Destination,
rw: mount.RW,
}))
.sort((left, right) => {
const leftKey = `${left.destination}|${left.type}|${left.name || left.source}`;
const rightKey = `${right.destination}|${right.type}|${right.name || right.source}`;
return leftKey.localeCompare(rightKey);
});
}
function sameMountSignature(leftMounts, rightMounts) {
if (leftMounts.length !== rightMounts.length) {
return false;
}
return leftMounts.every((left, index) => {
const right = rightMounts[index];
return (
left.type === right.type
&& left.name === right.name
&& left.source === right.source
&& left.destination === right.destination
);
});
}
function getMountBindingSource(mount) {
return mount.type === 'volume' ? mount.name : mount.source;
}
function normalizeBackupScope(value) {
return value === 'container' ? 'container' : 'volumes';
}
function containerSnapshotPath(profileId, containerId, scope) {
return `/tmp/dockerbackup-${slugify(profileId)}-${containerId.slice(0, 12)}-${scope}.snar`;
}
function toContainerRelPath(absPath) {
return String(absPath || '').replace(/^\/+/, '') || '.';
}
function parseManifestLines(rawOutput) {
const map = new Map();
for (const line of String(rawOutput || '').split(/\r?\n/)) {
const trimmed = line.trim();
if (!trimmed) {
continue;
}
const [relativePath, sizeRaw, mtimeRaw, scopeRaw] = trimmed.split('|');
if (!relativePath) {
continue;
}
const key = scopeRaw
? `${toContainerRelPath(scopeRaw)}/${relativePath}`.replace(/\/\//g, '/')
: relativePath;
map.set(key, {
size: Number(sizeRaw || 0),
mtime: Number(mtimeRaw || 0),
});
}
return map;
}
function calculateManifestDiff(beforeMap, afterMap) {
let deleted = 0;
let created = 0;
let modified = 0;
for (const [filePath, beforeEntry] of beforeMap.entries()) {
const afterEntry = afterMap.get(filePath);
if (!afterEntry) {
deleted += 1;
continue;
}
if (beforeEntry.size !== afterEntry.size || beforeEntry.mtime !== afterEntry.mtime) {
modified += 1;
}
}
for (const filePath of afterMap.keys()) {
if (!beforeMap.has(filePath)) {
created += 1;
}
}
return { deleted, created, modified };
}
class BackupService {
constructor({ dockerService, store }) {
this.dockerService = dockerService;
this.store = store;
}
async runProfile(profileId, options = {}) {
const onProgress = typeof options.onProgress === 'function' ? options.onProgress : () => {};
const profile = await this.store.getProfile(profileId);
if (!profile) {
throw new Error('Profile nao encontrado.');
}
if (!profile.containerIds.length) {
throw new Error('Selecione ao menos um container no profile.');
}
const effectiveMode = ['full', 'incremental'].includes(options.mode)
? options.mode
: (['full', 'incremental'].includes(profile.mode) ? profile.mode : 'full');
const backupScope = normalizeBackupScope(profile.backupScope);
const backupRun = {
id: randomUUID(),
profileId: profile.id,
profileName: profile.name,
createdAt: new Date().toISOString(),
mode: effectiveMode,
backupScope,
backupDir: profile.backupDir,
status: 'ok',
containers: [],
};
const progress = {
profileId: profile.id,
profileName: profile.name,
startedAt: backupRun.createdAt,
status: 'running',
overall: {
total: profile.containerIds.length,
completed: 0,
pending: profile.containerIds.length,
percent: 0,
},
currentContainer: null,
};
const emitProgress = () => {
onProgress(JSON.parse(JSON.stringify(progress)));
};
emitProgress();
for (const [index, containerId] of profile.containerIds.entries()) {
progress.currentContainer = {
containerId,
index: index + 1,
total: profile.containerIds.length,
containerName: null,
status: 'running',
step: 'iniciando',
message: 'Preparando backup do container.',
logs: [],
percent: 0,
file: {
current: 0,
total: 0,
currentFile: null,
percent: 0,
},
};
emitProgress();
const containerBackup = await this.backupContainer(profile, containerId, backupRun.createdAt, {
mode: effectiveMode,
backupScope,
onProgress: (containerProgress) => {
progress.currentContainer = {
...progress.currentContainer,
...containerProgress,
};
emitProgress();
},
});
backupRun.containers.push(containerBackup);
if (containerBackup.status !== 'ok') {
backupRun.status = 'partial';
}
progress.overall.completed += 1;
progress.overall.pending = Math.max(0, progress.overall.total - progress.overall.completed);
progress.overall.percent = progress.overall.total
? Math.round((progress.overall.completed / progress.overall.total) * 100)
: 100;
progress.currentContainer = {
...(progress.currentContainer || {}),
status: containerBackup.status,
percent: 100,
};
emitProgress();
}
await this.store.addBackup(backupRun);
progress.status = backupRun.status === 'ok' ? 'completed' : 'completed-with-errors';
progress.finishedAt = new Date().toISOString();
progress.currentContainer = null;
progress.overall.percent = 100;
progress.overall.pending = 0;
emitProgress();
return backupRun;
}
async backupContainer(profile, containerId, runDateIso, options = {}) {
const onProgress = typeof options.onProgress === 'function' ? options.onProgress : () => {};
const runMode = ['full', 'incremental'].includes(options.mode)
? options.mode
: (['full', 'incremental'].includes(profile.mode) ? profile.mode : 'full');
const backupScope = normalizeBackupScope(options.backupScope || profile.backupScope);
let inspect;
let mounts = [];
let containerName = containerId.slice(0, 12);
try {
inspect = await this.dockerService.inspectContainer(containerId);
mounts = normalizeMounts(inspect);
containerName = inspect.Name.replace(/^\//, '');
} catch (error) {
onProgress({ containerName, status: 'error', percent: 100, step: 'erro', message: error.message });
return {
containerId,
containerName,
status: 'error',
mode: runMode,
error: `Falha ao inspecionar container: ${error.message}`,
};
}
if (backupScope === 'volumes' && !mounts.length) {
onProgress({
containerName,
status: 'skipped',
step: 'concluido',
message: 'Container sem volumes elegiveis.',
percent: 100,
file: { current: 0, total: 0, currentFile: null, percent: 100 },
});
return {
containerId: inspect.Id,
containerName,
status: 'skipped',
mode: runMode,
message: 'Container sem volumes ou bind mounts elegiveis.',
};
}
const runInDocker = this.dockerService.isRunningInDocker();
const backupRoot = runInDocker
? normalizeContainerPath(profile.backupDir)
: normalizeDockerHostPath(profile.backupDir);
const safeContainerName = slugify(containerName);
const safeProfileName = slugify(profile.name);
const stamp = formatStamp(new Date(runDateIso));
const archiveRelativePath = path.posix.join(safeProfileName, safeContainerName, `${stamp}-${runMode}.tar.gz`);
const snapshotRelativePath = path.posix.join(safeProfileName, safeContainerName, 'latest.snar');
const containerBackup = {
containerId: inspect.Id,
containerName,
backupScope,
backupPaths: backupScope === 'container' ? ['/'] : mounts.map((mount) => mount.destination),
mountSignature: mounts,
archiveRelativePath,
snapshotRelativePath,
wasRunning: inspect.State?.Running === true,
mode: runMode,
status: 'ok',
};
const logs = [];
const pushLog = (message, step = 'processando') => {
const line = `[${new Date().toLocaleTimeString('pt-BR')}] ${message}`;
logs.push(line);
while (logs.length > 40) {
logs.shift();
}
onProgress({
containerName,
step,
message,
logs: [...logs],
});
};
let fileTotal = 0;
let fileCurrent = 0;
const updateFileProgress = (currentFile = null) => {
const filePercent = fileTotal > 0 ? Math.min(100, Math.round((fileCurrent / fileTotal) * 100)) : 0;
onProgress({
containerName,
status: 'running',
step: 'processando',
percent: filePercent,
file: {
current: fileCurrent,
total: fileTotal,
currentFile,
percent: filePercent,
},
});
};
try {
if (backupScope === 'container' && !runInDocker) {
throw new Error('Backup do container inteiro requer app executando via Docker.');
}
pushLog(`Escopo selecionado: ${backupScope === 'container' ? 'container inteiro' : 'somente volumes'}.`, 'preparando');
if (runInDocker) {
await this.dockerService.ensureLocalDirectory(backupRoot);
pushLog(`Diretorio de backup pronto em ${backupRoot}.`, 'preparando');
const originalRunning = inspect.State?.Running === true;
let tempStarted = false;
try {
if (originalRunning) {
pushLog('Container ativo detectado. Parando antes do backup.', 'preparando');
await this.dockerService.stopContainer(containerId);
}
pushLog('Iniciando container temporariamente para snapshot.', 'preparando');
await this.dockerService.startContainer(containerId);
tempStarted = true;
const sourcePaths = backupScope === 'container'
? ['/']
: mounts.map((mount) => mount.destination);
const relSourcePaths = sourcePaths.map((item) => toContainerRelPath(item));
if (backupScope === 'volumes') {
pushLog('Contando arquivos para barra de progresso.', 'contando');
const countCmd = `set -eu; TOTAL=0; for p in ${relSourcePaths.map((item) => shellQuote(item)).join(' ')}; do if [ -e \"/$p\" ]; then C=$(find \"/$p\" -type f 2>/dev/null | wc -l | tr -d \" \" ); TOTAL=$((TOTAL + C)); fi; done; echo \"$TOTAL\"`;
const output = await this.dockerService.runContainerCommand(containerId, countCmd);
const parsed = Number(output.split(/\r?\n/).pop());
fileTotal = Number.isFinite(parsed) ? parsed : 0;
pushLog(`Total de arquivos identificado: ${fileTotal}.`, 'contando');
}
const absoluteArchivePath = path.posix.join(backupRoot, archiveRelativePath);
let newerMtimeFlag = '';
if (runMode === 'incremental') {
const lastTime = await this.store.getLastContainerBackupTime(profile.id, containerId);
if (lastTime) {
const unixSec = Math.floor(new Date(lastTime).getTime() / 1000);
newerMtimeFlag = `--newer-mtime=@${unixSec}`;
pushLog(`Backup incremental: incluindo arquivos modificados apos ${lastTime}.`, 'preparando');
} else {
pushLog('Aviso: nenhum backup anterior encontrado, gerando full.', 'preparando');
}
}
const tarParts = [
'set -eu',
'umask 077',
'echo "__DBKP_TAR_BEGIN__" 1>&2',
];
if (backupScope === 'container') {
tarParts.push(
`tar --warning=no-file-changed --ignore-failed-read ${newerMtimeFlag} -czvf - -C / --exclude=proc --exclude=sys --exclude=dev --exclude=run --exclude=tmp .`
);
} else {
tarParts.push(
`tar --warning=no-file-changed --ignore-failed-read ${newerMtimeFlag} -czvf - -C / ${relSourcePaths.map((item) => shellQuote(item)).join(' ')}`
);
}
updateFileProgress();
pushLog('Iniciando compactacao tar do container.', 'gerando-tar');
await this.dockerService.streamContainerCommandToFile(containerId, tarParts.join(' && '), absoluteArchivePath, {
onOutput: (line, stream) => {
const normalizedLine = String(line || '').trim();
if (!normalizedLine || stream !== 'stderr' || normalizedLine.startsWith('__DBKP_TAR_BEGIN__')) {
return;
}
if (!normalizedLine.startsWith('tar:')) {
fileCurrent += 1;
updateFileProgress(normalizedLine);
} else {
pushLog(`Aviso do tar: ${normalizedLine}`, 'gerando-tar');
}
},
});
pushLog(`Arquivo gerado: ${absoluteArchivePath}`, 'finalizando');
} finally {
if (tempStarted) {
pushLog('Encerrando container apos backup.', 'finalizando');
await this.dockerService.stopContainer(containerId).catch(() => null);
}
if (originalRunning) {
pushLog('Reiniciando container (estava ativo antes do backup).', 'finalizando');
await this.dockerService.startContainer(containerId).catch(() => null);
}
}
onProgress({
containerName,
status: 'ok',
step: 'concluido',
message: 'Backup concluido com sucesso.',
percent: 100,
file: {
current: Math.max(fileCurrent, fileTotal),
total: fileTotal,
currentFile: null,
percent: 100,
},
});
return containerBackup;
}
if (backupScope === 'container') {
throw new Error('Backup de container inteiro sem Docker nativo nao e suportado.');
}
await this.dockerService.ensureHostDirectory(backupRoot);
const wasRunning = inspect.State?.Running === true;
if (wasRunning) {
await this.dockerService.stopContainer(containerId);
}
const binds = [`${backupRoot}:/backuproot`];
for (const [index, mount] of mounts.entries()) {
binds.push(`${getMountBindingSource(mount)}:/payload/m${index}:ro`);
}
const archivePath = `/backuproot/${archiveRelativePath}`;
const snapshotPath = `/backuproot/${snapshotRelativePath}`;
const parentDir = path.posix.dirname(archivePath);
const cmdParts = ['set -eu', `mkdir -p ${shellQuote(parentDir)}`];
if (runMode === 'full') {
cmdParts.push(`rm -f ${shellQuote(snapshotPath)}`);
}
cmdParts.push('TOTAL_FILES=$(find /payload -type f | wc -l | tr -d " ")');
cmdParts.push('echo "__DBKP_TOTAL_FILES__=${TOTAL_FILES}"');
cmdParts.push(`tar --listed-incremental=${shellQuote(snapshotPath)} -czvf ${shellQuote(archivePath)} -C /payload .`);
await this.dockerService.runHelper({
binds,
cmd: cmdParts.join(' && '),
onOutput: (line, stream) => {
const normalizedLine = String(line || '').trim();
if (!normalizedLine) {
return;
}
if (normalizedLine.startsWith('__DBKP_TOTAL_FILES__=')) {
const parsed = Number(normalizedLine.split('=')[1]);
fileTotal = Number.isFinite(parsed) ? parsed : 0;
updateFileProgress();
return;
}
if (stream === 'stdout' && !normalizedLine.startsWith('tar:')) {
fileCurrent += 1;
updateFileProgress(normalizedLine);
}
},
});
if (wasRunning) {
await this.dockerService.startContainer(containerId).catch(() => null);
}
onProgress({
containerName,
status: 'ok',
step: 'concluido',
message: 'Backup concluido com sucesso.',
percent: 100,
file: {
current: Math.max(fileCurrent, fileTotal),
total: fileTotal,
currentFile: null,
percent: 100,
},
});
return containerBackup;
} catch (error) {
onProgress({
containerName,
status: 'error',
step: 'erro',
message: error.message,
percent: 100,
});
return {
...containerBackup,
status: 'error',
error: error.message,
};
}
}
async restoreBackup(profileId, backupId, options = {}) {
const onProgress = typeof options.onProgress === 'function' ? options.onProgress : () => {};
const selectedContainerIds = options.selectedContainerIds;
const profile = await this.store.getProfile(profileId);
if (!profile) {
throw new Error('Profile nao encontrado.');
}
const backupRun = await this.store.getBackup(backupId);
if (!backupRun || backupRun.profileId !== profileId) {
throw new Error('Backup nao encontrado para este profile.');
}
const allowedContainerIds = new Set(Array.isArray(selectedContainerIds) && selectedContainerIds.length
? selectedContainerIds
: backupRun.containers.map((item) => item.containerId));
const targets = backupRun.containers.filter((item) => item.status === 'ok' && allowedContainerIds.has(item.containerId));
if (!targets.length) {
throw new Error('Nenhum container valido foi selecionado para restore.');
}
const progress = {
profileId: profile.id,
profileName: profile.name,
operation: 'restore',
startedAt: new Date().toISOString(),
status: 'running',
overall: {
total: targets.length,
completed: 0,
pending: targets.length,
percent: 0,
},
currentContainer: null,
};
const emitProgress = () => {
onProgress(JSON.parse(JSON.stringify(progress)));
};
emitProgress();
const results = [];
for (const containerEntry of targets) {
const logs = [];
const pushLog = (message, step = 'restaurando') => {
const line = `[${new Date().toLocaleTimeString('pt-BR')}] ${message}`;
logs.push(line);
while (logs.length > 40) {
logs.shift();
}
progress.currentContainer = {
...(progress.currentContainer || {}),
message,
step,
logs: [...logs],
};
emitProgress();
};
progress.currentContainer = {
containerId: containerEntry.containerId,
containerName: containerEntry.containerName,
status: 'running',
step: 'preparando',
message: 'Preparando restauracao do container.',
logs: [],
percent: 0,
file: {
current: 0,
total: 0,
currentFile: null,
percent: 0,
},
};
emitProgress();
try {
const chain = await this.store.getBackupsForContainer(profileId, containerEntry.containerId, backupId);
if (!chain.length || chain[0].mode !== 'full') {
throw new Error(`Nao existe cadeia full + incremental valida para ${containerEntry.containerName}.`);
}
pushLog(`Cadeia de restore encontrada com ${chain.length} arquivo(s).`, 'preparando');
const restoreInfo = await this.restoreContainer(profile, containerEntry, chain, {
onProgress: (snapshot) => {
progress.currentContainer = {
...progress.currentContainer,
...snapshot,
};
emitProgress();
},
pushLog,
});
progress.currentContainer = {
...progress.currentContainer,
status: 'ok',
step: 'concluido',
message: 'Restore concluido com sucesso.',
percent: 100,
file: {
...(progress.currentContainer?.file || {}),
percent: 100,
},
};
emitProgress();
results.push({
containerId: containerEntry.containerId,
containerName: containerEntry.containerName,
status: 'ok',
stats: restoreInfo?.stats || null,
});
} catch (error) {
pushLog(`Falha no restore: ${error.message}`, 'erro');
progress.currentContainer = {
...progress.currentContainer,
status: 'error',
step: 'erro',
message: error.message,
percent: 100,
};
emitProgress();
results.push({
containerId: containerEntry.containerId,
containerName: containerEntry.containerName,
status: 'error',
error: error.message,
});
}
progress.overall.completed += 1;
progress.overall.pending = Math.max(0, progress.overall.total - progress.overall.completed);
progress.overall.percent = progress.overall.total
? Math.round((progress.overall.completed / progress.overall.total) * 100)
: 100;
emitProgress();
}
progress.status = results.every((item) => item.status === 'ok') ? 'completed' : 'completed-with-errors';
progress.finishedAt = new Date().toISOString();
progress.currentContainer = null;
progress.overall.percent = 100;
progress.overall.pending = 0;
emitProgress();
return {
backupId,
status: results.every((item) => item.status === 'ok') ? 'ok' : 'partial',
containers: results,
};
}
async restoreContainer(profile, targetEntry, chain, options = {}) {
const onProgress = typeof options.onProgress === 'function' ? options.onProgress : () => {};
const pushLog = typeof options.pushLog === 'function' ? options.pushLog : () => {};
const inspect = await this.dockerService.inspectContainer(targetEntry.containerId);
const backupScope = normalizeBackupScope(targetEntry.backupScope || profile.backupScope);
const currentMounts = normalizeMounts(inspect);
if (backupScope === 'volumes' && !sameMountSignature(targetEntry.mountSignature, currentMounts)) {
throw new Error(`Os mounts atuais do container ${targetEntry.containerName} nao batem com o backup selecionado.`);
}
const runInDocker = this.dockerService.isRunningInDocker();
const useNativeRestore = runInDocker;
if (useNativeRestore) {
const backupRoot = normalizeContainerPath(profile.backupDir);
const originalWasRunning = inspect.State?.Running === true;
const restoreStats = { deleted: 0, created: 0, modified: 0 };
try {
if (originalWasRunning) {
pushLog('Container ativo detectado. Parando antes do restore.', 'preparando');
await this.dockerService.stopContainer(targetEntry.containerId);
}
if (backupScope === 'volumes') {
const restorePaths = (chain[0]?.backupPaths && chain[0].backupPaths.length)
? chain[0].backupPaths
: currentMounts.map((mount) => mount.destination);
// Validar que todos os archives existem antes de tocar nos dados.
for (const entry of chain) {
await fs.access(path.posix.join(backupRoot, entry.archiveRelativePath));
}
// Restaurar via Docker API (putArchive) — funciona com container parado.
// O archive foi gerado com -C / incluindo os caminhos relativos dos volumes,
// portanto o destino do putArchive e sempre /.
for (const [index, entry] of chain.entries()) {
const absoluteArchivePath = path.posix.join(backupRoot, entry.archiveRelativePath);
onProgress({
step: 'restaurando',
file: {
current: index + 1,
total: chain.length,
currentFile: entry.archiveRelativePath,
percent: Math.round(((index + 1) / chain.length) * 100),
},
percent: Math.round(((index + 1) / chain.length) * 100),
});
pushLog(`Aplicando arquivo ${index + 1}/${chain.length}: ${entry.archiveRelativePath}`, 'restaurando');
await this.dockerService.putCompressedArchiveFromFile(
targetEntry.containerId,
'/',
absoluteArchivePath,
);
}
pushLog('Restore de volumes concluido.', 'finalizando');
} else {
// Escopo container inteiro.
for (const entry of chain) {
await fs.access(path.posix.join(backupRoot, entry.archiveRelativePath));
}
for (const [index, entry] of chain.entries()) {
const absoluteArchivePath = path.posix.join(backupRoot, entry.archiveRelativePath);
onProgress({
step: 'restaurando',
file: {
current: index + 1,
total: chain.length,
currentFile: entry.archiveRelativePath,
percent: Math.round(((index + 1) / chain.length) * 100),
},
percent: Math.round(((index + 1) / chain.length) * 100),
});
pushLog(`Aplicando arquivo ${index + 1}/${chain.length}: ${entry.archiveRelativePath}`, 'restaurando');
await this.dockerService.putCompressedArchiveFromFile(targetEntry.containerId, '/', absoluteArchivePath);
}
pushLog('Restore do container concluido.', 'finalizando');
}
} finally {
if (originalWasRunning) {
pushLog('Reiniciando container (estava ativo antes do restore).', 'finalizando');
await this.dockerService.startContainer(targetEntry.containerId).catch(() => null);
}
}
return { stats: restoreStats };
}
if (backupScope === 'container') {
throw new Error('Restore do container inteiro requer app executando via Docker.');
}
const backupRoot = normalizeDockerHostPath(profile.backupDir);
const wasRunning = inspect.State?.Running === true;
const binds = [`${backupRoot}:/backuproot:ro`];
for (const [index, mount] of currentMounts.entries()) {
binds.push(`${getMountBindingSource(mount)}:/restore/m${index}`);
}
const cleanupCommands = currentMounts.map((_mount, index) => (
`find ${shellQuote(`/restore/m${index}`)} -mindepth 1 -maxdepth 1 -exec rm -rf -- {} +`
));
const restoreCommands = chain.map((entry) => (
`tar --listed-incremental=/dev/null -xzf ${shellQuote(`/backuproot/${entry.archiveRelativePath}`)} -C /restore`
));
try {
if (wasRunning) {
await this.dockerService.stopContainer(targetEntry.containerId);
}
const cmd = ['set -eu', ...cleanupCommands, ...restoreCommands].join(' && ');
await this.dockerService.runHelper({ binds, cmd });
} finally {
if (wasRunning) {
await this.dockerService.startContainer(targetEntry.containerId).catch(() => null);
}
}
return { stats: null };
}
}
module.exports = BackupService;
+11
View File
@@ -0,0 +1,11 @@
const path = require('path');
const dataDir = process.env.DATA_DIR || path.join(process.cwd(), 'data');
module.exports = {
port: Number(process.env.PORT || 3000),
dataDir,
storePath: path.join(dataDir, 'store.json'),
dockerSocketPath: process.env.DOCKER_SOCKET_PATH || '/var/run/docker.sock',
helperImage: process.env.HELPER_IMAGE || 'node:20-bookworm-slim',
};
+486
View File
@@ -0,0 +1,486 @@
const Docker = require('dockerode');
const { PassThrough } = require('stream');
const fs = require('fs');
const fsp = require('fs/promises');
const path = require('path');
const zlib = require('zlib');
const { spawn } = require('child_process');
const { pipeline } = require('stream/promises');
function detectRunningInContainer() {
if (fs.existsSync('/.dockerenv')) {
return true;
}
try {
const cgroup = fs.readFileSync('/proc/1/cgroup', 'utf8');
return /(docker|containerd|kubepods|cri-o)/i.test(cgroup);
} catch {
return false;
}
}
function shellQuote(value) {
return `'${String(value).replace(/'/g, `"'"'`)}'`;
}
class DockerService {
constructor({ socketPath, helperImage }) {
this.docker = new Docker({ socketPath });
this.helperImage = helperImage;
this.runningInContainer = detectRunningInContainer();
}
isRunningInDocker() {
return this.runningInContainer;
}
async listContainers() {
const containers = await this.docker.listContainers({ all: true });
return containers
.map((container) => ({
id: container.Id,
name: (container.Names[0] || '').replace(/^\//, ''),
image: container.Image,
state: container.State,
status: container.Status,
}))
.sort((left, right) => left.name.localeCompare(right.name));
}
async inspectContainer(containerId) {
return this.docker.getContainer(containerId).inspect();
}
async stopContainer(containerId) {
await this.docker.getContainer(containerId).stop();
}
async startContainer(containerId) {
await this.docker.getContainer(containerId).start();
}
async ensureImage(imageName = this.helperImage) {
try {
await this.docker.getImage(imageName).inspect();
return;
} catch {
const stream = await this.docker.pull(imageName);
await new Promise((resolve, reject) => {
this.docker.modem.followProgress(stream, (error) => {
if (error) {
reject(error);
return;
}
resolve();
});
});
}
}
async ensureHostDirectory(hostPath) {
const normalized = String(hostPath || '').trim().replace(/\\/g, '/');
if (!normalized || !normalized.startsWith('/')) {
throw new Error(`Diretorio de backup invalido: ${hostPath}`);
}
if (this.runningInContainer) {
throw new Error('App rodando em container nao deve criar helper para backup/restore.');
}
await this.ensureImage();
const container = await this.docker.createContainer({
Image: this.helperImage,
Cmd: ['sh', '-lc', `mkdir -p ${shellQuote(`/hostfs${normalized}`)}`],
Tty: false,
HostConfig: {
Binds: ['/:/hostfs'],
AutoRemove: false,
NetworkMode: 'none',
},
});
try {
await container.start();
const [result, logs] = await Promise.all([
container.wait(),
container.logs({ stdout: true, stderr: true, follow: false }),
]);
if (result.StatusCode !== 0) {
throw new Error(logs.toString('utf8').trim() || 'Falha ao criar diretorio de backup no host.');
}
} finally {
try {
await container.remove({ force: true });
} catch {
}
}
}
async ensureLocalDirectory(localPath) {
const normalized = String(localPath || '').trim().replace(/\\/g, '/');
if (!normalized || !normalized.startsWith('/')) {
throw new Error(`Diretorio de backup invalido: ${localPath}`);
}
await fsp.mkdir(normalized, { recursive: true });
}
async exportContainerPathArchive(containerId, containerPath, targetFilePath) {
const container = this.docker.getContainer(containerId);
const archiveStream = await container.getArchive({ path: containerPath });
await fsp.mkdir(path.dirname(targetFilePath), { recursive: true });
await pipeline(archiveStream, fs.createWriteStream(targetFilePath));
}
async putArchiveFromFile(containerId, destinationPath, sourceFilePath) {
const container = this.docker.getContainer(containerId);
const source = fs.createReadStream(sourceFilePath);
await container.putArchive(source, { path: destinationPath });
}
async putCompressedArchiveFromFile(containerId, destinationPath, sourceFilePath) {
const container = this.docker.getContainer(containerId);
const source = fs.createReadStream(sourceFilePath).pipe(zlib.createGunzip());
await container.putArchive(source, { path: destinationPath });
}
async runContainerCommand(containerId, cmd) {
const container = this.docker.getContainer(containerId);
const exec = await container.exec({
AttachStdout: true,
AttachStderr: true,
Tty: false,
Cmd: ['sh', '-lc', cmd],
});
const stream = await exec.start({ hijack: true, stdin: false });
const stdoutStream = new PassThrough();
const stderrStream = new PassThrough();
this.docker.modem.demuxStream(stream, stdoutStream, stderrStream);
let output = '';
stdoutStream.on('data', (chunk) => {
output += chunk.toString('utf8');
});
stderrStream.on('data', (chunk) => {
output += chunk.toString('utf8');
});
let info = await exec.inspect();
while (info.Running) {
await new Promise((resolve) => setTimeout(resolve, 120));
info = await exec.inspect();
}
if (typeof stream.destroy === 'function') {
stream.destroy();
}
stdoutStream.end();
stderrStream.end();
const trimmed = output.trim();
if (info.ExitCode !== 0) {
throw new Error(output.trim() || `Comando em container terminou com codigo ${info.ExitCode}`);
}
return trimmed;
}
async streamContainerCommandToFile(containerId, cmd, targetFilePath, options = {}) {
const onOutput = typeof options.onOutput === 'function' ? options.onOutput : () => {};
const container = this.docker.getContainer(containerId);
const exec = await container.exec({
AttachStdout: true,
AttachStderr: true,
Tty: false,
Cmd: ['sh', '-lc', cmd],
});
const stream = await exec.start({ hijack: true, stdin: false });
const stdoutStream = new PassThrough();
const stderrStream = new PassThrough();
this.docker.modem.demuxStream(stream, stdoutStream, stderrStream);
await fsp.mkdir(path.dirname(targetFilePath), { recursive: true });
const writeStream = fs.createWriteStream(targetFilePath);
stdoutStream.pipe(writeStream);
const stderrDone = new Promise((resolve) => {
let buffer = '';
stderrStream.on('data', (chunk) => {
const text = chunk.toString('utf8');
buffer += text;
const parts = buffer.split(/\r?\n/);
buffer = parts.pop() || '';
for (const line of parts) {
onOutput(line, 'stderr');
}
});
stderrStream.on('end', () => {
if (buffer) {
onOutput(buffer, 'stderr');
}
resolve();
});
});
let info = await exec.inspect();
while (info.Running) {
await new Promise((resolve) => setTimeout(resolve, 120));
info = await exec.inspect();
}
if (typeof stream.destroy === 'function') {
stream.destroy();
}
stdoutStream.end();
stderrStream.end();
await Promise.all([
stderrDone,
new Promise((resolve, reject) => {
writeStream.on('finish', resolve);
writeStream.on('error', reject);
}),
]);
if (info.ExitCode !== 0) {
throw new Error(`Comando de stream em container terminou com codigo ${info.ExitCode}`);
}
}
async copyFileToContainer(containerId, sourceFilePath, destinationDirInContainer) {
const container = this.docker.getContainer(containerId);
const tarProc = spawn('tar', ['-cf', '-', '-C', path.dirname(sourceFilePath), path.basename(sourceFilePath)], {
stdio: ['ignore', 'pipe', 'pipe'],
});
let stderr = '';
tarProc.stderr.on('data', (chunk) => {
stderr += chunk.toString('utf8');
});
await container.putArchive(tarProc.stdout, { path: destinationDirInContainer });
await new Promise((resolve, reject) => {
tarProc.on('close', (code) => {
if (code === 0) {
resolve();
return;
}
reject(new Error(stderr.trim() || `Falha ao empacotar arquivo para copia (codigo ${code})`));
});
tarProc.on('error', reject);
});
}
async streamArchiveToContainer(containerId, archiveFilePath) {
const container = this.docker.getContainer(containerId);
const exec = await container.exec({
AttachStdin: true,
AttachStdout: false,
AttachStderr: true,
Tty: false,
Cmd: ['sh', '-lc', 'tar --listed-incremental=/dev/null -xzf - -C /'],
});
const attachStream = await exec.start({ hijack: true, stdin: true });
const stderrStream = new PassThrough();
this.docker.modem.demuxStream(attachStream, new PassThrough(), stderrStream);
let stderrOutput = '';
stderrStream.on('data', (chunk) => {
stderrOutput += chunk.toString('utf8');
});
// Pipar o arquivo diretamente para o stdin do tar no container
const fileReadStream = fs.createReadStream(archiveFilePath);
await new Promise((resolve, reject) => {
fileReadStream.on('error', reject);
fileReadStream.on('end', () => {
try { attachStream.end(); } catch { /* ignore */ }
resolve();
});
fileReadStream.pipe(attachStream, { end: false });
});
// Aguardar tar finalizar
let info = await exec.inspect();
while (info.Running) {
await new Promise((r) => setTimeout(r, 120));
info = await exec.inspect();
}
if (typeof attachStream.destroy === 'function') {
attachStream.destroy();
}
if (info.ExitCode !== 0) {
throw new Error(stderrOutput.trim() || `Falha ao restaurar archive no container (codigo ${info.ExitCode})`);
}
}
async copyFileFromContainer(containerId, containerFilePath, localFilePath) {
const container = this.docker.getContainer(containerId);
let archiveStream;
try {
archiveStream = await container.getArchive({ path: containerFilePath });
} catch {
return false;
}
await fsp.mkdir(path.dirname(localFilePath), { recursive: true });
await new Promise((resolve, reject) => {
const tarProc = spawn('tar', ['-xOf', '-'], { stdio: ['pipe', 'pipe', 'pipe'] });
const writeStream = fs.createWriteStream(localFilePath);
archiveStream.pipe(tarProc.stdin);
tarProc.stdout.pipe(writeStream);
tarProc.on('close', (code) => {
if (code === 0) { resolve(); return; }
reject(new Error(`Falha ao extrair arquivo do container (codigo ${code})`));
});
tarProc.on('error', reject);
writeStream.on('error', reject);
});
return true;
}
async runHostCommand({ cmd, onOutput }) {
await new Promise((resolve, reject) => {
const child = spawn('sh', ['-lc', cmd], { stdio: ['ignore', 'pipe', 'pipe'] });
let output = '';
const streamOutput = (stream, streamName) => {
let buffer = '';
stream.on('data', (chunk) => {
const text = chunk.toString('utf8');
output += text;
if (typeof onOutput !== 'function') {
return;
}
buffer += text;
const parts = buffer.split(/\r?\n/);
buffer = parts.pop() || '';
for (const line of parts) {
onOutput(line, streamName);
}
});
stream.on('end', () => {
if (buffer && typeof onOutput === 'function') {
onOutput(buffer, streamName);
}
});
};
streamOutput(child.stdout, 'stdout');
streamOutput(child.stderr, 'stderr');
child.on('error', (error) => {
reject(error);
});
child.on('close', (code) => {
const trimmedOutput = output.trim();
if (code !== 0) {
reject(new Error(trimmedOutput || `Comando local terminou com codigo ${code}`));
return;
}
resolve(trimmedOutput);
});
});
}
async runHelper({ binds, cmd, onOutput }) {
await this.ensureImage();
const container = await this.docker.createContainer({
Image: this.helperImage,
Cmd: ['sh', '-lc', cmd],
Tty: false,
HostConfig: {
Binds: binds,
AutoRemove: false,
NetworkMode: 'none',
},
});
let attachStream;
try {
attachStream = await container.attach({ stream: true, stdout: true, stderr: true });
const stdoutStream = new PassThrough();
const stderrStream = new PassThrough();
this.docker.modem.demuxStream(attachStream, stdoutStream, stderrStream);
const parseOutput = (stream, streamName) => new Promise((resolve) => {
let buffer = '';
stream.on('data', (chunk) => {
const text = chunk.toString('utf8');
output += text;
if (typeof onOutput !== 'function') {
return;
}
buffer += text;
const parts = buffer.split(/\r?\n/);
buffer = parts.pop() || '';
for (const line of parts) {
onOutput(line, streamName);
}
});
stream.on('end', () => {
if (buffer && typeof onOutput === 'function') {
onOutput(buffer, streamName);
}
resolve();
});
});
let output = '';
const outputPromises = [
parseOutput(stdoutStream, 'stdout'),
parseOutput(stderrStream, 'stderr'),
];
await container.start();
const result = await container.wait();
await Promise.all(outputPromises);
output = output.trim();
if (result.StatusCode !== 0) {
throw new Error(output || `Helper container terminou com codigo ${result.StatusCode}`);
}
return output;
} finally {
if (attachStream && typeof attachStream.destroy === 'function') {
attachStream.destroy();
}
try {
await container.remove({ force: true });
} catch {
}
}
}
}
module.exports = DockerService;
+271
View File
@@ -0,0 +1,271 @@
const express = require('express');
const path = require('path');
const fs = require('fs/promises');
const crypto = require('crypto');
const config = require('./config');
const JsonStore = require('./store');
const DockerService = require('./dockerService');
const BackupService = require('./backupService');
async function main() {
await fs.mkdir(config.dataDir, { recursive: true });
const store = new JsonStore(config.storePath);
await store.init();
const dockerService = new DockerService({
socketPath: config.dockerSocketPath,
helperImage: config.helperImage,
});
const backupService = new BackupService({ dockerService, store });
const runJobs = new Map();
const app = express();
app.use(express.json());
app.use(express.static(path.join(process.cwd(), 'public')));
app.get('/api/health', (_request, response) => {
response.json({ ok: true });
});
app.get('/api/containers', async (_request, response) => {
try {
const containers = await dockerService.listContainers();
response.json(containers);
} catch (error) {
response.status(500).json({ error: error.message });
}
});
app.get('/api/profiles', async (_request, response) => {
try {
const profiles = await store.listProfiles();
response.json(profiles);
} catch (error) {
response.status(500).json({ error: error.message });
}
});
app.post('/api/profiles', async (request, response) => {
try {
const payload = request.body || {};
if (!payload.name || !payload.backupDir || !Array.isArray(payload.containerIds) || !payload.containerIds.length) {
response.status(400).json({ error: 'Informe nome, diretorio de backup e ao menos um container.' });
return;
}
if (payload.backupScope && !['volumes', 'container'].includes(payload.backupScope)) {
response.status(400).json({ error: 'Tipo de backup invalido.' });
return;
}
const existing = payload.id ? await store.getProfile(payload.id) : null;
const profile = await store.saveProfile({
id: payload.id,
createdAt: existing?.createdAt,
name: payload.name.trim(),
backupDir: payload.backupDir.trim(),
containerIds: payload.containerIds,
mode: existing?.mode || 'full',
backupScope: payload.backupScope || existing?.backupScope || 'volumes',
});
response.status(payload.id ? 200 : 201).json(profile);
} catch (error) {
response.status(500).json({ error: error.message });
}
});
app.delete('/api/profiles/:profileId', async (request, response) => {
try {
const profile = await store.getProfile(request.params.profileId);
await store.deleteProfile(request.params.profileId);
if (profile?.backupDir) {
const slugify = (value) => value.replace(/[^a-zA-Z0-9_-]+/g, '-').replace(/^-+|-+$/g, '').toLowerCase() || 'item';
const profileBackupDir = path.join(profile.backupDir, slugify(profile.name));
await fs.rm(profileBackupDir, { recursive: true, force: true });
}
response.status(204).end();
} catch (error) {
response.status(500).json({ error: error.message });
}
});
app.get('/api/profiles/:profileId/backups', async (request, response) => {
try {
const backups = await store.listBackups(request.params.profileId);
response.json(backups);
} catch (error) {
response.status(500).json({ error: error.message });
}
});
app.post('/api/profiles/:profileId/run', async (request, response) => {
try {
const profileId = request.params.profileId;
const requestedMode = request.body?.mode;
if (requestedMode && !['full', 'incremental'].includes(requestedMode)) {
response.status(400).json({ error: 'Modo de backup invalido.' });
return;
}
const runningJob = [...runJobs.values()].find((job) => job.profileId === profileId && job.status === 'running');
if (runningJob) {
response.status(409).json({ error: 'Ja existe um backup em execucao para este profile.', runId: runningJob.id });
return;
}
const runId = crypto.randomUUID();
const job = {
id: runId,
profileId,
kind: 'backup',
status: 'running',
startedAt: new Date().toISOString(),
progress: null,
result: null,
error: null,
};
runJobs.set(runId, job);
void backupService.runProfile(profileId, {
mode: requestedMode,
onProgress: (progressSnapshot) => {
const currentJob = runJobs.get(runId);
if (!currentJob) {
return;
}
currentJob.progress = progressSnapshot;
},
}).then((backupRun) => {
const currentJob = runJobs.get(runId);
if (!currentJob) {
return;
}
currentJob.status = backupRun.status === 'ok' ? 'completed' : 'completed-with-errors';
currentJob.result = backupRun;
currentJob.finishedAt = new Date().toISOString();
}).catch((error) => {
const currentJob = runJobs.get(runId);
if (!currentJob) {
return;
}
currentJob.status = 'error';
currentJob.error = error.message;
currentJob.finishedAt = new Date().toISOString();
});
response.status(202).json({ runId, status: 'running' });
} catch (error) {
response.status(500).json({ error: error.message });
}
});
app.get('/api/runs/:runId', (request, response) => {
const job = runJobs.get(request.params.runId);
if (!job) {
response.status(404).json({ error: 'Execucao nao encontrada.' });
return;
}
response.json({
id: job.id,
profileId: job.profileId,
kind: job.kind || 'backup',
status: job.status,
startedAt: job.startedAt,
finishedAt: job.finishedAt,
progress: job.progress,
result: job.result,
error: job.error,
});
});
app.post('/api/profiles/:profileId/restore', async (request, response) => {
try {
const profileId = request.params.profileId;
if (!request.body?.backupId) {
response.status(400).json({ error: 'Informe o backup a ser restaurado.' });
return;
}
const selectedContainerIds = request.body?.containerIds;
if (selectedContainerIds && (!Array.isArray(selectedContainerIds) || !selectedContainerIds.length)) {
response.status(400).json({ error: 'Selecione ao menos um container para restaurar.' });
return;
}
const runningJob = [...runJobs.values()].find((job) => job.profileId === profileId && job.status === 'running');
if (runningJob) {
response.status(409).json({ error: 'Ja existe uma execucao em andamento para este profile.', runId: runningJob.id });
return;
}
const runId = crypto.randomUUID();
const job = {
id: runId,
profileId,
kind: 'restore',
status: 'running',
startedAt: new Date().toISOString(),
progress: null,
result: null,
error: null,
};
runJobs.set(runId, job);
void backupService.restoreBackup(profileId, request.body.backupId, {
selectedContainerIds,
onProgress: (progressSnapshot) => {
const currentJob = runJobs.get(runId);
if (!currentJob) {
return;
}
currentJob.progress = progressSnapshot;
},
}).then((restoreResult) => {
const currentJob = runJobs.get(runId);
if (!currentJob) {
return;
}
currentJob.status = restoreResult.status === 'ok' ? 'completed' : 'completed-with-errors';
currentJob.result = restoreResult;
currentJob.finishedAt = new Date().toISOString();
}).catch((error) => {
const currentJob = runJobs.get(runId);
if (!currentJob) {
return;
}
currentJob.status = 'error';
currentJob.error = error.message;
currentJob.finishedAt = new Date().toISOString();
});
response.status(202).json({ runId, status: 'running', kind: 'restore' });
} catch (error) {
response.status(500).json({ error: error.message });
}
});
app.listen(config.port, () => {
console.log(`Docker Backup app ouvindo na porta ${config.port}`);
});
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
+157
View File
@@ -0,0 +1,157 @@
const fs = require('fs/promises');
const path = require('path');
const { randomUUID } = require('crypto');
class JsonStore {
constructor(filePath) {
this.filePath = filePath;
this.writeQueue = Promise.resolve();
}
async init() {
await fs.mkdir(path.dirname(this.filePath), { recursive: true });
try {
await fs.access(this.filePath);
} catch {
await fs.writeFile(this.filePath, JSON.stringify({ profiles: [], backups: [] }, null, 2));
}
}
async read() {
const raw = await fs.readFile(this.filePath, 'utf8');
const parsed = JSON.parse(raw);
parsed.profiles ||= [];
parsed.backups ||= [];
return parsed;
}
async write(mutator) {
this.writeQueue = this.writeQueue.then(async () => {
const current = await this.read();
const next = await mutator(current);
await fs.writeFile(this.filePath, JSON.stringify(next, null, 2));
return next;
});
return this.writeQueue;
}
async listProfiles() {
const data = await this.read();
return data.profiles;
}
async getProfile(profileId) {
const data = await this.read();
return data.profiles.find((profile) => profile.id === profileId) || null;
}
async saveProfile(profileInput) {
const now = new Date().toISOString();
const profile = {
id: profileInput.id || randomUUID(),
name: profileInput.name,
containerIds: profileInput.containerIds,
mode: profileInput.mode,
backupScope: profileInput.backupScope || 'volumes',
backupDir: profileInput.backupDir,
updatedAt: now,
createdAt: profileInput.createdAt || now,
};
await this.write((data) => {
const index = data.profiles.findIndex((item) => item.id === profile.id);
if (index >= 0) {
data.profiles[index] = profile;
} else {
data.profiles.push(profile);
}
return data;
});
return profile;
}
async deleteProfile(profileId) {
await this.write((data) => {
data.profiles = data.profiles.filter((profile) => profile.id !== profileId);
data.backups = data.backups.filter((backup) => backup.profileId !== profileId);
return data;
});
}
async addBackup(backupRun) {
await this.write((data) => {
data.backups.unshift(backupRun);
return data;
});
return backupRun;
}
async listBackups(profileId) {
const data = await this.read();
return data.backups.filter((backup) => backup.profileId === profileId);
}
async getBackup(backupId) {
const data = await this.read();
return data.backups.find((backup) => backup.id === backupId) || null;
}
async getBackupsForContainer(profileId, containerId, upToBackupId) {
const backups = await this.listBackups(profileId);
const ordered = backups
.slice()
.sort((left, right) => new Date(left.createdAt).getTime() - new Date(right.createdAt).getTime());
const chain = [];
for (const backup of ordered) {
const containerBackup = backup.containers.find(
(item) => item.containerId === containerId && item.status === 'ok' && item.archiveRelativePath,
);
if (!containerBackup) {
if (backup.id === upToBackupId) {
return chain;
}
continue;
}
if (containerBackup.mode === 'full') {
chain.length = 0;
}
chain.push({
backupId: backup.id,
createdAt: backup.createdAt,
...containerBackup,
});
if (backup.id === upToBackupId) {
return chain;
}
}
return [];
}
async getLastContainerBackupTime(profileId, containerId) {
const backups = await this.listBackups(profileId);
const ordered = backups
.slice()
.sort((left, right) => new Date(right.createdAt).getTime() - new Date(left.createdAt).getTime());
for (const backup of ordered) {
const found = backup.containers.find(
(item) => item.containerId === containerId && item.status === 'ok',
);
if (found) {
return backup.createdAt;
}
}
return null;
}
}
module.exports = JsonStore;