Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ask MongoDB if it is Master out of a bashscript

My mongodb is supposed to run a backup every day. It runs with one master and two slaves.

I need to make a backup of ONE of them (cause they're just replicas)

The easiest way would be to run the backup script on only one instance. but what if this instance is down? So I thought it would be good to run the backupscript on the master, cause there's always one master, even if one of the other ones is down.

So I need to tweak my backupscript in cron.daily to ask mongodb if it is the current master.

How do I do that?

like image 201
andre Avatar asked Jul 23 '13 15:07

andre


People also ask

Which command will you use for checking if you are on the master server in MongoDB?

Command syntax Db. isMaster() will tell you whether you are on the master server or not.

How do I know if MongoDB is running?

Here, in the mongo shell, we can also check the currently active connections of the database server. The serverStatus returns a document that gives an overview of the current status of the database process. Regularly running the serverStatus command will collect statistics about the MongoDB instance.

Which command can be used to show existing database when MongoDB is running?

Listing all the databases in mongoDB console is using the command show dbs .


1 Answers

You can use the --eval option to mongo to do this:

MASTER=`mongo --quiet --eval "db.isMaster().ismaster"`

And then test whether MASTER == "true" in your shell script.

like image 140
Derick Avatar answered Oct 13 '22 00:10

Derick