OPTIONS

copydb

Definition

copydb

Copies a database from a remote host to the current host, or from one name to another on the current host. See also db.copyDatabase(), clone, and db.cloneDatabase(). Also see MongoDB Backup Methods and Import and Export MongoDB Data documentation for more information.

Call copydb on the destination server with the following syntax:

{ copydb: 1,
  fromhost: <hostname>,
  fromdb: <database>,
  todb: <database>,
  slaveOk: <bool>,
  username: <username>,
  nonce: <nonce>,
  key: <key> }

copydb accepts the following options:

Field Type Description
fromhost string Optional. Hostname of the source mongod instance. If omitted, copydb copies one database to another within a single MongoDB instance.
fromdb string Name of the source database.
todb string Name of the target namespace.
slaveOk boolean Optional. Set slaveOK to true to allow copydb to copy data from secondary members as well as the primary. fromhost must also be set.
username string Optional. The username credentials on the fromhost MongoDB deployment.
nonce string Optional. A single use shared secret generated on the remote server using the copydbgetnonce command.
key string Optional. A hash of the password used for authentication.

Behavior

Be aware of the following properties of copydb:

  • copydb runs on the destination mongod instance, i.e. the host receiving the copied data.
  • copydb requires enough free disk space on the host instance for the copied database. Use the:method:db.stats() operation to check the size of the database on the source mongod instance.
  • copydb and clone do not produce point-in-time snapshots of the source database. Write traffic to the source or destination database during the copy process will result divergent data sets.
  • copydb does not lock the destination server during its operation, so the copy will occasionally yield to allow other operations to complete.

Authentication

  • Do not use copydb on a mongod instance that uses auth in combination with users who have privileges specified using the role-based user documents introduced in 2.4.

    To use copydb with access control enabled you must use the legacy user privilege documents from v2.2 and prior.

  • If the remote server has authentication enabled, then you must include a username, nonce, and key. The nonce is a one-time password that you request from the remote server using the copydbgetnonce command. The key is a hash generated as follows:

    hex_md5(nonce + username + hex_md5(username + ":mongo:" + pass))
    

Replica Sets

With read preference configured to set the slaveOk option to true, you may run copydb on a secondary member of a replica set.

Sharded Clusters

  • Do not use copydb from a mongos instance.
  • Do not use copydb to copy databases that contain sharded collections.

Examples

Copy on the Same Host

Copy the test database to a new records database on the same mongod instance:

db.runCommand({
   copydb: 1,
   fromdb: "test",
   todb: "records"
})

Copy from a Source Host to a Destination Host

Copy the test database to a new records database on a mongod instance running on the myhost.com system:

db.runCommand({
   copydb: 1,
   fromdb: "test",
   todb: "records",
   fromhost: "example.net"
})

Copy Databases from mongod Instances that Enforce Authentication

Copy the test database to a new records database on a mongod instance running on the myhost.com system, when myhost.com requires the user Sam to authenticate the operation:

db.runCommand({
   copydb: 1,
   fromdb: "test",
   todb: "records",
   fromhost: "example.net"
   username: "reportingAdmin"
   key: "<passwordhash>"
})