/ opencodehub / scripts / backup-verify.ts
Swadhin Biswas chore(infra): harden deployment, backups, and production docs · 3/6/2026
16d8399
187 lines · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
 * OpenCodeHub Backup Verification Script
 *
 * Verifies backup integrity by checking:
 * - Archive can be extracted
 * - Manifest exists and is valid JSON
 * - All files listed in manifest are present
 * - SHA-256 checksums match
 *
 * Usage:
 *   npx tsx scripts/backup-verify.ts <backup-archive>
 *   npx tsx scripts/backup-verify.ts backups/backup-2025-01-15.tar.gz
 */

import { execSync } from "child_process";
import { createHash } from "crypto";
import { existsSync, mkdirSync, readFileSync, readdirSync, statSync } from "fs";
import { dirname, join } from "path";
import { fileURLToPath } from "url";

const __dirname = dirname(fileURLToPath(import.meta.url));
const rootDir = join(__dirname, "..");

const archivePath = process.argv[2];
if (!archivePath) {
  console.error("Usage: npx tsx scripts/backup-verify.ts <backup-archive>");
  process.exit(1);
}

const fullPath = archivePath.startsWith("/")
  ? archivePath
  : join(process.cwd(), archivePath);

if (!existsSync(fullPath)) {
  console.error(`[Verify] Archive not found: ${fullPath}`);
  process.exit(1);
}

const tempDir = join(rootDir, "backups", `verify-${Date.now()}`);
mkdirSync(tempDir, { recursive: true });

let exitCode = 0;

function pass(msg: string) {
  console.log(`  ✅ ${msg}`);
}
function fail(msg: string) {
  console.log(`  ❌ ${msg}`);
  exitCode = 1;
}

try {
  console.log(`[Verify] Verifying: ${fullPath}`);
  console.log();

  // 1. Can we extract?
  console.log("── 1. Archive Extraction ────────────────────");
  try {
    if (fullPath.endsWith(".enc")) {
      fail(
        "Encrypted archives require decryption first. Use restore.ts or provide BACKUP_ENCRYPTION_KEY.",
      );
    } else {
      execSync(`tar -xzf "${fullPath}" -C "${tempDir}"`, { timeout: 120_000 });
      pass("Archive extracted successfully.");
    }
  } catch (err) {
    fail(`Archive extraction failed: ${err}`);
    process.exit(1);
  }

  // Find backup dir
  const entries = readdirSync(tempDir);
  const backupDirName =
    entries.find((e) => e.startsWith("backup-")) || entries[0];
  const extractedDir = join(tempDir, backupDirName);

  // 2. Manifest
  console.log("── 2. Manifest Validation ──────────────────");
  const manifestPath = join(extractedDir, "manifest.json");
  if (!existsSync(manifestPath)) {
    fail("No manifest.json found in backup.");
  } else {
    let manifest: any;
    try {
      manifest = JSON.parse(readFileSync(manifestPath, "utf-8"));
      pass(
        `Manifest is valid JSON (version=${manifest.version}, type=${manifest.type}).`,
      );
    } catch {
      fail("manifest.json is not valid JSON.");
      manifest = null;
    }

    // 3. File presence
    console.log("── 3. File Presence ────────────────────────");
    if (manifest?.files) {
      for (const file of manifest.files) {
        const filePath = join(extractedDir, file.name);
        if (existsSync(filePath)) {
          const actualSize = statSync(filePath).size;
          if (actualSize === file.size) {
            pass(`${file.name} (${formatBytes(file.size)})`);
          } else {
            fail(
              `${file.name} size mismatch: expected ${file.size}, got ${actualSize}`,
            );
          }
        } else {
          fail(`${file.name} is MISSING`);
        }
      }
    } else {
      fail("No files array in manifest.");
    }

    // 4. Checksum verification
    console.log("── 4. Checksum Verification ────────────────");
    if (manifest?.files) {
      for (const file of manifest.files) {
        if (!file.checksum) {
          console.log(`  ⚠️  ${file.name}: no checksum recorded`);
          continue;
        }
        const filePath = join(extractedDir, file.name);
        if (!existsSync(filePath)) continue;

        const actual = createHash("sha256")
          .update(readFileSync(filePath))
          .digest("hex");
        if (actual === file.checksum) {
          pass(`${file.name} SHA-256 OK`);
        } else {
          fail(
            `${file.name} SHA-256 MISMATCH! Expected ${file.checksum.substring(0, 16)}... got ${actual.substring(0, 16)}...`,
          );
        }
      }
    }

    // 5. Content checks
    console.log("── 5. Content Checks ───────────────────────");
    const dbFile = join(extractedDir, "database.sqlite");
    const dumpFile = join(extractedDir, "dump.sql.gz");
    const repoFile = join(extractedDir, "repos.tar.gz");

    if (existsSync(dbFile)) {
      const size = statSync(dbFile).size;
      if (size > 0) pass(`database.sqlite: ${formatBytes(size)}`);
      else fail("database.sqlite is empty (0 bytes)");
    } else if (existsSync(dumpFile)) {
      const size = statSync(dumpFile).size;
      if (size > 0) pass(`dump.sql.gz: ${formatBytes(size)}`);
      else fail("dump.sql.gz is empty (0 bytes)");
    } else {
      console.log("  ⚠️  No database backup found");
    }

    if (existsSync(repoFile)) {
      const size = statSync(repoFile).size;
      if (size > 0) pass(`repos.tar.gz: ${formatBytes(size)}`);
      else fail("repos.tar.gz is empty (0 bytes)");
    } else {
      console.log("  ⚠️  No repository backup found");
    }
  }

  console.log();
  if (exitCode === 0) {
    console.log("[Verify] ✅ Backup verification PASSED — all checks OK.");
  } else {
    console.log("[Verify] ❌ Backup verification FAILED — see errors above.");
  }
} finally {
  execSync(`rm -rf "${tempDir}"`);
}

function formatBytes(bytes: number): string {
  if (bytes < 1024) return `${bytes} B`;
  if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
  if (bytes < 1024 * 1024 * 1024)
    return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
  return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`;
}

process.exit(exitCode);