Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Databases»MongoDB»How to Backup and Restore MongoDB Database

    How to Backup and Restore MongoDB Database

    RahulBy RahulOctober 13, 20142 Mins ReadUpdated:September 16, 2020

    mongodump is a utility provided by MongoDB for creating backups of databases. This is an very useful utility and can be consider for taking backups for live servers databases very effectively. For database restore use mongorestore command. You can read our online mongodb tutorials to create database, drop database etc.

    1. Backup MongoDB Database – mongodump

    There are various options to take backups of mongodb database. Use mongodump command to take all databases backup or a single database backup of backup of single collection.

    Backup Single Database

    Use this command to take backup of a single database (named mydb) only. Backup will be created in /backup/mongo/ directory.

    mongodump --db mydb --out /backup/mongo/ 
    

    –db – database name to backup
    –out – database backup location. This will create folder with database name.

    You can specify host, port, username and password for remote databases connections backups like below.

    mongodump --host 10.0.1.7 --port 27017 --username admin --password somepassword --db mydb --out /backup/mongo/ 
    

    Backup All Databases

    To backup all databases you just need to run following command. Here /data/db/ is location of your mongodb data directory and /backup/mongo is location of backup directory.

    mongodump --out /backup/mongo/ 
    

    You can specify host, port for remote databases.

    Backup Single Collection

    This command will take backup of single collection from a database. Backup files will be created in /backup/mongo/mydb/ directory.

    mongodump --collection mycollection --db mydb --out /backup/mongo/ 
    

    2. Restore MongoDB Database – mongorestore

    mongorestore is command line tool for restoring mongodb database backup. Here /data/db/ is location of your mongodb data directory and /backup/mongo is location of backup directory.

    mongorestore --db mydb --drop /backup/mongo/mydb 
    

    –drop – Will remove database if already exist.

    You can simply move backup files to remote server and run the same command there to restore backups.

    3. MongoDB Backup Shell Script

    You can easily schedule the below script in scheduler to backup databases regularly. Create a file as following

    vi /backup/mongo-backup.sh 
    

    Add below content to file. Update database hostname, database name , username and password accordingly.

    Shell
    1
    2
    3
    4
    5
    6
    7
    8
    #!/bin/sh
     
    TODAY=`date +%d%b%Y`
    BACKUP_DIR=/backup/mongo
     
    mkdir -p ${BACKUP_DIR}/${TODAY}
     
    mongodump -h <DATABASE_HOST> -d <DATABASE_NAME> -u <USERNAME> -p <PASSWRD> --out ${BACKUP_DIR}/${TODAY}/

    Now configure this in crontab to run daily.

    Shell
    1
    0 2 * * * /backup/mongo-backup.sh

    backup database mongo mongodb mongodb backup mongodump restore
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Configure CGI Scripts in Apache
    Next Article How to Add Custom Settings in WHM/cPanel Apache VirtualHost

    Related Posts

    How To Install MySQL Server on Ubuntu 22.04

    Updated:April 6, 20224 Mins Read

    How to Backup Website to Amazon S3 using Shell Script

    Updated:March 24, 20222 Mins Read

    How To Setup Apache, PHP & MongoDB in Ubuntu & Debian

    Updated:October 8, 20213 Mins Read

    How To Install MariaDB on Debian 11

    4 Mins Read

    How to Install Redis on Debian 11 Linux

    Updated:September 16, 20213 Mins Read

    How To Install and Secure MongoDB on Ubuntu 20.04

    Updated:September 7, 20214 Mins Read

    1 Comment

    1. Manik on May 14, 2019 5:44 am

      As I read through for the mongodump, it mainly has 2 issues:
      1. It does not take the indices backup
      2. It takes a lot memory depending on the size of the DB.

      Is there some other way of taking backups of running DBs.

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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