Friday, November 16, 2018

MongoDB Export and Import Backup recoveries

Export Backups

mongoexport is a utility that produces a JSON or CSV export of data stored in a MongoDB instance.

Recommandation to Use mongodump and mongorestore as described in MongoDB Backup Methods for this kind of functionality.

namespace options:
  -d, --db=<database-name>                        database to use
  -c, --collection=<collection-name>              collection to use
  -o, --out=<filename>                            output file; if not specified, stdout is used
  -f, --fields=<field>[,<field>]*                 comma separated list of field names (required for exporting CSV) e.g. -f "name,age"
      --fieldFile=<filename>                      file with field names - 1 per line
  -q, --query=<json>                              query filter, as a JSON string, e.g., '{x:{$gt:1}}'

Example for export backup of collection employyee
DB Name : sai
collection Name:  employee

R:PRIMARY> use sai
switched to db sai
R:PRIMARY> show collections
emplyee
R:PRIMARY> db.employee.count()
991
R:PRIMARY>

mongoexport -d sai -c  employee -o /u02/mongo/backup/sai_employee.json --port 27027

mongoexport --db sai --collection  employee --out /u02/mongo/backup/sai_employee_1.json --port 27027

[mongodb@sainath backup]$ mongoexport -d sai -c  employee -o /u02/mongo/backup/sai_employee.json --port 27027
2018-11-17T00:00:27.863+0530    connected to: localhost:27027
2018-11-17T00:00:27.936+0530    exported 1982 records
[mongodb@sainath backup]$ mongoexport --db sai --collection  employee --out /u02/mongo/backup/sai_employee.json --port 27027
2018-11-17T00:01:13.742+0530    connected to: localhost:27027
2018-11-17T00:01:13.789+0530    exported 1982 records
[mongodb@sainath backup]$


[mongodb@sainath backup]$ ls -lrt
total 360
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:01 sai_employee.json
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:01 sai_employee_1.json
[mongodb@sainath backup]$

mongoexport -d sai -c  employee -o /u02/mongo/backup/sai_employee.Bson --port 27027

mongoexport --db sai --collection  employee --out /u02/mongo/backup/sai_employee_1.Bson --port 27027

[mongodb@sainath backup]$ ls -rtl
total 720
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:01 sai_employee.json
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:01 sai_employee_1.json
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:04 sai_employee.Bson
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:04 sai_employee_1.Bson
[mongodb@sainath backup]$




mongoexport -d sai -c  employee -o /u02/mongo/backup/sai_employee.csv --port 27027

mongoexport --db sai --collection  employee --out /u02/mongo/backup/sai_employee_1.csv --port 27027


mongoimport -d sai -c employee --type CSV --file /home/mongodb/Desktop/sai.csv --headerline --port 27027

mongoimport -d sai -c employee --type CSV --file /home/mongodb/Desktop/sai.csv --headerline --port 27027

[mongodb@sainath backup]$ ls -lr
total 1080
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:01 sai_employee.json
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:05 sai_employee.csv
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:04 sai_employee.Bson
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:01 sai_employee_1.json
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:05 sai_employee_1.csv
-rw-rw-r--. 1 mongodb mongodb 181704 Nov 17 00:04 sai_employee_1.Bson
[mongodb@sainath backup]$

No comments:

Post a Comment