/ opencodehub / scripts / backup.ts
swadhinbiswas feat: OpenCodeHub v1.1.0 - Enterprise Features & Storage Adapters · 2/15/2026
719a1cb
78 lines · 2.9 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

import { existsSync, mkdirSync, copyFileSync } from "fs";
import { join, dirname } from "path";
import { execSync } from "child_process";
import { fileURLToPath } from "url";

// Get root directory
const __dirname = dirname(fileURLToPath(import.meta.url));
const rootDir = join(__dirname, "..");
const backupDir = join(rootDir, "backups");
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
const targetDir = join(backupDir, `backup-${timestamp}`);

// Ensure backup directory exists
if (!existsSync(backupDir)) {
    mkdirSync(backupDir);
}
mkdirSync(targetDir);

console.log(`[Backup] Starting backup to ${targetDir}`);

// 1. Backup Database
const dbDriver = process.env.DATABASE_DRIVER || "sqlite";
const dbUrl = process.env.DATABASE_URL || "data/opencodehub.db";

console.log(`[Backup] Backing up database (${dbDriver})...`);

try {
    if (dbDriver === "sqlite") {
        const dbPath = join(rootDir, dbUrl);
        if (existsSync(dbPath)) {
            copyFileSync(dbPath, join(targetDir, "database.sqlite"));
            console.log(`[Backup] SQLite database copied.`);
        } else {
            console.warn(`[Backup] SQLite database not found at ${dbPath}`);
        }
    } else if (dbDriver === "postgres") {
        // Requires pg_dump to be installed
        const outputFile = join(targetDir, "dump.sql");
        execSync(`pg_dump "${dbUrl}" > "${outputFile}"`);
        console.log(`[Backup] Postgres dumped to dump.sql`);
    } else {
        console.log(`[Backup] Automatic backup not supported for driver: ${dbDriver}. Please check functionality manually.`);
    }
} catch (error) {
    console.error(`[Backup] Database backup failed:`, error);
}

// 2. Backup Repositories
// Assumes repos are in "repos" or "git-data" directory relative to root, or specified by config.
// Default configuration often puts them in ./repos or similar.
// We'll try to find the standard location or read from config if possible.
// For now, we assume "./repos" or check environment GIT_STORAGE_PATH

const repoPath = process.env.GIT_STORAGE_PATH ? join(rootDir, process.env.GIT_STORAGE_PATH) : join(rootDir, "repos");

if (existsSync(repoPath)) {
    console.log(`[Backup] Backing up repositories from ${repoPath}...`);
    const repoArchive = join(targetDir, "repos.tar.gz");
    try {
        execSync(`tar -czf "${repoArchive}" -C "${dirname(repoPath)}" "${process.env.GIT_STORAGE_PATH || "repos"}"`);
        console.log(`[Backup] Repositories archived.`);
    } catch (error) {
        console.error(`[Backup] Repository backup failed:`, error);
    }
} else {
    console.warn(`[Backup] Repository directory not found at ${repoPath}`);
}

// 3. Backup Config / Env
const envPath = join(rootDir, ".env");
if (existsSync(envPath)) {
    copyFileSync(envPath, join(targetDir, ".env.backup"));
    console.log(`[Backup] Environment file copied.`);
}

console.log(`[Backup] Completed successfully at ${targetDir}`);