Clone Database

881 views
Skip to first unread message

Amar

unread,
Sep 6, 2012, 9:14:26 AM9/6/12
to mongod...@googlegroups.com
Hi All,

How can I clone the database in JAVA. I am unable to find the method name in Mongo JAVA API. I tried in Mongo Shell but I am getting followng error.

db.cloneDatabase("localhost");

      "errmsg" : "exception: E11000 duplicate key error index: Amar.system.users.$_id_  dup key: { : ObjectId('50484211319a7b08dec09449') }",
      "code" : 11000,
      "ok" : 0

How can I resolve this...??????

And My Second question is If the clone was successful where Can I find the cloned database. Is there any parameters to accept like source and target in the command itself......??????

Or otherwise Could you please send any sample java code snippet for Clone Database.

Thanks & Regards,
Amar









Jenna deBoisblanc

unread,
Sep 6, 2012, 12:44:15 PM9/6/12
to mongodb-user
Hello Amar,

Can you tell us which database you're attempting to copy from
"localhost" (which is the server/process that contains the database
you're attempting to copy)? Could you also post the output of "show
dbs" followed by the clone command, and after cloning the database,
"show dbs" an additional time?

You may want to consider the db.copyDatabase() command if you haven't
already:
http://www.mongodb.org/display/DOCS/Copy+Database+Commands

On Sep 6, 9:14 am, Amar <amarshiv...@gmail.com> wrote:
> Hi All,
>
> How can I clone the database in *JAVA.* I am unable to find the method name
> in Mongo JAVA API. I tried in Mongo Shell but I am getting followng error.
>
> db.cloneDatabase("localhost");
>
>       "errmsg" : "exception: E11000 duplicate key error index:
> Amar.system.users.$_id_  dup key: { : ObjectId('50484211319a7b08dec09449')}",
>
>       "code" : 11000,
>       "ok" : 0
>
> How can I resolve this...??????
>
> And My Second question is If the clone was successful where Can I find the
> cloned database. Is there any parameters to accept like *source and target *in

amar shiva

unread,
Sep 6, 2012, 2:06:10 PM9/6/12
to mongod...@googlegroups.com
Hi Jenna,

Thanks for your reply. I think I didn't specify the db name along with the host. Is it right..??
Before Clone Command
> show dbs
Amar    (empty)
admin   0.078125GB
drs     0.078125GB
mydatabase      0.078125GB
nature  0.078125GB
rise    0.078125GB
simrise 0.078125GB

Clone Command:
> db.cloneDatabase("127.0.0.1","admin");
{ "ok" : 1 }

After Clone Command:
> show dbs
Amar    (empty)
admin   0.078125GB
drs     0.078125GB
mydatabase      0.078125GB
nature  0.078125GB
rise    0.078125GB
simrise 0.078125GB

I didn't find any cloned database here(In this list). Could you please help me where can I find the cloned database.
Please note that I also need to implement this part in JAVA as well. Is there any method in Mongo....??? Jenn Could you please help me on this......

Thanks & Regards,
Amar

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Jenna deBoisblanc

unread,
Sep 6, 2012, 3:11:08 PM9/6/12
to mongodb-user
Hi Amar,

The server where you run db.cloneDatabase() is the destination server
for "admin", and the host specified in db.cloneDatabase() is a
different server/mongod process that contains the database that you'd
like to copy.

Are you attempting to duplicate "admin" on the same server? If so, you
can't have two databases with the same name. You can use
db.copyDatabase() to copy and rename the database on the same server:
db.copyDatabase("admin", "admin2")
http://www.mongodb.org/display/DOCS/Copy+Database+Commands

This helper function is currently not supported by the Java driver,
but you can use DB.command() to run a database command:
http://api.mongodb.org/java/current/

On Sep 6, 2:06 pm, amar shiva <amarshiv...@gmail.com> wrote:
> Hi Jenna,
>
> Thanks for your reply. I think I didn't specify the db name along with the
> host. Is it right..??
> *Before Clone Command*> show dbs
>
> Amar    (empty)
> admin   0.078125GB
> drs     0.078125GB
> mydatabase      0.078125GB
> nature  0.078125GB
> rise    0.078125GB
> simrise 0.078125GB
>
> *Clone Command:*> db.cloneDatabase("127.0.0.1","admin");
>
> { "ok" : 1 }
>
> *After Clone Command:*> show dbs
>
> Amar    (empty)
> admin   0.078125GB
> drs     0.078125GB
> mydatabase      0.078125GB
> nature  0.078125GB
> rise    0.078125GB
> simrise 0.078125GB
>
> I didn't find any cloned database here(In this list). Could you please help
> me where can I find the cloned database.
> Please note that I also need to implement this part in JAVA as well. Is
> there any method in Mongo....??? Jenn Could you please help me on this......
>
> Thanks & Regards,
> Amar
>

amar shiva

unread,
Sep 7, 2012, 1:31:42 AM9/7/12
to mongod...@googlegroups.com
Thanks Jenna,

This is very much useful to me. Once again thank u very much.

Thanks & Regards,
Amar

amar shiva

unread,
Sep 7, 2012, 2:04:25 AM9/7/12
to mongod...@googlegroups.com
Hi Jenna,

I have another question related to MongoDB Clone. I tried this command  db.cloneDatabase("192.168.1.194") it imports the database from this host address. No my dbs are

> show dbs

drs     0.078125GB
mydatabase      0.078125GB
nature  0.078125GB
rise    0.078125GB
simrise 0.078125GB
test    0.078125GB (Imported from host)

The highlighted one is the imported one. Here my doubts is this host has a three different databases are there but when I run the clone database I got only one database instead of all the databases.
  • How can I clone all the databases from a different host.
  • Second one, How can I clone a particular database from a different host.

I stuck over here could you please help me on these things.

Thanks & Regards,

Amar

On 7 September 2012 00:41, Jenna deBoisblanc <jenna.de...@10gen.com> wrote:

amar shiva

unread,
Sep 8, 2012, 5:28:32 AM9/8/12
to mongod...@googlegroups.com
Hi Jenna,

I have another question related to MongoDB Clone. I tried this command  db.cloneDatabase("192.168.1.194") it imports the database from this host address. No my dbs are

> show dbs

drs     0.078125GB
mydatabase      0.078125GB
nature  0.078125GB
rise    0.078125GB
simrise 0.078125GB
test    0.078125GB (Imported from host)

The highlighted one is the imported one. Here my doubts is this host has a three different databases are there but when I run the clone database I got only one database instead of all the databases.
  • How can I clone all the databases from a different host.
  • Second one, How can I clone a particular database from a different host.

I stuck over here could you please help me on these things.

Thanks & Regards,

Amar

Thomas Rueckstiess

unread,
Sep 9, 2012, 10:39:10 PM9/9/12
to mongod...@googlegroups.com
Hi Amar,

the cloneDatabase command only clones the database with the same name as the one you are currently using on the destination server. See the docs for a detailed description:

If you want to clone a different database, type "use <dbname>" before cloning, where <dbname> is the database you want to clone.

use <dbname>
db.cloneDatabase('192.168.1.194')

Regards,
Thomas

amar shiva

unread,
Sep 10, 2012, 7:08:09 AM9/10/12
to mongod...@googlegroups.com
Hi Thomas,

Thanks you very much your detailed explanation. So If I want to clone any database I need to make sure the name of the database is there in my database list or not. If it's not there I need to create a database with the same name. Is it right Thomas..??????

Thanks & Regards,
Amar

Thomas Rueckstiess

unread,
Sep 12, 2012, 1:47:07 AM9/12/12
to mongod...@googlegroups.com
Hi Amar,

it is not necessary to create the database separately. Running 'use <dbname>' and then the 'db.cloneDatabase(<ip-addr>)' will create the database if it didn't exist before.

Regards,
Thomas

amar shiva

unread,
Sep 12, 2012, 7:10:56 AM9/12/12
to mongod...@googlegroups.com
Thanks Thomas, 

Thanks & Regards,
Amar

Dzmitry Lazerka

unread,
Aug 11, 2013, 7:44:03 PM8/11/13
to mongod...@googlegroups.com
Hi Jenna,

You've suggested to use DB.command() from Java driver to clone/copy databases. Unfortunately, I cannot find correct parameters to make it work. I try:
BasicDBObject cmd = new BasicDBObject("copyDatabase", Lists.newArrayList("fromDbName", "toDbName"));
db
.command(cmd);
But that gives me 
no such cmd: copyDatabase

Any ideas? I haven't found any info on how to do that from Java driver. I only want to copy databases on the same host (for integration tests).

Dzmitry Lazerka

unread,
Aug 11, 2013, 7:57:36 PM8/11/13
to mongod...@googlegroups.com
I've found the answer from a blog (it was about Node.js though). The command should be:
BasicDBObject cmd = new BasicDBObject();
cmd
.put("copydb", 1);
cmd
.put("fromdb", "fromDbName");
cmd
.put("todb", "toDbName");
And current database must be "admin".

Asya Kamsky

unread,
Aug 11, 2013, 11:56:53 PM8/11/13
to mongodb-user
The command is 'copydb'

Here's a tip (and also how I double-checked the command) - if you start up the mongo shell and then type the db.<commandHelper> but leave off the ( ) then you will see the source to the helper function:

>  db.copyDatabase
function (fromdb, todb, fromhost, username, password) {
    assert( isString(fromdb) && fromdb.length );
    assert( isString(todb) && todb.length );
    fromhost = fromhost || "";
    if ( username && password ) {
        var n = this._adminCommand( { copydbgetnonce : 1, fromhost:fromhost } );
        return this._adminCommand( { copydb:1, fromhost:fromhost, fromdb:fromdb, todb:todb, username:username, nonce:n.nonce, key:this.__pwHash( n.nonce, username, password ) } );
    } else {
        return this._adminCommand( { copydb:1, fromhost:fromhost, fromdb:fromdb, todb:todb } );
    }
}

That's how you can verify what actual db command is being executed by the helper and how.

Asya



--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages