Copy Databases Between Instances

Synopsis

MongoDB provides the copydb and clone database commands to support migrations of entire logical databases between mongod instances. With these commands you can copy data between instances with a simple interface without the need for an intermediate stage. The db.cloneDatabase() and db.copyDatabase() provide helpers for these operations in the mongo shell.

Data migrations that require an intermediate stage or that involve more than one database instance are beyond the scope of this tutorial. copydb and clone are more ideal for use cases that resemble the following use cases:

  • data migrations,
  • data warehousing, and
  • seeding test environments.

Also consider the Backup Strategies for MongoDB Systems and Import and Export MongoDB Data documentation for more related information.

Note

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.

Considerations

  • You must run copydb or clone on the destination server.
  • You cannot use copydb or clone with databases that have a sharded collection in a sharded cluster, or any database via a mongos.
  • You can use copydb or clone with databases that do not have sharded collections in a cluster when you’re connected directly to the mongod instance.
  • You can run copydb or clone commands on a secondary member of a replica set, with properly configured read preference.
  • Each destination mongod instance must have enough free disk space on the destination server for the database you are copying. Use the db.stats() operation to check the size of the database on the source mongod instance. For more information, see db.stats().

Processes

Copy and Rename a Database

To copy a database from one MongoDB instance to another and rename the database in the process, use the copydb command, or the db.copyDatabase() helper in the mongo shell.

Use the following procedure to copy the database named test on server db0.example.net to the server named db1.example.net and rename it to records in the process:

  • Verify that the database, test exists on the source mongod instance running on the db0.example.net host.

  • Connect to the destination server, running on the db1.example.net host, using the mongo shell.

  • Model your operation on the following command:

    db.copyDatabase( "test", "records", db0.example.net )
    

Rename a Database

You can also use copydb or the db.copyDatabase() helper to:

  • rename a database within a single MongoDB instance or
  • create a duplicate database for testing purposes.

Use the following procedure to rename the test database records on a single mongod instance:

  • Connect to the mongod using the mongo shell.

  • Model your operation on the following command:

    db.copyDatabase( "test", "records" )
    

Copy a Database with Authentication

To copy a database from a source MongoDB instance that has authentication enabled, you can specify authentication credentials to the copydb command or the db.copyDatabase() helper in the mongo shell.

In the following operation, you will copy the test database from the mongod running on db0.example.net to the records database on the local instance (e.g. db1.example.net.) Because the mongod instance running on db0.example.net requires authentication for all connections, you will need to pass db.copyDatabase() authentication credentials, as in the following procedure:

  • Connect to the destination mongod instance running on the db1.example.net host using the mongo shell.

  • Issue the following command:

    db.copyDatabase( "test", "records", db0.example.net, "<username>", "<password>")
    

Replace <username> and <password> with your authentication credentials.

Clone a Database

The clone command copies a database between mongod instances like copydb; however, clone preserves the database name from the source instance on the destination mongod.

For many operations, clone is functionally equivalent to copydb, but it has a more simple syntax and a more narrow use. The mongo shell provides the db.cloneDatabase() helper as a wrapper around clone.

You can use the following procedure to clone a database from the mongod instance running on db0.example.net to the mongod running on db1.example.net:

  • Connect to the destination mongod instance running on the db1.example.net host using the mongo shell.

  • Issue the following command to specify the name of the database you want to copy:

    use records
    
  • Use the following operation to initiate the clone operation:

    db.cloneDatabase( "db0.example.net" )
    

MongoDB Manual 2.4

Formats

About MongoDB

MongoDB Ecosystem

MongoDB Resources