- Reference >
- Database Commands >
- Administration Commands >
- copydb
copydb¶
On this page
Definition¶
-
copydb¶ Copies a database from a remote host to the current host or copies a database to another database within the current host. Run
copydbin theadmindatabase of the destination server with the following syntax:copydbaccepts the following options:Field Type Description fromhoststring Optional. Hostname of the remote source mongodinstance. Omitfromhostto copy from one database to another on the same server.fromdbstring Name of the source database. todbstring Name of the target database. slaveOkboolean Optional. Set slaveOKtotrueto allowcopydbto copy data from secondary members as well as the primary.fromhostmust also be set.usernamestring Optional. The username credentials on the fromhostMongoDB deployment.noncestring Optional. A single use shared secret generated on the remote server, i.e. fromhost, using thecopydbgetnoncecommand. See Authentication for details.keystring Optional. A hash of the password used for authentication. See Authentication for details. The
mongoshell provides thedb.copyDatabase()wrapper for thecopydbcommand.
Behavior¶
Be aware of the following properties of copydb:
copydbruns on the destinationmongodinstance, i.e. the host receiving the copied data.- If the destination
mongodhasauthorizationenabled,copydbmust specify the credentials of a user present in the source database who has the privileges described in Required Access. copydbcreates the target database if it does not exist.copydbrequires enough free disk space on the host instance for the copied database. Use thedb.stats()operation to check the size of the database on the sourcemongodinstance.copydbandclonedo not produce point-in-time snapshots of the source database. Write traffic to the source or destination database during the copy process will result in divergent data sets.copydbdoes not lock the destination server during its operation, so the copy will occasionally yield to allow other operations to complete.
Required Access¶
Changed in version 2.6.
On systems running with authorization, the copydb
command requires the following authorization on the target and source
databases.
Source Database (fromdb)¶
Source is non-admin Database¶
If the source database is a non-admin database, you must have
privileges that specify find action on the source
database, and find action on the system.js collection
in the source database. For example:
If the source database is on a remote server, you also need the
find action on the system.indexes and
system.namespaces collections in the source database; e.g.
Source is admin Database¶
If the source database is the admin database, you must have
privileges that specify find action on the admin
database, and find action on the system.js,
system.users, system.roles, and system.version collections
in the admin database. For example:
If the source database is on a remote server, the you also need the
find action on the system.indexes and
system.namespaces collections in the admin database; e.g.
Source Database is on a Remote Server¶
If copying from a remote server and the remote server has authentication enabled, you must authenticate to the remote host as a user with the proper authorization. See Authentication.
Target Database (todb)¶
Copy from non-admin Database¶
If the source database is not the admin database, you must
have privileges that specify insert and
createIndex actions on the target database, and
insert action on the system.js collection in the
target database. For example:
Copy from admin Database¶
If the source database is the admin database, you must have
privileges that specify insert and
createIndex actions on the target database, and
insert action on the system.js, system.users,
system.roles, and system.version collections in the target
database. For example:
Authentication¶
If copying from a remote server and 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, as in the following:
If running the copydbgetnonce command directly on the
remote host, you can omit the fromhost field in the
copydbgetnonce command.
The key is a hash generated as follows:
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¶
Examples¶
Copy on the Same Host¶
To copy from the same host, omit the fromhost field.
The following command copies the test database to a new records
database on the current mongod instance:
Copy from a Remote Host to the Current Host¶
To copy from a remote host, include the fromhost field.
The following command copies the test database from the remote host
example.net to a new records database on the current
mongod instance:
Copy Databases from Remote mongod Instances that Enforce Authentication¶
To copy from a remote host that enforces authentication, include the
fromhost, username, nonce and key fields.
The following command copies the test database from a remote host
example.net that runs with authorization to a new records
database on the local mongod instance. Because the
example.net has authorization enabled, the command includes the
username, nonce and key fields: