feat: 增加生产数据库每日备份与状态门禁
This commit is contained in:
1 parent
072a1037ac
commit
015947f8fb
9 files changed
+384
-55
No files matched your search
@@ -0,0 +1,53 @@
|
||||
import { mkdir, rm } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import {
|
||||
backupPostgresDatabase,
|
||||
pruneBackups,
|
||||
scheduledBackupName,
|
||||
verifyPostgresBackup,
|
||||
} from "./database-backup.mjs";
|
||||
|
||||
const root = process.env.DEPLOY_ROOT;
|
||||
if (!root || !path.isAbsolute(root)) throw new Error("DEPLOY_ROOT must be absolute");
|
||||
process.loadEnvFile(path.join(root, "shared/.env.production"));
|
||||
|
||||
const container = process.env.POSTGRES_CONTAINER;
|
||||
if (!container || !/^[a-zA-Z0-9_.-]+$/.test(container)) {
|
||||
throw new Error("POSTGRES_CONTAINER is required");
|
||||
}
|
||||
const retentionDays = Number(process.env.BACKUP_RETENTION_DAYS ?? "14");
|
||||
if (!Number.isInteger(retentionDays) || retentionDays < 1) {
|
||||
throw new Error("BACKUP_RETENTION_DAYS must be a positive integer");
|
||||
}
|
||||
|
||||
const directory = path.join(root, "shared/backups");
|
||||
await mkdir(directory, { recursive: true });
|
||||
const destination = path.join(directory, scheduledBackupName());
|
||||
|
||||
await backupPostgresDatabase({ container, destination });
|
||||
try {
|
||||
await verifyPostgresBackup({ container, file: destination });
|
||||
} catch (error) {
|
||||
await rm(destination, { force: true });
|
||||
throw error;
|
||||
}
|
||||
await pruneBackups(directory, { retentionDays });
|
||||
|
||||
const offsiteTarget = process.env.BACKUP_OFFSITE_TARGET?.trim();
|
||||
if (offsiteTarget) {
|
||||
if (
|
||||
!/^[a-zA-Z0-9_.@:/-]+$/.test(offsiteTarget) ||
|
||||
offsiteTarget.startsWith("-") ||
|
||||
offsiteTarget.includes("..")
|
||||
) {
|
||||
throw new Error("BACKUP_OFFSITE_TARGET contains unsupported characters");
|
||||
}
|
||||
const result = spawnSync("rsync", ["-a", "--partial", destination, offsiteTarget], {
|
||||
stdio: "inherit",
|
||||
env: process.env,
|
||||
});
|
||||
if (result.status !== 0) throw new Error("Offsite backup copy failed");
|
||||
}
|
||||
|
||||
console.log(`scheduled backup written: ${destination}`);
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { chmod, mkdtemp, readFile, rm, stat, utimes, writeFile } from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import test from "node:test";
|
||||
import {
|
||||
backupPostgresDatabase,
|
||||
pruneBackups,
|
||||
scheduledBackupName,
|
||||
selectExpiredBackups,
|
||||
verifyPostgresBackup,
|
||||
} from "./database-backup.mjs";
|
||||
|
||||
test("定时备份文件名使用 UTC 时间和进程号", () => {
|
||||
assert.equal(
|
||||
scheduledBackupName(new Date("2026-10-23T09:45:00.123Z"), 42),
|
||||
"scheduled-20261023T094500Z-42.dump",
|
||||
);
|
||||
});
|
||||
|
||||
test("发布备份只保留最近 7 份,定时备份按保留天数清理", () => {
|
||||
const now = Date.parse("2026-10-23T12:00:00Z");
|
||||
const day = 24 * 60 * 60 * 1000;
|
||||
const entries = [
|
||||
...Array.from({ length: 8 }, (_, index) => ({
|
||||
name: `2026100${index + 1}000000-release${index}.dump`,
|
||||
mtimeMs: now,
|
||||
})),
|
||||
{ name: "scheduled-20261001T000000Z-1.dump", mtimeMs: now - 30 * day },
|
||||
{ name: "scheduled-20261020T000000Z-2.dump", mtimeMs: now - 3 * day },
|
||||
];
|
||||
|
||||
assert.deepEqual(selectExpiredBackups(entries, { now, retentionDays: 14 }), [
|
||||
"20261001000000-release0.dump",
|
||||
"scheduled-20261001T000000Z-1.dump",
|
||||
]);
|
||||
});
|
||||
|
||||
test("非法保留天数会被拒绝", () => {
|
||||
assert.throws(() => selectExpiredBackups([], { retentionDays: 0 }), /positive integer/);
|
||||
});
|
||||
|
||||
test("pruneBackups 删除过期备份和残留临时文件", async () => {
|
||||
const directory = await mkdtemp(path.join(os.tmpdir(), "personal-brand-backup-"));
|
||||
const old = path.join(directory, "scheduled-20260101T000000Z-1.dump");
|
||||
const current = path.join(directory, "scheduled-20261023T000000Z-2.dump");
|
||||
const temporary = path.join(directory, "scheduled-20261023T000000Z-3.dump.tmp");
|
||||
try {
|
||||
await writeFile(old, "old");
|
||||
await writeFile(current, "current");
|
||||
await writeFile(temporary, "partial");
|
||||
const oldTime = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
|
||||
await utimes(old, oldTime, oldTime);
|
||||
|
||||
await pruneBackups(directory, { retentionDays: 14 });
|
||||
|
||||
await assert.rejects(stat(old), { code: "ENOENT" });
|
||||
await assert.rejects(stat(temporary), { code: "ENOENT" });
|
||||
assert.equal((await stat(current)).size, 7);
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
async function writeFakeDocker(directory, body) {
|
||||
const file = path.join(directory, "docker");
|
||||
await writeFile(file, "#!/bin/sh\n" + body, { mode: 0o755 });
|
||||
await chmod(file, 0o755);
|
||||
return file;
|
||||
}
|
||||
|
||||
test("备份写入权限受限的 dump 文件并清理临时文件", async () => {
|
||||
const directory = await mkdtemp(path.join(os.tmpdir(), "personal-brand-dump-"));
|
||||
const destination = path.join(directory, "release.dump");
|
||||
try {
|
||||
const docker = await writeFakeDocker(directory, "printf 'dump-bytes'\n");
|
||||
await backupPostgresDatabase({ container: "postgres", destination, docker });
|
||||
|
||||
assert.equal(await readFile(destination, "utf8"), "dump-bytes");
|
||||
assert.equal((await stat(destination)).mode & 0o777, 0o600);
|
||||
await assert.rejects(stat(destination + ".tmp"), { code: "ENOENT" });
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("pg_dump 失败时删除半成品备份", async () => {
|
||||
const directory = await mkdtemp(path.join(os.tmpdir(), "personal-brand-dump-"));
|
||||
const destination = path.join(directory, "release.dump");
|
||||
try {
|
||||
const docker = await writeFakeDocker(directory, "printf 'partial'; exit 1\n");
|
||||
await assert.rejects(
|
||||
backupPostgresDatabase({ container: "postgres", destination, docker }),
|
||||
/Database backup failed/,
|
||||
);
|
||||
|
||||
await assert.rejects(stat(destination), { code: "ENOENT" });
|
||||
await assert.rejects(stat(destination + ".tmp"), { code: "ENOENT" });
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("备份可被 pg_restore 读取校验", async () => {
|
||||
const directory = await mkdtemp(path.join(os.tmpdir(), "personal-brand-verify-"));
|
||||
const file = path.join(directory, "release.dump");
|
||||
try {
|
||||
await writeFile(file, "dump-bytes");
|
||||
const docker = await writeFakeDocker(directory, "cat >/dev/null; exit 0\n");
|
||||
await verifyPostgresBackup({ container: "postgres", file, docker });
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("pg_restore 校验失败时报告不可读", async () => {
|
||||
const directory = await mkdtemp(path.join(os.tmpdir(), "personal-brand-verify-"));
|
||||
const file = path.join(directory, "release.dump");
|
||||
try {
|
||||
await writeFile(file, "broken");
|
||||
const docker = await writeFakeDocker(
|
||||
directory,
|
||||
"cat >/dev/null; echo 'invalid header' >&2; exit 1\n",
|
||||
);
|
||||
await assert.rejects(
|
||||
verifyPostgresBackup({ container: "postgres", file, docker }),
|
||||
/invalid header/,
|
||||
);
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
+39
-51
@@ -1,4 +1,3 @@
|
||||
import { createWriteStream } from "node:fs";
|
||||
import {
|
||||
copyFile,
|
||||
cp,
|
||||
@@ -14,9 +13,10 @@ import {
|
||||
writeFile,
|
||||
} from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { checkPackage } from "../scripts/deploy/check-package.mjs";
|
||||
import { assertReleaseEnvironment } from "./release-environment.mjs";
|
||||
import { backupPostgresDatabase, pruneBackups, verifyPostgresBackup } from "./database-backup.mjs";
|
||||
|
||||
const root = process.env.DEPLOY_ROOT;
|
||||
if (!root || !path.isAbsolute(root)) throw new Error("DEPLOY_ROOT must be absolute");
|
||||
@@ -98,6 +98,37 @@ async function installPm2Service() {
|
||||
run("systemctl", ["--user", "enable", pm2ServiceName]);
|
||||
}
|
||||
|
||||
const backupTimerName = "personal-brand-backup.timer";
|
||||
|
||||
async function installBackupTimer(release) {
|
||||
const directory = path.dirname(adminServiceFile);
|
||||
const serviceFile = path.join(directory, "personal-brand-backup.service");
|
||||
const timerFile = path.join(directory, backupTimerName);
|
||||
await mkdir(directory, { recursive: true });
|
||||
// 定时任务从 shared 读取稳定副本;发布回滚后 current 变了,备份任务仍然可用。
|
||||
await copyFile(
|
||||
path.join(release, "deploy/backup-db.mjs"),
|
||||
path.join(root, "shared/backup-db.mjs"),
|
||||
);
|
||||
await copyFile(
|
||||
path.join(release, "deploy/database-backup.mjs"),
|
||||
path.join(root, "shared/database-backup.mjs"),
|
||||
);
|
||||
await writeFile(
|
||||
serviceFile,
|
||||
`[Unit]\nDescription=Personal Brand daily database backup\nAfter=network-online.target\nWants=network-online.target\n\n[Service]\nType=oneshot\nEnvironment=DEPLOY_ROOT=${root}\nEnvironment=HOME=${process.env.HOME ?? "/home/ubuntu"}\nEnvironment=PATH=${process.env.PATH}\nExecStart=${process.execPath} ${root}/shared/backup-db.mjs\n`,
|
||||
{ mode: 0o644 },
|
||||
);
|
||||
await writeFile(
|
||||
timerFile,
|
||||
`[Unit]\nDescription=Daily Personal Brand database backup\n\n[Timer]\nOnCalendar=*-*-* 03:30:00\nPersistent=true\nRandomizedDelaySec=10m\n\n[Install]\nWantedBy=timers.target\n`,
|
||||
{ mode: 0o644 },
|
||||
);
|
||||
run("systemctl", ["--user", "daemon-reload"]);
|
||||
run("systemctl", ["--user", "enable", "--now", backupTimerName]);
|
||||
run("systemctl", ["--user", "restart", backupTimerName]);
|
||||
}
|
||||
|
||||
async function waitForAdminHealth() {
|
||||
const url = "http://127.0.0.1:" + (process.env.ADMIN_STATIC_PORT ?? "3010") + "/admin/";
|
||||
for (let attempt = 0; attempt < 20; attempt++) {
|
||||
@@ -134,53 +165,6 @@ async function switchCurrent(target) {
|
||||
await rename(temporary, current);
|
||||
}
|
||||
|
||||
async function backupDatabase(destination) {
|
||||
const temporary = destination + ".tmp";
|
||||
const child = spawn(
|
||||
"docker",
|
||||
[
|
||||
"exec",
|
||||
postgresContainer,
|
||||
"sh",
|
||||
"-c",
|
||||
'exec pg_dump -Fc -U "$POSTGRES_USER" -d "$POSTGRES_DB"',
|
||||
],
|
||||
{ stdio: ["ignore", "pipe", "inherit"] },
|
||||
);
|
||||
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) {
|
||||
await rm(temporary, { force: true });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function pruneBackups(directory) {
|
||||
const backups = (await readdir(directory, { withFileTypes: true }))
|
||||
.filter((entry) => entry.isFile() && entry.name.endsWith(".dump"))
|
||||
.map((entry) => entry.name)
|
||||
.sort()
|
||||
.reverse();
|
||||
for (const backup of backups.slice(7)) await rm(path.join(directory, backup));
|
||||
for (const entry of await readdir(directory)) {
|
||||
if (entry.endsWith(".dump.tmp")) await rm(path.join(directory, entry), { force: true });
|
||||
}
|
||||
}
|
||||
|
||||
async function pruneReleases(directory, currentRelease, previousRelease) {
|
||||
const releases = (await readdir(directory, { withFileTypes: true }))
|
||||
.filter((entry) => entry.isDirectory())
|
||||
@@ -367,8 +351,11 @@ try {
|
||||
release,
|
||||
);
|
||||
run("pnpm", ["--filter", "@personal-brand/api", "exec", "prisma", "generate"], release);
|
||||
await backupDatabase(path.join(root, "shared/backups", manifest.version + ".dump"));
|
||||
await pruneBackups(path.join(root, "shared/backups"));
|
||||
const backupDirectory = path.join(root, "shared/backups");
|
||||
const releaseBackup = path.join(backupDirectory, manifest.version + ".dump");
|
||||
await backupPostgresDatabase({ container: postgresContainer, destination: releaseBackup });
|
||||
await verifyPostgresBackup({ container: postgresContainer, file: releaseBackup });
|
||||
await pruneBackups(backupDirectory);
|
||||
// 旧数据的模板包声明必须先收敛,migration 才能安全地删除兼容字段。
|
||||
run(process.execPath, ["deploy/prepare-template-contract.mjs", ".", "--apply"], release);
|
||||
run("pnpm", ["--filter", "@personal-brand/api", "exec", "prisma", "migrate", "deploy"], release);
|
||||
@@ -390,6 +377,7 @@ try {
|
||||
await writeFile(path.join(root, "shared/previous-release"), previous + "\n");
|
||||
run("pm2", ["save"]);
|
||||
await pruneReleases(path.join(root, "releases"), release, previous);
|
||||
await installBackupTimer(release);
|
||||
console.log("release installed: " + manifest.version);
|
||||
} catch (error) {
|
||||
if (routeUpdated) {
|
||||
|
||||
Reference in new issue
Block a user