Backup a Sharded Cluster

Share us

This article will help you to take sharded cluster backup.

To take backup of sharded cluster, we will use mongodump from mongod running server, we can also use File System-Snapshot to capture the data.

    Procedure To take backup

To create backups of a sharded cluster, we need to stop the cluster balancer and take backup of the config database, and then take backups of each shard in the cluster using mongodump.

1. Disable the balancer –
Connect to mongo shell of mongos instance and then disable balancer and in config database stop balancer like given below.

use config
sh.stopBalancer()

2. Lock one secondary member of each replica set –
Lock a secondary member of each replica set in shardserver and one secondary instance of Config Server Replica Set.
Also make sure oplog has enough space to sync with primary instance, when these secondary instances will be unlock.
Connect to each secondary instance of MongoD of replica set and lock with below given command.

db.fsyncLock()

Lock one secondary member of Config Server Replica Set
When locking one member of CSRS, make sure you have replicated data up-to some control point.
To verify, first connect a mongo shell to the CSRS primary and perform a write operation with “majority” write concern on a control collection:

use config
db.BackupControl.findAndModify(
   {
     query: { _id: 'BackupControlDocument' },
     update: { $inc: { counter : 1 } },
     new: true,
     upsert: true,
     writeConcern: { w: 'majority', wtimeout: 15000 }
   }
);

The above operation will return either the newly inserted document or the updated document:

{ "_id" : "BackupControlDocument", "counter" : 1 }

Query the CSRS secondary member for the returned control document. Connect a mongo shell to the CSRS secondary to lock and use db.BackupControl.find() to query for the control document:

rs.slaveOk();
use config;

db.BackupControl.find(
   { "_id" : "BackupControlDocument", "counter" : 1 }
).readConcern('majority');

If the secondary member contains the latest control document, it is safe to lock the member. Otherwise, wait until the member contains the document or select a different secondary member that contains the latest control document.
To lock the secondary member, run db.fsyncLock() on the member:

db.fsyncLock();

3. Backup Config Server(use the secondary instance where we locked it) –
We will use mongodump to take backup, we will also use –oplog option to make sure we have point-in-time data.

mongodump --oplog

Unlock the Config Server

db.fsyncUnlock();

4. Backup Replica Set member for each Shard –
Backup the locked replica set of each shard with mongodump, use —oplog option, so that we will have poin-in-time data. You can also run this command in parallel.

mongodump --oplog

Unlock the Shard Server

db.fsyncUnlock();

5. Re-Enable the Balancer from MongoS –

use config
sh.setBalancerState(true);

For more details, please visit official website of MongoDB or you can click over here

One Commentto Backup a Sharded Cluster

  1. Josue Smeja says:

    Thanks for the sensible critique. Me and my neighbor were just preparing to do a little research about this. We got a grab a book from our area library but I think I learned more from this post. I am very glad to see such great info being shared freely out there.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.