feat: 增加生产数据库每日备份与状态门禁
This commit is contained in:
1 parent
072a1037ac
commit
015947f8fb
9 files changed
+384
-55
No files matched your search
@@ -0,0 +1,138 @@
|
||||
import { createReadStream, createWriteStream } from "node:fs";
|
||||
import { readdir, rename, rm, stat } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
|
||||
const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
const SCHEDULED_BACKUP = /^scheduled-\d{8}T\d{6}Z-\d+\.dump$/;
|
||||
|
||||
/**
|
||||
* 定时备份文件名包含 UTC 秒级时间和进程号,避免同一秒内重复执行时覆盖已有备份。
|
||||
*/
|
||||
export function scheduledBackupName(date = new Date(), pid = process.pid) {
|
||||
const stamp = date
|
||||
.toISOString()
|
||||
.replace(/[-:]/g, "")
|
||||
.replace(/\.\d{3}Z$/, "Z");
|
||||
if (!/^\d{8}T\d{6}Z$/.test(stamp)) throw new Error("Invalid backup timestamp");
|
||||
return `scheduled-${stamp}-${pid}.dump`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保留策略:
|
||||
* - 发布前备份按文件名保留最近 7 份,保持原有发布回滚能力;
|
||||
* - 定时备份按修改时间保留最近 retentionDays 天;
|
||||
* - 同名临时文件由 pruneBackups 统一清理。
|
||||
*/
|
||||
export function selectExpiredBackups(
|
||||
entries,
|
||||
{ now = Date.now(), retentionDays = 14, keepReleaseBackups = 7 } = {},
|
||||
) {
|
||||
if (!Number.isInteger(retentionDays) || retentionDays < 1) {
|
||||
throw new Error("retentionDays must be a positive integer");
|
||||
}
|
||||
if (!Number.isInteger(keepReleaseBackups) || keepReleaseBackups < 1) {
|
||||
throw new Error("keepReleaseBackups must be a positive integer");
|
||||
}
|
||||
|
||||
const cutoff = now - retentionDays * DAY_MS;
|
||||
const expired = [];
|
||||
const releaseBackups = entries
|
||||
.filter((entry) => entry.name.endsWith(".dump") && !SCHEDULED_BACKUP.test(entry.name))
|
||||
.sort((left, right) => right.name.localeCompare(left.name));
|
||||
|
||||
for (const entry of releaseBackups.slice(keepReleaseBackups)) expired.push(entry.name);
|
||||
for (const entry of entries) {
|
||||
if (SCHEDULED_BACKUP.test(entry.name) && entry.mtimeMs < cutoff) expired.push(entry.name);
|
||||
}
|
||||
|
||||
return [...new Set(expired)];
|
||||
}
|
||||
|
||||
export async function backupPostgresDatabase({
|
||||
container,
|
||||
destination,
|
||||
docker = "docker",
|
||||
env = process.env,
|
||||
}) {
|
||||
const temporary = destination + ".tmp";
|
||||
const child = spawn(
|
||||
docker,
|
||||
["exec", container, "sh", "-c", 'exec pg_dump -Fc -U "$POSTGRES_USER" -d "$POSTGRES_DB"'],
|
||||
{ stdio: ["ignore", "pipe", "inherit"], env },
|
||||
);
|
||||
const output = createWriteStream(temporary, { flags: "wx", mode: 0o600 });
|
||||
child.stdout.pipe(output);
|
||||
try {
|
||||
await Promise.all([
|
||||
new Promise((resolve, reject) => {
|
||||
child.on("error", reject);
|
||||
child.on("exit", (code) =>
|
||||
code === 0 ? resolve() : reject(new Error("Database backup failed")),
|
||||
);
|
||||
}),
|
||||
new Promise((resolve, reject) => {
|
||||
output.on("finish", resolve);
|
||||
output.on("error", reject);
|
||||
}),
|
||||
]);
|
||||
await rename(temporary, destination);
|
||||
} catch (error) {
|
||||
child.kill("SIGTERM");
|
||||
output.destroy();
|
||||
await rm(temporary, { force: true });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function verifyPostgresBackup({
|
||||
container,
|
||||
file,
|
||||
docker = "docker",
|
||||
env = process.env,
|
||||
}) {
|
||||
const child = spawn(docker, ["exec", "-i", container, "pg_restore", "-l"], {
|
||||
stdio: ["pipe", "ignore", "pipe"],
|
||||
env,
|
||||
});
|
||||
const input = createReadStream(file);
|
||||
let stderr = "";
|
||||
child.stderr.setEncoding("utf8");
|
||||
child.stderr.on("data", (chunk) => {
|
||||
stderr += chunk;
|
||||
});
|
||||
input.pipe(child.stdin);
|
||||
|
||||
try {
|
||||
await new Promise((resolve, reject) => {
|
||||
child.on("error", reject);
|
||||
input.on("error", reject);
|
||||
child.stdin.on("error", (error) => {
|
||||
if (error.code !== "EPIPE") reject(error);
|
||||
});
|
||||
child.on("exit", (code) => {
|
||||
if (code === 0) resolve();
|
||||
else reject(new Error(`Backup is not readable: ${stderr.trim() || "pg_restore failed"}`));
|
||||
});
|
||||
});
|
||||
} finally {
|
||||
input.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
export async function pruneBackups(directory, options = {}) {
|
||||
const entries = await readdir(directory);
|
||||
const withStats = [];
|
||||
for (const name of entries) {
|
||||
const info = await stat(path.join(directory, name));
|
||||
if (!info.isFile()) continue;
|
||||
withStats.push({ name, mtimeMs: info.mtimeMs });
|
||||
}
|
||||
|
||||
for (const name of selectExpiredBackups(withStats, options)) {
|
||||
await rm(path.join(directory, name));
|
||||
}
|
||||
for (const name of entries) {
|
||||
if (name.endsWith(".dump.tmp")) await rm(path.join(directory, name), { force: true });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user