发布于 2015-09-14 14:43:45 | 248 次阅读 | 评论: 0 | 来源: 网络整理

This document describes the process for writing the entire contents of your MongoDB instance to a file in a binary format. If disk-level snapshots are not available, this approach provides the best option for full system database backups. If your system has disk level snapshot capabilities, consider the backup methods described in 使用文件系统快照备份和恢复MongoDB数据库.

Backup a Database with mongodump

Basic mongodump Operations

The mongodump utility can perform a live backup of data or can work against an inactive set of database files. The mongodump utility can create a dump for an entire server/database/collection (or part of a collection using of query), even when the database is running and active. If you run mongodump without any arguments, the command connects to the local database instance (e.g. 127.0.0.1 or localhost) and creates a database backup named dump/ in the current directory.

注解

The format of data created by mongodump tool from the 2.2 distribution or later is different and incompatible with earlier versions of mongod.

To limit the amount of data included in the database dump, you can specify --db and --collection as options to the mongodump command. For example:

mongodump --collection collection --db test

This command creates a dump of the collection named collection from the database test in a dump/ subdirectory of the current working directory.

Point in Time Operation Using Oplogs

Use the --oplog option with mongodump to collect the oplog entries to build a point-in-time snapshot of a database within a replica set. With --oplog, mongodump copies all the data from the source database as well as all of the oplog entries from the beginning of the backup procedure to until the backup procedure completes. This backup procedure, in conjunction with mongorestore --oplogReplay, allows you to restore a backup that reflects a consistent and specific moment in time.

Create Backups Without a Running mongod Instance

If your MongoDB instance is not running, you can use the --dbpath option to specify the location to your MongoDB instance’s database files. mongodump reads from the data files directly with this operation. This locks the data directory to prevent conflicting writes. The mongod process must not be running or attached to these data files when you run mongodump in this configuration. Consider the following example:

mongodump --dbpath /srv/mongodb

Create Backups from Non-Local mongod Instances

The --host and --port options for mongodump allow you to specify a non-local host to connect to capture the dump. Consider the following example:

mongodump --host mongodb1.example.net --port 3017 --username user --password pass --out /opt/backup/mongodump-2011-10-24

On any mongodump command you may, as above, specify username and password credentials to specify database authentication.

Restore a Database with mongorestore

The mongorestore utility restores a binary backup created by mongodump. Consider the following example command:

mongorestore dump-2011-10-25/

Here, mongorestore imports the database backup located in the dump-2011-10-25 directory to the mongod instance running on the localhost interface. By default, mongorestore looks for a database dump in the dump/ directory and restores that. If you wish to restore to a non-default host, the --host and --port options allow you to specify a non-local host to connect to capture the dump. Consider the following example:

mongorestore --host mongodb1.example.net --port 3017 --username user --password pass /opt/backup/mongodump-2011-10-24

On any mongorestore command you may specify username and password credentials, as above.

Restore Point in Time Oplog Backup

If you created your database dump using the --oplog option to ensure a point-in-time snapshot, call mongorestore with the --oplogReplay option, as in the following example:

mongorestore --oplogReplay

You may also consider using the mongorestore --objcheck option to check the integrity of objects while inserting them into the database, or you may consider the mongorestore --drop option to drop each collection from the database before restoring from backups.

Restore a Subset of data from a Binary Database Dump

mongorestore also includes the ability to a filter to all input before inserting it into the new database. Consider the following example:

mongorestore --filter '{"field": 1}'

Here, mongorestore only adds documents to the database from the dump located in the dump/ folder if the documents have a field name field that holds a value of 1. Enclose the filter in single quotes (e.g. ') to prevent the filter from interacting with your shell environment.

Restore without a Running mongod

mongorestore can write data to MongoDB data files without needing to connect to a mongod directly.

mongorestore --dbpath /srv/mongodb --journal

Here, mongorestore restores the database dump located in dump/ folder into the data files located at /srv/mongodb. Additionally, the --journal option ensures that mongorestore records all operation in the durability journal. The journal prevents data file corruption if anything (e.g. power failure, disk failure, etc.) interrupts the restore operation.

也可以参考

mongodump and mongorestore.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务