database restore in mongodb

How To Take MongoDB Database Backup And Restore 

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

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 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 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 defeting 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

Sponsored

LEAVE A REPLY

Please enter your comment!
Please enter your name here

four + seven =