How to Create a Backup and Restore the Databases in MongoDB 4.0.11 on CentOs 7.6

Backup and Restore the database In MongoDB 4.0.11 On CentOs 7.6

Process:

Check the version of MongoDB as follows

[root@linuxhelp ~]# mongod --version
db version v4.0.11
git version: 417d1a712e9f040d54beca8e4943edce218e9a8c
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
    distmod: rhel70
    distarch: x86_64
    target_arch: x86_64

Login to the MongoDB Shell

[root@linuxhelp ~]# mongo
MongoDB shell version v4.0.11
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("3b67ef4b-ea2b-44c6-af97-171de1c28016") }
MongoDB server version: 4.0.11
Server has startup warnings: 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---


##List the databases that are present by default in MongoDB
> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB

##Create a database named test
> use test;
switched to db test

##Verify the test database in the databases list
> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB

##As it hasn’t showed up the created database test,Switch Over to the newly created database and Insert the users id document into the test database as follows.
> use test;
switched to db test

##Insert the user data whose id is 1 into the test database as follwos
> db.users.insert( { "id" : 1 } );
WriteResult({ "nInserted" : 1 })


##After successful Insertion,List the databases in the MongoDB where it shows up the test database
> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB
test    0.000GB

##Exit from the MongoDB shell.
> exit
Bye

Backup process :

Single Database backup

Create a backup for the single database using mongodump as follows

[root@linuxhelp ~]# mongodump --db test --out /backup/
2019-08-28T08:36:14.905+0530	writing test.users to 
2019-08-28T08:36:14.906+0530	done dumping test.users (1 document)

Navigate to the Backup directory

[root@linuxhelp ~]# cd /backup/

Check whether the test database backup has been created or not.

[root@linuxhelp backup]# ls -la
total 0
drwxr-xr-x   3 root root  18 Aug 28 08:36 .
dr-xr-xr-x. 18 root root 258 Aug 28 08:36 ..
drwxr-xr-x   2 root root  51 Aug 28 08:36 test

Whole MongoDB database list backup

Create a backup for all the databases in the MongoDB in the following location

[root@linuxhelp backup]# mongodump --out /backup/all/
2019-08-28T08:37:12.384+0530	writing admin.system.version to 
2019-08-28T08:37:12.385+0530	done dumping admin.system.version (1 document)
2019-08-28T08:37:12.385+0530	writing test.users to 
2019-08-28T08:37:12.385+0530	done dumping test.users (1 document)

Navigate to the all directory

[root@linuxhelp backup]# cd all

Check whether all the database backups have been created or not

[root@linuxhelp all]# ls
admin  test

Restoring Process

Restore the single database named test from the below mentioned location to the MongoDB

[root@linuxhelp ~]# mongorestore --db test --drop /backup/all/test
2019-08-28T08:47:05.947+0530	the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2019-08-28T08:47:05.947+0530	building a list of collections to restore from /backup/all/test dir
2019-08-28T08:47:05.949+0530	reading metadata for test.users from /backup/all/test/users.metadata.json
2019-08-28T08:47:05.979+0530	restoring test.users from /backup/all/test/users.bson
2019-08-28T08:47:05.981+0530	no indexes to restore
2019-08-28T08:47:05.981+0530	finished restoring test.users (1 document)
2019-08-28T08:47:05.981+0530	done

Restore all the databses using the –drop option as follows

[root@linuxhelp ~]# mongorestore --drop /backup/all
2019-08-28T08:47:49.952+0530	preparing collections to restore from
2019-08-28T08:47:49.954+0530	reading metadata for test.users from /backup/all/test/users.metadata.json
2019-08-28T08:47:49.990+0530	restoring test.users from /backup/all/test/users.bson
2019-08-28T08:47:49.998+0530	no indexes to restore
2019-08-28T08:47:49.998+0530	finished restoring test.users (1 document)
2019-08-28T08:47:49.998+0530	done

Navigate to the location where all the database backups have been created

[root@linuxhelp ~]# cd /backup/all

Change the directory to test

[root@linuxhelp all]# cd test

List the database to view the content inside it.

[root@linuxhelp test]# ls
users.bson  users.metadata.json

Create a new database in the current working parent directory

[root@linuxhelp test]# mkdir ../surf

Copy the files of test to the surf directory as follows

[root@linuxhelp test]# cp users.* ../surf/

Change the directory to the parent Directory.

[root@linuxhelp test]# cd ..

List the directory to verify the newly created directory

[root@linuxhelp all]# ls
admin  surf  test

Restore all the databases of MongoDB from the following location

[root@linuxhelp all]# mongorestore --drop /backup/all/
2019-08-28T08:49:45.343+0530	preparing collections to restore from
2019-08-28T08:49:45.346+0530	reading metadata for test.users from /backup/all/test/users.metadata.json
2019-08-28T08:49:45.346+0530	reading metadata for surf.users from /backup/all/surf/users.metadata.json
2019-08-28T08:49:45.376+0530	restoring surf.users from /backup/all/surf/users.bson
2019-08-28T08:49:45.389+0530	restoring test.users from /backup/all/test/users.bson
2019-08-28T08:49:45.390+0530	no indexes to restore
2019-08-28T08:49:45.390+0530	finished restoring surf.users (1 document)
2019-08-28T08:49:45.391+0530	no indexes to restore
2019-08-28T08:49:45.391+0530	finished restoring test.users (1 document)
2019-08-28T08:49:45.391+0530	done

Login to the MongoDB Shell

[root@linuxhelp all]# mongo
MongoDB shell version v4.0.11
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a52389be-17fe-4740-b3dc-16f0cf5416b0") }
MongoDB server version: 4.0.11
Server has startup warnings: 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-06T15:33:04.607+0530 I CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
## Check the database surf in the default databases list after the successful restoration.
> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB
surf    0.000GB
test    0.000GB

With this,Creation of Backup and Restoration In MongoDB 4.0.11 On CentOS 7.6 comes to end.

Tag : Mongodb
FAQ
Q
IS it possible to specify the hostname or port for the remote connection database to take backups in MongoDB?
A
Yes,it is possible to specify the hostname or port for the remote connections database to take backups in MongoDB.
Q
What does --drop option do In MongoDB?
A
--drop option removes the databases that already exists in the MongoDB and restores the database
Q
What does --out option in MongoDB do?
A
--out option represents the database backup location In MongoDB
Q
Does --drop option restores the newly created databases into the MongoDB?
A
Yes, --drop option restores the newly created databases into the MongoDB
Q
Does --drop option deletes the existing databases in MongoDB during database restoration process?
A
Yes , --drop option deletes the existing databases in MongoDB during the restoration process .