0
votes

We have 1 test mongodb cluster that includes

  • 1 mongos servers

  • 3 config servers

  • 6 shards

Q1. We have tried to restore a outdated config server backup. We can only find that the config.chunks have less records than before but we can query and insert/update data in the mongodb. What will be the worst result if we use an outdated config server backup ?

Q2. Is there any tools that can re-build the loss records in config server with the existing data in each shard ?

1

1 Answers

0
votes

Answer to Q1

With outdated config server contents, iirc, there may be an unnoticed gigantic loss of data. Here is why:

Sharding in MongoDB is based on key ranges. That is, each shard is assigned a range of the shard keys it is responsible for.

For illustration purposes, let's assume you have a shard key of integer numbers starting from 1 to infinity. So the key ranges could look like this (exclusive the boundaries)

shard0001: -infinity to 100
shard0002: 101 - 200
shard0003: 201 - 300
shard0004: 301 - 400
shard0005: 401 - 500
shard0006: 501 - 600

So how does you mongo know about this distribution? It is stored on the config servers. Now let's assume that your metadata has changed and your shard0002 actually holds the data from 100-500. Let's assume you want to retrieve the document with the shard key 450. According to the old metadata, this document has to be on shard0005, if it exists. So the query gets routed to shard0005. An index lookup will be done and the shard finds out that it does not have the document. So while the document exists (on shard0002), due to the outdated metadata it will be looked up on shard0005, where it does not exist.

Answer to Q2

Not as far as I know. What you could do, however is to use the following procedure for MongoDB < 3.0.0.

Disclaimer

I haven't tested this procedure. Make sure you have the backups ready before wiping the data directories and do not omit the --repair and --objcheck flags For maximum security, create filesystem snapshots before using it. If you don't, please do not blame me for any data loss.

  1. Shut down the whole cluster gracefully
  2. Use mongodump against the data directory

     mongodump --repair --dbpath /path/to/your/datafiles -o /path/for/backups/mongo
    

    Do this once for each shard.

  3. Wipe all data directories and recreate your sharded cluster
  4. Connect to a mongos

     sh.enableSharding({"database":yourDb})
     sh.shardCollection("yourdDb.yourShardedCollection",{"yourShardKey":1})
    
  5. From each shard, use mongorestore to write the backups to a mongos

     mongorestore -h mongosHost:mongosPort --db yourDb --dir /path/for/backups/ \
     --objcheck --write-concern "{w:1}"
    

Note that you should NOT do the restores in parallel, since this might well overload the balancer.

What we basically do is to gather all data from the individual shards, create a new sharded collection within a new database and put the collected data into that database, with the sharded collection being automatically balanced.

Watch the process very carefully and make absolutely positively sure that you do not overload the balancer, otherwise you might run out of disk space on a shard in case you do not have an optimal shard key.

Of course, you can recreate other sharded databases from the backup by using mongorestore accordingly. To restore unsharded databases, simply connect to the replicaset you want to hold the collection instead of connecting to mongos.

Side note:

If you need to restore a config server, simply dump one of the other two and restore the config database to that server.

The reason this works is because the metadata can not be updated unless all config servers are up, running and in sync.