How to restore a single MongoDB database from a Snapshot Backup

06 November,2020 by Jack Vamvas

Get the MongoDB snapshot archival download link

You will need to obtain this link from the MongoDB Operations Manager. (There is an alternative option to scp the .tar.gz file ).From the Continuous Backup section section obtain the download link 

Log onto the server and download the file using using curl. 

----curl is a useful tool to download the tar file

curl  download_url --output myfile.tar.gz

----unpack the tar file

tar -xvf myfile.tar.gz

----the temp-database-path should be accessible by mongod

----e.g of creating the the temp-database-path 

mkdir temp-database-path

chown mongod:mongod temp-database-path

mv <backupRestoreNameOutputFile> <temp-db-path>

 

Start a new, temporary MongoDB process on a new and not used port using the unpacked backup snapshot as the dbpath.

----Note:use a different port than the current live MongoDB port

mongod --port 27018 --dbpath <temporary-db-path> --logpath <temporary-db-path>/mongodb.log --fork

mongod --port 27018 --dbpath /temp-database-path/rstest-1603796293-5fa3f35te78je --logpath /temp-database-path/rstest-1603796293-5fa3f35te78je/mongodb.log --fork

 

Use the mongodump command to export a single database or collection from the temp running mongod process

Specify the single database name using --db and, if needed, a --collection for a single collection.

The --out option specifies where the mongodump extracts the target database. Choose an empty directory the user executing mongodump can access.

mongodump --port 27018 --db <single-database> --out <new-database-path>

mongodump --port 27018 --db MyDB --out /temp-database-path/

 

Use the mongorestore command to import the single database or collection.

mongorestore --port 27017 --db <single-database> <temp-database-path> --drop

mongorestore --port 27017 -u="myun" -p="mypw" --authenticationDatabase admin /temp-database-path/mydb

 

Shutdown the temporary MongoDB instance and remove the temporary database

--Start the mongo shell.

mongo <port>

---Drop the database and shutdown the process.

admin = db.getSiblingDB("admin")
admin.shutdownServer()
exit
Delete the temporary database directory

rm -rf <temp-database-path>

 

Read More on MongoDB topics

MongoDB 4.2.7 Bug - Off-by-one overflow in block-modification bitmaps for incremental backup

How to migrate databases with mongodump and mongorestore


Author: Jack Vamvas (http://www.dba-ninja.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to restore a single MongoDB database from a Snapshot Backup


dba-ninja.com