Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

3.4 KB
Raw Blame
import fs from "node:fs";
import path from "node:path";
import type { SqliteAdapter } from "./adapters/types";
import { pruneBackupDirectory, resolveDbBackupRetention } from "./backupRetention";
import { chmodQuiet, DB_SECRET_DIR_MODE, DB_SECRET_FILE_MODE } from "./filePermissions";

interface SqlJsSnapshotDatabase {
  constructor: unknown;
  exec(sql: string): Array<{ values: unknown[][] }>;
  run(sql: string, params: string[]): void;
  export(): Uint8Array;
  close(): void;
}

type SqlJsSnapshotConstructor = new (data: Uint8Array) => SqlJsSnapshotDatabase;

function exportSqlJsSnapshot(owner: SqlJsSnapshotDatabase): Uint8Array {
  // Export closes its connection. Copy main into an exclusively allocated
  // WASM file and export only that disposable connection, never the owner.
  const Database = owner.constructor as SqlJsSnapshotConstructor;
  // Supplying bytes makes sql.js create the file exclusively before open.
  // Omitting them could alias an existing random filename and unlink it.
  const snapshot = new Database(new Uint8Array());
  let image: Uint8Array;
  try {
    const filename = snapshot
      .exec("PRAGMA database_list")[0]
      ?.values.find((row) => row[1] === "main")?.[2];
    if (typeof filename !== "string" || !filename) {
      throw new Error("Missing sql.js snapshot filename");
    }
    owner.run("VACUUM main INTO ?", [filename]);
    image = snapshot.export();
  } finally {
    snapshot.close();
  }
  return image;
}

export function createManagedDbBackup(
  db: SqliteAdapter,
  reason: string,
  backupDir: string
): boolean {
  try {
    if (!fs.existsSync(backupDir)) {
      fs.mkdirSync(backupDir, { recursive: true, mode: DB_SECRET_DIR_MODE });
    }

    const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
    const backupPath = path.join(backupDir, `db_${timestamp}_${reason}.sqlite`);
    const escapedBackupPath = backupPath.replace(/'/g, "''");

    if (db.driver === "sql.js") {
      const image = exportSqlJsSnapshot(db.raw as SqlJsSnapshotDatabase);
      const fd = fs.openSync(backupPath, "wx", 0o600);
      try {
        fs.writeFileSync(fd, image);
        fs.fsyncSync(fd);
      } catch (error) {
        try {
          fs.unlinkSync(backupPath);
        } catch {
          // Preserve the original write failure.
        }
        throw error;
      } finally {
        fs.closeSync(fd);
      }
    } else {
      db.exec(`VACUUM INTO '${escapedBackupPath}'`);
      // SQLite creates the VACUUM INTO target itself, so it inherits the umask.
      chmodQuiet(backupPath, DB_SECRET_FILE_MODE);
    }
    console.log(`[DB] Backup created (${reason}): ${backupPath}`);

    // Prune old backups to prevent the directory from growing without bound.
    // This mirrors the post-backup pruning in backup.ts but avoids a circular
    // dependency by importing directly from backupRetention.ts. Resolving
    // through resolveDbBackupRetention() (not an env-only inline fallback)
    // means the persisted Storage-page setting is honored here too, not just
    // for manual/API/auto backups (#13308).
    try {
      pruneBackupDirectory({ backupDir, ...resolveDbBackupRetention(db) });
    } catch {
      // Retention is best-effort; never let a pruning failure obscure the backup result.
    }

    return true;
  } catch (error: unknown) {
    const message = error instanceof Error ? error.message : String(error);
    console.warn(`[DB] Failed to create ${reason} backup:`, message);
    return false;
  }
}