feat: 增加生产数据库每日备份与状态门禁
CI / Browser smoke (pull_request) Successful in 50s
CI / Static checks (pull_request) Successful in 52s
CI / API integration (pull_request) Successful in 1m23s
CI / Production build (pull_request) Successful in 1m48s

This commit is contained in:
que01 committed 2026-10-23 17:54:27 +08:00
1 parent 072a1037ac
commit 015947f8fb
9 files changed
+384 -55

No files matched your search

+39 -51
View File
@@ -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) {