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
#!/bin/bash
clear
echo "------------------------------------------"
echo "Backup of MongoDB"
echo "------------------------------------------"
#create the date for the file name
datum=$(date +%x | tr -d "https://dev.to/")
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"
