MongoDB Database Backup

Overview

The provided script is a Bash shell script that automates the process of creating a backup of a MongoDB database using the mongodump tool. It defines the necessary variables, performs the backup, and outputs a message indicating the completion of the backup process.




#!/bin/bash By: Rohid Safi

# Define the connection string
MONGO_URI='mongodb://2sqrUsr:3BMGZLYvEVt2MMm2vc9L@127.0.0.1:27017/2sqr_qr_api?authSource=admin'

# Extract the database name from the connection string
DB_NAME=$(echo $MONGO_URI | sed -n 's/.*\/\([^?]*\).*/\1/p')

# Define the output directory for the backup
BACKUP_DIR='/NewDb'

# Create the backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"

# Perform the mongodump
mongodump --uri="$MONGO_URI" --out="$BACKUP_DIR"

# Print a message indicating the backup is complete
echo "Backup of database '$DB_NAME' completed at: $(date +'%Y-%m-%d %H:%M:%S')"
 

Script Description

  1. Define Connection StringThe script starts by defining the MongoDB connection string MONGO_URI. It contains the necessary information to connect to the MongoDB server, including the username, password, host (IP address or hostname), port number, authentication database, and target database.
  2. Extract Database NameThe script then uses the sed command to extract the database name (DB_NAME) from the connection string. It retrieves the portion of the connection string after the last slash (/), which represents the target database name.
  3. Define Backup DirectoryThe variable BACKUP_DIR is used to specify the directory where the MongoDB database backup will be stored. In this example, the backup will be saved in the /NewDb directory. If the directory does not exist, the script will create it using the mkdir command.
  4. Perform the BackupThe script executes the mongodump command with the specified connection string (MONGO_URI) and the output directory (BACKUP_DIR). The mongodump tool creates a binary export of the entire MongoDB database, including all collections and indexes, and stores it in the specified backup directory.
  5. Print Backup Completion MessageAfter the mongodump operation is complete, the script outputs a message indicating the successful completion of the backup process. The message includes the name of the backed-up database (DB_NAME) and the timestamp of when the backup was created, obtained using the date command.

Usage

To run the script, you can save the provided content into a file ( backup_script.sh) and make it executable with the following command:

bashCopy codechmod +x backup_script.sh

Then, execute the script:

bashCopy code./backup_script.sh

Note

  • Ensure that you have the necessary permissions to access the MongoDB server and perform backup operations.
  • Always validate the connection string, backup directory, and other variables in the script to ensure they match your specific MongoDB setup.
  • Regularly back up your MongoDB database to prevent data loss in case of any unexpected events.

source : GhatGPT and internet
personal link