Monday, 23 June 2014

How do I create a MongoDB dump/Restore of my database?



To DAMP SPECIFIC DATABASE

Syn: $mongodump --db [database name]
Ex:   $mongodump --db mongo

I t wil create a dumb file in home/dump folder with the name of mongo

The --out option allows you to specify the directory where mongodump will save the backup.mongodump creates a separate backup directory for each of the backed up databases

Syn: $mongodump --db [database name] --out [dumb foldername]
Ex:   $mongodump --db mongo --out saritha

I t wil create a backup files in saritha/dump folder with the name of mongo

To dump specific collection in database:
Syn: $mongodump --db [database name] --collection [collection name]
Ex: $mongodump --db mongo --collection users

To zip the dump file:

Syn: $mongodump -db [dtabasename] --collection [collection name] --out - | gzip > [zip file name].gz

Ex: $mongodump -db mongo --collection users --out - | gzip > mongo1.gz

CHeck it in your home folder with name mongo1.gz

[or] With a date in the filename:

Syn: $mongodump -db [database name] --collection [collection name] --out - | gzip > dump_`date "+%Y-%m-%d"`.gz


TO RESTORE DATABASE


Syn:$mongorestore [database name with path]/

Ex:$mongorestore mongo/

Syn:
$ mongorestore --drop -d <database-name> <directory-of-dumped-backup>

--drop:
The restoration procedure will drop every collection from the target database before restoring the data from the dumped backup.

-d <database-name>:

To specify the database name.


1 comment: