Mongodb provides a lot of flexibilities to the application developers and system administrators. As we know mongodb is an nosql database server. Standard Sql database servers provides different tools to dump and backup given database. Mongodb provides
mongodump tool for dump and backup operations.
Check MongoDB Service Status
First we should check the status of the mongodb. We can use
systemctl command which is provided by popular Linux distributions.
$ systemctl status mongod
Dump MongoDb Database
We only need to specify the hostname of the mongodb database server. We will use
--host option in order to specify the hostname. In this example we will dump remote host named
poftut1 . This will backup all databases to the file named
$ mongodump --host poftut1
Only Dump Specified Database
We do not need to dump all databases. We can specify the database we want to dump. We will use
--db option in order to specify the database name. In this example we will only dump the database named
$ mongodump --host poftut1 --db school
Only Dump Specific Collection
We can also specify specific collection. We will use
--collection option in order to specify the collection’s name. In this example we will dump the collection named
students in database
$ mongodump --host poftut1 --db school --collection students
Dump and Compress
Dumping big databases or collections will create a lot data with big size. If we need to do dump multiple times in a day the disk will be full. We can compress the dumped data by using
gzip or similar compression tools. We will redirect the output of dump to the gzip with
--out - option. In this example we also set the compressed dump name with the current date.
$ mongodump --host poftut1 --out - | gzip > dump_`date "+%Y-%m-%d"`.gz
For more information about compression look following tutorials.