In MySQL world, we can backup and restore our database pretty easy via phpmyadmin or mysqldump etc. But in mongoDB, it is even easier! Just running 2 commands in total, one for backup and one for restore, and you can seamlessly transfer your mongoDB data to the new machine.
In my case, I try to transfer the DB data from my old raspberry gen1 to my new raspberry pi 3, of course, it works on all sorts of Linux platform and Windows also.
First, to backup your DB, run
mongodump --db myDB --out destPath
This is the simplest backup method, of course, mongodump provides many options for customization, but in most situation, this would be enough. If you want to backup all the databases, do not specify --db.
Then you just copy the dumped mongoDB file to the desired location and run
mongorestore --db myDB --drop /data/db
/data/db is my designated path, you can have it yourself, after that, all the data will be restored! Easy right? Remember --drop parameter is important as changes in the database will not be restored if not specified.
Update: 19-6-2016
Remember, the restore path is always the relative path of the current directory, i.e. Say
mongorestore --drop -d db abc/
"abc" is a RELATIVE directory of the current shell
Reference:
- http://stackoverflow.com/questions/3884418/mongorestore-of-a-db-causing-me-trouble
- https://docs.mongodb.org/manual/reference/program/mongodump/#bin.mongodump
- https://docs.mongodb.org/manual/reference/program/mongorestore/