UNPKG

@syngrisi/syngrisi

Version:
69 lines (68 loc) 3.47 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 }); 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('Please be sure that \'mongodump\', \'mongorestore\' and \'rsync\' tools are present in your system.'); const currDate = new Date().toLocaleString('en-US', { year: 'numeric', month: '2-digit', day: 'numeric' }) .replace(/[/]/gm, '_'); const backupSubFolder = `${currDate}_${Date.now()}`; const answers = { folder: await (0, prompts_1.input)({ message: 'Enter the Backup Folder name Filename', default: backupSubFolder }), connectionString: await (0, prompts_1.input)({ message: 'Enter the Database Connection String URI', default: config_1.config.connectionString }), imagesPath: await (0, prompts_1.input)({ message: 'Enter the Images Folder Path', default: config_1.config.defaultImagesPath }), confirm: await (0, prompts_1.confirm)({ message: 'Continue?' }), }; if (!answers.confirm) { return "Skipped"; } const fullBackupPath = path.join(backupFolder, answers.folder); if (fs.existsSync(fullBackupPath)) { console.log('The folder is already exists, please enter another folder '); return; } fs.mkdirSync(fullBackupPath, { recursive: true }); const destDatabasePath = path.join(fullBackupPath, 'database'); fs.mkdirSync(destDatabasePath, { recursive: true }); const destImagesPath = path.join(fullBackupPath, 'images'); fs.mkdirSync(fullBackupPath, { recursive: true }); console.log('Backup the Database'); const dbDumpResult = (0, child_process_1.execSync)(`mongodump --uri=${answers.connectionString} --gzip --out ${destDatabasePath}`).toString(); console.log(dbDumpResult); console.log('Backup Images'); const imagesBackupResult = (0, child_process_1.execSync)(`rsync -vah --progress ${answers.imagesPath} ${destImagesPath}`) .toString('utf8'); console.log(imagesBackupResult); return '✅ Success'; }; run().then((result) => console.log(`operation complete: ${result}`));