Using MongoDB Backup and Restore Tools
Assuming that we have a remote MongoDB machine running and wish to create a snapshot of that database on a local machine, this can be done using the mongodump
command on the primary node. We just need to indicate the host and port number (the default is port 27017) for the remote server, and provide some argument parameters like the name of the database, our user name, and our password. Finally, we indicate the dump directory we want to create the snapshot in.
mongodump -h sample.mongodbhost.com:27017 -d DATABASE_NAME -u USER_NAME -p SAMPLE_PASSWORD -o ~/Desktop
One of the best practices of backing up large databases is splitting them up. You can pass a query into mongodump using the --query
parameter so that you, in case of a failure, are able to use some kind of timestamp/ordering field in your collection to resume the backup process.
In order to restore a database with a saved snapshot, we just have to use the mongorestore command. It restores data by connecting to a running mongod
directly. You can limit the output from the database by running restore in quiet mode using the --quiet
option. Once more, we provide the MongoDB host and port, along with the user name, database name, and password. Finally, we provide the output directory.
mongorestore --host sample.mongohost.com --port 27017 --username USER_NAME --password SAMPLE_PA