Don’t Let Your MongoDB Standalone Stand Alone: How to Back Up a MongoDB Single Server
Lots of people start with standalone servers when they first try MongoDB. If you are running standalone, it’s especially critical that you back it up because you don’t have the fault tolerance and redundancy of a multi-node replica set.
MongoDB Management Service (MMS) requires that you run a replica set in order to back up. What is not widely known is that you can run a one node replica set, thereby producing the oplog that MMS Backup requires to work, while still running only a single mongod server.
Here is how you do it.
- Shut down the standalone mongod instance. The safe way do this is by calling db.shutdownServer() from the mongo shell.
- Restart the instance. Use the –replSet option to specify the name of the new replica set.
- Connect to the mongod instance.
- Use rs.initiate() to initiate the new replica set.
Here is a concrete example. If you your database is running on the standard ports and uses the standard location for the data directory (/data/db) then you might issue the following command to start the server:
# mongod --replSet rs0
You can name the replica set however you like. Using “rs0” was just an example. After you do this, initiate the replica set:
# mongo # rs.initiate()
You now have a replica set! If you need more detailed instructions, you can visit the docs.
Now that your single server is a one-node replica set, you can configure backup through MMS Backup.
1) Get Monitoring Running
Using MMS Monitoring is prerequisite of using MMS Backup because the backup service relies on the configuration information gathered by the monitoring service to figure out what to backup. Monitoring is free.
If you are not already monitoring your server using MMS Monitoring, go to mms.mongodb.com and signup for a free account. You will then need to download the monitoring agent and run it on a machine that can see your server.
2) Register for Backup
Visit the Backup tab within MMS to register for the service. This includes setting up two-factor authentication for restores, entering your credit card for billing and accepting the terms of service.
3) Install the Agent
The next step is installing the backup agent on your deployment. This step is easy - just download the appropriate agent for your operating system from the Settings section of mms.mongodb.com and unzip or untar the file in the preferred directory. In the backup-agent directory, issue the following command to start the agent (unix variant):
4) Start Your Sync
Now that you’ve got replica sets going and the backup agent installed, the final step is to go the backups page on the MMS UI and enable your one-node replica set for backup.
5) Rest Easy - Your Standalone Server is No Longer Standing Alone - It’s Backed Up!
An initial sync will begin, and soon your database server will be backed up to MongoDB’s datacenters.
Start backing up now. Create an MMS account!