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?
Command syntax Db. isMaster() will tell you whether you are on the master server or not.
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.
Listing all the databases in mongoDB console is using the command show dbs .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With