|
| 1 | +import { copyFile } from "node:fs/promises"; |
| 2 | +import { dirname, resolve } from "node:path"; |
| 3 | +import { parseArgs } from "node:util"; |
| 4 | +import { findAndLoadConfig } from "../../config/loader"; |
| 5 | +import { setLogLevel } from "../../utils/logger"; |
| 6 | +import { color, ui } from "../ui"; |
| 7 | + |
| 8 | +export async function exportDbCommand(args: string[]): Promise<number> { |
| 9 | + const { values, positionals } = parseArgs({ |
| 10 | + args, |
| 11 | + options: { |
| 12 | + config: { type: "string", short: "c" }, |
| 13 | + verbose: { type: "boolean", short: "v", default: false }, |
| 14 | + help: { type: "boolean", short: "h", default: false }, |
| 15 | + }, |
| 16 | + allowPositionals: true, |
| 17 | + }); |
| 18 | + |
| 19 | + if (values.help) { |
| 20 | + printHelp(); |
| 21 | + return 0; |
| 22 | + } |
| 23 | + |
| 24 | + if (values.verbose) { |
| 25 | + setLogLevel("debug"); |
| 26 | + } |
| 27 | + |
| 28 | + // Get output path from positional argument |
| 29 | + const outputPath = positionals[0]; |
| 30 | + if (!outputPath) { |
| 31 | + ui.error("Missing required argument: <output-path>"); |
| 32 | + ui.info("Run 'backitup export-db --help' for usage information."); |
| 33 | + return 1; |
| 34 | + } |
| 35 | + |
| 36 | + try { |
| 37 | + const config = await findAndLoadConfig(values.config); |
| 38 | + const dbPath = config.database.path; |
| 39 | + |
| 40 | + ui.banner("export-db"); |
| 41 | + |
| 42 | + // Check if database file exists |
| 43 | + const dbFile = Bun.file(dbPath); |
| 44 | + if (!(await dbFile.exists())) { |
| 45 | + ui.error(`Database file not found: ${dbPath}`); |
| 46 | + ui.info("No backups have been created yet, so there's nothing to export."); |
| 47 | + return 1; |
| 48 | + } |
| 49 | + |
| 50 | + const resolvedOutput = resolve(outputPath); |
| 51 | + const s = ui.spinner(); |
| 52 | + s.start("Exporting database..."); |
| 53 | + |
| 54 | + // Ensure output directory exists |
| 55 | + await Bun.write(dirname(resolvedOutput) + "/.keep", ""); |
| 56 | + await Bun.file(dirname(resolvedOutput) + "/.keep").delete(); |
| 57 | + |
| 58 | + // Copy the database file |
| 59 | + await copyFile(dbPath, resolvedOutput); |
| 60 | + |
| 61 | + const stats = await Bun.file(resolvedOutput).size; |
| 62 | + s.stop("Database exported"); |
| 63 | + |
| 64 | + ui.note( |
| 65 | + `Source: ${dbPath}\nOutput: ${resolvedOutput}\nSize: ${formatBytes(stats)}`, |
| 66 | + "Export Summary", |
| 67 | + ); |
| 68 | + |
| 69 | + ui.outro("Database export complete!"); |
| 70 | + return 0; |
| 71 | + } catch (error) { |
| 72 | + ui.error(`Export failed: ${(error as Error).message}`); |
| 73 | + if (values.verbose) { |
| 74 | + console.error(error); |
| 75 | + } |
| 76 | + return 1; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +function formatBytes(bytes: number): string { |
| 81 | + if (bytes === 0) return "0 B"; |
| 82 | + const k = 1024; |
| 83 | + const sizes = ["B", "KB", "MB", "GB"]; |
| 84 | + const i = Math.floor(Math.log(bytes) / Math.log(k)); |
| 85 | + return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`; |
| 86 | +} |
| 87 | + |
| 88 | +function printHelp(): void { |
| 89 | + console.log(` |
| 90 | +${color.bold("backitup export-db")} - Export the database file |
| 91 | +
|
| 92 | +${color.dim("USAGE:")} |
| 93 | + backitup export-db <output-path> [OPTIONS] |
| 94 | +
|
| 95 | +${color.dim("ARGUMENTS:")} |
| 96 | + <output-path> Path where the database backup will be saved |
| 97 | +
|
| 98 | +${color.dim("OPTIONS:")} |
| 99 | + -c, --config <path> Path to config file (default: ./backitup.config.yaml) |
| 100 | + -v, --verbose Verbose output |
| 101 | + -h, --help Show this help message |
| 102 | +
|
| 103 | +${color.dim("DESCRIPTION:")} |
| 104 | + Creates a copy of the BackItUp SQLite database file. This is useful for: |
| 105 | + - Backing up your backup history and metadata |
| 106 | + - Migrating to a new machine |
| 107 | + - Creating snapshots before major changes |
| 108 | +
|
| 109 | +${color.dim("EXAMPLES:")} |
| 110 | + backitup export-db ./backitup-db-backup.sqlite |
| 111 | + backitup export-db /backups/db/backitup-$(date +%Y%m%d).sqlite |
| 112 | + backitup export-db ~/backitup-db.sqlite -c /etc/backitup/config.yaml |
| 113 | +`); |
| 114 | +} |
0 commit comments