Database Reference
In-Depth Information
The
--drop
option tells the
mongorestore
utility to discard each collection in the database before restoring it.
Consequently, the backed-up data replaces the data currently in the database. If you were to choose not to use the
--drop
option, the restored data would be appended to the end of each collection, which would result in duplicated items.
Let's examine what happened in this example more closely.
By default, the
mongodump
utility connects to the local database using the default port, and pulls out all of the data
associated with each database and collection and stores them in a predefined folder structure.
The default folder structure created by
mongodump
takes this form:
./dump/[databasename]/[collectionname].bson
The database system used in the example consists of a single database called
blog
. The
blog
database contains
three collections:
authors
,
posts
, and
tagcloud
.
The
mongodump
saves the data it retrieves from the database server in
.bson
files that are just a copy of the internal
BSON format that MongoDB uses internally to store documents. You can also see the indexes on each collection
being restored in the preceding example. The MongoDB server maintains indexes, and it records index definitions
for each collection, which are stored in the
metadata.json
files. It is these metadata files that allow you to rebuild the
indexes when restoring from a backup.
Once you have dumped the database, you can archive and store the folder on any online or offline media, such as
CD, USB drive, tape, or S3 format.
■
the
mongodump
utility does not empty the contents of the
output
directory before it writes the backup files to it.
if you have existing contents in this directory, they won't be removed unless they match the name of a file
(
collectionname
.bson
) that
mongodump
has been instructed to back up. this is good if you want to add multiple
collection dumps to the same dump directory; however, it could cause problems if you use the same dump directory each
time you back up data but don't clear it out. For example, assume you have a database that you back up regularly, and at
some point you decide to delete a collection from this database. Unless you either clear out the directory where you are
performing your backup or manually remove the file associated with the deleted collection, then the next time you restore
the data, the deleted collection will reappear. Unless you want to overlay data in your backups, you should make sure
you clear out the destination directory before you use
mongodump
.
Note
Backing Up a Single Database
When you have multiple applications running on the same server, often you may find yourself wanting to back up
each database individually, rather than all at once, as in the preceding example.
With
mongodump
, you can do this by adding the
-d
database_name
option to the command line. This causes
mongodump
to create the
./dump
folder; however, this folder will contain only the backup files for a single database.