Copying DB's over in MongoDB - mongodb

I am going to be doing some major DB restructuring in MongoDB that converts a bunch of records. I've ran this script against a copied DB locally and it works fine, so it should also work on the production database.
Is there an easy way of copying a DB instance into a new DB? I figured I could shut down MongoDB and copy the files into a new directory, rename them to the new DB, and then move them into the MongoDB data directory.
It's usually pretty slow to copy large DB files like that, so I wondered if there was a Mongo-specific way of copying DB's.

In case it is sufficient to copy only some collections, you can copy them as follows without shutting down the db server:
http://xmlquerying.blogspot.de/2012/10/copying-data-between-mongo-databases.html
Otherwise use mongodump and mongorestore.
http://docs.mongodb.org/manual/tutorial/backup-databases-with-binary-database-dumps/

Related

mongodb - How to carry over indexes while doing file backup

We have a cluster of mongo servers. We stop one of the mongo servers and copy all the data files except admin and journal and tar into one file.
We untar the file in our test environments. Though we get all the data carried over properly, we don't get the indexes carried over.
Any suggestions?

How to backup Backup mongo db in meteorjs as mongodump is not working

I am new in Meteorjs and i am using mongo db which comes with the meteor package.
I have made one small meteor application using mongodb and now i want to take backup of the mongo db database. I have seen many web sites and still i am not able to backup my data base. Everyone explained the same thing that in mongo db folder use mongodump and mongostore but when i use mongodump and mongostore on my terminal then it displays something like 'mongodump' is not an internal or external source command. Can u please help me in finding out the solution.
I have found that the easiest way to backup your database (or move it between meteor installations for that matter) is to simple copy the files in .meteor\local\db
Then you don't have to worry about all that mongodb dump stuff. Simple.

MongoDb - copy one database to another keeping the users also

I want to copy from one mongo db to another db on the same server. Mongo version is 2.6.3 on Win 2008 64bit.
I ran the command:
mongo localhost:27017/admin -u <> -p <> --eval "db.copyDatabase('db_master','db_copy1')"
This worked and created db_copy1 with all the users in it. I did db.getUsers() on db_copy1 and it returned all users. All was fine.
Then I went on to copy the database db_copy1 to db_copy2 using the same command above (with different database names obviously). But the resultant db_copy2 had no users in it.
Fairly new to mongo, so quite possible I have missed something.
Thanks in advance for all your help!
Vikram
One of the things I love about Mongodb is that rather than mess about with commands like that you can just copy the files.
Just go to the directory with the data files in it and copy them to the dbpath for your new database. If you don't want a certain database, don't copy the files with that database name!

How to import data into MongoDB from another MongoDB?

I've installed new MongoDB server and now I want to import data from the old one. My MongoDB stores monitoring data and it's a bit problematic to export the data from old database (it's over 10Gb), so I though it might be possible to import directly from DB, but haven't found how to do that with mongoimport.
The export/import would be the fastest option.
But if you really want to bypass it you can use the new server as a replica of the old one, and wait for full replication.
It takes longer but it's an easy way to set up a full copy without impact on the first one.
Follow this:
http://docs.mongodb.org/manual/tutorial/convert-standalone-to-replica-set/
And then, once it's done, change configuration again.
It's easiest than it seems, but I recommend you to do a dry run with a sample database before doing it...
Note that another benefit is that the new replica will be probably smaller in size than the initial database, because MongoDb is not very good at freeing space of deleted members
mongoimport/mongoexport is per collection operating, so it's not proper for this kind of operation.
Instead to use mongodump/mongorestore.
If the old MongoDB instance can be shutdown to do this task, you can shut down it then copy all data files to the new server as its own data. And run the new instance.
Also db.cloneDatabase() can handle it to copy data directly from old instance to new one. It should be slower against copying data files directly.
You can use mongodump and pipe directly to the new database with mongorestore like:
mongodump --archive --db=test | mongorestore --archive --nsFrom='test.*' --nsTo='examples.*'
add --host --port and --username to mongorestore to connect to the remote db.
db.cloneDatabase() has been deprecated for a while.
You can use the copydb command discribed here.
Copies a database from a remote host to the current host or copies a database to another database within the current host.
copydb runs on the destination mongod instance, i.e. the host receiving the copied data.

How to perform one-time DB sync to another DB in MongoDB?

I have separate development and production MongoDB servers and I want to keep actual data in development server for sometime. What I should use for it: mongodump, mongoimport or something else?
Clarification: I want to copy data from production to development.
If it's a one time-thing
and you want fine control over parameters such as which collections to sync, you should use:
mongodump to dump bson files of your Production DB to your local machine
mongorestore to then, retrieve the dumped BSON files in your Local DB
Otherwise you should check out mongo-sync
It's a script I wrote for my self when I had to constantly copy my Local MongoDB database to and from my Production DB for a Project (I know it's stupid).
Once you put your DB details in config.yml, you can start syncing using two simple commands:
./mongo-sync push # Push DB to Remote
./mongo-sync pull # Pull DB to Local
If you use it inside some project, it's a good idea to add config.yml to .gitignore
You can use the db.copyDatabase(...) or db.cloneDatabase(...) commands:
http://www.mongodb.org/display/DOCS/Copy+Database+Commands
This is faster than mongodump / mongorestore because it skips creating the bson representation on disk.
When you want the dev database to look exactly like the production database, you can just copy the files. I am currently running a setup where I synchronize my MongoDB database between my desktop and my notebook with dropbox - even that works flawless.