OPTIONS

Restore a Replica Set from MongoDB Backups

This procedure outlines the process for taking MongoDB data and restoring that data into a new replica set. Use this approach for seeding test deployments from production backups as well as part of disaster recovery.

You cannot restore a single data set to three new mongod instances and then create a replica set. In this situation MongoDB will force the secondaries to perform an initial sync. The procedures in this document describe the correct and efficient ways to deploy a replica set.

Restore Database into a Single Node Replica Set

  1. Obtain backup MongoDB Database files. These files may come from a file system snapshot. The MongoDB Management Service (MMS) produces MongoDB database files for stored snapshots and point and time snapshots. You can also use mongorestore to restore database files using data created with mongodump. See Back Up and Restore with MongoDB Tools for more information.

  2. Start a mongod using data files from the backup as the dbpath. In the following example, /data/db is the dbpath to the data files:

    mongod --dbpath /data/db
    
  3. Convert your standalone mongod process to a single node replica set by shutting down the mongod instance, and restarting it with the --replSet option, as in the following example:

    mongod --dbpath /data/db --replSet <replName>
    

    Optional

    Consider explicitly setting a oplogSizeMB to control the size of the oplog created for this replica set member.

  4. Connect to the mongod instance.

  5. Use rs.initiate() to initiate the new replica set.

Add Members to the Replica Set

MongoDB provides two options for restoring secondary members of a replica set:

  1. Manually copy the database files to each data directory.
  2. Allow initial sync to distribute data automatically.

The following sections outlines both approaches.

Note

If your database is large, initial sync can take a long time to complete. For large databases, it might be preferable to copy the database files onto each host.

Copy Database Files and Restart mongod Instance

Use the following sequence of operations to “seed” additional members of the replica set with the restored data by copying MongoDB data files directly.

  1. Shut down the mongod instance that you restored. Using --shutdown or db.shutdownServer() to ensure a clean shut down.
  2. Copy the primary’s data directory into the dbPath of the other members of the replica set. The dbPath is /data/db by default.
  3. Start the mongod instance that you restored.
  4. In a mongo shell connected to the primary, add the secondaries to the replica set using rs.add(). See Deploy a Replica Set for more information about deploying a replica set.

Update Secondaries using Initial Sync

Use the following sequence of operations to “seed” additional members of the replica set with the restored data using the default initial sync operation.

  1. Ensure that the data directories on the prospective replica set members are empty.
  2. Add each prospective member to the replica set. Initial Sync will copy the data from the primary to the other members of the replica set.