Fedora 42 Backup script for MongoDB database
What is the advantage of this script?
- You define the path to the database and the collection name in advance.
- Every file has an automatically generated name (the date)
- You see when you did the backup directly (the date in the file name)
- You get a notification for your desktop
- You can do it regularly (cron job) – every day if you want
- The output is a JSON file
What is MongoDB?
MongoDB is a document-oriented NoSQL database that stores data in flexible, JSON-like documents, allowing for easy storage and retrieval of structured, semi-structured, and unstructured data. Unlike traditional relational databases that use tables and rows, MongoDB uses collections and documents, making it more adaptable to changing data structures. It was designed to handle large volumes of data and is known for its scalability, flexibility, and ease of use. MongoDB is open-source and has a wide range of deployment options, including cloud-based services like MongoDB Atlas and on-premise solutions. Its architecture supports horizontal scaling, enabling it to manage high traffic and large datasets efficiently. Additionally, MongoDB provides powerful querying capabilities, indexing, and real-time aggregation, making it suitable for a variety of applications, from simple CRUD operations to complex data processing tasks.
What is a Bash script?
A Bash script is a plain text file containing a series of commands that can be executed by the Bash shell. These commands are typically those that would be entered manually at the command line, such as ls or cp, but they can also include more complex instructions. Bash scripts allow users to automate tasks, making them a powerful tool for system administration and development.
The script
#!/bin/bash
clear
echo "------------------------------------------"
echo "Backup of MongoDB"
echo "------------------------------------------"
#create the date for the file name
datum=$(date +%x | tr -d '/')
BACKUP_PATH="$HOME/Linux_PC/Datenbankmanagementsysteme/MongoDB/backups"
DB_NAME="Artikel"
COLLECTION_NAME="Nachrichten"
OUTPUT_FILENAME="${COLLECTION_NAME}_${datum}.json"
#hello to the user with the current date -
echo "Hello" ${USER^} today is $datum
#checking the disc space on /home/sven
freespace=$(df -h /home/sven | awk 'NR==2 {print $4}')
echo "Available space in home directory: $freespace"
#creating the backup of the database: artikel in the collection Nachrichten with the current date
echo "Backup MongoDB Collection ${COLLECTION_NAME}"
mongoexport --db "$DB_NAME" --collection "$COLLECTION_NAME" --type=json --out="$BACKUP_PATH/$OUTPUT_FILENAME"
#notification that the backups were made
notify-send "MongoDB Backup completed"