I was Looking for a Quick solution to get mongoDB data Backup and notification in telegam.

Got many awesome tutorial, but my needs is bit different. So i solved it and sharing with you , so that you can use if its match with you.

First, let’s start by creating a new Bash script file. You can use any text editor you prefer, but for this tutorial, we’ll use the nano editor. Open your terminal and run the following command

This will create a new file called backup_script.sh and open it in the nano editor. Now, copy and paste the following code into the file:

nano backup_script.sh

Now copy this code and paste into backup_script.sh

<em># Set the database name and backup directory</em>
BACKUP_DIR="/path/to/backup/directory/$(date +"%Y-%m-%d*%H-%M-%S")"

<em># Set the connection string for the MongoDB URL</em>


<em># Use mongodump to create a backup of the database</em>

mongodump --uri="$MONGO_URL" --db="$DB_NAME" --out="$BACKUP_DIR"

<em># Move the backup to the zip directory and compress it</em>

cd $(dirname "$BACKUP_DIR")
cd -

<em># Send a message to Telegram to confirm the backup has been created</em>

BACKUP_SIZE=$(du -h "$ZIP_DIR/$BACKUP_FILENAME.zip" | awk '{print $1}')

if [ -f "$ZIP_DIR/$BACKUP_FILENAME.zip" ]; then
sleep 5s
if curl -s -X POST "https://api.telegram.org/bot$TOKEN/sendMessage" \
 -d chat_id="$CHAT_ID" \
 -d text="The backup for database $DB_NAME has been created. Backup file name: $BACKUP_FILENAME.zip. Size: $BACKUP_SIZE"; then
echo "Telegram message sent successfully."
echo "Failed to send Telegram message."
echo "Backup file not found. Skipping Telegram message."

<em># Remove backups older than 7 days</em>

find /path/to/backup/directory -type d -mtime +7 -name "your*backup_prefix*\*" -exec rm -r {} \;

Before you run the script, you need to update some variables.

Replace your_database_name with the name of your MongoDB database, /path/to/backup/directory with the path to the directory where you want to store your backups, /path/to/zip/directory with the path to the directory where you want to store your compressed backups, your_username and your_password with your MongoDB username and password, your_mongodb_host with the host of your MongoDB instance (e.g. cluster0.mongodb.net), your_telegram_bot_token with the token of your Telegram bot, and your_telegram_chat_id with the chat ID of the Telegram chat where you want to receive notifications about your backups.

Save the changes by pressing Ctrl+O, then Enter, and exit nano by pressing Ctrl+X.

Now, you can run the script by executing the following command in your terminal:

chmod +x backup_script.sh

😊 Thanks for reading.