123
This commit is contained in:
@@ -1,22 +1,22 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { spawn } = require('child_process');
|
||||
|
||||
const BACKUPS_DIR = path.join(__dirname, '..', '..', 'backups');
|
||||
const USERS_FILE = path.join(__dirname, '..', '..', 'users.json');
|
||||
const AUDIT_LOG_FILE = path.join(__dirname, '..', '..', 'audit.log');
|
||||
const SETTINGS_FILE = path.join(__dirname, '..', '..', 'settings.json');
|
||||
const BACKUP_PREFIX = 'backup-';
|
||||
|
||||
function ensureBackupsDir() {
|
||||
fs.mkdirSync(BACKUPS_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
function makeBackupFilename() {
|
||||
const now = new Date().toISOString().replace(/[:.]/g, '-');
|
||||
return `backup-${now}.json`;
|
||||
function makeBackupStamp() {
|
||||
return new Date().toISOString().replace(/[:.]/g, '-');
|
||||
}
|
||||
|
||||
async function createBackup(pool, actor = 'system') {
|
||||
ensureBackupsDir();
|
||||
|
||||
async function collectAppSnapshot(pool, actor = 'system') {
|
||||
const tablesResult = await pool.query(`
|
||||
SELECT table_name
|
||||
FROM information_schema.tables
|
||||
@@ -45,44 +45,123 @@ async function createBackup(pool, actor = 'system') {
|
||||
});
|
||||
}
|
||||
|
||||
const backup = {
|
||||
return {
|
||||
meta: {
|
||||
createdAt: new Date().toISOString(),
|
||||
createdBy: actor,
|
||||
version: 1,
|
||||
},
|
||||
users: fs.existsSync(USERS_FILE) ? JSON.parse(fs.readFileSync(USERS_FILE, 'utf8')) : { users: [] },
|
||||
settings: fs.existsSync(SETTINGS_FILE) ? JSON.parse(fs.readFileSync(SETTINGS_FILE, 'utf8')) : null,
|
||||
audit: fs.existsSync(AUDIT_LOG_FILE)
|
||||
? fs.readFileSync(AUDIT_LOG_FILE, 'utf8').split(/\r?\n/).filter(Boolean)
|
||||
: [],
|
||||
tables,
|
||||
};
|
||||
}
|
||||
|
||||
const filename = makeBackupFilename();
|
||||
const filePath = path.join(BACKUPS_DIR, filename);
|
||||
fs.writeFileSync(filePath, JSON.stringify(backup, null, 2), 'utf8');
|
||||
function runPgDump() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const args = [
|
||||
'-h', process.env.DB_HOST || 'db',
|
||||
'-p', String(process.env.DB_PORT || '5432'),
|
||||
'-U', process.env.DB_USER || 'postgres',
|
||||
'-d', process.env.DB_NAME || 'postgres',
|
||||
'--clean',
|
||||
'--if-exists',
|
||||
'--no-owner',
|
||||
'--no-privileges',
|
||||
];
|
||||
|
||||
const child = spawn('pg_dump', args, {
|
||||
env: {
|
||||
...process.env,
|
||||
PGPASSWORD: process.env.DB_PASSWORD || '',
|
||||
},
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
});
|
||||
|
||||
const stdout = [];
|
||||
const stderr = [];
|
||||
child.stdout.on('data', (chunk) => stdout.push(chunk));
|
||||
child.stderr.on('data', (chunk) => stderr.push(chunk));
|
||||
child.on('error', reject);
|
||||
child.on('close', (code) => {
|
||||
if (code !== 0) {
|
||||
reject(new Error(Buffer.concat(stderr).toString('utf8') || `pg_dump exited with code ${code}`));
|
||||
return;
|
||||
}
|
||||
resolve(Buffer.concat(stdout).toString('utf8'));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function formatBackupEntry(filePath, filename) {
|
||||
const stats = fs.statSync(filePath);
|
||||
const kind = filename.endsWith('.sql') ? 'database' : 'application';
|
||||
const match = filename.match(/^backup-(.+?)-(db|app)\.(sql|json)$/);
|
||||
return {
|
||||
filename,
|
||||
filePath,
|
||||
size: fs.statSync(filePath).size,
|
||||
createdAt: backup.meta.createdAt,
|
||||
size: stats.size,
|
||||
createdAt: stats.birthtime.toISOString(),
|
||||
kind,
|
||||
bundle: match ? match[1] : null,
|
||||
};
|
||||
}
|
||||
|
||||
function pruneBackups(keepLast = 14) {
|
||||
ensureBackupsDir();
|
||||
const maxFiles = Math.max(1, keepLast) * 2;
|
||||
const files = fs.readdirSync(BACKUPS_DIR)
|
||||
.filter((name) => name.startsWith(BACKUP_PREFIX) && (name.endsWith('.json') || name.endsWith('.sql')))
|
||||
.map((name) => ({
|
||||
name,
|
||||
filePath: path.join(BACKUPS_DIR, name),
|
||||
mtimeMs: fs.statSync(path.join(BACKUPS_DIR, name)).mtimeMs,
|
||||
}))
|
||||
.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
||||
|
||||
files.slice(maxFiles).forEach((file) => {
|
||||
fs.unlinkSync(file.filePath);
|
||||
});
|
||||
}
|
||||
|
||||
async function createBackup(pool, actor = 'system', options = {}) {
|
||||
ensureBackupsDir();
|
||||
const stamp = makeBackupStamp();
|
||||
const createdAt = new Date().toISOString();
|
||||
const includeAppSnapshot = options.includeAppSnapshot !== false;
|
||||
const files = [];
|
||||
|
||||
const sqlDump = await runPgDump();
|
||||
const sqlFilename = `${BACKUP_PREFIX}${stamp}-db.sql`;
|
||||
const sqlPath = path.join(BACKUPS_DIR, sqlFilename);
|
||||
fs.writeFileSync(sqlPath, sqlDump, 'utf8');
|
||||
files.push(formatBackupEntry(sqlPath, sqlFilename));
|
||||
|
||||
if (includeAppSnapshot) {
|
||||
const snapshot = await collectAppSnapshot(pool, actor);
|
||||
const jsonFilename = `${BACKUP_PREFIX}${stamp}-app.json`;
|
||||
const jsonPath = path.join(BACKUPS_DIR, jsonFilename);
|
||||
fs.writeFileSync(jsonPath, JSON.stringify(snapshot, null, 2), 'utf8');
|
||||
files.push(formatBackupEntry(jsonPath, jsonFilename));
|
||||
}
|
||||
|
||||
if (options.keepLast) {
|
||||
pruneBackups(options.keepLast);
|
||||
}
|
||||
|
||||
return {
|
||||
createdAt,
|
||||
files,
|
||||
};
|
||||
}
|
||||
|
||||
function listBackups() {
|
||||
ensureBackupsDir();
|
||||
return fs.readdirSync(BACKUPS_DIR)
|
||||
.filter((name) => name.endsWith('.json'))
|
||||
.map((name) => {
|
||||
const filePath = path.join(BACKUPS_DIR, name);
|
||||
const stats = fs.statSync(filePath);
|
||||
return {
|
||||
filename: name,
|
||||
size: stats.size,
|
||||
createdAt: stats.birthtime.toISOString(),
|
||||
};
|
||||
})
|
||||
.filter((name) => name.startsWith(BACKUP_PREFIX) && (name.endsWith('.json') || name.endsWith('.sql')))
|
||||
.map((name) => formatBackupEntry(path.join(BACKUPS_DIR, name), name))
|
||||
.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
|
||||
}
|
||||
|
||||
@@ -99,4 +178,5 @@ module.exports = {
|
||||
createBackup,
|
||||
getBackupPath,
|
||||
listBackups,
|
||||
pruneBackups,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user