MongoDB Database Backup using Shell Script

In this article, we will learn how to automate MongoDB database backups using a shell script. Automating backups is crucial for maintaining data integrity and ensuring that you have up-to-date copies of your data in case of unexpected failures. We will walk through the process of setting up MongoDB, creating a backup directory, and writing a shell script to handle the backup process.

Prerequisites

  • AWS Account with Ubuntu 24.04 LTS EC2 Instance.
  • Basic knowledge of MongoDB and Shell scripting.

Step #1:Install MongoDB on Ubuntu

Update the system before start installation process.

sudo apt update 
MongoDB Database Backup using Shell Script 1

To import the MongoDB public GPG key, run the following command.

curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
   sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
   --dearmor
MongoDB Database Backup using Shell Script 2

Create the /etc/apt/sources.list.d/mongodb-org-7.0.list file for Ubuntu 24.04.

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
MongoDB Database Backup using Shell Script 3

Reload the local package database using following command.

sudo apt-get update
MongoDB Database Backup using Shell Script 4

Install the latest stable version of MongoDB using following command.

sudo apt-get install -y mongodb-org
MongoDB Database Backup using Shell Script 5

After this reload the daemon service and also enable the mongod and after that start the mongod.

sudo systemctl daemon-reload
sudo systemctl enable mongod
sudo systemctl start mongod
MongoDB Database Backup using Shell Script 6

Check the status to if MongoDB is running successfully.

sudo systemctl status mongod
MongoDB Database Backup using Shell Script 7

Step #2:Set Up MongoDB Authentication

Next set up MongoDB authentication and create a user. Begin by connecting with mongosh.

mongosh

After connecting to your MongoDB instance with mongosh, switch to the admin database using the command.

use admin

Create a new user with a username, password, and appropriate roles.

db.createUser({
user: "fosstechnix", pwd: "devops",
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, { role: "dbAdminAnyDatabase", db: "admin" }, { role: "readWriteAnyDatabase", db: "admin" } ]
})

Exit the shell after creating the user.

exit
MongoDB Database Backup using Shell Script 8

Step #3:Create Backup Directory

Next, create a directory where the MongoDB backups will be stored. This directory will be used by the shell script to save the backup files.

mkdir -p mongo_backup
MongoDB Database Backup using Shell Script 9

Step #4:Run the Backup Script

Navigate to the directory.

cd mongo_backup
MongoDB Database Backup using Shell Script 10

Create a shell script that will perform the backup operation. The script will use the mongodump command to dump the MongoDB data into the specified backup directory.

nano backup.sh
MongoDB Database Backup using Shell Script 11

add the following content into it.

#!/bin/bash

# Get current date and time for filename
now="$(date +'%d-%m-%Y_%H-%M')"
filename1="Mongo_bk_$now"
backupfolder="/home/ubuntu/mongo_backup/"
fullpathbackupfile1="$backupfolder/$filename1"
logfile="$backupfolder/backup_log_$(date +'%Y_%m').txt"

# Log the start time of mongodump
echo "mongodump started at $(date +'%d-%m-%Y %H:%M')" >> "$logfile"

# Perform the mongodump
mongodump --username fosstechnix --password devops --gzip --out "$fullpathbackupfile1" --authenticationDatabase admin

# Log the finish time of mongodump
echo "mongodump finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"

# Change file permissions
chown ubuntu "$fullpathbackupfile1"
chown ubuntu "$logfile"
echo "file permission changed" >> "$logfile"

# Find and delete backup files older than 1 day
find "$backupfolder" -name "Mongo_bk*" -mtime +1 -exec rm -rf {} \;
echo "old files deleted" >> "$logfile"

# Log the end of the operation
echo "operation finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
echo "*******" >> "$logfile"

# Exit the script
exit 0
MongoDB Database Backup using Shell Script 12

save the file and exit.

Explanation of the script:

  • now="$(date +'%d-%m-%Y_%H-%M')": Gets the current date and time formatted as dd-mm-yyyy_hh-mm.
  • filename1="Mongo_bk_$now": Sets the backup file name using the current date and time.
  • backupfolder="/home/ubuntu/mongo_backup/": Defines the directory where backup files will be stored.
  • fullpathbackupfile1="$backupfolder/$filename1": Combines the backup folder path and the backup file name.
  • logfile="$backupfolder/backup_log_$(date +'%Y_%m').txt": Sets the log file path, which includes the current year and month.
  • Logs the start time of the mongodump operation to the log file.
  • mongodump: The command used to back up the MongoDB database.
  • --username fosstechnix --password devops: Authenticates the operation with the specified username and password.
  • --gzip: Compresses the backup output.
  • --out "$fullpathbackupfile1": Specifies the output directory for the backup.
  • --authenticationDatabase admin: Specifies the authentication database.
  • Logs the finish time of the mongodump operation to the log file.
  • chown ubuntu "$fullpathbackupfile1": Changes the owner of the backup file to ubuntu.
  • chown ubuntu "$logfile": Changes the owner of the log file to ubuntu.
  • Logs the permission change operation to the log file.
  • find "$backupfolder" -name "Mongo_bk*" -mtime +1 -exec rm -rf {} \;: Finds and deletes files in the backup folder that start with “Mongo_bk” and are older than 1 day.
  • Logs the deletion of old files to the log file.
  • Logs the end of the backup operation and adds a separator line for clarity.
  • Exits the script successfully.

Change the file permissions to make it executable using the chmod command.

chmod +x backup.sh
MongoDB Database Backup using Shell Script 13

Run the script by executing the following command.

./backup.sh
MongoDB Database Backup using Shell Script 14

You can verify the backup by listing out the contents from the mongo_backup directory.

ls
MongoDB Database Backup using Shell Script 15

open the created txt file.

nano backup_log_2024_05.txt
MongoDB Database Backup using Shell Script 16

This indicates that mongodump is correctly creating backups. The backup appears to have been successful and completed at 6:53:40 on May 31, 2024.This indicates that mongodump is correctly creating backups.

MongoDB Database Backup using Shell Script 17

Conclusion:

In conclusion, automating MongoDB backups using a shell script is a simple and effective way to ensure your data is regularly backed up and secure. By following the steps outlined in this article, you can set up an automated backup system that runs at regular intervals, logs important information, and maintains your data’s integrity. Additionally, setting up MongoDB authentication for monitoring purposes helps you keep an eye on your cluster’s health and performance.

Related Articles:

How to Install LAMP Stack on Ubuntu 24.04 LTS

Reference:

Bash reference manual

Prasad Hole

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap