This Tutorial explains how to Backup a MongoDB database and restore data from a backup. MongoDB provides utility to Backup and Restore Database.
Create Directory:
First create a directory to save mongodb backup.
$ mkdir -p /opt/mongodb-backups
MongoDB Backup:
Use below command to backup MongoDB database.
Syntax:
mongodump --host <hostname> OR <IP> --port 27017 -u <mongouser> --authenticationDatabase <authdb> --db <dbname> --out /opt/mongodb-backups/
Example:
Here We are taking backup of database name “Employee”,
$ mongodump --host 192.168.100.10 --port 27017 -u fossadmin -p --authenticationDatabase admin --db Employee --out /opt/mongodb-backups/
Restore MongoDB backup:
To Restore MongoDB Backup.
Syntax:
$ mongorestore --host <hostname> OR <IP> --port <portnumber> -u $mongo_user -p $mongo_pass -d dbname -c $collection $file
Example:
Restoring MongoDB backup of database name “Employee” to another database.
$ mongorestore --host 192.168.100.12 --port 27017 -u $fossadmin -p $fosstechnix@123 -d Employee -c $collection $file