Add Backup and Restore

This commit is contained in:
2026-03-02 14:51:19 +01:00
parent 634be9b01d
commit ec27ff9c04
37 changed files with 505 additions and 8 deletions
+2
View File
@@ -0,0 +1,2 @@
import { Command } from "commander";
export default function (): Command;
+22
View File
@@ -0,0 +1,22 @@
import { Command } from "commander";
import init from "../functions/init";
import path from "path";
import grabDBDir from "../utils/grab-db-dir";
import fs from "fs";
import grabDBBackupFileName from "../utils/grab-db-backup-file-name";
import chalk from "chalk";
import trimBackups from "../utils/trim-backups";
export default function () {
return new Command("backup")
.description("Backup Database")
.action(async (opts) => {
console.log(`Backing up database ...`);
const { config } = await init();
const { backup_dir, db_file_path } = grabDBDir({ config });
const new_db_file_name = grabDBBackupFileName({ config });
fs.cpSync(db_file_path, path.join(backup_dir, new_db_file_name));
trimBackups({ config });
console.log(`${chalk.bold(chalk.green(`DB Backup Success!`))}`);
process.exit();
});
}
+4
View File
@@ -2,6 +2,8 @@
import { program } from "commander";
import schema from "./schema";
import typedef from "./typedef";
import backup from "./backup";
import restore from "./restore";
/**
* # Describe Program
*/
@@ -14,6 +16,8 @@ program
*/
program.addCommand(schema());
program.addCommand(typedef());
program.addCommand(backup());
program.addCommand(restore());
/**
* # Handle Unavailable Commands
*/
+2
View File
@@ -0,0 +1,2 @@
import { Command } from "commander";
export default function (): Command;
+44
View File
@@ -0,0 +1,44 @@
import { Command } from "commander";
import init from "../functions/init";
import grabDBDir from "../utils/grab-db-dir";
import fs from "fs";
import chalk from "chalk";
import grabSortedBackups from "../utils/grab-sorted-backups";
import { select } from "@inquirer/prompts";
import grabBackupData from "../utils/grab-backup-data";
import path from "path";
export default function () {
return new Command("restore")
.description("Restore Database")
.action(async (opts) => {
console.log(`Restoring up database ...`);
const { config } = await init();
const { backup_dir, db_file_path } = grabDBDir({ config });
const backups = grabSortedBackups({ config });
if (!backups?.[0]) {
console.error(`No Backups to restore. Use the \`backup\` command to create a backup`);
process.exit(1);
}
try {
const selected_backup = await select({
message: "Select a backup:",
choices: backups.map((b, i) => {
const { backup_date } = grabBackupData({
backup_name: b,
});
return {
name: `Backup #${i + 1}: ${backup_date.toDateString()} ${backup_date.getHours()}:${backup_date.getMinutes()}:${backup_date.getSeconds().toString().padStart(2, "0")}`,
value: b,
};
}),
});
fs.cpSync(path.join(backup_dir, selected_backup), db_file_path);
console.log(`${chalk.bold(chalk.green(`DB Restore Success!`))}`);
process.exit();
}
catch (error) {
console.error(`Backup Restore ERROR => ${error.message}`);
process.exit();
}
});
}
+2
View File
@@ -7,6 +7,7 @@ import dbSchemaToTypeDef from "../lib/sqlite/schema-to-typedef";
import _ from "lodash";
import { DefaultFields } from "../types";
import appendDefaultFieldsToDbSchema from "../utils/append-default-fields-to-db-schema";
import chalk from "chalk";
export default function () {
return new Command("schema")
.description("Build DB From Schema")
@@ -32,6 +33,7 @@ export default function () {
dst_file: out_file,
});
}
console.log(`${chalk.bold(chalk.green(`DB Schema setup success!`))}`);
process.exit();
});
}
+2
View File
@@ -4,6 +4,7 @@ import dbSchemaToTypeDef from "../lib/sqlite/schema-to-typedef";
import path from "path";
import grabDirNames from "../data/grab-dir-names";
import appendDefaultFieldsToDbSchema from "../utils/append-default-fields-to-db-schema";
import chalk from "chalk";
export default function () {
return new Command("typedef")
.description("Build DB From Schema")
@@ -23,6 +24,7 @@ export default function () {
console.error(``);
process.exit(1);
}
console.log(`${chalk.bold(chalk.green(`Typedef gen success!`))}`);
process.exit();
});
}