Atikh's DBA blog
  • Home
  • Oracle
  • MySQL
  • MongoDB
  • PostgreSQL
  • Snowflake
  • About Me
  • Contact Us

MongoDB Database Backup and Restore

 Atikh Shaikh     MongoDB     No comments   


MongoDB stores all its data in the data directory mentioned by --dbpath during startup. Taking backup of MongoDB is as simple as taking a copy of all data files in the data directory.

Creating a copy of datafile is not recommended while MongoDB is running as it can create a corrupt or useless backup. It is always good to take a copy of datafile after safely shutting down the MongoDB server, but this will surely require downtime. In order to avoid downtime, we can use the below methods to take backup.

MONGODUMP


One of the methods of taking MongoDB backing while the database is running is to use the mongodump utility. mongodump generally works on query mechanism, it will cause slight performance degradation on the database during the backup time.
It can be run against a live instance, even one handling other requests and performing writes.
We can see the options with mongodump using mongodump –help

C:\Program Files\MongoDB\Server\3.6\bin>mongodump --help
Usage:mongodump
/help           print usage
/v, /verbose:   more detailed log output (include multiple                           times for more verbosity, e.g. -        vvvvv, or                      specify a numeric value, e.g. --verbose=N)
/h, /host:      mongodb host to connect to                                   (setname/host1,host- 2 for replica sets)

/u, /username:     username for authentication
/p, /password:     password for authentication
/d, /db:           database to use
/c, /collection:   collection to use
/uri:mongodb-uri   mongodb uri connection string
/q, /query:         query filter, as a JSON string,                               e.g., '{x:{$gt:1}}'
/o, /out:          output directory,
gzip              compress archive our collection output                               with Gzip


Below is an example for taking the backup of database "techon_db" in directory D:\mongodb\backup_dump

mongodump -d techon_db -o D:\mongodb\backup_dump

techondba-mongodump, mongodb database backu


MONGORESTORE

Along with mongodump, there comes mongorestore for importing data in MongoDB which was taken using mongodump, backup taken using mongodump can be restored in different or new database as well.

Example

mongorestore -d techon1_db  D:\mongodb\backup_dump\techon_db

techondba-mongodb-mongorestore, mongodump

In the above example, we have restored backup taken using mongodump for database techon_db into techon1_db. all steps are clearly displayed in the screenshot.

all options can be displayed using mongorestore --help

fsync and Lock


As we already discussed, the above methods allow us to perform its operations while the database is running, but here we tend to lose some ongoing activity backup as we will miss on point in time recovery backups. MongoDB's fsync command allows us to copy the data directory of the running MongoDB server while the database is running.

fsync command forces to flush all pending writes to disk. It also, optionally holds a lock preventing any further writes to the database until it is unlocked.

Example of fsync

>db.runCommand ({"fsync" : 1, "lock" : 1});
{
"info" : "now locked against writes, use db.$cmd.sys.unlock.findOne() to unlock",
"ok" : 1
}

This will allow us to take consistent backup. Once the backup complete, we need to unlock the database

>db.$cmd.sys.unlock.findOne();
{"ok" : 1, "info" : "unlock requested"}
>db.current0p ();
{"inprog" : []}

The meaning of db.current0p is confirmation of the lock has been released.
Pros for fsync are only putting a lock on reads or write on the database. The only solution for this is Slave Backup

Slave Backup

All methods discussed above provide enough flexible backups, but the most flexible backup is taking backup from a slave server. When running MongoDB with replication, above-all methods can be applied to take a backup from the slave server instead of the primary one
Slave backup of the recommended way to backup MongoDB database

  • Share This:  
  •  Facebook
  •  Twitter
  •  Instagram
  •  Pin
  •  linkedin
  •  reddit
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • CRUD (Create, Read, Update, Delete) Operations in MongoDB In previous articles, we have talked about installation, database, collection, and document, now we will learn to perform basic CRUD operations on D… Read More
  • Data Types in MongoDB MongoDB supports a wide range of data types as values in documents, we will discuss those below Basic Data Types Documents in MongoDB can be tho… Read More
  • MongoDB Storage Engines With version 3.0, MongoDB introduced the Pluggable Storage Engine API. WiredTiger is one of the pluggable storage engines bundled with MongoDB Pl… Read More
  • MongoDB Database Backup and Restore MongoDB stores all its data in the data directory mentioned by --dbpath during startup. Taking backup of MongoDB is as simple … Read More
  • Starting and Stopping MongoDB Starting the MongoDB MongoDB is started with a mongod executable file, it has many configurable options which can be viewed with mongod--help on … Read More
Newer Post Older Post Home

0 comments:

Post a Comment

Author

Atikh Shaikh
View my complete profile

Categories

  • MongoDB (18)
  • Oracle 12c (30)
  • Oracle12cR2 New Feature (3)
  • PostgreSQL (20)
  • RMAN (10)
  • Snowflake (8)
  • mysql (23)
  • oracle (74)

Blog Archive

  • ▼  2018 (38)
    • ►  November (25)
    • ▼  December (13)
      • Starting and Stopping MongoDB
      • Oracle Database Architecture Physical and Logical
      • MongoDB Database Backup and Restore
      • Startup and Shutdown Oracle Database
      • Oracle 12c New features – Multitenant Database
      • MongoDB Storage Engines
      • Create Pluggable Database (PDB) in Oracle 12c
      • Warning: PDB altered with errors- opening PDB in O...
      • Oracle 12c: Starting and Stopping PDB
      • Know your Hostname in MySQL
      • Everything you need to know about Oracle Data Pump
      • List Databases, Tables, schemas and other basic c...
      • User Managed Backups in Oracle
  • ►  2019 (33)
    • ►  January (15)
    • ►  February (6)
    • ►  March (2)
    • ►  April (5)
    • ►  May (5)
  • ►  2020 (5)
    • ►  April (1)
    • ►  May (2)
    • ►  July (2)
  • ►  2021 (8)
    • ►  June (3)
    • ►  July (3)
    • ►  August (1)
    • ►  December (1)
  • ►  2022 (33)
    • ►  May (3)
    • ►  June (10)
    • ►  July (3)
    • ►  August (4)
    • ►  September (8)
    • ►  October (3)
    • ►  November (2)
  • ►  2023 (14)
    • ►  February (1)
    • ►  April (5)
    • ►  May (2)
    • ►  June (1)
    • ►  September (1)
    • ►  October (1)
    • ►  December (3)
  • ►  2024 (5)
    • ►  January (2)
    • ►  March (3)
  • ►  2025 (6)
    • ►  March (1)
    • ►  April (3)
    • ►  May (2)

Popular Posts

  • ORA-29283: invalid file operation: unexpected "LFI" error (1509)[29437]
    I was trying to export the schema in my windows PC, it got stuck with below error    C:\Users\shaik\Videos\technodba exp>expdp userid...
  • PostgreSQL : How to get data directory location for PostgreSQL instance
    Sometimes, you start working on a PostgreSQL instance but forget about the data directory, here we will discuss different methods to know th...
  • ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
    In previous articles, we have learned about user creation and grants  in MySQL in detail, but there are a few privileges called global priv...
  • Oracle 23ai : Use of NOVALIDATE Constraints in IMPDP
    While performing impdp operations in the Oracle database, Oracle performs validation checks for every constraint on the imported table, that...
  • Oracle Dataguard Broker Configuration (DGMGRL)
    Data Guard Broker is a command-line interface that makes managing primary and standby databases easy. DBA can use a single command to switch...

Labels

oracle Oracle 12c mysql PostgreSQL MongoDB oracle 19c Oracle23c oracle19c Orale PDB-CDB oracle12c python AWS Oracle ASM Virtualbox pluggable database storage engine

Pages

  • Disclaimer
  • Privacy Policy

Follow TechnoDBA

Copyright © 2025 Atikh's DBA blog | Powered by Blogger