MongoDB Backup
Backup
Backup is important for data storage.
For RDMS, they have dump command to make backup.
For Mongo, it is same as RDMS.
It has mongodump command.
Also, mongo data is file. If we can use file, just copy them.
mongodump
If you install mongodb, you can use mongodump command.
Test
mongod --dbpath ./data >use data >db.items.insert({"title":"Taiyo"})
Start mongo. All data are under /data
Create “data” database and create “items” collection and insert one item.
Create dump
Let’s make dump
The simplest command is following
mongodump --out /mongobackup/dump
It’s O.K. to run this command while mongo is running
Set only output directory.
All data are dumped under /mongobackup/dump. Look this directory.
data directory was made and items.bson and items.metadata.json are created.
These are backupfile.
If you want to add other parameters
mongodump --host localhost:27019 --db data --out /mongobackup/dump
Set host name
Set database name
Stop mongo and delete old files
Break current data!
Stop mongod and delete all files /data
So, “data” database and “items” collection and item has gone.
Let’s start and make database named “data” again.
mongod --dbpath ./data use data # Craete database
We need to create “data” database. If you skip this operation, we cannot run next restore command correctly.
Restore data
mongorestore --db data --drop ./mongobackup/dump/data
Restore works, please check data