UNPKG

@syngrisi/syngrisi

Version:
87 lines (86 loc) 4.55 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable no-console */ const prompts_1 = require("@inquirer/prompts"); const fs = __importStar(require("node:fs")); const path = __importStar(require("node:path")); const child_process_1 = require("child_process"); const config_1 = require("../../config"); const run = async () => { const backupFolder = config_1.config.backupsFolder; if (!fs.existsSync(backupFolder)) { fs.mkdirSync(backupFolder, { recursive: true }); } console.log('Be sure that Application is down and \'mongodump\', \'mongorestore\' and \'rsync\' tools are present in your system.'); const backupsFolders = fs.readdirSync(backupFolder, { withFileTypes: true }) .filter((x) => x.isDirectory()) .map((x) => ({ name: x.name, value: x.name })); const answers = { backupSubFolder: await (0, prompts_1.select)({ message: 'Enter the Backup Folder name Filename', choices: backupsFolders }), destConnectionString: await (0, prompts_1.input)({ message: 'Enter the Destination Database Connection String URI', default: config_1.config.connectionString }), destImagesSubFolder: await (0, prompts_1.input)({ message: 'Enter the Images Folder Path', default: config_1.config.defaultImagesPath }), confirm: await (0, prompts_1.confirm)({ message: '⚠️ Caution! All current Application Data will be removed, before the Restoring! Continue?', default: false }), }; if (!answers.confirm) { return 'Skipped'; } const fullBackupPath = path.join(backupFolder, answers.backupSubFolder); console.log({ fullBackupPath }); const fullSourceDatabasePath = path.join(fullBackupPath, 'database'); if (!fs.existsSync(fullSourceDatabasePath)) { console.log('The Source Database Folder is not exists, please select tha another folder'); return; } const fullSourceImagesPath = path.join(fullBackupPath, 'images'); if (!fs.existsSync(fullSourceImagesPath)) { console.log('The Source Images Folder is not exists, please select tha another folder'); return; } if (!fs.existsSync(answers.destImagesSubFolder)) { console.log('The Destination Images Folder is not exists, please select tha another folder'); return; } console.log('Remove the Destination Database'); const removeDbResult = (0, child_process_1.execSync)(`mongosh '${answers.destConnectionString}' --eval "db.dropDatabase();"`) .toString(); console.log(removeDbResult); console.log('Restore the Database'); const restoreDbResult = (0, child_process_1.execSync)(`mongorestore --uri ${answers.destConnectionString} --gzip ${fullSourceDatabasePath}/*`) .toString(); console.log(restoreDbResult); console.log('Clean the Destination Images Folder'); // eslint-disable-next-line max-len const removeImagesResult = (0, child_process_1.execSync)(`ls ${answers.destImagesSubFolder};rm -rfv ${answers.destImagesSubFolder} && mkdir ${answers.destImagesSubFolder}`) .toString(); console.log(removeImagesResult); console.log('Restore the Images'); console.log({ fullSourceImagesPath }); const restoreImagesResult = (0, child_process_1.execSync)(`rsync -vah --progress ${fullSourceImagesPath}/ ${answers.destImagesSubFolder}`) .toString('utf8'); console.log(restoreImagesResult); return '✅ Success'; }; run().then((result) => console.log(`operation complete: ${result}`));