How To Take MongoDB Database Backup And Restore On Linux

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 in Linux.

Check Out: Installation Of MongoDB Database Server

How To Take MongoDB Database Backup And Restore:

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/

backup mongodb in linux

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/

Check Out: How To Install OpenShot Video Editor On Ubuntu 20.04

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/

Take MongoDB Database Backup

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 backup mongodb in linux

Share on:

I'm the founder of Curious Viral. I hope this blog will provide you complete information about Linux Technology & I would like to share my technical knowledge with you which I have learned during this period.

Other Posts You May Like...

Leave a comment