51 lines
2.1 KiB
JavaScript
51 lines
2.1 KiB
JavaScript
import { Command } from "commander";
|
|
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";
|
|
import { restoreDatabase } from "../utils/mariadb-dump-restore";
|
|
export default function () {
|
|
return new Command("restore")
|
|
.description("Restore MariaDB database from an SQL dump")
|
|
.action(async () => {
|
|
console.log(`Restoring database ...`);
|
|
const config = global.CONFIG;
|
|
const { backup_dir } = grabDBDir({ config });
|
|
const backups = grabSortedBackups({ config }).filter((b) => b.endsWith(".sql"));
|
|
if (!backups?.[0]) {
|
|
console.error(`No SQL 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,
|
|
};
|
|
}),
|
|
});
|
|
const backup_path = path.join(backup_dir, selected_backup);
|
|
if (!fs.existsSync(backup_path)) {
|
|
console.error(`Backup file not found: ${backup_path}`);
|
|
process.exit(1);
|
|
}
|
|
const sql = fs.readFileSync(backup_path, "utf-8");
|
|
await restoreDatabase(config, sql);
|
|
console.log(`${chalk.bold(chalk.green(`DB Restore Success!`))} ← ${selected_backup}`);
|
|
process.exit(0);
|
|
}
|
|
catch (error) {
|
|
console.error(`Backup Restore ERROR => ${error.message}`);
|
|
process.exit(1);
|
|
}
|
|
});
|
|
}
|