Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Databases»MongoDB»A Shell Script to Backup MongoDB Database

    A Shell Script to Backup MongoDB Database

    By RahulAugust 22, 20222 Mins Read

    Did you know that MongoDB databases have a built-in backup mechanism that is accessible via shell or the mongod process? The mongod process automatically takes a snapshot every time a database transitions to another state. These different states are: starting, stopping, upgrading, and recovering after a crash. However, these snapshots won’t be enough in case of catastrophic failures such as disk corruption or natural disaster. To protect your valuable data from such threats, it is advisable to implement an automated backup strategy for your MongoDB databases.

    Advertisement

    In this article, we will discuss how to create automated backups for your MongoDB databases using a simple Shell script.

    Shell Script for MongoDB Backup

    The shell script for MongoDB database backup is available on Github. You can use the below link to get access of the shell script.

    https://github.com/tecrahul/shell-scripts/blob/master/backup-mongo.sh

    Alternatively, You can copy the below script and save it on your Linux system.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    #!/bin/bash
    ######################################################################
    ##
    ##   MongoDB Database Backup Script
    ##   Written By: Rahul Kumar
    ##   URL: https://tecadmin.net/shell-script-backup-mongodb-database/
    ##   Update on: June 20, 2020
    ##
    ######################################################################
    export PATH=/bin:/usr/bin:/usr/local/bin
    TODAY=`date +"%d%b%Y"`
    ######################################################################
    ######################################################################
    DB_BACKUP_PATH='/backup/mongo'
    MONGO_HOST='localhost'
    MONGO_PORT='27017'
     
    # If MongoDB is protected with a username password.
    # Set AUTH_ENABLED to 1
    # and add MONGO_USER and MONGO_PASSWD values correctly
     
    AUTH_ENABLED=0
    MONGO_USER=''
    MONGO_PASSWD=''
     
     
    # Set DATABASE_NAMES to "ALL" to backup all databases.
    # or specify databases names separated with space to backup
    # specific databases only.
     
    DATABASE_NAMES='ALL'
    #DATABASE_NAMES='mydb db2 newdb'
     
    ## Number of days to keep a local backup copy
    BACKUP_RETAIN_DAYS=30  
    ######################################################################
    ######################################################################
    mkdir -p ${DB_BACKUP_PATH}/${TODAY}
     
    AUTH_PARAM=""
     
    if [ ${AUTH_ENABLED} -eq 1 ]; then
    AUTH_PARAM=" --username ${MONGO_USER} --password ${MONGO_PASSWD} "
    fi
     
    if [ ${DATABASE_NAMES} = "ALL" ]; then
    echo "You have choose to backup all databases"
    mongodump --host ${MONGO_HOST} --port ${MONGO_PORT} ${AUTH_PARAM} --out ${DB_BACKUP_PATH}/${TODAY}/
    else
    echo "Running backup for selected databases"
    for DB_NAME in ${DATABASE_NAMES}
    do
    mongodump --host ${MONGO_HOST} --port ${MONGO_PORT} --db ${DB_NAME} ${AUTH_PARAM} --out ${DB_BACKUP_PATH}/${TODAY}/
    done
    fi
     
    ######## Remove backups older than {BACKUP_RETAIN_DAYS} days  ########
    DBDELDATE=`date +"%d%b%Y" --date="${BACKUP_RETAIN_DAYS} days ago"`
    if [ ! -z ${DB_BACKUP_PATH} ]; then
          cd ${DB_BACKUP_PATH}
          if [ ! -z ${DBDELDATE} ] && [ -d ${DBDELDATE} ]; then
                rm -rf ${DBDELDATE}
          fi
    fi
    ######################### End of script ##############################

    Run Script Manually

    Save the above script in a file with .sh extension. I want to save all the backups under /the backup directory. So placed the shell script in the same directory. Then set the execute permission on the script.

    chmod +x /backup/backup-mongo.sh 
    

    Run the shell script as below:

    bash /backup/backup-mongo.sh 
    

    Schedule MongoDB Backup Script

    You can easily schedule this script under crontab to backup databases regularly. To edit the crontab, run crontab -e command and append the below code:

    1
    2
    ## Backup database daily at 02:00 AM
    0 2 * * * /backup/mongo-backup.sh

    Wrap Up

    In this tutorial, we have discussed a shell script that helps to backup MongoDB databases manually. Also, you can schedule scripts to backup databases on regular basis.

    backup mongodb shell script
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Backing Up Your Linux System with Rsync: A Step-by-Step Guide

    How to Connect MongoDB Database in Python

    Bash – Sending Email via Amazon SES

    View 2 Comments

    2 Comments

    1. Jose on August 20, 2022 10:22 am

      Deletion is not working. Help appreciated. Thanks

      Reply
    2. pires on April 16, 2021 5:21 am

      very nice tuto

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • 20 Basic Linux Commands for the Beginners (Recommended)
    • tail Command in Linux with Examples
    • What is a Orphan Process in Unix/Linux
    • How To Display Warning Message to Unauthorized SSH Access
    • How to Set a Custom SSH Login Banner and MOTD
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.