fix mongo connection while backup

This commit is contained in:
cawa 2023-06-06 15:53:03 +03:00
parent abee37a52c
commit 85fc6b6a86

View File

@ -11,7 +11,7 @@ const getDirectories = async source =>
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
async function performBackup(databaseName) {
async function performBackup(databaseName, uri) {
try {
const timestamp = moment().format("DD.MM.YYYY_HHmmss");
const { backupFolder } = await prompt([
@ -24,37 +24,64 @@ async function performBackup(databaseName) {
]);
const folderName = "dumps/" + backupFolder;
const backupCommand = `mongodump --db ${databaseName} --out ${folderName}`;
console.log(`Creating backup of database ${databaseName}...`);
await exec(backupCommand);
let backupCommand = spawn("mongodump", [
"--authenticationDatabase=admin",
"--uri",
uri,
"--db",
databaseName,
"--out",
folderName,
]);
console.log(`Backup created successfully in folder ${folderName}.`);
backupCommand.stdout.on("data", function (data) {
console.log(data.toString());
});
backupCommand.stderr.on("data", function (data) {
console.log(data.toString());
});
const waitForEnd = new Promise((resolve, reject) => {
backupCommand.on("exit", function (code) {
console.log("child process exited with code " + code.toString());
console.log(`Search you backup in folder ${folderName}.`);
resolve();
});
});
await waitForEnd;
return;
} catch (error) {
console.error("An error occurred during the backup process:", error);
return;
}
}
function performRestore(restoreFolder, uri) {
async function performRestore(restoreFolder, uri) {
try {
console.log(`Restoring database from folder ${restoreFolder}...`);
let ls = spawn("mongorestore", ["--uri", uri, "--drop", restoreFolder]);
let restoreCommand = spawn("mongorestore", ["--uri", uri, "--drop", restoreFolder]);
ls.stdout.on("data", function (data) {
restoreCommand.stdout.on("data", function (data) {
console.log(data.toString());
});
ls.stderr.on("data", function (data) {
restoreCommand.stderr.on("data", function (data) {
console.log(data.toString());
});
ls.on("exit", function (code) {
console.log("child process exited with code " + code.toString());
const waitForEnd = new Promise((resolve, reject) => {
restoreCommand.on("exit", function (code) {
console.log("child process exited with code " + code.toString());
resolve();
});
});
console.log("Database restored successfully.");
await waitForEnd;
return;
} catch (error) {
console.error("An error occurred during the restore process:", error);
return;
}
}
@ -161,74 +188,77 @@ async function main() {
await client.connect();
console.log("Connected to MongoDB successfully.");
const { action } = await prompt([
{
type: "list",
name: "action",
message: "Choose an action:",
choices: ["Clear ...", "Backup", "Restore"],
},
]);
if (action === "Backup") {
const { databaseName } = await prompt([
while (true) {
const { action } = await prompt([
{
type: "list",
name: "databaseName",
message: "Choose a database to backup:",
choices: await client
.db()
.admin()
.listDatabases()
.then(({ databases }) => databases.map(({ name }) => name)),
name: "action",
message: "Choose an action:",
choices: ["Clear ...", "Backup", "Restore"],
},
]);
await performBackup(databaseName);
} else if (action === "Restore") {
try {
const dirs = await getDirectories("./dumps");
const { restoreFolder } = await prompt([
if (action === "Backup") {
const { databaseName } = await prompt([
{
type: "list",
name: "restoreFolder",
message: "Choose the folder to restore from:",
choices: dirs,
name: "databaseName",
message: "Choose a database to backup:",
choices: await client
.db()
.admin()
.listDatabases()
.then(({ databases }) => databases.map(({ name }) => name)),
},
]);
await performRestore("./dumps/" + restoreFolder, uri);
} catch (err) {
if (err.errno === -2) {
console.log("No 'dumps' dir found");
} else {
throw new Error(err);
await performBackup(databaseName, uri);
} else if (action === "Restore") {
try {
const dirs = await getDirectories("./dumps");
const { restoreFolder } = await prompt([
{
type: "list",
name: "restoreFolder",
message: "Choose the folder to restore from:",
choices: dirs,
},
]);
await performRestore("./dumps/" + restoreFolder, uri);
} catch (err) {
if (err.errno === -2) {
console.log("No 'dumps' dir found");
} else {
throw new Error(err);
}
console.log("Disconnected from MongoDB.");
await client.close();
return;
}
console.log("Disconnected from MongoDB.");
} else if (action === "Clear ...") {
const { subject } = await prompt([
{
type: "list",
name: "subject",
message: "Choose what you are going to clean up:",
choices: ["collection", "field"],
},
]);
if (subject === "collection") {
const collection = await chooseCollection(client);
await clearColleciton(collection);
} else if (subject === "field") {
const collection = await chooseCollection(client);
const field = await chooseField(client);
await clearField(collection, field);
}
await client.close();
return;
console.log("Disconnected from MongoDB.");
}
} else if (action === "Clear ...") {
const { subject } = await prompt([
{
type: "list",
name: "subject",
message: "Choose what you are going to clean up:",
choices: ["collection", "field"],
},
]);
if (subject === "collection") {
const collection = await chooseCollection(client);
await clearColleciton(collection);
} else if (subject === "field") {
const collection = await chooseCollection(client);
const field = await chooseField(client);
await clearField(collection, field);
}
await client.close();
console.log("Disconnected from MongoDB.");
console.log("\nPress Ctrl+C to exit\n");
}
} catch (error) {
console.error("An error occurred:", error);