Linux (Fedora 41) Bash Script to do a backup for MongoDB
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.
In essence, a Bash script is a set of instructions given to the computer to execute different meaningful tasks. It helps automate different tasks along with the luxury to accomplish results faster than the normal procedure. A Bash script is a combination of multiple commands. The Bash file ends with the .sh
extension. Before running the Bash script, we need to update the file permissions and provide the executable permission to the .sh
file.
Bash scripts are given an extension of .sh
. However, Bash scripts can run perfectly fine without the .sh
extension. Scripts are also identified with a shebang. Shebang is a combination of #
and !
followed by the Bash shell path. This is the first line of the script. Shebang tells the shell to execute it via the Bash shell. Shebang is simply an absolute path to the Bash interpreter.
A Bash script is a text file containing a series of commands. Any command that can be executed in the terminal can be put into a Bash script. Any series of commands to be executed in the terminal can be written in a text file, in that order, as a Bash script. Bash scripts are given an extension of .sh
.
Bash is a command-line interpreter or Unix Shell, and it is widely used in GNU/Linux Operating System. It is written by Brian Jhan Fox. It is used as a default login shell for most Linux distributions. Scripting is used to automate the execution of the tasks so that humans do not need to perform them individually. Bash scripting is a great way to automate different types of tasks in a system.
Bash (AKA Bourne Again Shell) is a type of interpreter that processes shell commands. A shell interpreter takes commands in plain text format and calls Operating System services to do something. For example, ls
command lists the files and folders in a directory. Bash is the improved version of Sh (Bourne Shell). A shell scripting is writing a program for the shell to execute and a shell script is a file or program that the shell will execute.
Bash is a Unix shell, which is a command line interface (CLI) for interacting with an operating system (OS). Any command that you can run from the command line can be used in a Bash script. Scripts are used to run a series of commands. Bash is available by default on Linux and macOS operating systems.
In summary, a Bash script is a powerful tool that allows users to automate tasks and streamline operations within the Linux environment. It is a text file containing a set of commands that can be executed by the Bash shell, making it an essential skill for system administrators and developers.
What is a backup?
Backup is the process of creating a copy of data, files, or systems to protect them from loss or damage, allowing for data restoration if the primary source becomes compromised or unavailable. This process is essential for preventing data loss caused by hardware failure, software corruption, human error, or natural disasters. Backups are a critical component of data management and security, as they allow for the restoration of data to its original state if the primary source is affected.
There are various types of backups, including full backups, which create a complete copy of all data, and incremental backups, which only back up the data that has changed since the last backup. Additionally, the 3-2-1 backup strategy is recommended, which involves maintaining at least three copies of data, stored on two different types of storage media, with at least one backup stored offsite or in the cloud.
Backup strategies also consider Recovery Point Objectives (RPO) and Recovery Time Objectives (RTO), which determine the amount of data an organization is willing to lose and the time it takes to restore data or systems from backup and resume normal operations.
In summary, backup is a crucial practice for ensuring data integrity and minimizing the risk of data loss, providing a safety net against various potential threats
What is the command mongoexport?
Mongoexport is a command-line tool provided by MongoDB that allows users to export data from a MongoDB database into JSON or CSV files. It is part of the MongoDB database tools and is used to produce a JSON or CSV export of data stored in a MongoDB instance. The tool is not intended for backing up deployments, and users are advised to use other methods such as Back Up and Restore a Self-Managed Deployment with MongoDB Tools for that purpose. Mongoexport can be used to export data from a local MongoDB instance running on port 27017 without specifying the host or port. It also supports exporting data from replica sets and sharded clusters. Additionally, it provides options to customize the output format, filter the exported data, and control the behavior of the export process.
The script:
#!/bin/bash
#create the date
datum=$(date --utc --date "$1" +%x)
#hello to the user with the current date
echo "Hallo" ${USER^} "heute ist der" $datum
#checking the disc space on /home/sven
echo "Frei sind auf dem Homeverzeichnis noch"
df -h /home/sven | awk 'NR==2 {print $4}'
#creating the backup of the database: artikel in the collection Nachrichten with the current date
echo "Backup MongoDB Collection Nachrichten"
mongoexport --db Artikel --collection Nachrichten --type=json --out=Nachrichten_$datum.json
sleep 2
#creating the backup of the database: Lander in the collection Espana with the current date
echo "Backup MongoDB Collection Espana"
mongoexport --db Laender --collection Espana --type=json --out=Espana_$datum.json
#notification that the backups were made
notify-send "Backups wurden gemacht"
The backup in action (Screenshot)
