Automated Backups in a replSet?

102 views
Skip to first unread message

Floppy Disk

unread,
Aug 23, 2016, 9:43:28 AM8/23/16
to mongodb-user
I have a replSet cluster running x2 MongoDB servers and an arbiter on a production Linux (CentOS 7.2) servers. I need to configure automated backups of the replSet but obviously I would need some kind of logic inside my script to detect rs.isMaster() & then perform a dump as follows:

mongodump -h 127.0.0.1 -d mydb -u carlos -p -o /var/lib/mongodb/backups

Thanks for any clarification or help in getting a basic working script for backing up my MongoDB / Linux deployment. I checked Google and didn't really find anything that was just basic guides randomly written years ago that don't work with a replSet.

Thanks!

-Carlos 

davidmo

unread,
Aug 23, 2016, 11:02:54 AM8/23/16
to mongodb-user
hey carlos. i have a bash script on RHEL 6 and in the script this is how i check:

#
#   check if primary
#
PRIMARY=`/usr/bin/mongo ${SERVERNAME}:${PORT} --quiet ${SCRIPTDIR}/isMaster.js`;
#
#
# Check if any errors
#
if [ "$?" -ne "0" ]; then
    echo "Error running isMaster.js for $SERVERNAME" > ${LOGDIR}/dumplog.${DATE_NOW}
    cat ${LOGDIR}/dumplog.${DATE_NOW}  | mailx -s "ERROR mongodump output from $SERVERNAME" $EMAILIST
    exit 1
  fi
#
#
if [ "$PRIMARY" != "false" ]; then
    echo "No mongodump for ${SERVERNAME} - not the secondary" > ${LOGDIR}/dumplog.${DATE_NOW}
    cat ${LOGDIR}/dumplog.${DATE_NOW} | mailx -s "mongodump output from ${SERVERNAME}" $EMAILIST
    exit 2
fi
#

i only dump from a secondary.

Floppy Disk

unread,
Aug 24, 2016, 11:43:37 AM8/24/16
to mongodb-user
Thanks David. I am a bit confused because you're setting a variable for 'PRIMARY' but I don't understand:
...${SCRIPTDIR}/isMaster.js`;

What is 'isMaster.js'? I am creating this backup script to backup the db inside /usr/local/sbin. Where do I need to have this isMaster.js script and what does that look like? I am fairly confused here so I apologize. 

I was going to have crontab just call the backup script /usr/local/sbin/mongodb-backup.sh:

#!/bin/sh
## Set variables
SCRIPTDIR='/usr/local/sbin/mongodb-backup.sh'
MONGOBIN='/usr/bin/'
SERVERNAME='mongo1.domain.tld'
LOGDIR='/var/log/mongodb/'
DATENOW='date +%Y%m%d%H%M%S'
## Check if the primary in cluster
PRIMARY='MONGO_BIN/mongo $SERVERNAME:27017 --quiet $SCRIPTDIR/isMaster.js';

# Check if any errors
if [ "$?" -ne "0" ]; then
    echo "Error running isMaster.js for $SERVERNAME" > $LOGDIR/mongodb-backup.$DATE_NOW
#   cat $LOGDIR/mongodb-backup.$DATE_NOW  | mailx -s "ERROR mongodump output from $SERVERNAME" $EMAILIST
    exit 1
fi


if [ "$PRIMARY" != "false" ]; then
    echo "No mongodump for $SERVERNAME - not the secondary" > $LOGDIR/dumplog.$DATE_NOW
#   cat $LOGDIR/mongodb-backup.$DATE_NOW | mailx -s "mongodump output from ${SERVERNAME}" $EMAILIST
    exit 2
fi
#

Where are you performing the dump? I see the shell script performing the check which I'm confused about above but also can you share the code for performing the dump as well?

davidmo

unread,
Aug 25, 2016, 11:47:18 AM8/25/16
to mongodb-user
carlos, you asked about determining if a server is a PRIMARY or not.

i sent you the piece of my script that does that. PRIMARY is a variable that receives the ouput of executing isMaster.js

isMaster.js is a file that contains this:  printjson(db.isMaster().ismaster);

the dump is performed after the script determines that the server is not the PRIMARY using mongodump,  which is heavily documented.


On Tuesday, August 23, 2016 at 9:43:28 AM UTC-4, Floppy Disk wrote:

boncalo mihai

unread,
Aug 25, 2016, 3:34:03 PM8/25/16
to mongodb-user
Hi Carlos,

It's simple to check if server is primary with  mongo --eval "printjson(db.isMaster())" and awk but for production environement rsync is recommended.

davidmo

unread,
Aug 25, 2016, 3:56:16 PM8/25/16
to mongodb-user
carlos,

--eval is deprecated so using it is not a good idea.

On Tuesday, August 23, 2016 at 9:43:28 AM UTC-4, Floppy Disk wrote:

Kevin Adistambha

unread,
Aug 26, 2016, 2:14:38 AM8/26/16
to mongodb-user

Hi Carlos,

I have a replSet cluster running x2 MongoDB servers and an arbiter on a production Linux (CentOS 7.2) servers. I need to configure automated backups of the replSet but obviously I would need some kind of logic inside my script to detect rs.isMaster() & then perform a dump as follows:

Do I understand correctly that you want to perform mongodump on a Secondary?

In MongoDB 3.0 and earlier, mongodump will always perform the dump from Secondaries if connected to a replica set.

This was changed in MongoDB 3.2, where mongodump defaults to Primary instead. In MongoDB 3.2, you can set the --readPreference setting to dump from Secondaries if you wish, giving you more flexibility. For example (if you are using MongoDB 3.2):

mongodump -h 127.0.0.1 -d mydb -u carlos -p -o /var/lib/mongodb/backups --readPreference=secondary

Please see mongodump read preference page for more details.

Also, please note that dumping from Secondaries is not recommended for a sharded cluster deployment. This is because Secondaries are eventually consistent with the Primary, and in a sharded cluster this could result in an inconsistent backup state.

Best regards,
Kevin

boncalo mihai

unread,
Aug 26, 2016, 12:27:10 PM8/26/16
to mongod...@googlegroups.com
Hi Carlos,
it's true that --eval is deprecated but can still be found in mongo man page in 3.2 
The other way is to use a command like mongo --shell 'test.js'
where test.js contains 
printjson(db.isMaster())
printjson(quit())

and then get your info again with awk/cut or any other method you are familiar with.But still, I recommend full backup with rsync for primary


Michael.

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/rGwizI-g3dY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user+unsubscribe@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/42fc8f1a-689f-457e-b7cc-7283a129e9fb%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages