- 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
copydb
in theadmin
database of the destination server with the following syntax:copydb
accepts the following options:Field Type Description fromhost
string Optional. Hostname of the remote source mongod
instance. Omitfromhost
to copy from one database to another on the same server.fromdb
string Name of the source database. todb
string Name of the target database. slaveOk
boolean Optional. Set slaveOK
totrue
to allowcopydb
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, i.e. fromhost
, using thecopydbgetnonce
command. See Authentication for details.key
string Optional. A hash of the password used for authentication. See Authentication for details. The
mongo
shell provides thedb.copyDatabase()
wrapper for thecopydb
command.
Behavior¶
Be aware of the following properties of copydb
:
copydb
runs on the destinationmongod
instance, i.e. the host receiving the copied data.- If the destination
mongod
hasauthorization
enabled,copydb
must specify the credentials of a user present in the source database who has the privileges described in Required Access. copydb
creates the target database if it does not exist.copydb
requires 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 sourcemongod
instance.copydb
andclone
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 in 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.
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: