Ask MongoDB if it is Master out of a bashscript

2020-06-10 14:47发布

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?

1条回答
可以哭但决不认输i
2楼-- · 2020-06-10 15:14

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

MASTER=`mongo --quiet --eval "d=db.isMaster(); print( d['ismaster'] );"`

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

查看更多
登录 后发表回答