How do I specify any secondary member of replica set when scripting a backup?

316 views
Skip to first unread message

fryj00

unread,
Feb 25, 2016, 5:00:47 PM2/25/16
to mongodb-user
Hi, I am attempting to write a backup script that will automatically pick a non-primary member. Every method that I have seen intended for replica sets either includes the set name and all members (which will connect you to the primary) or assumes a static secondary IP and port like:

mongodump --host rs1/127.0.0.1:27017,127.0.0.1:27018 --oplog --gzip --archive=$DEST.archive.gz

OR

mongodump --host <secondary IP>:27017 --oplog --gzip --archive=/path/to/file.archive.gz


Note, I am currently running multiple instances of mongod on a test box, hence the multiple loopback addy's

If there is a dynamic variable to specify a secondary member, I have not been able to find it. Anyone dealt with this before?

Thanks!

-Bryan

Kevin Adistambha

unread,
Mar 6, 2016, 10:51:50 PM3/6/16
to mongodb-user

Hi Bryan,

Every method that I have seen intended for replica sets either includes the set name and all members (which will connect you to the primary) or assumes a static secondary IP and port

Specifying the set name and all replica set members in mongodump connection string does not necessarily mean that the dump will be performed by the primary node:

  • Before version 3.2, if mongodump is specified to connect to a replica set, it will perform the dump from a secondary node.

  • For MongoDB version 3.2 and above, mongodump will by default perform the dump from the nearest node. There is, however, a new --readPreference setting (shown in the contents of mongodump --help).

If you are using 3.2.x, to perform a dump from a secondary node your command line can be modified to:

mongodump --host rs1/127.0.0.1:27017,127.0.0.1:27018 --oplog --gzip --archive=$DEST.archive.gz --readPreference=secondary

Please note that the secondary nodes in a replica set are eventually consistent and may not contain the latest data from the primary node. Also, mongodump is not the only backup method. See MongoDB Backup Methods for more information regarding other backup methods.

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages