Blog, COLDSURF

How to “dump (backup)” MongoDB and also “restore” data

#mongodb

Let’s Dump (backup)!

If we have installed MongoDB on our computer, we have to dump by mongodump
Run the bottom line. And we now have dump directory.
mongodump --host 127.0.0.1 --port 27017
 
We also can choose where to locate dumped files will be located by --out
I used default host and port which are 127.0.0.1 and 27017.
mongodump --out ~/mongo_backup --host 127.0.0.1 --port 27017
 
If you have secured up by user name and password, you can use -u and -p
mongodump --out ~/mongo_backup --host 127.0.0.1 --port 27017 -uusername -ppassword
 
You can also choose specific database to dump by using --db
mongodump --out ~/mongo_backup --host 127.0.0.1 --port 27017 -uusername -ppassword --db dbname
 
So, next line will be completed version of dump cli.
mongodump --out 덤프지정위치(디렉터리) --host 127.0.0.1 --port 27017 -uusername -ppassword --db dbname
 

Let’s Restore!

So you dumped your mongo databases.
So let’s restore from it.
You can use mongorestore this time. (if you already installed mongodb on your computer)
mongorestore --host 127.0.0.1 --port 27017 -uusername -ppassword --drop dbname_which_you_want_to_drop_on_existing_db --db dbname "directory path which have dumped files"
--drop option will be used when you want to drop the database which is duplicated name with database name you want to backup.
 
So, next line will be completed example of restore cli.
mongorestore --host 127.0.0.1 --port 27017 --drop myblog --db myblog ~/mongo_backup
← Go home