database restore in mongodb

How To Take MongoDB Database Backup And Restore 

Mongodump is a utility provided by MongoDB for taking the backup of the database. This command will take the whole back up of the database. If you don’t want to take all the database backup, So you can also take a single DB backup. Let’s see how to take Mongodb database backup and restore it.

Check Out: Installation Of MongoDB Database Server

Step 1: Create a directory where you will store the database backup.

mkdir backup

The below command will take the backup of the entire DB.

mongodump –out backup/

Database Backup at location cd backup/admin/

you can take a single backup using the ‘–db

mongodump –db test –out backup/

if you want to take a remote database backup, So use the below command

mongodump –host ip_address –port 27017 –username root –password pass –db test –out backup/

you can also use this command to take back in date format.

mongodump –out backup/`date +”%m-%d-%y”

Step 2: Restore Database

you can use “–drop”, this will make sure to delete the database if already exists in MongoDB database then you can simply restore the database.

mongorestore –db test –drop backup/09-01-19/test/

Test Database successfully restored. You can also create a crontab for backup and delete the old database.

Cron for backup,

5 5 * * * mongodump –out backup/`date +”%m-%d-%y”

Cron for deleting old backup,

5 5 * * * find backup/ -mtime +10 -exec rm -rf {} \;

YouTube Link:

You’re done

Leave a Reply

Notify of