Whether you’re restoring a backup or migrating a server, importing data is a key step in the process. MongoDB provides many ways to import data into your MongoDB deployment. You can import a collection, database or the whole deployment. In this post, we will talk about three different ways to import data into your ScaleGrid MongoDB hosting deployment.
MongoRestore & MongoDump
MongoDump and MongoRestore tools are popular utilities when importing data into deployments. MongoRestore allows you to import collections, database, and deployments. The MongoRestore tool imports content from a binary database dump, created by MongoDump, into a specific database. MongoRestore can import content to an existing database or create a new one.
MongoRestore only performs inserts into the existing database and does not perform updates or upserts. If existing data with the same _id already exists on the target database, MongoRestore will not replace it.
To import data into your ScaleGrid MongoDB deployment utilizing MongoRestore, the following command line can be used:
mongorestore --host <host> --port <port> --username admin --password <pass> /location/to/mongodumpfiles
db.copyDatabase is a easy-to-use command built into the MongoShell which allows you to easily migrate and copy data from one MongoDB instance or deployment to another. The db.copyDatabase command also supports username and passwords, making copying of databases across deployments a breeze. Follow these steps to copy a database from the remote server:
- Connect to your MongoDB deployment using the mongo shell. ie. mongo <host> -u <user> -p <password>
- Run the following command – “db.copyDatabase(“<NewDBName”, “<SourceDBName>”, “<host>”, “<username>”, “<password>”); “
Snapshots provide an easy way to store large amount of MongoDB data and provides point-in-time snapshots that are great to restore MongoDB deployments from scratch. ScaleGrid’s MongoDB deployments support the restoration of snapshots. NOTE: Restoring of snapshots using the following commands will overwrite all your existing data. We highly recommend you backup the existing data before overwriting it with the snapshot.
- SSH to the MongoDB instance you want to restore the snapshot at. More information on how to get credentials for your deployment can be found here.
- Stop the MongoDB service – “sudo service mongod stop”
- Delete all the data in mongodb_data folder – “sudo /bin/rm -rf /mongodb_data/*”
- Copy your snapshot to the mongodb_data folder – “sudo mv -r snapshot/* /mongodb_data/”
- Start the MongoDB service – “sudo service mongod start”
Thats it! It’s that simple. We’ll continue to monitor and manage the MongoDB deployment. You can backup, restore or clone this MongoDB deployment through ScaleGrid utilizing the new snapshot data.