Categories: Development

MongoDB Database Backup Bash Script

This simple Linux BASH script creates a MongoDB database dump to a file, the script can be used later for some cronjobs.

MongoDB Database Backup Bash Script

mongodb-garanet


GitHub https://github.com/garanet/MongoDB-bkp-script.git

  • Create a folder / backup.
  • Place the following script named ‘mongo_db_bkp.sh’ in the newly created folder.

#!/bin/bash
# www.garanet.net
# MySQL Full Backup Bash Script
# https://github.com/garanet/MongoDB-bkp-script.git

# Define variables
m_host="localhost"
m_user="root"
m_password="secret"
m_database="nameMongoDB"
m_dump="/backup/mongo-dump"

# Backup Database.
/usr/bin/mongodump -h ${m_host} -u ${m_user} -p ${m_password} -d ${m_database} -o ${m_dump}; 
# Compress dump.
tar cvfz /backup/${m_dump}.tar.gz ${m_dump}/; 
# Delete old.
rm -rf ${m_dump};

  • Check that the script owner is root and change the permissions to 744.
  • As root, run the script with the command:: # sh mongo_db_bkp.sh.

Share

Recent Posts

FastAPI, SQLAlchemy & Uvicorn

A FastAPI, SQLAlchemy & Uvicorn to fetch a Google Sheet A FastAPI, SQLAlchemy & Uvicorn to fetch a Google Sheet… Read More

Client TrueNas & Ldap with python Flask

A webtool in Python Flask that creates an LDAP user and his TrueNAS shares pool in ISCSI or NFS. There… Read More

Simple Terraform configuration

A Terraform configuration that provisions the following infrastructure: A VPC with: Public subnets for external resources. Private subnets for internal… Read More